Goods.php 12 KB

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