Service.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace addon\cashier\storeapi\controller;
  13. use app\model\goods\ServiceCategory;
  14. use app\model\goods\Goods as GoodsModel;
  15. use app\model\storegoods\StoreGoods as StoreGoodsModel;
  16. use app\storeapi\controller\BaseStoreApi;
  17. use think\facade\Db;
  18. /**
  19. * 活动管理控制器
  20. * Class Activity
  21. * @package addon\shop\storeapi\controller
  22. */
  23. class Service extends BaseStoreApi
  24. {
  25. /**
  26. * 获取商品分类的组织
  27. * @return false|string
  28. */
  29. public function category()
  30. {
  31. $level = $this->params[ 'level' ] ?? 1;
  32. $service_category_model = new ServiceCategory();
  33. $condition = [
  34. [ 'is_show', '=', 0 ],
  35. [ 'level', '<=', $level ],
  36. [ 'site_id', '=', $this->site_id ]
  37. ];
  38. $list = $service_category_model->getCategoryTree($condition, 'category_id,category_name,image,level', 'sort asc,category_id desc');
  39. return $this->response($list);
  40. }
  41. public function page()
  42. {
  43. $page_index = $this->params[ 'page' ] ?? 1;
  44. $page_size = $this->params[ 'page_size' ] ?? PAGE_LIST_ROWS;
  45. $goods_category = $this->params[ 'category' ] ?? 'all';
  46. $search_text = $this->params[ 'search_text' ] ?? '';
  47. $goods_state = $this->params[ 'goods_state' ] ?? 'all';
  48. $sku_no = $this->params[ 'sku_no' ] ?? '';
  49. $is_virtual = $this->params[ 'is_virtual' ] ?? 0;
  50. $model = new GoodsModel();
  51. $condition = [
  52. [ 'g.site_id', '=', $this->site_id ],
  53. [ 'g.is_delete', '=', 0 ],
  54. [ 'g.goods_class', '=', 4 ],
  55. [ 'g.sale_store', 'like', [ '%all%', '%,' . $this->store_id . ',%' ], 'or' ],
  56. [ '', 'exp', Db::raw("(g.sale_channel = 'all' OR g.sale_channel = 'offline')") ]
  57. ];
  58. if ($goods_state !== 'all') {
  59. $condition[] = [ 'g.goods_state', '=', $goods_state ];
  60. }
  61. if ($goods_category != 'all') $condition[] = [ 'g.service_category', 'like', "%,{$goods_category},%" ];
  62. if ($search_text != '') $condition[] = [ 'g.goods_name', 'like', "%{$search_text}%" ];
  63. if (!empty($sku_no)) {
  64. $goods_sku_list = $model->getGoodsSkuList([ [ 'sku_no', 'like', '%' . $sku_no . '%' ] ], 'goods_id')[ 'data' ];
  65. $goods_id_arr = array_unique(array_column($goods_sku_list, 'goods_id'));
  66. $condition[] = [ 'g.goods_id', 'in', $goods_id_arr ];
  67. }
  68. if (addon_is_exit('store')) {
  69. $status = $this->params[ 'status' ] ?? 1;
  70. if ($status !== 'all') {
  71. $condition[] = [ 'sg.status', '=', $status ];
  72. }
  73. $field = 'g.goods_id,g.goods_name,g.introduction,g.goods_image,g.goods_state,g.sku_id,g.price,gs.discount_price,g.goods_spec_format,gs.service_length,
  74. IFNULL(IF(g.is_unify_price = 1,g.price,sg.price), g.price) as price, IFNULL(IF(g.is_unify_price = 1,gs.discount_price,sg.price), gs.discount_price) as discount_price,
  75. sg.price as store_price';
  76. $join = [
  77. [ 'goods_sku gs', 'gs.sku_id = g.sku_id', 'left' ],
  78. [ 'store_goods sg', 'g.goods_id=sg.goods_id and sg.store_id=' . $this->store_id, 'left' ],
  79. [ 'store s', 's.store_id = sg.store_id', 'left' ]
  80. ];
  81. $stock_store_id = ( new \app\model\store\Store() )->getStoreStockTypeStoreId([ 'store_id' => $this->store_id ])[ 'data' ] ?? 0;
  82. if ($stock_store_id == $this->store_id) {
  83. $field .= ', IFNULL(sg.stock, 0) as stock';
  84. } else {
  85. $join[] = [ 'store_goods sg2', 'g.goods_id=sg2.goods_id and sg2.store_id=' . $stock_store_id, 'left' ];
  86. $field .= ', IFNULL(sg2.stock, 0) as stock';
  87. }
  88. } else {
  89. $field = 'g.goods_id,g.goods_name,g.introduction,g.goods_image,g.goods_state,g.sku_id,g.price,gs.discount_price,g.goods_spec_format,gs.service_length';
  90. $join = [
  91. [ 'goods_sku gs', 'gs.sku_id = g.sku_id', 'left' ],
  92. ];
  93. }
  94. $data = $model->getGoodsPageList($condition, $page_index, $page_size, 'g.sort asc,g.create_time desc', $field, 'g', $join);
  95. return $this->response($data);
  96. }
  97. /**
  98. * 商品详情
  99. * @return false|string
  100. */
  101. public function detail()
  102. {
  103. $goods_id = $this->params[ 'goods_id' ] ?? 0;
  104. $goods_model = new GoodsModel();
  105. $field = 'g.goods_id, g.goods_name, g.introduction, g.goods_class_name, g.goods_image, g.goods_state, g.sku_id, g.price, g.unit, g.cost_price, g.category_id, g.brand_name,g.is_unify_price,
  106. sg.price as store_price, sg.cost_price as store_cost_price, sg.status as store_status';
  107. $goods_info = $goods_model->getGoodsInfo([ [ 'g.goods_id', '=', $goods_id ], [ 'g.site_id', '=', $this->site_id ] ], $field, 'g', [
  108. [ 'store_goods sg', 'g.goods_id=sg.goods_id and sg.store_id=' . $this->store_id, 'left' ]
  109. ])[ 'data' ];
  110. if (empty($goods_info)) return $this->response($goods_model->error(null, '商品信息缺失'));
  111. //查询商品规格
  112. $sku_filed = 'sku.sku_id,sku.sku_name,sku.sku_no,sku.price,sku.discount_price,sku.cost_price,sku.sku_image,sku.sku_images,sku.spec_name,
  113. sgs.price as store_stock, sgs.cost_price as store_cost_price, sgs.status as store_status';
  114. $join = [
  115. [ 'store_goods_sku sgs', 'sku.sku_id=sgs.sku_id and sgs.store_id=' . $this->store_id, 'left' ]
  116. ];
  117. $goods_info[ 'sku_list' ] = $goods_model->getGoodsSkuList([ [ 'sku.goods_id', '=', $goods_id ], [ 'sku.site_id', '=', $this->site_id ] ], $sku_filed, 'sku.sku_id asc', 0, 'sku', $join)[ 'data' ];
  118. return $this->response($goods_model->success($goods_info));
  119. }
  120. /**
  121. * 上下架
  122. */
  123. public function setStatus()
  124. {
  125. $goods_id = $this->params[ 'goods_id' ] ?? 0;
  126. $status = $this->params[ 'status' ] ?? 0;
  127. $model = new StoreGoodsModel();
  128. $res = $model->modifyGoodsState($goods_id, $status, $this->site_id, $this->store_id);
  129. return $this->response($res);
  130. }
  131. /**
  132. * 商品编辑
  133. */
  134. public function editGoods()
  135. {
  136. $goods_sku_array = isset($this->params[ 'goods_id' ]) ? json_decode($this->params[ 'goods_id' ]) : [];
  137. $model = new StoreGoodsModel();
  138. $res = $model->editStoreGoods($goods_sku_array, $this->site_id, $this->store_id, $this->uid);
  139. return $this->response($res);
  140. }
  141. }