Goods.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\service;
  10. use app\common\library\helper;
  11. use app\common\model\Goods as GoodsModel;
  12. /**
  13. * 商品服务类
  14. * Class Goods
  15. * @package app\store\service
  16. */
  17. class Goods
  18. {
  19. /**
  20. * 设置商品数据
  21. * @param $data
  22. * @param bool $isMultiple
  23. * @param string $goodsIndex
  24. * @return mixed
  25. * @throws \think\db\exception\DataNotFoundException
  26. * @throws \think\db\exception\ModelNotFoundException
  27. * @throws \think\exception\DbException
  28. */
  29. public static function setGoodsData($data, $isMultiple = true, $goodsIndex = 'goods_id')
  30. {
  31. if (!$isMultiple) $dataSource = [&$data]; else $dataSource = &$data;
  32. // 获取商品列表
  33. $model = new GoodsModel;
  34. $goodsData = $model->getListByIds(helper::getArrayColumn($dataSource, $goodsIndex));
  35. $goodsList = helper::arrayColumn2Key($goodsData, 'goods_id');
  36. // 整理列表数据
  37. foreach ($dataSource as &$item) {
  38. $item['goods'] = isset($goodsList[$item[$goodsIndex]]) ? $goodsList[$item[$goodsIndex]] : null;
  39. }
  40. return $data;
  41. }
  42. /**
  43. * 商品多规格信息
  44. * @param GoodsModel|null $model
  45. * @return null|array
  46. */
  47. public static function getSpecData($model = null)
  48. {
  49. // 商品sku数据
  50. if (!is_null($model) && $model['spec_type'] == 20) {
  51. return $model->getManySpecData($model['spec_rel'], $model['sku']);
  52. }
  53. return null;
  54. }
  55. }