GoodsService.php 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace app\data\service;
  3. use app\data\model\ShopGoods;
  4. use app\data\model\ShopGoodsCate;
  5. use app\data\model\ShopGoodsItem;
  6. use app\data\model\ShopGoodsMark;
  7. use app\data\model\ShopGoodsStock;
  8. use app\data\model\ShopOrder;
  9. use think\admin\Service;
  10. /**
  11. * 商品数据服务
  12. * Class GoodsService
  13. * @package app\data\service
  14. */
  15. class GoodsService extends Service
  16. {
  17. /**
  18. * 更新商品库存数据
  19. * @param string $code
  20. * @return boolean
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\DbException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. */
  25. public static function stock(string $code): bool
  26. {
  27. // 商品入库统计
  28. $query = ShopGoodsStock::mk()->field('goods_code,goods_spec,ifnull(sum(goods_stock),0) stock_total');
  29. $stockList = $query->where(['goods_code' => $code])->group('goods_code,goods_spec')->select()->toArray();
  30. // 商品销量统计
  31. $query = ShopOrder::mk()->alias('a')->field('b.goods_code,b.goods_spec,ifnull(sum(b.stock_sales),0) stock_sales');
  32. $query->leftJoin('shop_order_item b', 'a.order_no=b.order_no')->where("b.goods_code='{$code}' and a.status>0 and a.deleted_status<1");
  33. $salesList = $query->group('b.goods_code,b.goods_spec')->select()->toArray();
  34. // 组装更新数据
  35. $dataList = [];
  36. foreach (array_merge($stockList, $salesList) as $vo) {
  37. $key = "{$vo['goods_code']}@@{$vo['goods_spec']}";
  38. $dataList[$key] = isset($dataList[$key]) ? array_merge($dataList[$key], $vo) : $vo;
  39. if (empty($dataList[$key]['stock_sales'])) $dataList[$key]['stock_sales'] = 0;
  40. if (empty($dataList[$key]['stock_total'])) $dataList[$key]['stock_total'] = 0;
  41. }
  42. unset($salesList, $stockList);
  43. // 更新商品规格销量及库存
  44. foreach ($dataList as $vo) {
  45. $map = ['goods_code' => $code, 'goods_spec' => $vo['goods_spec']];
  46. $set = ['stock_total' => $vo['stock_total'], 'stock_sales' => $vo['stock_sales']];
  47. ShopGoodsItem::mk()->where($map)->update($set);
  48. }
  49. // 更新商品主体销量及库存
  50. ShopGoods::mk()->where(['code' => $code])->update([
  51. 'stock_total' => intval(array_sum(array_column($dataList, 'stock_total'))),
  52. 'stock_sales' => intval(array_sum(array_column($dataList, 'stock_sales'))),
  53. 'stock_virtual' => ShopGoodsItem::mk()->where(['goods_code' => $code])->sum('number_virtual'),
  54. ]);
  55. return true;
  56. }
  57. /**
  58. * 商品数据绑定
  59. * @param array $data 商品主数据
  60. * @param boolean $simple 简化数据
  61. * @return array
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public static function bindData(array &$data = [], bool $simple = true): array
  67. {
  68. $marks = ShopGoodsMark::items();
  69. $cates = ShopGoodsCate::treeTable();
  70. $codes = array_unique(array_column($data, 'code'));
  71. $items = ShopGoodsItem::mk()->whereIn('goods_code', $codes)->where(['status' => 1])->select()->toArray();
  72. foreach($items as $k => $v){
  73. $arr = explode("|", $v['show_image']);
  74. $items[$k]['show_image'] = $arr[0];
  75. }
  76. // dump($data);die;
  77. foreach ($data as &$vo) {
  78. [$vo['marks'], $vo['cateids'], $vo['cateinfo']] = [str2arr($vo['marks'], ',', $marks), str2arr($vo['cateids']), []];
  79. $vo['slider']?$vo['slider']=str2arr($vo['slider'], '|'):$vo['slider']=null;
  80. [$vo['specs'], $vo['items']] = [ json_decode($vo['data_specs'], true), []];
  81. if(!$vo['specs']){
  82. $vo['specs']=null;
  83. }
  84. // [$vo['slider'], $vo['specs'], $vo['items']] = [[], json_decode($vo['data_specs'], true), []];
  85. foreach ($cates as $cate) if (in_array($cate['id'], $vo['cateids'])) $vo['cateinfo'] = $cate;
  86. foreach ($items as $item) if ($item['goods_code'] === $vo['code']) $vo['items'][] = $item;
  87. if ($simple) unset($vo['marks'], $vo['sort'], /*$vo['status'],*/ $vo['deleted'], $vo['data_items'], $vo['data_specs'], $vo['cateinfo']['parent']);
  88. }
  89. return $data;
  90. }
  91. }