Goods.php 1.5 KB

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