StoreController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. namespace addons\Mch\api\modules\v1\controllers;
  3. use addons\Coupon\common\models\AddonsCoupon;
  4. use addons\Coupon\common\models\AddonsCouponUserRelate;
  5. use addons\Mch\common\models\Mch;
  6. use addons\Mch\common\models\MchCate;
  7. use addons\Store\common\enums\StoreEnum;
  8. use addons\Store\common\models\Store;
  9. use addons\Store\common\models\StoreCate;
  10. use addons\Store\common\models\StoreClerk;
  11. use addons\Store\common\models\StoreClerkLog;
  12. use addons\Store\common\models\StoreGoods;
  13. use addons\Store\common\models\StoreGoodsCate;
  14. use addons\Store\common\models\StoreGoodsCateRelate;
  15. use addons\Store\common\models\StoreNotice;
  16. use addons\Store\common\models\StoreOrder;
  17. use common\enums\AddonsEnum;
  18. use common\enums\StatusEnum;
  19. use common\models\goods\Goods;
  20. use common\models\goods\GoodsCate;
  21. use common\models\order\Order;
  22. use common\models\order\OrderComments;
  23. use common\models\user\User;
  24. use Yii;
  25. use api\controllers\OnAuthController;
  26. use common\helpers\DebrisHelper;
  27. use common\models\common\AddonsMarketingSetting;
  28. /**
  29. * 默认控制器
  30. *
  31. * Class DefaultController
  32. * @package addons\Store\api\modules\v1\controllers
  33. */
  34. class StoreController extends OnAuthController
  35. {
  36. public $modelClass = '';
  37. /**
  38. * 不用进行登录验证的方法
  39. *
  40. * 例如: ['index', 'update', 'create', 'view', 'delete']
  41. * 默认全部需要验证
  42. *
  43. * @var array
  44. */
  45. protected $authOptional = ['index'];
  46. /**
  47. * 首页
  48. *
  49. * @return string
  50. */
  51. public function actionIndex()
  52. {
  53. $params = Yii::$app->request->get();
  54. $mch_id = $params['mch_id'];
  55. if (empty($mch_id)) return $this->error('参数错误');
  56. $mall_id = $this->mall_id;
  57. $store = Mch::getOne([['id' => $mch_id], ['mall_id' => $mall_id]]);
  58. if (empty($store)) return $this->error('店铺打烊了');
  59. $data['store_status'] =$store['shop_status']==1?'正在营业': '休息中';
  60. $data['mch_id'] = $store['id'];
  61. $data['logo_img'] = $store['logo_img'];
  62. $data['store_name'] = $store['store_name'];
  63. $data['store_background'] = $store['store_background'];
  64. $data['store_style'] = $store['store_style'];
  65. $data['sales_volume'] = $store['sales_volume'];
  66. $data['praise'] = $store['praise'];//好评
  67. $intra_city_distribution = json_decode($store['intra_city_distribution'], true);
  68. $data['distribution_fee'] = $intra_city_distribution['distribution_fee'] ?? 0;//配送费
  69. $comment_where = [['mch_id' => $mch_id], ['mall_id' => $mall_id], ['status' => StatusEnum::ENABLED]];
  70. $order_comment_count = OrderComments::listCount(['where' => $comment_where,]);
  71. $data['order_comment_count'] = $order_comment_count??0;//评论条数
  72. $goods_cate_list = GoodsCate::lists([
  73. 'where' => [
  74. ['mall_id' => $mall_id],
  75. ['status' => StatusEnum::ENABLED],
  76. ['is_show' => 1],
  77. ],
  78. 'select' => 'id,name'
  79. ]);
  80. $data['goods_cate_list'] = $goods_cate_list;
  81. return $this->success('成功', $data);
  82. }
  83. //商户列表
  84. public function actionStoreList()
  85. {
  86. $params = Yii::$app->request->get();
  87. $sort = $params['sort'] ?? '';
  88. $mall_id = $this->mall_id;
  89. $status = StatusEnum::ENABLED;
  90. $shop_status = 1;
  91. $check_status = 1;
  92. $where[] = ['mall_id' => $mall_id];
  93. $where[] = ['status' => $status];
  94. $where[] = ['shop_status' => $shop_status];
  95. $where[] = ['check_status' => $check_status];
  96. if (!empty($params['keyword'])) $where[] = ['like', 'store_name', trim($params['keyword']) . '%', false];
  97. if (!empty($params['cate_id'])) $where[] = ['c_id' => $params['cate_id']];
  98. if (in_array($sort, ['sales_volume', 'praise'])) $params['order'] = "{$sort} desc";
  99. $params['where'] = $where;
  100. $list_page = Mch::listPage($params);
  101. return $this->success('成功', $list_page);
  102. }
  103. public function actionStoreCate()
  104. {
  105. $list = GoodsCate::lists(['where' => ['mall_id' => $this->mall_id, 'is_show' => 1, 'status' => StatusEnum::ENABLED], 'select' => 'id,parent_id,name']);
  106. $list = GoodsCate::getTreeCate($list, 0);
  107. return $this->success('ok', compact('list'));
  108. }
  109. public function actionCate()
  110. {
  111. $params = \Yii::$app->request->get();
  112. $cate_id = empty($params['cate_id']) ? 0 : $params['cate_id'];
  113. $params['where'] = [['mall_id' => $this->mall_id, 'parent_id' => $cate_id, 'is_show' => 1, 'status' => StatusEnum::ENABLED]];
  114. $params['select'] = 'id,name,pic,advert_pic,advert_url';
  115. $params['order'] = 'sort desc';
  116. $cate_list = MchCate::listPage($params);
  117. if (false === $cate_list) {
  118. return $this->error(MchCate::getStaticError());
  119. }
  120. if (!empty($cate_list['list'])) {
  121. foreach ($cate_list['list'] as &$lv) {
  122. if (!empty($lv['advert_pic'])) {
  123. $lv['advert_pic'] = json_decode($lv['advert_pic'], true);
  124. if(!empty($lv['advert_pic'])){
  125. $temp = [];
  126. foreach ($lv['advert_pic'] as $value){
  127. $temp[] = ['pic_url'=>$value];
  128. }
  129. $lv['advert_pic'] = $temp;
  130. }
  131. }
  132. if (!empty($lv['advert_url'])) {
  133. $lv['advert_url'] = json_decode($lv['advert_url'], true);
  134. }
  135. }
  136. }
  137. $cate = MchCate::find()
  138. ->select('id,name,pic,advert_pic,advert_url')
  139. ->where(['mall_id' => $this->mall_id, 'id' => $cate_id, 'is_show' => 1, 'status' => StatusEnum::ENABLED])
  140. ->asArray()
  141. ->one();
  142. if (!empty($cate['advert_pic'])) {
  143. $cate['advert_pic'] = json_decode($cate['advert_pic'], true);
  144. if(!empty( $cate['advert_pic'] )){
  145. $temp = [];
  146. foreach ($cate['advert_pic'] as $value){
  147. $temp[] = ['pic_url'=>$value];
  148. }
  149. $cate['advert_pic'] = $temp;
  150. }
  151. }
  152. if (!empty($cate['advert_url'])) {
  153. $cate['pic_url'] = json_decode($cate['advert_url'], true);
  154. }
  155. $cate_list['cate_info'] = $cate;
  156. return $this->success('操作成功', $cate_list);
  157. }
  158. /**
  159. * @name:
  160. * @msg: 添加门店聚合页--为你优选商品列表接口
  161. * @return {*}
  162. */
  163. public function actionOptimizationGoodsList()
  164. {
  165. if (!\Yii::$app->request->isGet) {
  166. return $this->error('请求方式错误');
  167. }
  168. $longitude = \Yii::$app->request->get('longitude');
  169. $latitude = \Yii::$app->request->get('latitude');
  170. if (empty($longitude) || empty($latitude)) {
  171. return $this->error('参数错误');
  172. }
  173. $wheres = ['mall_id' => $this->mall_id, 'addons_name' => StoreEnum::ADDONS_NAME, 'status' => StatusEnum::ENABLED];
  174. $res = AddonsMarketingSetting::findOne($wheres);
  175. if ($res == false) return $this->success('success', []);
  176. $data = $res->toArray();
  177. $content = json_decode($data['content'], true);
  178. // dd($content);
  179. if (empty($content[4]['data']['goods'])) {
  180. return $this->success('success', []);
  181. }
  182. $good_ids = [];
  183. foreach ($content[4]['data']['goods'] as $good) {
  184. $good_ids[] = $good['params']['id'] ?? 0;
  185. }
  186. $good_ids = array_unique($good_ids);
  187. $goods = Goods::find()
  188. ->alias('sg')
  189. ->select('sg.*')
  190. ->join('INNER JOIN', Mch::tableName() . ' as s', 'sg.mch_id = s.id')
  191. ->with(['attr'])
  192. ->where(['sg.mall_id' => $this->mall_id, 'sg.status' => StatusEnum::ENABLED, 'sg.id' => $good_ids, 'sg.check_status' => StoreEnum::CHECK_ADOPT, 'sg.is_on_sale' => 1, 's.status' => StatusEnum::ENABLED, 's.check_status' => StoreEnum::CHECK_ADOPT])
  193. ->andWhere(['or', ['>', 'expire_time', time()], ['expire_time' => 0]])
  194. ->asArray()
  195. ->all();
  196. if (!empty($goods)) {
  197. $mch_ids = array_column($goods, 'mch_id');
  198. $comment_scores = OrderComments::find()
  199. ->select('goods_id,score')
  200. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'mch_id' => $mch_ids, 'goods_id' => $good_ids])
  201. ->asArray()
  202. ->all();
  203. // dd($comment_scores);
  204. $good_comment_rate_map = [];
  205. if (!empty($comment_scores)) {
  206. foreach ($comment_scores as $val) {
  207. if (empty($scores[$val['goods_id']])) {
  208. $scores[$val['goods_id']] = [
  209. 'all' => 0,
  210. 'good_comment' => 0,
  211. ];
  212. }
  213. $scores[$val['goods_id']]['all'] += 1;
  214. if ($val['score'] >= 4) {
  215. $scores[$val['goods_id']]['good_comment'] += 1;
  216. }
  217. }
  218. foreach ($scores as $goods_id => $score) {
  219. if ($score['all'] > 0) {
  220. $good_comment_rate_map[$goods_id] = bcdiv($score['good_comment'], $score['all'], 2);
  221. } else {
  222. $good_comment_rate_map[$goods_id] = 0;
  223. }
  224. }
  225. }
  226. foreach ($goods as $gk => &$good) {
  227. $good['good_comment_rate'] = ($good_comment_rate_map[$good['id']] ?? 0) * 100;
  228. }
  229. }
  230. return $this->success('success', $goods);
  231. }
  232. public function actionOptimizationStoresList()
  233. {
  234. if (!\Yii::$app->request->isGet) {
  235. return $this->error('请求方式错误');
  236. }
  237. $wheres = ['mall_id' => $this->mall_id, 'addons_name' => AddonsEnum::ADDONS_NAME_MCH, 'status' => StatusEnum::ENABLED];
  238. $res = AddonsMarketingSetting::findOne($wheres);
  239. if ($res == false) return $this->success('success', []);
  240. $data = $res->toArray();
  241. $content = json_decode($data['content'], true);
  242. if (empty($content[3]['data']['goods'])) {
  243. return $this->success('success', []);
  244. }
  245. $mch_ids = [];
  246. foreach ($content[3]['data']['goods'] as $good) {
  247. $mch_ids[] = $good['params']['id'] ?? 0;
  248. }
  249. $mch_ids = array_unique($mch_ids);
  250. $mch_list = Mch::find()
  251. ->select('id,store_name,logo_img,praise')
  252. ->where( ['mall_id' => $this->mall_id,'status' => StatusEnum::ENABLED, 'check_status' => StoreEnum::CHECK_ADOPT, 'id' => $mch_ids])
  253. ->andWhere(['or', ['>', 'expire_time', time()], ['expire_time' => 0]])
  254. ->asArray()
  255. ->all();
  256. if (!empty($mch_list)) {
  257. $good_sales = Goods::find()
  258. ->select('mch_id,sum(sales_num) as sales_num,sum(virtual_sales) as virtual_sales')
  259. ->where(['mall_id' => $this->mall_id,'status' => StatusEnum::ENABLED, 'check_status' => StoreEnum::CHECK_ADOPT, 'mch_id' => $mch_ids])
  260. ->groupBy('mch_id')
  261. ->asArray()
  262. ->indexBy('mch_id')
  263. ->all();
  264. foreach ($mch_list as &$mch) {
  265. $sales = $good_sales[$mch['id']] ?? [];
  266. $mch['sales_volume'] = $sales['sales_num'] + $sales['virtual_sales'];
  267. }
  268. }
  269. return $this->success('success', $mch_list);
  270. }
  271. //开屏优惠券列表
  272. public function actionOpenScreenCouponList()
  273. {
  274. $where[] = ['mall_id' => $this->mall_id];
  275. $where[] = ['coupon_status' => 2];
  276. $where[] = ['is_open_screen' => 1];
  277. $where[] = ['status' => StatusEnum::ENABLED];
  278. $where[] = ['<>', 'mch_id', 0];
  279. $params['order'] = 'id desc';
  280. $params['where'] = $where;
  281. $params['select'] = 'id,mch_id,name as coupon_name,discount,discount_limit,min_price,sub_price,type,mall_id,store_id,expire_type,expire_day,begin_at,end_at';
  282. $platform_coupon_list = AddonsCoupon::lists($params);
  283. $params['select'] = 'id,name as coupon_name,discount,discount_limit,min_price,sub_price,type,mall_id,store_id,expire_type,
  284. expire_day,begin_at,end_at,first_frame,video_url,watch_times';
  285. $platform_coupon_list = $this->formatCouponList($platform_coupon_list, 1);
  286. $store_coupon_list = [];//预留
  287. $list = array_merge($platform_coupon_list, $store_coupon_list);
  288. return $this->success('success', $list);
  289. }
  290. protected function formatCouponList($coupon_list, $from_type)
  291. {
  292. $coupon_ids = array_column($coupon_list, 'id');
  293. $relate_list = AddonsCouponUserRelate::lists([
  294. 'where' => [
  295. ['coupon_id' => $coupon_ids],
  296. ['user_id' => $this->user_id]
  297. ],
  298. 'index' => 'coupon_id'
  299. ]);
  300. foreach ($coupon_list as &$item) {
  301. $item['coupon_id'] = $item['id'];
  302. $item['from_type'] = $from_type;
  303. $item['from_type_text'] = $from_type == 1 ? '平台券' : '商户券';
  304. $item['is_receive'] = $item['is_use'] = $item['user_coupon_id'] = 0;
  305. if (isset($relate_list[$item['id']])) {
  306. $item['is_receive'] = 1;
  307. $item['is_use'] = $relate_list[$item['id']]['is_use'];
  308. $item['user_coupon_id'] = $relate_list[$item['id']]['id'];
  309. }
  310. }
  311. return $coupon_list;
  312. }
  313. }