Goods.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 addon\weighgoods\model\WeighGoods as WeighGoodsModel;
  14. use app\model\goods\Goods as GoodsModel;
  15. use app\model\goods\GoodsCategory;
  16. use app\model\storegoods\StoreGoods as StoreGoodsModel;
  17. use app\storeapi\controller\BaseStoreApi;
  18. use think\facade\Db;
  19. /**
  20. * 活动管理控制器
  21. * Class Activity
  22. * @package addon\shop\storeapi\controller
  23. */
  24. class Goods extends BaseStoreApi
  25. {
  26. /**
  27. * 获取商品分类的组织
  28. * @return false|string
  29. */
  30. public function category()
  31. {
  32. $level = $this->params[ 'level' ] ?? 1;
  33. $service_category_model = new GoodsCategory();
  34. $condition = [
  35. [ 'is_show', '=', 0 ],
  36. [ 'level', '<=', $level ],
  37. [ 'site_id', '=', $this->site_id ]
  38. ];
  39. $list = $service_category_model->getCategoryTree($condition, 'pid,category_id,category_name,image,level', 'sort asc,category_id desc');
  40. return $this->response($list);
  41. }
  42. public function page()
  43. {
  44. $page_index = $this->params[ 'page' ] ?? 1;
  45. $page_size = 30 ?? PAGE_LIST_ROWS;
  46. $goods_category = $this->params[ 'category' ] ?? 'all';
  47. $search_text = $this->params[ 'search_text' ] ?? '';
  48. $goods_class = $this->params[ 'goods_class' ] ?? 'all';
  49. $status = $this->params[ 'status' ] ?? 1;
  50. $sku_no = $this->params[ 'sku_no' ] ?? '';
  51. $start_price = $this->params[ 'start_price' ] ?? 0;
  52. $end_price = $this->params[ 'end_price' ] ?? 0;
  53. $model = new GoodsModel();
  54. $condition = [
  55. [ 'g.site_id', '=', $this->site_id ],
  56. [ 'g.is_delete', '=', 0 ],
  57. [ 'g.goods_state', '=', 1 ],
  58. [ 'g.sale_store', 'like', [ '%all%', '%,' . $this->store_id . ',%' ], 'or' ],
  59. [ '', 'exp', Db::raw("(g.sale_channel = 'all' OR g.sale_channel = 'offline')") ]
  60. ];
  61. if ($goods_class !== 'all') {
  62. $condition[] = [ 'g.goods_class', 'in', $goods_class ];
  63. } else {
  64. $condition[] = [ 'g.goods_class', 'in', '1,4,5,6' ];
  65. }
  66. if ($goods_category != 'all') $condition[] = [ 'g.category_id', 'like', "%,{$goods_category},%" ];
  67. if (!empty($search_text)) $condition[] = [ 'g.goods_name', 'like', "%{$search_text}%" ];
  68. if ($status !== 'all') {
  69. $condition[] = [ 'sg1.status', '=', $status ];
  70. }
  71. if (!empty($sku_no)) {
  72. $goods_sku_list = $model->getGoodsSkuList([ [ 'sku_no', 'like', '%' . $sku_no . '%' ] ], 'goods_id')[ 'data' ];
  73. $goods_id_arr = array_unique(array_column($goods_sku_list, 'goods_id'));
  74. $condition[] = [ 'g.goods_id', 'in', $goods_id_arr ];
  75. }
  76. if (!empty($start_price)) {
  77. $condition[] = [ 'g.price', '>=', $start_price ];
  78. }
  79. if (!empty($end_price)) {
  80. $condition[] = [ 'g.price', '<=', $end_price ];
  81. }
  82. $field = 'g.goods_id,g.goods_name,g.goods_class,g.goods_class_name,g.introduction,g.goods_image,g.goods_state,g.sku_id,g.price,gs.discount_price,g.goods_spec_format,g.is_unify_price,
  83. IFNULL(IF(g.is_unify_price = 1,g.price,sg1.price), g.price) as price, IFNULL(IF(g.is_unify_price = 1,gs.discount_price,sg1.price), gs.discount_price) as discount_price,
  84. IFNULL(sg1.price, 0) as store_price,IFNULL(sg1.status, 0) as store_status';
  85. $join = [
  86. [ 'goods_sku gs', 'gs.sku_id = g.sku_id', 'left' ],
  87. [ 'store_goods sg1', 'g.goods_id=sg1.goods_id and sg1.store_id=' . $this->store_id, 'left' ],
  88. ];
  89. //todo 这部分可以封装
  90. $stock_store_id = ( new \app\model\store\Store() )->getStoreStockTypeStoreId([ 'store_id' => $this->store_id ])[ 'data' ] ?? 0;
  91. if ($stock_store_id == $this->store_id) {
  92. $field .= ', IFNULL(sg1.stock, 0) as stock';
  93. } else {
  94. $join[] = [ 'store_goods sg2', 'g.goods_id = sg2.goods_id and sg2.store_id=' . $stock_store_id, 'left' ];
  95. $field .= ', IFNULL(sg2.stock, 0) as stock';
  96. }
  97. $data = $model->getGoodsPageList($condition, $page_index, $page_size, 'g.sort asc,g.create_time desc', $field, 'g', $join);
  98. return $this->response($data);
  99. }
  100. /**
  101. * 商品详情
  102. * @return false|string
  103. */
  104. public function detail()
  105. {
  106. $goods_id = $this->params[ 'goods_id' ] ?? 0;
  107. $goods_model = new GoodsModel();
  108. $field = 'g.goods_id, g.goods_name, g.introduction,g.goods_class,g.is_virtual, 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,
  109. sg1.price as store_price, sg1.cost_price as store_cost_price, sg1.status as store_status';
  110. $join = [
  111. [ 'store_goods sg1', 'g.goods_id=sg1.goods_id and sg1.store_id=' . $this->store_id, 'left' ],
  112. ];
  113. //todo 这部分可以封装
  114. $stock_store_id = ( new \app\model\store\Store() )->getStoreStockTypeStoreId([ 'store_id' => $this->store_id ])[ 'data' ] ?? 0;
  115. if ($stock_store_id == $this->store_id) {
  116. $field .= ',sg1.stock';
  117. } else {
  118. $join[] = [ 'store_goods sg2', 'g.goods_id = sg2.goods_id and sg2.store_id=' . $stock_store_id, 'left' ];
  119. $field .= ', sg2.stock';
  120. }
  121. $condition = [
  122. [ 'g.site_id', '=', $this->site_id ],
  123. [ 'g.is_delete', '=', 0 ],
  124. [ 'g.goods_state', '=', 1 ],
  125. [ 'g.goods_id', '=', $goods_id ],
  126. [ 'g.sale_store', 'like', [ '%all%', '%,' . $this->store_id . ',%' ], 'or' ],
  127. [ '', 'exp', Db::raw("(g.sale_channel = 'all' OR g.sale_channel = 'offline')") ]
  128. ];
  129. $goods_info = $goods_model->getGoodsInfo($condition, $field, 'g', $join)[ 'data' ];
  130. if (empty($goods_info)) return $this->response($goods_model->error(null, '商品信息缺失'));
  131. //查询商品规格
  132. $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,sku.unit,
  133. IF(sku.is_unify_price = 1,sku.discount_price,sgs1.price) as store_price, sgs1.cost_price as store_cost_price, sgs1.status as store_status';
  134. $join = [
  135. [ 'store_goods_sku sgs1', 'sku.sku_id=sgs1.sku_id and sgs1.store_id=' . $this->store_id, 'left' ],
  136. ];
  137. if ($stock_store_id == $this->store_id) {
  138. $sku_filed .= ', sgs1.stock';
  139. } else {
  140. $join[] = [ 'store_goods_sku sgs2', 'sku.sku_id = sgs2.sku_id and sgs2.store_id=' . $stock_store_id, 'left' ];
  141. $sku_filed .= ', sgs2.stock';
  142. }
  143. $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' ];
  144. return $this->response($goods_model->success($goods_info));
  145. }
  146. /**
  147. * 上下架
  148. */
  149. public function setStatus()
  150. {
  151. $goods_id = $this->params[ 'goods_id' ] ?? 0;
  152. $status = $this->params[ 'status' ] ?? 0;
  153. $model = new StoreGoodsModel();
  154. $res = $model->modifyGoodsState($goods_id, $status, $this->site_id, $this->store_id);
  155. return $this->response($res);
  156. }
  157. /**
  158. * 商品编辑
  159. */
  160. public function editGoods()
  161. {
  162. $goods_sku_array = isset($this->params[ 'goods_sku_list' ]) ? json_decode($this->params[ 'goods_sku_list' ], true) : [];
  163. $model = new StoreGoodsModel();
  164. $res = $model->editStoreGoods($goods_sku_array, $this->site_id, $this->store_id, $this->uid);
  165. return $this->response($res);
  166. }
  167. /**
  168. * 获取商品规格
  169. */
  170. public function skuList()
  171. {
  172. $goods_id = $this->params[ 'goods_id' ] ?? 0;
  173. $sku_filed = 'sku.goods_id,sku.sku_id,sku.sku_name,sku.goods_name,sku.sku_no,sku.sku_image,sku.sku_images,sku.spec_name,sku.goods_spec_format,sku.unit,IFNULL(IF(g.is_unify_price = 1,sku.price,sgs.price), sku.price) as price,g.goods_class';
  174. $join = [
  175. [ 'goods g', 'sku.goods_id=g.goods_id', 'inner' ],
  176. [ 'store_goods_sku sgs', 'sku.sku_id=sgs.sku_id and sgs.store_id=' . $this->store_id, 'inner' ],
  177. ];
  178. $stock_store_id = ( new \app\model\store\Store() )->getStoreStockTypeStoreId([ 'store_id' => $this->store_id ])[ 'data' ] ?? 0;
  179. if ($stock_store_id == $this->store_id) {
  180. $sku_filed .= ', IFNULL(sgs.stock, 0) as stock';
  181. } else {
  182. $join[] = [ 'store_goods_sku sgs2', 'sku.sku_id = sgs2.sku_id and sgs2.store_id=' . $stock_store_id, 'left' ];
  183. $sku_filed .= ', IFNULL(sgs2.stock, 0) as stock';
  184. }
  185. $goods_model = new GoodsModel();
  186. $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' ];
  187. return $this->response($this->success($sku_list));
  188. }
  189. /**
  190. * 查询sku信息
  191. * @return false|string
  192. */
  193. public function skuInfo()
  194. {
  195. $sku_id = $this->params[ 'sku_id' ] ?? 0;
  196. $sku_no = $this->params[ 'sku_no' ] ?? '';
  197. $condition = [
  198. [ 'sku.site_id', '=', $this->site_id ],
  199. [ 'sku.is_delete', '=', 0 ]
  200. ];
  201. if ($sku_id) $condition[] = [ 'sku.sku_id', '=', $sku_id ];
  202. $weigh_int = '0'; // 称重个位数
  203. $weigh_decimal = '0'; // 称重小数点位数
  204. // 检测是否存在称重商品插件
  205. if (!empty($sku_no)) {
  206. if (addon_is_exit('weighgoods') == 1) {
  207. $weigh_goods_model = new WeighGoodsModel();
  208. $weigh_config = $weigh_goods_model->getWeighConfig($this->site_id)[ 'data' ][ 'value' ];
  209. $prefix_len = strlen($weigh_config[ 'prefix' ]); // 标识码长度
  210. $prefix = substr($sku_no, 0, $prefix_len); // 标识码,区分电子秤
  211. // 检测是否为电子秤条码规则
  212. if ($prefix == $weigh_config[ 'prefix' ]) {
  213. $goods_code = substr($sku_no, $prefix_len, $weigh_config[ 'goods_code' ]);// 商品编码位数
  214. $weigh_int = substr($sku_no, $prefix_len + $weigh_config[ 'goods_code' ], $weigh_config[ 'weigh_int' ]); // 重量个位数
  215. $weigh_decimal = substr($sku_no, $prefix_len + $weigh_config[ 'goods_code' ] + $weigh_config[ 'weigh_int' ], $weigh_config[ 'weigh_decimal' ]); // 重量小数点位数
  216. $sku_no = $goods_code;
  217. }
  218. }
  219. $condition[] = [ 'sku.sku_no', '=', $sku_no ];
  220. }
  221. $sku_filed = 'g.goods_class,sku.goods_id,sku.sku_id,sku.sku_name,sku.goods_name,sku.sku_no,sku.sku_image,sku.sku_images,sku.spec_name,sku.goods_spec_format,sku.unit,IFNULL(IF(g.is_unify_price = 1,sku.price,sgs.price), sku.price) as price,sku.cost_price';
  222. $join = [
  223. [ 'goods g', 'sku.goods_id=g.goods_id', 'inner' ],
  224. [ 'store_goods_sku sgs', 'sku.sku_id=sgs.sku_id and sgs.store_id=' . $this->store_id, 'inner' ],
  225. ];
  226. $stock_store_id = ( new \app\model\store\Store() )->getStoreStockTypeStoreId([ 'store_id' => $this->store_id ])[ 'data' ] ?? 0;
  227. if ($stock_store_id == $this->store_id) {
  228. $sku_filed .= ', IFNULL(sgs.stock, 0) as stock, IFNULL(sgs.real_stock, 0) as real_stock';
  229. } else {
  230. $join[] = [ 'store_goods_sku sgs2', 'sku.sku_id = sgs2.sku_id and sgs2.store_id=' . $stock_store_id, 'left' ];
  231. $sku_filed .= ', IFNULL(sgs2.stock, 0) as stock, IFNULL(sgs2.real_stock, 0) as real_stock';
  232. }
  233. $goods_model = new GoodsModel();
  234. $sku_info = $goods_model->getGoodsSkuInfo($condition, $sku_filed, 'sku', $join);
  235. if (!empty($sku_info[ 'data' ])) {
  236. $sku_info[ 'data' ][ 'weigh' ] = numberFormat($weigh_int) . '.' . numberFormat($weigh_decimal);
  237. }
  238. return $this->response($sku_info);
  239. }
  240. }