StoreController.php 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  1. <?php
  2. namespace addons\Store\api\modules\v1\controllers;
  3. use addons\Coupon\common\enums\AddonsCouponEnum;
  4. use addons\Coupon\common\models\AddonsCoupon;
  5. use addons\Coupon\common\models\AddonsCouponUserRelate;
  6. use addons\Coupon\common\models\CouponStoreGoodsRelate;
  7. use addons\ReservationService\common\models\ReservationServiceStoreGoods;
  8. use addons\Store\common\enums\StoreEnum;
  9. use addons\Store\common\logic\store\StoreEntryLimit;
  10. use addons\Store\common\models\Store;
  11. use addons\Store\common\models\StoreCate;
  12. use addons\Store\common\models\StoreClerk;
  13. use addons\Store\common\models\StoreClerkLog;
  14. use addons\Store\common\models\StoreCollect;
  15. use addons\Store\common\models\StoreCoupon;
  16. use addons\Store\common\models\StoreCouponCatRelate;
  17. use addons\Store\common\models\StoreCouponGoodsRelate;
  18. use addons\Store\common\models\StoreCouponShareLog;
  19. use addons\Store\common\models\StoreCouponUserRelate;
  20. use addons\Store\common\models\StoreGoods;
  21. use addons\Store\common\models\StoreGoodsCate;
  22. use addons\Store\common\models\StoreMiniCode;
  23. use addons\Store\common\models\StoreNotice;
  24. use addons\Store\common\services\StoreOrderService;
  25. use addons\Store\common\services\StoreReserveSettingService;
  26. use addons\Store\common\services\StoreService;
  27. use addons\Store\common\services\StoreSettingService;
  28. use addons\Store\common\services\StoreAssociatedUserService;
  29. use addons\TeamLeader\common\models\UserPartitionRelationship;
  30. use common\enums\AddonsEnum;
  31. use common\enums\StatusEnum;
  32. use common\models\common\Region;
  33. use common\models\mall\MallAddons;
  34. use common\models\order\Order;
  35. use common\models\order\OrderComments;
  36. use common\models\user\User;
  37. use Yii;
  38. use api\controllers\OnAuthController;
  39. use common\helpers\DebrisHelper;
  40. use common\models\common\AddonsMarketingSetting;
  41. use common\enums\OrderStatusEnum;
  42. use common\enums\ShippingTypeEnum;
  43. use addons\Store\common\services\StoreTrackingService;
  44. /**
  45. * 默认控制器
  46. *
  47. * Class DefaultController
  48. * @package addons\Store\api\modules\v1\controllers
  49. */
  50. class StoreController extends OnAuthController
  51. {
  52. public $modelClass = '';
  53. /**
  54. * 不用进行登录验证的方法
  55. *
  56. * 例如: ['index', 'update', 'create', 'view', 'delete']
  57. * 默认全部需要验证
  58. *
  59. * @var array
  60. */
  61. protected $authOptional = ['index','get-content-by-code','store-list', 'store-more'];
  62. /**
  63. * 首页
  64. *
  65. * @return string
  66. */
  67. public function actionIndex()
  68. {
  69. date_default_timezone_set('PRC');
  70. $params = Yii::$app->request->get();
  71. $store_id = $params['store_id'];
  72. if (empty($store_id)) {
  73. return $this->error('参数错误');
  74. }
  75. $mall_id = $this->mall_id;
  76. $store = Store::getOne([['id' => $store_id], ['mall_id' => $mall_id]]);
  77. if (empty($store)) return $this->error('店铺打烊了');
  78. if(empty($store['status'])) return $this->error('该门店已下架');
  79. $data['store_status'] = StoreService::checkBusinessTime($store, 'front') ?? '';
  80. // $virtual_sales = StoreGoods::find()->where(['mall_id'=>$mall_id,'store_id'=>$store_id, 'status' => StatusEnum::ENABLED])->sum('virtual_sales');
  81. $data['store_id'] = $store['id'];
  82. $data['bannar_pic'] = !empty($store['bannar_pic'])?json_decode($store['bannar_pic'],true):[];
  83. $data['introduce'] = !empty($store['introduce'])?json_decode($store['introduce'],true):[];
  84. $data['freight_type'] = !empty($store['freight_type'])?json_decode($store['freight_type'],true):[];
  85. $data['logo_img'] = $store['logo_img'];
  86. $data['store_img'] = $store['store_img'];
  87. $data['store_desc'] = $store['store_desc'];
  88. $data['store_name'] = $store['store_name'];
  89. $data['store_background'] = $store['store_background'];
  90. $data['store_background'] = $store['store_background'];
  91. $decoration = \Yii::$app->services->setting->addons->get($mall_id, StoreEnum::ADDONS_NAME, 'decoration');
  92. // 是否开启分类
  93. if(!empty($decoration['custom_store_style'])){
  94. $data['store_category_switch'] = $store['store_category_switch'] ?? 0;
  95. $data['store_style'] = $store['show_style']??1;
  96. $data['default_store_cate_style'] = $store['default_store_cate_style']??1;
  97. }else{
  98. $data['store_category_switch'] = $decoration['store_category_switch'] ?? 0;
  99. $data['store_style'] = $decoration['default_store_style']??1;
  100. $data['default_store_cate_style'] = $decoration['default_store_cate_style']??1;
  101. }
  102. $data['license_img'] = $store['license_img'];
  103. $store_score_deduction = (new StoreSettingService(['mall_id'=>$this->mall_id]))->getDeductionProportion(['store_id'=>[$store['id']]]);
  104. $data['store_score_deduction'] = $store_score_deduction[$store['id']]??[];
  105. $data['sales_volume'] = $store['sales_volume'];
  106. $data['latitude'] = $store['latitude'];
  107. $data['praise'] = $store['praise'];//好评
  108. $intra_city_distribution = json_decode($store['intra_city_distribution'], true);
  109. $data['distribution_fee'] = $intra_city_distribution['distribution_fee'] ?? 0;//配送费
  110. $notice_list = StoreNotice::lists(['where' => [['store_id' => $store_id], ['mall_id' => $mall_id], ['status' => StatusEnum::ENABLED]],
  111. 'order' => 'id desc', 'select' => 'id,title,content', 'limit' => 1]);
  112. $data['notice_list'] = $notice_list;
  113. $comment_where = [['store_id' => $store_id], ['mall_id' => $mall_id], ['status' => StatusEnum::ENABLED]];
  114. $order_comment_count = OrderComments::listCount(['where' => $comment_where,]);
  115. $data['order_comment_count'] = $order_comment_count ?? 0;//评论条数
  116. $store_goods_cate_list = StoreGoodsCate::lists([
  117. 'where' => [
  118. ['store_id' => $store_id],
  119. ['mall_id' => $mall_id],
  120. ['status' => StatusEnum::ENABLED],
  121. ['is_show' => 1],
  122. ],
  123. 'select' => 'id,name',
  124. 'order' => 'sort asc',
  125. ]);
  126. $data['store_goods_cate_list'] = $store_goods_cate_list;
  127. $store_goods_cate_list = StoreGoodsCate::lists([
  128. 'where' => [
  129. ['store_id' => $store_id],
  130. ['mall_id' => $mall_id],
  131. ['status' => StatusEnum::ENABLED],
  132. ['is_show' => 1],
  133. ['parent_id' => 0],
  134. ],
  135. 'with'=> ['child'=>function ($q) {
  136. $q->select('id,name,parent_id')->where(['status' => StatusEnum::ENABLED]);
  137. },'child.child'=>function ($q) {
  138. $q->select('id,name,parent_id')->where(['status' => StatusEnum::ENABLED]);
  139. }],
  140. 'select' => 'id,name,parent_id',
  141. 'order' => 'sort asc',
  142. ]);
  143. $data['store_goods_cate_list_unlimited'] = $store_goods_cate_list;
  144. $select = 'id,name,store_id,type,expire_type,expire_day,effect_days,begin_at,end_at,appoint_type,rule,discount_limit,sub_price,discount,min_price,coupon_status';
  145. $coupon_list = StoreCoupon::lists([
  146. 'where' => [
  147. ['store_id' => $store['id']],
  148. ['status' => StatusEnum::ENABLED],
  149. ['coupon_status' => AddonsCouponEnum::STATUS_RECEIVING],
  150. ],
  151. 'select' => $select,
  152. 'limit' => 6
  153. ]);
  154. $coupon_ids = array_column($coupon_list, 'id');
  155. $relate_list = StoreCouponUserRelate::lists(['where' => [['coupon_id' => $coupon_ids],['user_id'=>$this->user_id], ['from_type' => 2]], 'index' => 'coupon_id']);
  156. foreach ($coupon_list as &$item) {
  157. $item['is_receive'] = 0;
  158. if (isset($relate_list[$item['id']])) {
  159. $item['is_receive'] = 1;
  160. $item['is_use'] = $relate_list[$item['id']]['is_use'];
  161. }
  162. }
  163. $data['coupon_list'] = $coupon_list ?? [];
  164. $data['coupon_counts'] = count($data['coupon_list']);
  165. $data['shop_mobile'] = $store['shop_mobile'];
  166. $data['business_time_start'] = $store['business_time_start'];
  167. $data['business_time_end'] = $store['business_time_end'];
  168. $store_cate = StoreCate::findOne(['id' => $store['c_id']]);
  169. $data['store_cate_name'] = $store_cate['name'];
  170. $data['distance'] = 0;
  171. if (!empty($params['longitude']) && !empty($params['latitude'])) {
  172. // $distance = (new LocationHelper())->getDistanceByGeo($params['latitude'], $params['longitude'], $store['latitude'], $store['longitude']);
  173. //为了和下面列表“actionStoreList()”输出的经纬度保持一致,所以使用新的方法来计算“自己和商家的距离”
  174. $distance = round(
  175. 2 * 6378.137 * ASIN(
  176. SQRT(
  177. POW(
  178. SIN(
  179. PI() * ($params['longitude'] - $store['longitude']) / 360
  180. ),
  181. 2
  182. ) + COS(PI() * $params['latitude'] / 180) * COS($store['latitude'] * PI() / 180) * POW(
  183. SIN(
  184. PI() * ($params['latitude'] - $store['latitude']) / 360
  185. ),
  186. 2
  187. )
  188. )
  189. )
  190. ,1);
  191. $data['distance'] = bcmul($distance, 1, 1);
  192. }
  193. $data['address'] = $store['address'];
  194. $data['longitude'] = $store['longitude'];
  195. $data['latitude'] = $store['latitude'];
  196. $recommend_goods_list = StoreGoods::getRecommendList($mall_id,$store_id);
  197. $data['recommend_goods_list'] = $recommend_goods_list;
  198. $store_collect = StoreCollect::getOne([['store_id'=>$store['id']],['user_id'=>$this->user_id],['status'=>StatusEnum::ENABLED]]);
  199. $data['is_collect'] = !empty($store_collect)?1:0;
  200. $data['share_title'] = $store['share_title'];
  201. $data['share_desc'] = $store['share_desc'];
  202. $data['share_pic'] = $store['share_pic'];
  203. $data['show_sync_goods'] = $store['show_sync_goods'];
  204. // 客服配置信息
  205. $data['customer'] = StoreService::getCustomer($mall_id, $store_id);
  206. // 判断是否安装智慧城市插件(此功能为智慧城市客服定制)
  207. $data['is_install_smart_city'] = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_SMART_CITY) ? StatusEnum::ENABLED : StatusEnum::DISABLED;
  208. // 是否显示技师列表
  209. $is_show_staff = 0;
  210. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_RESERVATION_SERVICE)) {
  211. $is_show_staff = StatusEnum::ENABLED;
  212. $store_reserve_setting = (new StoreReserveSettingService(['mall_id' => $this->mall_id, 'store_id' => $store_id]))->detail();
  213. if (!empty($store_reserve_setting['employee_selection']) && $store_reserve_setting['employee_selection'] == 3) {
  214. $is_show_staff = 0;
  215. } else {
  216. $goods_ids = ReservationServiceStoreGoods::find()->where(['store_id' => $store, 'status' => StatusEnum::ENABLED])->select('goods_id')->column();
  217. $res = StoreGoods::find()->where(['id' => $goods_ids, 'store_id' => $store_id, 'mall_id' => $this->mall_id, 'check_status' => 1, 'is_on_sale' => 1, 'status' => StatusEnum::ENABLED])
  218. ->one();
  219. if (empty($res)) {
  220. $is_show_staff = 0;
  221. }
  222. }
  223. }
  224. $data['is_show_staff'] = $is_show_staff;
  225. //新增浏览记录
  226. $StoreTrackingService = new StoreTrackingService();
  227. $trackingData = [
  228. 'store_id' => $params['store_id'],
  229. 'mall_id' => $mall_id,
  230. 'user_id' => $this->user_id
  231. ];
  232. $StoreTrackingService->addStoreTracking($trackingData);
  233. return $this->success('成功', $data);
  234. }
  235. //店铺列表
  236. public function actionStoreList()
  237. {
  238. $params = Yii::$app->request->get();
  239. $longitude = $params['longitude'] ?? 0;//经度
  240. $latitude = $params['latitude'] ?? 0;//纬度
  241. $city_name = $params['city_name'] ?? '';
  242. if ($longitude && $latitude) {
  243. $page = $params['page'] ?? 1;
  244. $limit = $params['limit'] ?? 10;
  245. // 默认按距离排序
  246. $sort = empty($params['sort']) ? 'distance' : $params['sort'];
  247. if ($sort == 'comprehensive') $sort = 'distance';
  248. $mall_id = $this->mall_id;
  249. $status = StatusEnum::ENABLED;
  250. $shop_status = 1;
  251. $check_status = 1;
  252. $sql = " SELECT id,store_name,latitude,longitude,logo_img,sales_volume,praise,show_style,bannar_pic,address,is_show_coupon,default_store_cate_style,
  253. round(
  254. 2 * 6378.137 * ASIN(
  255. SQRT(
  256. POW(
  257. SIN(
  258. PI() * ({$longitude} - longitude) / 360
  259. ),
  260. 2
  261. ) + COS(PI() * {$latitude} / 180) * COS(latitude * PI() / 180) * POW(
  262. SIN(
  263. PI() * ({$latitude} - latitude) / 360
  264. ),
  265. 2
  266. )
  267. )
  268. )
  269. ,1) AS distance
  270. FROM `qimall_addons_store` WHERE (`mall_id`={$mall_id}) AND (`status`={$status}) AND (`shop_status`={$shop_status}) AND (`check_status`={$check_status})";
  271. $query = Store::find()->where(['mall_id' => $mall_id, 'status' => $status, 'shop_status' => $shop_status, 'check_status' => $check_status]);
  272. if (!empty($params['keyword'])) {
  273. $store_goods_list = StoreGoods::lists([
  274. 'where'=>[
  275. ['mall_id'=>$this->mall_id],
  276. ['check_status'=>1],
  277. ['is_on_sale'=>1],
  278. ['status'=>StatusEnum::ENABLED],
  279. ['like', 'goods_name', trim($params['keyword'])],
  280. ],
  281. 'select'=>'id,store_id,goods_name,price,original_price,cover_pic',
  282. 'order'=>'id desc',
  283. ]);
  284. $store_id_arr = array_column($store_goods_list,'store_id');
  285. $query->andWhere([
  286. 'or',
  287. ['like', 'store_name', trim($params['keyword'])],
  288. ['id'=>$store_id_arr]
  289. ]);
  290. $store_ids = join(',',$store_id_arr);
  291. if(empty($store_ids)) $store_ids = 0;
  292. $sql .= " and (
  293. store_name LIKE '%" . $params['keyword'] . "%' or id in ({$store_ids})
  294. )";
  295. }
  296. if (!empty($params['cate_id'])) {
  297. $query->andWhere(['c_id' => $params['cate_id']]);
  298. $cid_arr = StoreCate::find()->where(['parent_id'=>$params['cate_id'],'is_show'=>1,'status'=>1])->select('id')->column();
  299. $cid_arr = array_merge([$params['cate_id']],$cid_arr);
  300. $sql .= " and c_id in (" . implode(',',$cid_arr).')';
  301. }
  302. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic');
  303. // $is_show_location_store = $setting['is_show_location_store'] ?? 0;
  304. // if($is_show_location_store && $city_name){
  305. // $city_id = Region::find()->where(['name' => $city_name])->select('id')->scalar();
  306. // if(!empty($city_id)) {
  307. // $query->andWhere(['city_id' => $city_id]);
  308. // $sql .= " and city_id = " . $city_id;
  309. // }
  310. // }
  311. if(!empty($params['store_id'])){
  312. $query->andWhere(['id' => explode(',',$params['store_id'])]);
  313. $sql .= " and id in (" . $params['store_id'].')';
  314. }
  315. //获取门店配置
  316. $basic = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic');
  317. if ($basic['is_location_limit'] == 1) { //是否开启了限制距离按钮,开启则获取再限制距离内的门店
  318. $localtion_limit = $basic['localtion_limit'] ? $basic['localtion_limit'] : 10;
  319. $sql .= " having (`distance`<={$localtion_limit})";
  320. }
  321. if (in_array($sort, ['sales_volume', 'praise'])) {
  322. $sql .= " order by {$sort} desc";
  323. } else if ($sort == 'distance') {
  324. $sql .= " order by {$sort} asc";
  325. }
  326. // print_r($query);exit;
  327. // $count = $query->count();
  328. $count_list = \Yii::$app->db->createCommand($sql)->queryAll();
  329. $count = count($count_list);
  330. $sql .= " limit " . ($page - 1) * $limit . ",{$limit}";
  331. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  332. if (!empty($list)) {
  333. $store_ids = array_column($list, 'id');
  334. // $order_comment_arr = OrderComments::lists(['where' => [['store_id' => $store_ids]], 'group' => 'store_id', 'order' => 'created_at desc', 'select' => 'store_id,content', 'index' => 'store_id']);
  335. $select = 'id,name,store_id,type,expire_type,expire_day,effect_days,begin_at,end_at,appoint_type,rule,discount_limit,sub_price,discount,min_price,coupon_status';
  336. $coupon_list = StoreCoupon::lists([
  337. 'where' => [
  338. ['store_id' => $store_ids],
  339. ['status' => StatusEnum::ENABLED],
  340. ['coupon_status' => AddonsCouponEnum::STATUS_RECEIVING],
  341. ],
  342. 'select' => $select
  343. ]);
  344. $coupon_ids = array_column($coupon_list, 'id');
  345. $relate_list = $this->user_id ? StoreCouponUserRelate::lists(['where' => [['coupon_id' => $coupon_ids], ['from_type' => 2], ['user_id' => $this->user_id]], 'index' => 'coupon_id']) : [];
  346. $coupon_rows = [];
  347. foreach ($coupon_list as $item) {
  348. $item['is_receive'] = 0;
  349. if (isset($relate_list[$item['id']])) {
  350. $item['is_receive'] = 1;
  351. $item['is_use'] = $relate_list[$item['id']]['is_use'];
  352. }
  353. $coupon_rows[$item['store_id']][] = $item;
  354. }
  355. // $goods_list =StoreGoods::lists([
  356. // 'where'=>[['store_id'=>$store_ids]],
  357. // 'select'=>'sum(virtual_sales) as virtual_sales,sum(sales_num) as sales_num,store_id',
  358. // 'group'=>'store_id',
  359. // 'index'=>'store_id'
  360. // ]);
  361. // $location_obj = new LocationHelper();
  362. $store_goods_arr = [];
  363. if(isset($store_goods_list)){
  364. foreach ($store_goods_list as $val){
  365. $store_goods_arr[$val['store_id']][]=$val;
  366. }
  367. }
  368. $store_score_deduction = (new StoreSettingService(['mall_id'=>$this->mall_id]))->getDeductionProportion(['store_id'=>$store_ids]);
  369. $decoration = \Yii::$app->services->setting->addons->get($mall_id, StoreEnum::ADDONS_NAME, 'decoration');
  370. foreach ($list as &$item) {
  371. if(!empty($decoration['custom_store_style'])){
  372. $item['store_style'] = $item['show_style']??1;
  373. $item['default_store_cate_style'] = $item['default_store_cate_style']??1;
  374. }else{
  375. $item['store_style'] = $decoration['default_store_style']??1;
  376. $item['default_store_cate_style'] = $decoration['default_store_cate_style']??1;
  377. }
  378. $item['store_goods_list'] = $store_goods_arr[$item['id']]??[];
  379. $item['coupon_list'] = $coupon_rows[$item['id']] ?? [];
  380. $item['order_comment'] = '';
  381. $item['store_score_deduction'] = $store_score_deduction[$item['id']]??[];
  382. // $distance = $location_obj->getDistanceByGeo($item['latitude'],$item['longitude'],$latitude,$longitude);
  383. // $item['distance'] = bcmul($distance,1,1);
  384. if(empty($item['bannar_pic'])){
  385. $item['bannar_pic'] = [];
  386. }else{
  387. $item['bannar_pic'] = json_decode($item['bannar_pic'],true);
  388. }
  389. // if(isset($goods_list[$item['id']])){
  390. // $item['sales_volume'] += $goods_list[$item['id']]['virtual_sales'];
  391. // }
  392. }
  393. }
  394. $list_page['list'] = $list;
  395. $list_page['page'] = $page;
  396. $list_page['page_size'] = $limit;
  397. $list_page['count'] = $count;
  398. $list_page['page_count'] = ceil($count / $limit);
  399. } else {
  400. $list_page['list'] = [];
  401. $list_page['page'] = 0;
  402. $list_page['page_size'] = 10;
  403. $list_page['count'] = 0;
  404. $list_page['page_count'] = 0;
  405. }
  406. return $this->success('成功', $list_page);
  407. }
  408. /**
  409. * 更多门店
  410. *
  411. * @return mixed
  412. */
  413. public function actionStoreMore()
  414. {
  415. $params = Yii::$app->request->get();
  416. $service = new StoreService();
  417. $params['mall_id'] = $this->mall_id;
  418. $MoreStore = $service->getMoreStore($params);
  419. return $this->success('获取成功', compact('MoreStore'));
  420. }
  421. public function actionStoreCate()
  422. {
  423. $list = StoreCate::lists(['where' => ['mall_id' => $this->mall_id, 'is_show' => 1, 'status' => StatusEnum::ENABLED], 'select' => 'id,parent_id,name']);
  424. $list = StoreCate::getTreeMenu($list, 0);
  425. return $this->success('ok', compact('list'));
  426. }
  427. public function actionCate()
  428. {
  429. $params = \Yii::$app->request->get();
  430. $cate_id = empty($params['cate_id']) ? 0 : $params['cate_id'];
  431. $params['where'] = [['mall_id' => $this->mall_id, 'parent_id' => $cate_id, 'is_show' => 1, 'status' => StatusEnum::ENABLED]];
  432. $params['select'] = 'id,name,pic,advert_pic,advert_url';
  433. $params['order'] = 'sort desc';
  434. $cate_list = StoreCate::listPage($params);
  435. if (false === $cate_list) {
  436. return $this->error(StoreCate::getStaticError());
  437. }
  438. if (!empty($cate_list['list'])) {
  439. foreach ($cate_list['list'] as &$lv) {
  440. if (!empty($lv['advert_pic'])) {
  441. $lv['advert_pic'] = json_decode($lv['advert_pic'], true);
  442. }
  443. if (!empty($lv['advert_url'])) {
  444. $lv['advert_url'] = json_decode($lv['advert_url'], true);
  445. }
  446. }
  447. }
  448. $cate = StoreCate::find()
  449. ->select('id,name,pic,advert_pic,advert_url')
  450. ->where(['mall_id' => $this->mall_id, 'id' => $cate_id, 'is_show' => 1, 'status' => StatusEnum::ENABLED])
  451. ->asArray()
  452. ->one();
  453. if (!empty($cate['advert_pic'])) {
  454. $cate['advert_pic'] = json_decode($cate['advert_pic'], true);
  455. }
  456. if (!empty($cate['advert_url'])) {
  457. $cate['advert_url'] = json_decode($cate['advert_url'], true);
  458. }
  459. $cate_list['cate_info'] = $cate;
  460. return $this->success('操作成功', $cate_list);
  461. }
  462. //判断是否是核销员
  463. public function actionIsClerk()
  464. {
  465. $mall_id = $this->mall_id;
  466. $user_id = $this->user_id;
  467. $rse = StoreClerk::getOne([['mall_id' => $mall_id], ['user_id' => $user_id], ['status' => StatusEnum::ENABLED]]);
  468. if (empty($rse)) return $this->error('你不是核销员');
  469. return $this->success('操作成功', ['store_id' => $rse['store_id']]);
  470. }
  471. //核销
  472. public function actionClerk()
  473. {
  474. $params = Yii::$app->request->post();
  475. $storeService = new StoreService();
  476. $storeService->mall_id = $this->mall_id;
  477. $storeService->store_id = $this->store_id;
  478. $params['clerk_id'] = $storeService->getClerkIdByUserId($this->user_id);
  479. $mall_id = $this->mall_id;
  480. $user_id = $this->user_id;
  481. $params['mall_id'] = $mall_id;
  482. $rse = StoreClerk::getOne([['mall_id' => $mall_id], ['user_id' => $user_id], ['status' => StatusEnum::ENABLED]]);
  483. if (empty($rse)) return $this->error('你不是核销员');
  484. $params['store_id'] = $rse['store_id'];
  485. $params['user_id'] = $user_id;
  486. $rse = StoreClerkLog::setData($params);
  487. if ($rse == false) return $this->error(StoreClerkLog::getStaticError());
  488. return $this->success('操作成功');
  489. }
  490. //核销记录表
  491. // 暂时不要,备份
  492. // 使用新写的 actionClerkLog()
  493. public function actionClerkLogBackup()
  494. {
  495. $params = \Yii::$app->request->get();
  496. $params['mall_id'] = $this->mall_id;
  497. $params['clerk_id'] = $this->user_id;
  498. $params['where'] = [
  499. ['mall_id' => $this->mall_id],
  500. ['clerk_id' => $this->user_id],
  501. // ['store_id' => $params['store_id']],
  502. ['status' => StatusEnum::ENABLED],
  503. ];
  504. $params['order'] = 'id desc';
  505. $list_page = StoreClerkLog::listPage($params);
  506. if (!empty($list_page['list'])) {
  507. $order_nos = array_column($list_page['list'], 'order_no');
  508. $user_ids = array_column($list_page['list'], 'user_id');
  509. $user_list = User::lists(['where' => [['id' => $user_ids]], 'select' => 'id,nickname,avatar_url', 'index' => 'id']);
  510. $order_list = Order::lists([
  511. 'where' => [['order_no' => $order_nos]],
  512. 'select' => 'id,pay_money,order_no',
  513. 'with' => ['detail'],
  514. 'index' => 'order_no'
  515. ]);
  516. foreach ($list_page['list'] as &$item) {
  517. $item['nickname'] = '';
  518. $item['avatar_url'] = '';
  519. $item['clerk_status'] = '已核销';
  520. if (isset($user_list[$item['user_id']])) {
  521. $item['nickname'] = $user_list[$item['user_id']]['nickname'];
  522. $item['avatar_url'] = $user_list[$item['user_id']]['avatar_url'];
  523. }
  524. $item['details'] = [];
  525. $item['total_num'] = 0;
  526. $item['order_id'] = 0;
  527. if (isset($order_list[$item['order_no']])) {
  528. $item['order_id'] = $order_list[$item['order_no']]['id'];
  529. $details = [];
  530. foreach ($order_list[$item['order_no']]['detail'] as $val) {
  531. $details[] = [
  532. 'goods_name' => $val['goods_name'],
  533. 'num' => $val['num'],
  534. 'pic_url' => $val['pic_url'],
  535. 'price' => $val['price'],
  536. ];
  537. $item['total_num'] += $val['num'];
  538. }
  539. $item['details'] = $details;
  540. }
  541. }
  542. }
  543. return $this->success('操作成功', $list_page);
  544. }
  545. // 已核销和未核销的订单
  546. public function actionClerkLog()
  547. {
  548. $params = \Yii::$app->request->get();
  549. $params['take_status'] = $params['take_status'] ?? 0;
  550. $order_params['order'] = 'id desc';
  551. $order_params['with'] = ['detail', 'base_user'];
  552. $order_params['select'] = 'id,mall_id,pay_money,store_id,shipping_type,order_no,order_status,user_id';
  553. $order_params['where'] = [
  554. ['mall_id' => $this->mall_id],
  555. ['store_id' => $params['store_id']],
  556. ['shipping_type' => ShippingTypeEnum::PICKUP]
  557. ];
  558. if ($params['take_status'] == 1) { // 已核销
  559. $order_params['where'][] = ['order_status' => [OrderStatusEnum::TAKE_FINISH, OrderStatusEnum::ACCOMPLISH]];
  560. } else if ($params['take_status'] == 2) { // 待核销
  561. $order_params['where'][] = ['order_status' => OrderStatusEnum::TAKE];
  562. } else { // 全部
  563. $order_params['where'][] = ['order_status' => [OrderStatusEnum::TAKE, OrderStatusEnum::TAKE_FINISH, OrderStatusEnum::ACCOMPLISH]];
  564. }
  565. $order_params['limit'] = $params['limit'] ?? 10;
  566. $order_list = Order::listPage($order_params, false);
  567. $order_nos = array_column($order_list['list'], 'order_no');
  568. $clerk_log = StoreClerkLog::lists([
  569. 'where' => [['order_no' => $order_nos]],
  570. 'select' => '*',
  571. 'index' => 'order_no'
  572. ]);
  573. foreach ($order_list['list'] as $key => $item) {
  574. $item['nickname'] = $item['base_user']['nickname'] ?? '';
  575. $item['avatar_url'] = $item['base_user']['avatar_url'] ?? '';
  576. if (in_array($item['order_status'], [OrderStatusEnum::TAKE_FINISH, OrderStatusEnum::ACCOMPLISH])) {
  577. $item['clerk_status'] = '已核销';
  578. } else {
  579. $item['clerk_status'] = '待核销';
  580. }
  581. $item['details'] = $item['detail'];
  582. $item['total_num'] = count($item['detail']);
  583. $item['order_id'] = $item['id'];
  584. $item['clerk_id'] = '';
  585. if (isset($clerk_log[$item['order_no']])) {
  586. $clerk = $clerk_log[$item['order_no']];
  587. $item['clerk_id'] = $clerk['clerk_id'] ?? 0;
  588. $item['mobile'] = $clerk['mobile'] ?? '';
  589. }
  590. $item['mobile'] = $item['mobile'] ?? '';
  591. unset($item['detail'], $item['base_user']);
  592. $order_list['list'][$key] = $item;
  593. }
  594. return $this->success('操作成功', $order_list);
  595. }
  596. /**
  597. * @name:
  598. * @msg: 添加门店聚合页--为你优选商品列表接口
  599. * @return {*}
  600. */
  601. public function actionOptimizationGoodsList()
  602. {
  603. if (!\Yii::$app->request->isGet) {
  604. return $this->error('请求方式错误');
  605. }
  606. $longitude = \Yii::$app->request->get('longitude');
  607. $latitude = \Yii::$app->request->get('latitude');
  608. $is_main_mall = \Yii::$app->request->get('is_main_mall',0);
  609. $goods_id = \Yii::$app->request->get('goods_id');
  610. if (empty($longitude) || empty($latitude)) {
  611. return $this->error('您未定位,请定位后预览!');
  612. }
  613. $good_ids = [];
  614. if(!$is_main_mall){
  615. $wheres = ['mall_id' => $this->mall_id, 'addons_name' => StoreEnum::ADDONS_NAME, 'status' => StatusEnum::ENABLED];
  616. $res = AddonsMarketingSetting::findOne($wheres);
  617. if ($res == false) return $this->success('success', []);
  618. $data = $res->toArray();
  619. $content = json_decode($data['content'], true);
  620. // dd($content);
  621. if (empty($content[4]['data']['goods'])) {
  622. return $this->success('success', []);
  623. }
  624. foreach ($content[4]['data']['goods'] as $good) {
  625. $good_ids[] = $good['params']['id'] ?? 0;
  626. }
  627. $good_ids = array_unique($good_ids);
  628. }else{
  629. if(!empty($goods_id)){
  630. $good_ids = explode(',',$goods_id);
  631. }
  632. }
  633. $goods = StoreGoods::find()
  634. ->alias('sg')
  635. ->select('sg.*')
  636. ->join('INNER JOIN', Store::tableName() . ' as s', 'sg.store_id = s.id')
  637. ->with(['attr'])
  638. ->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])
  639. ->andWhere(['or', ['>', 'expire_time', time()], ['expire_time' => 0]])
  640. ->asArray()
  641. ->all();
  642. if (!empty($goods)) {
  643. $store_ids = array_column($goods, 'store_id');
  644. $stores = Store::find()
  645. ->select('id,longitude,latitude,store_name,logo_img')
  646. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'check_status' => StoreEnum::CHECK_ADOPT,'id'=>$store_ids])
  647. ->asArray()
  648. ->indexBy('id')
  649. ->all();
  650. $comment_scores = OrderComments::find()
  651. ->select('goods_id,score')
  652. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'store_id' => $store_ids, 'goods_id' => $good_ids])
  653. ->asArray()
  654. ->all();
  655. $select = 'id,name,store_id,type,expire_type,expire_day,effect_days,begin_at,end_at,appoint_type,rule,discount_limit,sub_price,discount,min_price,coupon_status';
  656. $store_coupon_goods_relates = StoreCouponGoodsRelate::lists([
  657. 'where' => [['goods_id' => $good_ids]],
  658. 'index' => 'goods_id'
  659. ]);
  660. $coupon_ids = array_column($store_coupon_goods_relates, 'coupon_id');
  661. $coupon_list = StoreCoupon::lists([
  662. 'where' => [
  663. ['id' => $coupon_ids],
  664. ['status' => StatusEnum::ENABLED],
  665. ['coupon_status' => StoreEnum::STATUS_RECEIVING],
  666. ],
  667. 'select' => $select,
  668. 'index' => 'id',
  669. 'group' => 'id'
  670. ]);
  671. $coupon_ids = array_column($coupon_list, 'id');
  672. $user_relate = StoreCouponUserRelate::lists([
  673. 'where' => [
  674. ['user_id' => $this->user_id],
  675. ['from_type' => 1],
  676. ['coupon_id' => $coupon_ids],
  677. ],
  678. 'index' => 'coupon_id',
  679. ]);
  680. // dd($comment_scores);
  681. $good_comment_rate_map = [];
  682. if (!empty($comment_scores)) {
  683. foreach ($comment_scores as $val) {
  684. if (empty($scores[$val['goods_id']])) {
  685. $scores[$val['goods_id']] = [
  686. 'all' => 0,
  687. 'good_comment' => 0,
  688. ];
  689. }
  690. $scores[$val['goods_id']]['all'] += 1;
  691. if ($val['score'] >= 4) {
  692. $scores[$val['goods_id']]['good_comment'] += 1;
  693. }
  694. }
  695. foreach ($scores as $goods_id => $score) {
  696. if ($score['all'] > 0) {
  697. $good_comment_rate_map[$goods_id] = bcdiv($score['good_comment'], $score['all'], 2);
  698. } else {
  699. $good_comment_rate_map[$goods_id] = 0;
  700. }
  701. }
  702. }
  703. foreach ($goods as $gk => &$good) {
  704. $store = $stores[$good['store_id']] ?? [];
  705. $distance = DebrisHelper::getDistance($latitude, $longitude, $store['latitude'] ?? '', $store['longitude'] ?? '');
  706. if ($distance >= 1000) {
  707. $distance = round($distance / 1000, 1) . 'km';
  708. } else {
  709. $distance .= 'm';
  710. }
  711. $good['distance'] = $distance;
  712. $good['store_name'] = $store['store_name'] ?? '';
  713. $good['logo_img'] = $store['logo_img'] ?? '';
  714. $good['good_comment_rate'] = ($good_comment_rate_map[$good['id']] ?? 0) * 100;
  715. $good['coupon_list'] = [];
  716. $goods_id = $good['id'];
  717. if (isset($store_coupon_goods_relates[$goods_id])) {
  718. $coupon_id = $store_coupon_goods_relates[$goods_id]['coupon_id'];
  719. if (isset($coupon_list[$coupon_id])) {
  720. $coupon = [
  721. 'id' => $coupon_list[$coupon_id]['id'],
  722. 'name' => $coupon_list[$coupon_id]['name'],
  723. 'is_receive' => 0,
  724. ];
  725. if (isset($user_relate[$coupon['id']])) $coupon['is_receive'] = 1;
  726. $good['coupon_list'] = $coupon;
  727. }
  728. }
  729. }
  730. }
  731. return $this->success('success', $goods);
  732. }
  733. public function actionOptimizationStoresList()
  734. {
  735. if (!\Yii::$app->request->isGet) {
  736. return $this->error('请求方式错误');
  737. }
  738. $longitude = \Yii::$app->request->get('longitude');
  739. $latitude = \Yii::$app->request->get('latitude');
  740. if (empty($longitude) || empty($latitude)) {
  741. return $this->error('参数错误');
  742. }
  743. $wheres = ['mall_id' => $this->mall_id, 'addons_name' => StoreEnum::ADDONS_NAME, 'status' => StatusEnum::ENABLED];
  744. $res = AddonsMarketingSetting::findOne($wheres);
  745. if ($res){
  746. $data = $res->toArray();
  747. $content = json_decode($data['content'], true);
  748. if (!empty($content[3]['data']['goods'])) {
  749. $store_ids = [];
  750. foreach ($content[3]['data']['goods'] as $good) {
  751. $store_ids[] = $good['params']['id'] ?? 0;
  752. }
  753. $store_ids = array_unique($store_ids);
  754. }
  755. }
  756. $where = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'check_status' => StoreEnum::CHECK_ADOPT];
  757. $stores = Store::find()
  758. ->select('id,store_name,logo_img,longitude,latitude,praise')
  759. ->where($where)
  760. ->andWhere(['or', ['>', 'expire_time', time()], ['expire_time' => 0]])
  761. ->asArray()
  762. ->all();
  763. if (!empty($stores)) {
  764. $where = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'check_status' => StoreEnum::CHECK_ADOPT];
  765. $good_sales = StoreGoods::find()
  766. ->select('store_id,sum(sales_num) as sales_num,sum(virtual_sales) as virtual_sales')
  767. ->where($where)
  768. ->groupBy('store_id')
  769. ->asArray()
  770. ->indexBy('store_id')
  771. ->all();
  772. $select = 'id,name,store_id,type,expire_type,expire_day,effect_days,begin_at,end_at,appoint_type,rule,discount_limit,sub_price,discount,min_price,coupon_status';
  773. $where = [
  774. ['status' => StatusEnum::ENABLED],
  775. ['coupon_status' => StoreEnum::STATUS_RECEIVING],
  776. ];
  777. $coupon_list = StoreCoupon::lists([
  778. 'where' => $where,
  779. 'select' => $select,
  780. 'index' => 'store_id',
  781. 'group' => 'store_id'
  782. ]);
  783. $coupon_ids = array_column($coupon_list, 'id');
  784. $user_relate = StoreCouponUserRelate::lists([
  785. 'where' => [
  786. ['user_id' => $this->user_id],
  787. ['from_type' => 1],
  788. ['coupon_id' => $coupon_ids],
  789. ],
  790. 'index' => 'coupon_id',
  791. ]);
  792. foreach ($stores as &$store) {
  793. $sales = $good_sales[$store['id']] ?? [];
  794. $store['sales_volume'] = $sales['sales_num'] + $sales['virtual_sales'];
  795. $distance = DebrisHelper::getDistance($latitude, $longitude, $store['latitude'] ?? '', $store['longitude'] ?? '');
  796. if ($distance >= 1000) {
  797. $distance = bcdiv($distance, 1000, 1) . 'km';
  798. } else {
  799. $distance .= 'm';
  800. }
  801. $store['distance'] = $distance;
  802. $store['coupon_list'] = [];
  803. $store_id = $store['id'];
  804. if (isset($coupon_list[$store_id])) {
  805. $coupon = [
  806. 'id' => $coupon_list[$store_id]['id'],
  807. 'name' => $coupon_list[$store_id]['name'],
  808. 'is_receive' => 0,
  809. ];
  810. if (isset($user_relate[$coupon['id']])) $coupon['is_receive'] = 1;
  811. $store['coupon_list'] = $coupon;
  812. }
  813. }
  814. }
  815. return $this->success('success', $stores);
  816. }
  817. //领取优惠券
  818. public function actionReceiveCoupon()
  819. {
  820. $params = \Yii::$app->request->post();
  821. $res = Yii::$app->services->validate->validate($params, [
  822. [['type', 'receive_type', 'coupon_id'], 'required'],
  823. [['coupon_share_log_id','from_type'], 'safe'],
  824. ]);
  825. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  826. $params['mall_id'] = $this->mall_id;
  827. $params['user_id'] = $this->user_id;
  828. $params['num'] = 1;//数量
  829. $params['receive_platform'] = $this->platform ?? 'h5';
  830. $params['user'] = $this->user;
  831. $user_coupon_id = 0;
  832. if ($params['type'] == 1) {
  833. //领取平台优惠券 -- 商品详情页公开领取
  834. $res = StoreCouponUserRelate::setData($params);
  835. if ($res == false) return $this->error(StoreCouponUserRelate::getStaticError());
  836. $user_coupon_id = $res;
  837. } else {
  838. if ($params['receive_type'] == 6 && empty($params['coupon_share_log_id'])) {
  839. // 扫描后台生成的海报进行领取(没有coupon_share_log_id),商城私发的优惠券也是走公开领取的逻辑
  840. $params['receive_type'] = StoreEnum::SEND_FROM1;
  841. $res = StoreCouponUserRelate::setData($params);
  842. if ($res == false) return $this->error(StoreCouponUserRelate::getStaticError());
  843. $user_coupon_id = 0;
  844. } elseif ($params['receive_type'] == 6 && !empty($params['coupon_share_log_id'])) {
  845. // 领取转赠优惠券 -- 好友转赠
  846. $params['receive_type'] = StoreEnum::SEND_FROM6;
  847. $res = StoreCouponUserRelate::receiveByGive($params);
  848. if ($res == false) return $this->error(StoreCouponUserRelate::getStaticError());
  849. } else {
  850. return $this->error('参数错误');
  851. }
  852. }
  853. return $this->success('领取成功', ['user_coupon_id' => $user_coupon_id]);
  854. }
  855. //转赠优惠券
  856. public function actionCouponGive()
  857. {
  858. if (!\Yii::$app->request->isPost) return $this->error('请求方式错误');
  859. $user_coupon_id = \Yii::$app->request->post('user_coupon_id');
  860. $receive_user_id = \Yii::$app->request->post('receive_user_id');
  861. if (empty($user_coupon_id)) return $this->error('参数错误');
  862. $wheres = [
  863. 'mall_id' => $this->mall_id,
  864. 'id' => $user_coupon_id,
  865. 'is_force_failure' => 0,
  866. 'is_use' => 0,
  867. 'status' => StatusEnum::ENABLED,
  868. 'coupon_status' => AddonsCouponEnum::STATUS_RECEIVING
  869. ];
  870. $user_coupon = StoreCouponUserRelate::find()
  871. ->where($wheres)->andWhere(['or', ['is_giving' => 0], ['is_giving' => 1, 'giving_status' => [0, 1]]])->one();
  872. if (empty($user_coupon)) return $this->error('当前优惠券不可转赠');
  873. $from_type = $user_coupon['from_type'];
  874. $coupon_id = $user_coupon['coupon_id'];
  875. $coupon_class = $from_type == 1 ? AddonsCoupon::class : StoreCoupon::class;
  876. $coupon = $coupon_class::findOne(['mall_id' => $this->mall_id, 'id' => $coupon_id, 'coupon_status' => 2, 'status' => StatusEnum::ENABLED]);
  877. if (empty($coupon)) return $this->error('优惠券不存在或者已经失效');
  878. if ($coupon['is_giving'] != 1) return $this->error('优惠券不可转赠');
  879. $params = [
  880. 'mall_id' => $this->mall_id,
  881. 'coupon_id' => $coupon_id,
  882. 'store_id' => $user_coupon['store_id'],
  883. 'user_coupon_id' => $user_coupon_id,
  884. 'giving_user_id' => $this->user_id,
  885. ];
  886. $res = StoreCouponShareLog::setData($params);
  887. if (!$res) {
  888. return $this->error(StoreCouponShareLog::getStaticError());
  889. }
  890. // 如果有接收用户ID,则执行直接转赠
  891. if (!empty($receive_user_id)) {
  892. $params = [
  893. 'mall_id' => $this->mall_id,
  894. 'coupon_share_log_id' => $res->id, // 注意这里改为 ->id 而不是 ['id']
  895. 'coupon_id' => $coupon_id,
  896. 'user_id' => $receive_user_id,
  897. 'num' => 1,
  898. 'receive_platform' => $this->platform ?? '',
  899. 'direct_gift' => 1,
  900. ];
  901. if (!StoreCouponUserRelate::receiveByGive($params)) {
  902. return $this->error(StoreCouponShareLog::getStaticError());
  903. }
  904. }
  905. return $this->success('转赠成功', ['coupon_share_log_id' => $res->id]);
  906. }
  907. public function actionMyCouponList()
  908. {
  909. $params = \Yii::$app->request->get();
  910. $from_type = !empty($params['from_type']) ? $params['from_type'] : 1;
  911. $where[] = ['mall_id' => $this->mall_id];
  912. if ($from_type == 3) {
  913. $where[] = ['user_id' => $this->user_id];
  914. //历史记录
  915. $status = !empty($params['status']) ? $params['status'] : 0;
  916. //状态
  917. switch ($status) {
  918. case 0:
  919. //正常
  920. $where[] = ['is_use' => 0];
  921. $where[] = ['is_giving' => 0];
  922. $where[] = ['coupon_status' => 2];
  923. break;
  924. case 1:
  925. //已使用
  926. $where[] = ['is_use' => 1];
  927. break;
  928. case 2:
  929. //已失效
  930. $where[] = ['coupon_status' => [3, 4]];
  931. break;
  932. case 3:
  933. //已赠送
  934. $where[] = ['is_giving' => 1];
  935. break;
  936. }
  937. $params['order'] = 'id desc';
  938. $params['where'] = $where;
  939. $params['select'] = 'id as user_coupon_id,coupon_id,store_id,start_time,end_time,from_type,updated_at,is_use,is_giving';
  940. $page_list = StoreCouponUserRelate::listPage($params);
  941. if (!empty($page_list['list'])) {
  942. $coupon_ids = array_column($page_list['list'], 'coupon_id');
  943. switch ($from_type) {
  944. case 3:
  945. $store_coupon_ids = $platform_coupon_ids = [];
  946. foreach ($page_list['list'] as $item) {
  947. switch ($item['from_type']) {
  948. case 1:
  949. $platform_coupon_ids[] = $item['coupon_id'];
  950. break;
  951. case 2:
  952. $store_coupon_ids[] = $item['coupon_id'];
  953. break;
  954. }
  955. }
  956. if (!empty($store_coupon_ids)) {
  957. $goods_arr = $this->getGoodsArr(StoreCouponGoodsRelate::class, StoreGoods::class, $store_coupon_ids);
  958. $goods_cate_arr = $this->getGoodsCateArr(StoreCouponCatRelate::class, StoreGoodsCate::class, $store_coupon_ids);
  959. $store_coupon_list = StoreCoupon::lists(['where' => [['id' => $coupon_ids]], 'index' => 'id']);
  960. }
  961. if (!empty($platform_coupon_ids)) {
  962. $store_ids = array_column($page_list['list'], 'store_id');
  963. $store_arr = Store::lists(['where' => [['id' => $store_ids]], 'index' => 'id', 'select' => 'id,store_name']);
  964. $platform_coupon_list = AddonsCoupon::lists(['where' => [['id' => $coupon_ids]], 'index' => 'id']);
  965. }
  966. break;
  967. }
  968. if ($status == 3) {
  969. $user_coupon_ids = array_column($page_list['list'], 'user_coupon_id');
  970. $share_logs = StoreCouponShareLog::lists(['where' => [['user_coupon_id' => $user_coupon_ids]], 'index' => 'user_coupon_id', 'select' => 'user_coupon_id,receive_user_id']);
  971. $receive_user_ids = array_column($share_logs, 'receive_user_id');
  972. $receive_user_list = User::lists(['where' => [['id' => $receive_user_ids]], 'index' => 'id', 'select' => 'id,mobile,nickname']);
  973. }
  974. foreach ($page_list['list'] as &$item) {
  975. $item['coupon_name'] = $item['rule'] = '';
  976. $item['appoint_type'] = $item['type'] = $item['min_price'] = $item['discount'] = $item['sub_price'] = 0;
  977. $item['goods_arr'] = $item['goods_cate_arr'] = $item['store_arr'] = [];
  978. $item['give_name'] = $item['give_mobile'] = '';
  979. $item['use_time'] = $item['use_time'] = 0;
  980. if ($item['is_use']) {
  981. $item['use_time'] = $item['updated_at'];
  982. }
  983. //赠送
  984. if ($status == 3) {
  985. if ($item['is_giving']) {
  986. $item['give_time'] = $item['updated_at'];
  987. }
  988. if (isset($share_logs[$item['user_coupon_id']])) {
  989. $receive_user_id = $share_logs[$item['user_coupon_id']]['receive_user_id'];
  990. if (isset($receive_user_list) && isset($receive_user_list[$receive_user_id])) {
  991. $item['give_name'] = $receive_user_list[$receive_user_id]['nickname'];
  992. $item['give_mobile'] = $receive_user_list[$receive_user_id]['mobile'];
  993. }
  994. }
  995. }
  996. switch ($from_type) {
  997. case 3:
  998. switch ($item['from_type']) {
  999. case 1:
  1000. if (isset($platform_coupon_list[$item['coupon_id']])) $coupon = $platform_coupon_list[$item['coupon_id']];
  1001. break;
  1002. case 2:
  1003. if (isset($store_coupon_list[$item['coupon_id']])) $coupon = $store_coupon_list[$item['coupon_id']];
  1004. break;
  1005. }
  1006. break;
  1007. }
  1008. if (!empty($coupon)) {
  1009. $item['coupon_name'] = $coupon['name'];
  1010. $item['rule'] = $coupon['rule'];
  1011. $item['appoint_type'] = $coupon['appoint_type'];
  1012. $item['type'] = $coupon['type'];
  1013. $item['min_price'] = $coupon['min_price'];
  1014. $item['discount'] = $coupon['discount'];
  1015. $item['sub_price'] = $coupon['sub_price'];
  1016. }
  1017. if (isset($goods_arr[$item['coupon_id']])) $item['goods_arr'] = $goods_arr[$item['coupon_id']];
  1018. if (isset($goods_cate_arr[$item['coupon_id']])) $item['goods_cate_arr'] = $goods_cate_arr[$item['coupon_id']];
  1019. if (isset($store_arr[$item['store_id']])) $item['store_arr'] = $store_arr[$item['store_id']];
  1020. }
  1021. }
  1022. } else {
  1023. if ($from_type == 1) {
  1024. $coupon_class = AddonsCoupon::class;
  1025. } else {
  1026. $coupon_class = StoreCoupon::class;
  1027. }
  1028. $where[] = ['coupon_status' => 2];
  1029. $where[] = ['is_public_receive' => StatusEnum::ENABLED];
  1030. $where[] = ['<>', 'store_id', ''];
  1031. $where[] = ['status'=>StatusEnum::ENABLED];
  1032. $params['order'] = 'id desc';
  1033. $params['where'] = $where;
  1034. $page_list = $coupon_class::listPage($params);
  1035. if (!empty($page_list['list'])) {
  1036. $coupon_ids = array_column($page_list['list'], 'id');
  1037. $store_ids = array_column($page_list['list'], 'store_id');
  1038. $store_arr = Store::lists(['where' => [['id' => $store_ids]], 'index' => 'id', 'select' => 'id,store_name']);
  1039. if ($from_type == 1) {
  1040. $store_ids = array_column($page_list['list'], 'store_id');
  1041. $store_arr = Store::lists(['where' => [['id' => $store_ids]], 'index' => 'id', 'select' => 'id,store_name']);
  1042. } else {
  1043. $goods_arr = $this->getGoodsArr(StoreCouponGoodsRelate::class, StoreGoods::class, $coupon_ids);
  1044. $goods_cate_arr = $this->getGoodsCateArr(StoreCouponCatRelate::class, StoreGoodsCate::class, $coupon_ids);
  1045. }
  1046. $user_relate_list = StoreCouponUserRelate::lists([
  1047. 'where' => [
  1048. ['coupon_id' => $coupon_ids],
  1049. ['user_id' => $this->user_id],
  1050. ['status'=>StatusEnum::ENABLED],
  1051. ['is_giving'=>StatusEnum::DISABLED],
  1052. ['giving_status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]],
  1053. ],
  1054. 'select' => 'id,coupon_id,from_type,is_use,is_giving,coupon_status,start_time,end_time',
  1055. ]);
  1056. $user_relate_arr = [];
  1057. foreach ($user_relate_list as $val) {
  1058. $user_relate_arr[$val['from_type']][$val['coupon_id']] = $val;
  1059. }
  1060. foreach ($page_list['list'] as &$item) {
  1061. $item['coupon_name'] = $item['name'];
  1062. $item['is_gived'] = 0;
  1063. $item['is_use'] = 0;
  1064. $item['is_receive'] = 0;
  1065. $item['user_coupon_id'] = 0;
  1066. $item['goods_arr'] = $item['goods_cate_arr'] = $item['store_arr'] = [];
  1067. if (isset($user_relate_arr[$from_type][$item['id']])) {
  1068. $user_coupon = $user_relate_arr[$from_type][$item['id']];
  1069. $item['is_use'] = $user_coupon['is_use'];
  1070. $item['is_gived'] = $user_coupon['is_giving'];
  1071. $item['user_coupon_id'] = $user_coupon['id'];
  1072. $item['begin_at'] = $user_coupon['start_time'];
  1073. $item['end_at'] = $user_coupon['end_time'];
  1074. $item['is_receive'] = 1;
  1075. if($item['receive_count']>1&&!empty($user_coupon['is_use'])){
  1076. $item['is_receive'] = 0;
  1077. }
  1078. }
  1079. if (isset($goods_arr[$item['id']])) $item['goods_arr'] = $goods_arr[$item['id']];
  1080. if (isset($goods_cate_arr[$item['id']])) $item['goods_cate_arr'] = $goods_cate_arr[$item['id']];
  1081. if (isset($store_arr[$item['store_id']])) $item['store_arr'] = $store_arr[$item['store_id']];
  1082. }
  1083. }
  1084. }
  1085. return $this->success('success', $page_list);
  1086. }
  1087. protected function getGoodsArr($relate_class, $goods_class, $coupon_ids)
  1088. {
  1089. $store_coupon_goods_relate_list = $relate_class::lists(['where' => [['coupon_id' => $coupon_ids]]]);
  1090. $goods_ids = array_column($store_coupon_goods_relate_list, 'goods_id');
  1091. $goods_list = $goods_class::lists(['where' => [['id' => $goods_ids]], 'select' => 'id,goods_name', 'index' => 'id']);
  1092. $goods_arr = [];
  1093. foreach ($store_coupon_goods_relate_list as $item) {
  1094. if (isset($goods_list[$item['goods_id']])) {
  1095. $goods_arr[$item['coupon_id']][] = $goods_list[$item['goods_id']];
  1096. }
  1097. }
  1098. return $goods_arr;
  1099. }
  1100. protected function getGoodsCateArr($relate_class, $cate_class, $coupon_ids)
  1101. {
  1102. $goods_cate_arr = [];
  1103. $store_coupon_cate_relate_list = $relate_class::lists(['where' => [['coupon_id' => $coupon_ids]]]);
  1104. $cate_ids = array_column($store_coupon_cate_relate_list, 'cate_id');
  1105. $goods_cate_list = $cate_class::lists(['where' => [['id' => $cate_ids]], 'select' => 'id,name', 'index' => 'id']);
  1106. foreach ($store_coupon_cate_relate_list as $item) {
  1107. if (isset($goods_cate_list[$item['cate_id']])) {
  1108. $goods_cate_arr[$item['coupon_id']][] = $goods_cate_list[$item['cate_id']];
  1109. }
  1110. }
  1111. return $goods_cate_arr;
  1112. }
  1113. //优惠券列表
  1114. public function actionCouponList()
  1115. {
  1116. $params = \Yii::$app->request->get();
  1117. $from_type = !empty($params['from_type']) ? $params['from_type'] : 1;
  1118. $where[] = ['mall_id' => $this->mall_id];
  1119. $where[] = ['status' => StatusEnum::ENABLED];
  1120. $where[] = ['coupon_status' => 2];
  1121. $params['select'] = 'id,name,store_id,type,expire_type,expire_day,effect_days,begin_at,end_at,appoint_type,rule,discount_limit,sub_price,discount,min_price,coupon_status';
  1122. switch ($from_type) {
  1123. case 1:
  1124. $where[] = ['<>', 'store_id', ''];
  1125. $class = AddonsCoupon::class;
  1126. break;
  1127. case 2:
  1128. $params['select'] = 'id,name as coupon_name,discount,discount_limit,min_price,sub_price,type,mall_id,store_id,expire_type,
  1129. expire_day,begin_at,end_at,first_frame,video_url,watch_times';
  1130. $class = StoreCoupon::class;
  1131. break;
  1132. }
  1133. $params['where'] = $where;
  1134. $params['order'] = 'id desc';
  1135. $page_list = $class::listPage($params);
  1136. if (!empty($page_list)) {
  1137. $coupon_ids = array_column($page_list['list'], 'id');
  1138. switch ($from_type) {
  1139. case 1:
  1140. case 2:
  1141. switch ($from_type) {
  1142. case 1:
  1143. $store_ids = array_column($page_list['list'], 'store_id');
  1144. $store_arr = Store::lists(['where' => [['id' => $store_ids]], 'index' => 'id', 'select' => 'id,store_name']);
  1145. break;
  1146. case 2:
  1147. $goods_arr = $this->getGoodsArr(StoreCouponGoodsRelate::class, StoreGoods::class, $coupon_ids);
  1148. $goods_cate_arr = $this->getGoodsCateArr(StoreCouponCatRelate::class, StoreGoodsCate::class, $coupon_ids);
  1149. }
  1150. break;
  1151. }
  1152. foreach ($page_list['list'] as &$item) {
  1153. $item['goods_arr'] = $item['goods_cate_arr'] = $item['store_arr'] = [];
  1154. $item['from_type'] = $from_type;
  1155. $item['from_type_text'] = $from_type == 1 ? '平台优惠券' : '门店优惠券';
  1156. if (isset($goods_arr[$item['id']])) $item['goods_arr'] = $goods_arr[$item['id']];
  1157. if (isset($goods_cate_arr[$item['id']])) $item['goods_cate_arr'] = $goods_cate_arr[$item['id']];
  1158. if (isset($store_arr[$item['store_id']])) $item['store_arr'] = $store_arr[$item['store_id']];
  1159. }
  1160. }
  1161. $page_list['expire_type_map'] = StoreEnum::getExpireTypeMap();
  1162. $page_list['appoint_type_map'] = StoreEnum::getAppointTypeMap();
  1163. $page_list['coupon_status_map'] = StoreEnum::getCouponStatusMap();
  1164. return $this->success('success', $page_list);
  1165. }
  1166. public function actionCouponListByStoreId()
  1167. {
  1168. $params = \Yii::$app->request->get();
  1169. if (empty($params['store_id'])) return $this->success('success', []);
  1170. $store_id = $params['store_id'];
  1171. $where[] = ['mall_id' => $this->mall_id];
  1172. $where[] = ['status' => StatusEnum::ENABLED];
  1173. $where[] = ['coupon_status' => 2];
  1174. $where[] = ['store_id' => [$store_id]];
  1175. $params['where'] = $where;
  1176. $params['order'] = 'id desc';
  1177. $params['select'] = 'id,name,store_id,type,expire_type,expire_day,effect_days,begin_at,end_at,appoint_type,rule,discount_limit,sub_price,discount,min_price,coupon_status';
  1178. $platform_coupon_list = AddonsCoupon::lists($params);
  1179. $coupon_ids = array_column($platform_coupon_list, 'id');
  1180. $relate_list = StoreCouponUserRelate::lists(['where' => [['coupon_id' => $coupon_ids], ['user_id' => $this->user_id], ['from_type' => 1]], 'index' => 'coupon_id']);
  1181. foreach ($platform_coupon_list as &$item) {
  1182. $item['is_receive'] = 0;
  1183. if (isset($relate_list[$item['id']])) {
  1184. $item['is_receive'] = 1;
  1185. $item['is_use'] = $relate_list[$item['id']]['is_use'];
  1186. }
  1187. }
  1188. $params['select'] = 'id,name,discount,discount_limit,min_price,sub_price,type,mall_id,store_id,expire_type,
  1189. expire_day,begin_at,end_at,first_frame,video_url,watch_times';
  1190. $store_coupon_list = StoreCoupon::lists($params);
  1191. $coupon_ids = array_column($store_coupon_list, 'id');
  1192. $relate_list = StoreCouponUserRelate::lists(['where' => [['coupon_id' => $coupon_ids], ['user_id' => $this->user_id], ['from_type' => 2]], 'index' => 'coupon_id']);
  1193. foreach ($store_coupon_list as &$item) {
  1194. $item['is_receive'] = 0;
  1195. if (isset($relate_list[$item['id']])) {
  1196. $item['is_receive'] = 1;
  1197. $item['is_use'] = $relate_list[$item['id']]['is_use'];
  1198. }
  1199. }
  1200. $list = [
  1201. 'store_coupon_list' => $store_coupon_list,
  1202. 'platform_coupon_list' => $platform_coupon_list,
  1203. ];
  1204. $list['expire_type_map'] = StoreEnum::getExpireTypeMap();
  1205. $list['appoint_type_map'] = StoreEnum::getAppointTypeMap();
  1206. $list['coupon_status_map'] = StoreEnum::getCouponStatusMap();
  1207. return $this->success('success', $list);
  1208. }
  1209. /**
  1210. * @name:
  1211. * @msg: 优惠券信息
  1212. * @param {integer} $coupon_id 优惠券 id
  1213. * @return {*}
  1214. */
  1215. public function actionCouponInfo()
  1216. {
  1217. if (!\Yii::$app->request->isGet) return $this->error('请求方式错误');
  1218. $user_id = $this->user_id;
  1219. $coupon_share_log_id = \Yii::$app->request->get('coupon_share_log_id');
  1220. $coupon_id = \Yii::$app->request->get('coupon_id');
  1221. $coupon = [];
  1222. if(!empty($coupon_share_log_id)){
  1223. // 转赠优惠券
  1224. $coupon_share = StoreCouponShareLog::findOne(['id' => $coupon_share_log_id, 'mall_id' => $this->mall_id]);
  1225. if (empty($coupon_share)) return $this->error('未找到相应的分享记录');
  1226. $coupon_user = StoreCouponUserRelate::findOne(['id' => $coupon_share['user_coupon_id']]);
  1227. if (empty($coupon_user)) return $this->error('未找到相应的分享记录');
  1228. if ($coupon_user['from_type'] == 1) {
  1229. $coupon = AddonsCoupon::find()->where(['id' => $coupon_share['coupon_id'], 'mall_id' => $this->mall_id])->asArray()->one();
  1230. } else {
  1231. $coupon = StoreCoupon::find()->where(['id' => $coupon_share['coupon_id'], 'mall_id' => $this->mall_id])->asArray()->one();
  1232. }
  1233. if(empty($coupon)) return $this->error('优惠券不存在');
  1234. $store = Store::findOne(['id' => $coupon['store_id']]);
  1235. $mall_log = $store['logo_img'];
  1236. $mall_name = $store['store_name'];
  1237. $coupon_id = $coupon['id'];
  1238. $user_id = $coupon_share->giving_user_id;
  1239. $user_coupon_info = StoreCouponUserRelate::find()
  1240. ->select('start_time,end_time,coupon_status')
  1241. ->where(['id' => $coupon_share['user_coupon_id'], 'mall_id' => $this->mall_id, 'user_id' => $user_id, 'is_giving' => 1])
  1242. ->orderBy('id desc')
  1243. ->limit(1)
  1244. ->asArray()
  1245. ->one();
  1246. }else{
  1247. if(!empty($coupon_id)){
  1248. // 非转赠优惠券
  1249. $coupon = StoreCoupon::find()->where(['id' => $coupon_id, 'mall_id' => $this->mall_id])->asArray()->one();
  1250. if(empty($coupon)) return $this->error('优惠券不存在');
  1251. $store = Store::findOne(['id' => $coupon['store_id']]);
  1252. $mall_log = $store['logo_img'];
  1253. $mall_name = $store['store_name'];
  1254. $coupon_id = $coupon['id'];
  1255. $user_id = 0;
  1256. }
  1257. $user_coupon_info = StoreCouponUserRelate::find()
  1258. ->select('start_time,end_time,coupon_status')
  1259. ->where(['coupon_id' => $coupon_id, 'mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'is_giving' => 0])
  1260. ->orderBy('id desc')
  1261. ->limit(1)
  1262. ->asArray()
  1263. ->one();
  1264. }
  1265. if (!empty($user_coupon_info)) {
  1266. $coupon = array_merge($coupon, $user_coupon_info);
  1267. $coupon = array_merge($coupon, AddonsCoupon::parseCouponInfo($coupon, true));
  1268. } else {
  1269. $coupon = array_merge($coupon, AddonsCoupon::parseCouponInfo($coupon));
  1270. }
  1271. $user = User::findOne(['id' => $user_id, 'mall_id' => $this->mall_id]);
  1272. if (empty($user)) {
  1273. $coupon_picture = $mall_log;
  1274. $coupon_user_name = $mall_name;
  1275. } else {
  1276. $coupon_picture = $user->avatar_url ?: \Yii::$app->request->hostInfo . '/resources/img/mall/default_avatar_url.png';
  1277. $coupon_user_name = ($user->nickname ?: $user->username) ?: '会员';
  1278. }
  1279. $coupon['coupon_picture'] = $coupon_picture;
  1280. $coupon['give_user_name'] = $coupon_user_name;
  1281. $coupon['is_lapse'] = in_array($user_coupon_info['coupon_status'], [AddonsCouponEnum::STATUS_EXPIRED, AddonsCouponEnum::STATUS_FAILURE]) ||
  1282. $user_coupon_info['is_use'] == 1 ||
  1283. $user_coupon_info['giving_status'] == AddonsCouponEnum::GIVING_STATUS2;
  1284. $coupon['is_lapse_by_latest_user_coupon'] = false;
  1285. if ($coupon['get_user_coupon_id'] && $coupon_share['receive_user_id'] == $this->user_id) {
  1286. $latest_user_coupon_info = AddonsCouponUserRelate::find()
  1287. ->select('start_time,end_time,coupon_status,is_use,is_giving,giving_status,user_id')
  1288. ->where(['id' => $coupon['get_user_coupon_id'], 'mall_id' => $this->mall_id])
  1289. ->limit(1)
  1290. ->asArray()
  1291. ->one();
  1292. $coupon['is_lapse_by_latest_user_coupon'] = in_array($latest_user_coupon_info['coupon_status'], [AddonsCouponEnum::STATUS_EXPIRED, AddonsCouponEnum::STATUS_FAILURE]) ||
  1293. $latest_user_coupon_info['is_use'] == 1 ||
  1294. $latest_user_coupon_info['giving_status'] == AddonsCouponEnum::GIVING_STATUS2;
  1295. }
  1296. $coupon['receive_user_id'] = $coupon_share['receive_user_id'];
  1297. $couponLink = [];
  1298. switch($coupon['appoint_type']){
  1299. case AddonsCouponEnum::APPOINT_TYPE4: // 指定门店
  1300. $couponLink = explode(',',$coupon['store_id']);
  1301. break;
  1302. case AddonsCouponEnum::APPOINT_TYPE6: // 指定门店商品
  1303. $goodsIds = CouponStoreGoodsRelate::find()->select('goods_id')->where(['coupon_id' => $coupon['id']])->asArray()->all();
  1304. $goods_id = [];
  1305. foreach($goodsIds as $k => $v){
  1306. array_push($goods_id, $v['goods_id']);
  1307. }
  1308. $storeGoods = StoreGoods::find()->select('store_id,id')->where(['id' => $goods_id])->asArray()->all();
  1309. $couponLink['store_id'] = [$storeGoods[0]['store_id']];
  1310. $couponLink['goods_id'] = [$storeGoods[0]['id']];
  1311. break;
  1312. }
  1313. $coupon['couponLink'] = $couponLink;
  1314. return $this->success('操作成功', ['coupon_info' => $coupon]);
  1315. }
  1316. //开屏优惠券列表
  1317. public function actionOpenScreenCouponList()
  1318. {
  1319. $where[] = ['mall_id' => $this->mall_id];
  1320. $where[] = ['coupon_status' => 2];
  1321. $where[] = ['is_open_screen' => 1];
  1322. $where[] = ['status' => StatusEnum::ENABLED];
  1323. $where[] = ['<>', 'store_id', ''];
  1324. $params['order'] = 'id desc';
  1325. $params['where'] = $where;
  1326. $params['select'] = '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,effect_days';
  1327. $platform_coupon_list = AddonsCoupon::lists($params);
  1328. $params['select'] = 'id,name as coupon_name,discount,discount_limit,min_price,sub_price,type,mall_id,store_id,expire_type,
  1329. expire_day,begin_at,end_at,first_frame,video_url,watch_times,effect_days,exchange_voucher_type';
  1330. $store_coupon_list = StoreCoupon::lists($params);
  1331. $platform_coupon_list = $this->formatCouponList($platform_coupon_list, 1);
  1332. $store_coupon_list = $this->formatCouponList($store_coupon_list, 2);
  1333. $list = array_merge($platform_coupon_list, $store_coupon_list);
  1334. return $this->success('success', $list);
  1335. }
  1336. protected function formatCouponList($coupon_list, $from_type)
  1337. {
  1338. $coupon_ids = array_column($coupon_list, 'id');
  1339. $relate_list = StoreCouponUserRelate::lists([
  1340. 'where' => [
  1341. ['from_type' => $from_type],
  1342. ['coupon_id' => $coupon_ids],
  1343. ['user_id' => $this->user_id],
  1344. ],
  1345. 'and_where' => ['>', 'end_time', time()],
  1346. 'index' => 'coupon_id'
  1347. ]);
  1348. foreach ($coupon_list as &$item) {
  1349. $item['coupon_id'] = $item['id'];
  1350. $item['from_type'] = $from_type;
  1351. $item['from_type_text'] = $from_type == 1 ? '平台券' : '门店券';
  1352. $item['is_receive'] = $item['is_use'] = $item['user_coupon_id'] = 0;
  1353. if (isset($relate_list[$item['id']])) {
  1354. $item['is_receive'] = 1;
  1355. $item['is_use'] = $relate_list[$item['id']]['is_use'];
  1356. $item['user_coupon_id'] = $relate_list[$item['id']]['id'];
  1357. if(!empty($relate_list[$item['id']]['start_time'])) $item['begin_at'] = $relate_list[$item['id']]['start_time'];
  1358. if(!empty($relate_list[$item['id']]['end_time'])) $item['end_at'] = $relate_list[$item['id']]['end_time'];
  1359. }
  1360. $expire_desc = '';
  1361. switch ($item['expire_type']){
  1362. case 1:
  1363. $expire_desc = '领取后'.$item['expire_day'].'天过期';
  1364. break;
  1365. case 3:
  1366. $expire_desc = '领取后'.$item['effect_days'].'天生效,生效后'.$item['expire_day'].'天过期';
  1367. break;
  1368. }
  1369. $item['expire_desc'] =$expire_desc;
  1370. }
  1371. return $coupon_list;
  1372. }
  1373. public function actionGetContentByCode(){
  1374. $params = \Yii::$app->request->post();
  1375. // 检查提交的参数
  1376. $res = \Yii::$app->services->validate->validate($params,[
  1377. [['code'], 'required'],
  1378. ]);
  1379. if($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  1380. $model = StoreMiniCode::findOne(['code'=>$params['code']]);
  1381. if(empty($model)) return $this->error('查无数据');
  1382. $content = json_decode($model['content'],true);
  1383. return $this->success('操作成功',$content);
  1384. }
  1385. public function actionGoodsList(){
  1386. $params = \Yii::$app->request->get();
  1387. $res = \Yii::$app->services->validate->validate($params, [
  1388. [['store_id'], 'required'],
  1389. [['keyword'], 'safe'],
  1390. ]);
  1391. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  1392. $store_id = $params['store_id'];
  1393. $where[] = ['store_id'=>$store_id,'mall_id' => $this->mall_id,'check_status'=>1, 'is_on_sale' => 1, 'status' => StatusEnum::ENABLED];
  1394. if(!empty($params['keyword'])){
  1395. $where[]=['like','goods_name',trim($params['keyword'])];
  1396. }
  1397. $params['where'] = $where;
  1398. $params['select'] = 'id,store_id,goods_name,cover_pic,price,original_price,show_style,goods_type';
  1399. $params['order'] = 'is_recommend desc,sort asc';
  1400. $goods_list = StoreGoods::listPage($params);
  1401. if(!empty($goods_list['list'])){
  1402. foreach ($goods_list['list'] as &$item){
  1403. $item['discount'] = $item['original_price'] - $item['price'];
  1404. if($item['discount']<0) $item['discount'] = 0;
  1405. }
  1406. }
  1407. return $this->success('操作成功', $goods_list);
  1408. }
  1409. public function actionCollectAdd()
  1410. {
  1411. $params = \Yii::$app->request->post();
  1412. $res = \Yii::$app->services->validate->validate($params, [
  1413. [['store_id', 'type'], 'required'],
  1414. [['type'], 'in', 'range' => [0, 1]],
  1415. ]);
  1416. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  1417. $params['mall_id'] = $this->mall_id;
  1418. $params['user_id'] = $this->user_id;
  1419. $params['status'] = $params['type'];
  1420. $res = StoreCollect::setData($params);
  1421. if ($res === false) return $this->error(StoreCollect::getStaticError());
  1422. if($params['type'] == StatusEnum::ENABLED){
  1423. $msg = "收藏成功";
  1424. }else{
  1425. $msg = "取消收藏";
  1426. }
  1427. return $this->success($msg, []);
  1428. }
  1429. public function actionGetStoreList(){
  1430. $params = \Yii::$app->request->post();
  1431. $res = \Yii::$app->services->validate->validate($params, [
  1432. [['store_id'], 'required'],
  1433. ]);
  1434. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  1435. }
  1436. /**
  1437. * 支付成功页面-订单信息
  1438. * @return mixed|null
  1439. * @throws \yii\db\Exception
  1440. */
  1441. public function actionPaySuccessOrderInfo()
  1442. {
  1443. $params = Yii::$app->request->get();
  1444. $service = new StoreOrderService(['mall_id' => $this->mall_id]);
  1445. $params['user_id'] = $this->user_id;
  1446. $data = $service->paySuccessOrderInfo($params);
  1447. return $this->success('成功', $data);
  1448. }
  1449. /**
  1450. * 支付成功页面-门店分类
  1451. * @return mixed|null
  1452. * @throws \yii\db\Exception
  1453. */
  1454. public function actionPaySuccessStoreCateList()
  1455. {
  1456. $list = StoreCate::lists([
  1457. 'where'=>[
  1458. ['mall_id'=>$this->mall_id],
  1459. ['parent_id'=>0],
  1460. ['status'=>StatusEnum::ENABLED],
  1461. ],
  1462. 'select'=>'id,name,pic'
  1463. ]);
  1464. return $this->success('成功', ['list'=>$list]);
  1465. }
  1466. public function actionGetAssociatedStore()
  1467. {
  1468. $params = Yii::$app->request->get();
  1469. $StoreAssociatedUserService = new StoreAssociatedUserService();
  1470. $params['user_id'] = $this->user_id;
  1471. $params['mall_id'] = $this->mall_id;
  1472. $store_id = $StoreAssociatedUserService->getStoreAssociated($params);
  1473. return $this->success('成功', [
  1474. 'store_id' => $store_id ? (int)$store_id : null
  1475. ]);
  1476. }
  1477. // 新门店聚合页
  1478. public function actionNewStoreList()
  1479. {
  1480. $settings = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'enter');
  1481. if ($settings['mode'] != 3) {
  1482. $limit = 10;
  1483. } else {
  1484. $limit = $settings['buy_store_num'] ?? 10;
  1485. }
  1486. if ($this->user->parent_id == 0) {
  1487. // 平台任意n家门店
  1488. // $list = StoreEntryLimit::getStoreList($this->mall_id, $limit);
  1489. $list = [];
  1490. } else {
  1491. $list = StoreEntryLimit::getSpecailStoreList($this->user_id, $this->mall_id, $limit);
  1492. }
  1493. return $this->success('成功', $list);
  1494. }
  1495. }