Card.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\cardservice\api\controller;
  11. use addon\cardservice\model\CardGoods;
  12. use app\api\controller\BaseApi;
  13. use app\model\goods\Goods;
  14. use app\model\goods\GoodsService;
  15. use app\model\web\Config as ConfigModel;
  16. use think\facade\Db;
  17. /**
  18. * 卡项
  19. */
  20. class Card extends BaseApi
  21. {
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. $this->initStoreData();
  26. }
  27. public function detail()
  28. {
  29. $sku_id = isset($this->params[ 'sku_id' ]) ? $this->params[ 'sku_id' ] : 0;
  30. $goods_id = isset($this->params[ 'goods_id' ]) ? $this->params[ 'goods_id' ] : 0;
  31. $goods = new CardGoods();
  32. if (empty($sku_id) && !empty($goods_id)) {
  33. $sku_id = $goods->getGoodsInfo([ [ 'goods_id', '=', $goods_id ] ], 'sku_id')[ 'data' ][ 'sku_id' ] ?? 0;
  34. }
  35. if (empty($sku_id) && empty($goods_id)) {
  36. return $this->response($this->error('', 'REQUEST_ID'));
  37. }
  38. $condition = [
  39. ['gs.sku_id', '=', $sku_id],
  40. ['gs.site_id', '=', $this->site_id],
  41. ['gs.is_delete', '=', 0 ],
  42. ['g.goods_class', '=', $goods->getGoodsClass()['id'] ]
  43. ];
  44. $field = 'gs.goods_id,gs.sku_id,gs.qr_id,gs.goods_name,gs.sku_name,gs.sku_spec_format,gs.price,gs.market_price,gs.discount_price,gs.promotion_type,gs.start_time
  45. ,gs.end_time,gs.stock,gs.click_num,(g.sale_num + g.virtual_sale) as sale_num,gs.collect_num,gs.sku_image,gs.sku_images
  46. ,gc.card_type,gc.card_type_name,gc.renew_price,gc.recharge_money,gc.common_num,gc.discount_goods_type,gc.discount,gc.validity_type,gc.validity_day,gc.validity_time
  47. ,gs.goods_content,gs.goods_state,gs.is_free_shipping,gs.goods_spec_format,gs.goods_attr_format,gs.introduction,gs.unit,gs.video_url
  48. ,gs.is_virtual,gs.goods_service_ids,gs.max_buy,gs.min_buy,gs.is_limit,gs.limit_type,gs.support_trade_type,g.goods_image,g.keywords,g.stock_show,g.sale_show,g.market_price_show,g.barrage_show,g.evaluate,g.sale_store,g.sale_channel';
  49. $join = [
  50. [ 'goods g', 'g.goods_id = gs.goods_id', 'inner' ]
  51. ];
  52. // 如果是连锁运营模式
  53. if ($this->store_data['config']['store_business'] == 'store') {
  54. $join[] = [ 'store_goods_sku sgs', 'gs.goods_id = sgs.goods_id and sgs.store_id=' . $this->store_id, 'left' ];
  55. $field .= ',sgs.status as store_goods_status';
  56. $field = str_replace('gs.price', 'IFNULL(IF(g.is_unify_price = 1,gs.price,sgs.price), gs.price) as price', $field);
  57. $field = str_replace('gs.discount_price', 'IFNULL(IF(g.is_unify_price = 1,gs.discount_price,sgs.price), gs.discount_price) as discount_price', $field);
  58. if ($this->store_data['store_info']['stock_type'] == 'store' ) {
  59. $field = str_replace('gs.stock', 'IFNULL(sgs.stock, 0) as stock', $field);
  60. }
  61. }
  62. $goods_sku_detail = $goods->getGoodsSkuInfo($condition, $field, 'gs', $join)[ 'data' ];
  63. if (empty($goods_sku_detail)) return $this->response($this->error());
  64. $goods_sku_detail[ 'purchased_num' ] = 0; // 该商品已购数量
  65. $res[ 'goods_sku_detail' ] = $goods_sku_detail;
  66. // 商品服务
  67. $goods_service = new GoodsService();
  68. $goods_service_list = $goods_service->getServiceList([ [ 'site_id', '=', $this->site_id ], [ 'id', 'in', $res[ 'goods_sku_detail' ][ 'goods_service_ids' ] ] ], 'service_name,desc,icon');
  69. $res[ 'goods_sku_detail' ][ 'goods_service' ] = $goods_service_list[ 'data' ];
  70. return $this->response($this->success($res));
  71. }
  72. /**
  73. * 列表信息
  74. */
  75. public function page()
  76. {
  77. $page = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
  78. $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
  79. $goods_id_arr = isset($this->params[ 'goods_id_arr' ]) ? $this->params[ 'goods_id_arr' ] : '';//goods_id数组
  80. $keyword = isset($this->params[ 'keyword' ]) ? trim($this->params[ 'keyword' ]) : '';//关键词
  81. $service_category = isset($this->params[ 'service_category' ]) ? $this->params[ 'service_category' ] : 0;//分类
  82. $is_free_shipping = isset($this->params[ 'is_free_shipping' ]) ? $this->params[ 'is_free_shipping' ] : 0;//是否免邮
  83. $order = isset($this->params[ 'order' ]) ? $this->params[ 'order' ] : "";//排序(综合、销量、价格)
  84. $sort = isset($this->params[ 'sort' ]) ? $this->params[ 'sort' ] : "";//升序、降序
  85. $card_type = isset($this->params[ 'card_type' ]) ? $this->params[ 'card_type' ] : '';
  86. $condition = [];
  87. $condition[] = [ 'gs.site_id', '=', $this->site_id ];
  88. $condition[] = [ 'g.goods_class', '=', (new CardGoods())->getGoodsClass()['id'] ];
  89. $condition[] = [ '', 'exp', Db::raw("(g.sale_channel = 'all' OR g.sale_channel = 'online')") ];
  90. if (!empty($goods_id_arr)) {
  91. $condition[] = [ 'gs.goods_id', 'in', $goods_id_arr ];
  92. }
  93. if (!empty($card_type)) {
  94. $condition[] = [ 'gc.card_type', '=', $card_type ];
  95. }
  96. if (!empty($service_category)) {
  97. $condition[] = [ 'g.service_category', 'like', '%,'.$service_category.',%' ];
  98. }
  99. if (!empty($keyword)) {
  100. $condition[] = [ 'g.goods_name|gs.sku_name|gs.keywords', 'like', '%' . $keyword . '%' ];
  101. }
  102. if (!empty($is_free_shipping)) {
  103. $condition[] = [ 'gs.is_free_shipping', '=', $is_free_shipping ];
  104. }
  105. // 非法参数进行过滤
  106. if ($sort != "desc" && $sort != "asc") {
  107. $sort = "";
  108. }
  109. // 非法参数进行过滤
  110. if ($order != '') {
  111. if ($order != "sale_num" && $order != "discount_price") {
  112. $order = 'gs.sort';
  113. } elseif ($order == "sale_num") {
  114. $order = 'sale_sort';
  115. } else {
  116. $order = 'gs.' . $order;
  117. }
  118. $order_by = $order . ' ' . $sort;
  119. } else {
  120. $config_model = new ConfigModel();
  121. $sort_config = $config_model->getGoodsSort($this->site_id);
  122. $sort_config = $sort_config[ 'data' ][ 'value' ];
  123. $order_by = 'g.sort ' . $sort_config[ 'type' ] . ',g.create_time desc';
  124. }
  125. $condition[] = [ 'g.goods_state', '=', 1 ];
  126. $condition[] = [ 'g.is_delete', '=', 0 ];
  127. $condition[] = [ '', 'exp', Db::raw("(g.sale_channel = 'all' OR g.sale_channel = 'offline')") ];
  128. $alias = 'gs';
  129. $field = 'gs.is_consume_discount,gs.discount_config,gs.discount_method,gs.member_price,gs.goods_id,gs.sort,gs.sku_id,gs.sku_name,gs.price,gs.market_price,gs.discount_price,gs.stock,(g.sale_num + g.virtual_sale) as sale_num,(gs.sale_num + gs.virtual_sale) as sale_sort,gs.sku_image,gs.goods_name,gs.site_id,gs.is_free_shipping,gs.introduction,gs.promotion_type,g.goods_image,g.promotion_addon,gs.is_virtual,g.goods_spec_format,g.recommend_way,gs.max_buy,gs.min_buy,gs.unit,gs.is_limit,gs.limit_type,g.label_name,g.stock_show,g.sale_show,g.market_price_show,g.barrage_show,g.sale_channel,g.sale_store,
  130. gc.card_type,gc.card_type_name,gc.renew_price,gc.recharge_money,gc.common_num,gc.discount_goods_type,gc.discount,gc.validity_type,gc.validity_day,gc.validity_time';
  131. $join = [
  132. [ 'goods g', 'gs.sku_id = g.sku_id', 'inner' ],
  133. [ 'goods_card gc', 'gc.goods_id=gs.goods_id','left'],
  134. ];
  135. // 如果是连锁运营模式
  136. if ($this->store_data['config']['store_business'] == 'store') {
  137. $join[] = [ 'store_goods_sku sgs', 'g.sku_id = sgs.sku_id and sgs.store_id=' . $this->store_id, 'left' ];
  138. $condition[] = [ 'g.sale_store', 'like', [ '%all%', '%,' . $this->store_id . ',%' ], 'or' ];
  139. $condition[] = [ 'sgs.status', '=', 1];
  140. $field = str_replace('gs.price', 'IFNULL(IF(g.is_unify_price = 1,gs.price,sgs.price), gs.price) as price', $field);
  141. $field = str_replace('gs.discount_price', 'IFNULL(IF(g.is_unify_price = 1,gs.price,sgs.price), gs.price) as discount_price', $field);
  142. if ($this->store_data['store_info']['stock_type'] == 'store' ) {
  143. $field = str_replace('gs.stock', 'IFNULL(sgs.stock, 0) as stock', $field);
  144. }
  145. }
  146. $goods = new Goods();
  147. $list = $goods->getGoodsSkuPageList($condition, $page, $page_size, $order_by, $field, $alias, $join);
  148. return $this->response($list);
  149. }
  150. public function getCardListByType()
  151. {
  152. $goods = new CardGoods();
  153. $list = $goods->getCardType();
  154. $condition = [
  155. [ 'gs.site_id', '=', $this->site_id ],
  156. [ 'g.goods_class', '=', $goods->getGoodsClass()['id'] ],
  157. [ 'g.goods_state', '=', 1 ],
  158. [ 'g.is_delete', '=', 0 ],
  159. [ '', 'exp', Db::raw("(g.sale_channel = 'all' OR g.sale_channel = 'online')") ]
  160. ];
  161. $field = 'gs.is_consume_discount,gs.discount_config,gs.discount_method,gs.member_price,gs.goods_id,gs.sort,gs.sku_id,gs.sku_name,gs.price,gs.market_price,gs.discount_price,gs.stock,(g.sale_num + g.virtual_sale) as sale_num,(gs.sale_num + gs.virtual_sale) as sale_sort,gs.sku_image,gs.goods_name,gs.site_id,gs.is_free_shipping,gs.introduction,gs.promotion_type,g.goods_image,g.promotion_addon,gs.is_virtual,g.goods_spec_format,g.recommend_way,gs.max_buy,gs.min_buy,gs.unit,gs.is_limit,gs.limit_type,g.label_name,g.stock_show,g.sale_show,g.market_price_show,g.barrage_show,g.sale_channel,g.sale_store,
  162. gc.card_type,gc.card_type_name,gc.renew_price,gc.recharge_money,gc.common_num,gc.discount_goods_type,gc.discount,gc.validity_type,gc.validity_day,gc.validity_time';
  163. $join = [
  164. [ 'goods_sku gs', 'gs.sku_id = g.sku_id', 'inner' ],
  165. [ 'goods_card gc', 'gc.goods_id=gs.goods_id','left'],
  166. ];
  167. // 如果是连锁运营模式
  168. if ($this->store_data['config']['store_business'] == 'store') {
  169. $join[] = [ 'store_goods_sku sgs', 'g.sku_id = sgs.sku_id and sgs.store_id=' . $this->store_id, 'left' ];
  170. $condition[] = [ 'g.sale_store', 'like', [ '%all%', '%,' . $this->store_id . ',%' ], 'or' ];
  171. $condition[] = [ 'sgs.status', '=', 1];
  172. $field = str_replace('gs.price', 'IFNULL(IF(g.is_unify_price = 1,gs.price,sgs.price), gs.price) as price', $field);
  173. $field = str_replace('gs.discount_price', 'IFNULL(IF(g.is_unify_price = 1,gs.price,sgs.price), gs.price) as discount_price', $field);
  174. if ($this->store_data['store_info']['stock_type'] == 'store' ) {
  175. $field = str_replace('gs.stock', 'IFNULL(sgs.stock, 0) as stock', $field);
  176. }
  177. }
  178. $card_list = $goods->getGoodsList($condition, $field, 'g.sort asc', 'g', $join);
  179. foreach ($list as $k => $v){
  180. $list[$k]['card_list'] = [];
  181. foreach ($card_list['data'] as $key => $val){
  182. if($val['card_type'] == $v['type']){
  183. $list[$k]['card_list'][] = $val;
  184. }
  185. }
  186. if(empty($list[$k]['card_list'])) unset($list[$k]);
  187. }
  188. return $this->response($this->success($list));
  189. }
  190. }