Goods.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 = 10;
  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. // $total_count = $model->getGoodsTotalCount($condition , $field, 'g', $join);
  99. $data = $model->getGoodsPageList($condition, $page_index, $page_size, 'g.sort asc,g.create_time desc', $field, 'g', $join);
  100. // $total_num = $data['count'];
  101. // $data['total_num'] = $total_num;
  102. if($data['data']){
  103. $total_count = $data['data']['count'];
  104. if($total_count>36){
  105. $page_size = ceil($total_count/4);
  106. $data = $model->getGoodsPageList($condition, $page_index, $page_size, 'g.sort asc,g.create_time desc', $field, 'g', $join);
  107. }
  108. }
  109. return $this->response($data);
  110. }
  111. /**
  112. * 商品详情
  113. * @return false|string
  114. */
  115. public function detail()
  116. {
  117. $goods_id = $this->params[ 'goods_id' ] ?? 0;
  118. $goods_model = new GoodsModel();
  119. $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,
  120. sg1.price as store_price, sg1.cost_price as store_cost_price, sg1.status as store_status';
  121. $join = [
  122. [ 'store_goods sg1', 'g.goods_id=sg1.goods_id and sg1.store_id=' . $this->store_id, 'left' ],
  123. ];
  124. //todo 这部分可以封装
  125. $stock_store_id = ( new \app\model\store\Store() )->getStoreStockTypeStoreId([ 'store_id' => $this->store_id ])[ 'data' ] ?? 0;
  126. if ($stock_store_id == $this->store_id) {
  127. $field .= ',sg1.stock';
  128. } else {
  129. $join[] = [ 'store_goods sg2', 'g.goods_id = sg2.goods_id and sg2.store_id=' . $stock_store_id, 'left' ];
  130. $field .= ', sg2.stock';
  131. }
  132. $condition = [
  133. [ 'g.site_id', '=', $this->site_id ],
  134. [ 'g.is_delete', '=', 0 ],
  135. [ 'g.goods_state', '=', 1 ],
  136. [ 'g.goods_id', '=', $goods_id ],
  137. [ 'g.sale_store', 'like', [ '%all%', '%,' . $this->store_id . ',%' ], 'or' ],
  138. [ '', 'exp', Db::raw("(g.sale_channel = 'all' OR g.sale_channel = 'offline')") ]
  139. ];
  140. $goods_info = $goods_model->getGoodsInfo($condition, $field, 'g', $join)[ 'data' ];
  141. if (empty($goods_info)) return $this->response($goods_model->error(null, '商品信息缺失'));
  142. //查询商品规格
  143. $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,
  144. 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';
  145. $join = [
  146. [ 'store_goods_sku sgs1', 'sku.sku_id=sgs1.sku_id and sgs1.store_id=' . $this->store_id, 'left' ],
  147. ];
  148. if ($stock_store_id == $this->store_id) {
  149. $sku_filed .= ', sgs1.stock';
  150. } else {
  151. $join[] = [ 'store_goods_sku sgs2', 'sku.sku_id = sgs2.sku_id and sgs2.store_id=' . $stock_store_id, 'left' ];
  152. $sku_filed .= ', sgs2.stock';
  153. }
  154. $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' ];
  155. return $this->response($goods_model->success($goods_info));
  156. }
  157. /**
  158. * 上下架
  159. */
  160. public function setStatus()
  161. {
  162. $goods_id = $this->params[ 'goods_id' ] ?? 0;
  163. $status = $this->params[ 'status' ] ?? 0;
  164. $model = new StoreGoodsModel();
  165. $res = $model->modifyGoodsState($goods_id, $status, $this->site_id, $this->store_id);
  166. return $this->response($res);
  167. }
  168. /**
  169. * 商品编辑
  170. */
  171. public function editGoods()
  172. {
  173. $goods_sku_array = isset($this->params[ 'goods_sku_list' ]) ? json_decode($this->params[ 'goods_sku_list' ], true) : [];
  174. $model = new StoreGoodsModel();
  175. $res = $model->editStoreGoods($goods_sku_array, $this->site_id, $this->store_id, $this->uid);
  176. return $this->response($res);
  177. }
  178. /**
  179. * 获取商品规格
  180. */
  181. public function skuList()
  182. {
  183. $goods_id = $this->params[ 'goods_id' ] ?? 0;
  184. $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';
  185. $join = [
  186. [ 'goods g', 'sku.goods_id=g.goods_id', 'inner' ],
  187. [ 'store_goods_sku sgs', 'sku.sku_id=sgs.sku_id and sgs.store_id=' . $this->store_id, 'inner' ],
  188. ];
  189. $stock_store_id = ( new \app\model\store\Store() )->getStoreStockTypeStoreId([ 'store_id' => $this->store_id ])[ 'data' ] ?? 0;
  190. if ($stock_store_id == $this->store_id) {
  191. $sku_filed .= ', IFNULL(sgs.stock, 0) as stock';
  192. } else {
  193. $join[] = [ 'store_goods_sku sgs2', 'sku.sku_id = sgs2.sku_id and sgs2.store_id=' . $stock_store_id, 'left' ];
  194. $sku_filed .= ', IFNULL(sgs2.stock, 0) as stock';
  195. }
  196. $goods_model = new GoodsModel();
  197. $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' ];
  198. return $this->response($this->success($sku_list));
  199. }
  200. /**
  201. * 查询sku信息
  202. * @return false|string
  203. */
  204. public function skuInfo()
  205. {
  206. $sku_id = $this->params[ 'sku_id' ] ?? 0;
  207. $sku_no = $this->params[ 'sku_no' ] ?? '';
  208. $condition = [
  209. [ 'sku.site_id', '=', $this->site_id ],
  210. [ 'sku.is_delete', '=', 0 ]
  211. ];
  212. if ($sku_id) $condition[] = [ 'sku.sku_id', '=', $sku_id ];
  213. $weigh_int = '0'; // 称重个位数
  214. $weigh_decimal = '0'; // 称重小数点位数
  215. // 检测是否存在称重商品插件
  216. if (!empty($sku_no)) {
  217. if (addon_is_exit('weighgoods') == 1) {
  218. $weigh_goods_model = new WeighGoodsModel();
  219. $weigh_config = $weigh_goods_model->getWeighConfig($this->site_id)[ 'data' ][ 'value' ];
  220. $prefix_len = strlen($weigh_config[ 'prefix' ]); // 标识码长度
  221. $prefix = substr($sku_no, 0, $prefix_len); // 标识码,区分电子秤
  222. // 检测是否为电子秤条码规则
  223. if ($prefix == $weigh_config[ 'prefix' ]) {
  224. $goods_code = substr($sku_no, $prefix_len, $weigh_config[ 'goods_code' ]);// 商品编码位数
  225. $weigh_int = substr($sku_no, $prefix_len + $weigh_config[ 'goods_code' ], $weigh_config[ 'weigh_int' ]); // 重量个位数
  226. $weigh_decimal = substr($sku_no, $prefix_len + $weigh_config[ 'goods_code' ] + $weigh_config[ 'weigh_int' ], $weigh_config[ 'weigh_decimal' ]); // 重量小数点位数
  227. $sku_no = $goods_code;
  228. }
  229. }
  230. $condition[] = [ 'sku.sku_no', '=', $sku_no ];
  231. }
  232. $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';
  233. $join = [
  234. [ 'goods g', 'sku.goods_id=g.goods_id', 'inner' ],
  235. [ 'store_goods_sku sgs', 'sku.sku_id=sgs.sku_id and sgs.store_id=' . $this->store_id, 'inner' ],
  236. ];
  237. $stock_store_id = ( new \app\model\store\Store() )->getStoreStockTypeStoreId([ 'store_id' => $this->store_id ])[ 'data' ] ?? 0;
  238. if ($stock_store_id == $this->store_id) {
  239. $sku_filed .= ', IFNULL(sgs.stock, 0) as stock, IFNULL(sgs.real_stock, 0) as real_stock';
  240. } else {
  241. $join[] = [ 'store_goods_sku sgs2', 'sku.sku_id = sgs2.sku_id and sgs2.store_id=' . $stock_store_id, 'left' ];
  242. $sku_filed .= ', IFNULL(sgs2.stock, 0) as stock, IFNULL(sgs2.real_stock, 0) as real_stock';
  243. }
  244. $goods_model = new GoodsModel();
  245. $sku_info = $goods_model->getGoodsSkuInfo($condition, $sku_filed, 'sku', $join);
  246. if (!empty($sku_info[ 'data' ])) {
  247. $sku_info[ 'data' ][ 'weigh' ] = numberFormat($weigh_int) . '.' . numberFormat($weigh_decimal);
  248. }
  249. return $this->response($sku_info);
  250. }
  251. }