CouponController.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <?php
  2. namespace addons\Coupon\api\modules\v1\controllers;
  3. use addons\Coupon\common\enums\AddonsCouponEnum;
  4. use addons\Coupon\common\models\AddonsCoupon;
  5. use addons\Coupon\common\models\AddonsCouponCatRelate;
  6. use addons\Coupon\common\models\AddonsCouponGoodsRelate;
  7. use addons\Coupon\common\models\AddonsCouponMemberRelate;
  8. use addons\Coupon\common\models\AddonsCouponShareLog;
  9. use addons\Coupon\common\models\AddonsCouponUserRelate;
  10. use addons\Coupon\common\services\SettingService;
  11. use addons\Mch\common\models\Mch;
  12. use addons\Store\common\enums\StoreEnum;
  13. use addons\Store\common\models\Store;
  14. use addons\Store\common\models\StoreCoupon;
  15. use addons\Store\common\models\StoreCouponShareLog;
  16. use addons\Store\common\models\StoreCouponUserRelate;
  17. use addons\Store\common\models\StoreGoods;
  18. use addons\Store\common\models\StoreGoodsAttr;
  19. use addons\Store\common\models\StoreGoodsCateRelate;
  20. use common\enums\AddonsEnum;
  21. use common\enums\StatusEnum;
  22. use common\helpers\ApiCode;
  23. use common\helpers\LocationHelper;
  24. use common\models\goods\GoodsCateRelate;
  25. use common\models\mall\MallAddons;
  26. use Yii;
  27. use api\controllers\OnAuthController;
  28. use common\helpers\ArrayHelper;
  29. use common\models\goods\Goods;
  30. use common\models\user\User;
  31. /**
  32. * 默认控制器
  33. *
  34. * Class DefaultController
  35. * @package addons\Coupon\api\modules\v1\controllers
  36. */
  37. class CouponController extends OnAuthController
  38. {
  39. public $modelClass = '';
  40. /**
  41. * 不用进行登录验证的方法
  42. *
  43. * 例如: ['index', 'update', 'create', 'view', 'delete']
  44. * 默认全部需要验证
  45. *
  46. * @var array
  47. */
  48. protected $authOptional = [];
  49. private function getCommonWhere($coupon_status)
  50. {
  51. $common_where = [
  52. ['mall_id' => $this->mall_id],
  53. ['user_id' => $this->user_id],
  54. ['status' => StatusEnum::ENABLED]
  55. ];
  56. switch ($coupon_status) {
  57. case 1:
  58. $common_where[] = ['is_use' => 1];
  59. break;
  60. case 2:
  61. $common_where[] = ['coupon_status' => 2];
  62. $common_where[] = ['is_use' => 0];
  63. break;
  64. case 3:
  65. $common_where[] = ['coupon_status' => [3, 4]];
  66. $common_where[] = ['is_giving' => 0];
  67. $common_where[] = ['is_use' => 0];
  68. break;
  69. case 4:
  70. $common_where[] = ['is_giving' => 1];
  71. $common_where[] = ['giving_status' => 2];
  72. break;
  73. }
  74. return $common_where;
  75. }
  76. public function actionMyCouponSummary()
  77. {
  78. $coupon_status = \Yii::$app->request->get('coupon_status', 1);
  79. $common_where = $this->getCommonWhere($coupon_status);
  80. $data = [
  81. 'platform_coupon_nums' => 0,
  82. 'store_coupon_nums' => 0,
  83. 'mch_coupon_nums' => 0,
  84. ];
  85. $platform_where = $common_where;
  86. $platform_where[] = ['mch_id' => ''];
  87. $platform_where[] = ['status' => StatusEnum::ENABLED];
  88. $platform_counts = AddonsCouponUserRelate::getOne($platform_where, true, [], false, 'count(*) as counts');
  89. if (!empty($platform_counts)) $data['platform_coupon_nums'] = $platform_counts['counts'];
  90. $mch_where = $common_where;
  91. $mch_where[] = ['<>', 'mch_id', ''];
  92. $mch_counts = AddonsCouponUserRelate::getOne($mch_where, true, [], false, 'count(*) as counts');
  93. if (!empty($mch_counts)) $data['mch_coupon_nums'] = $mch_counts['counts'];
  94. if (MallAddons::isInstall($this->mall_id, 'Store')) {
  95. $store_counts = StoreCouponUserRelate::getOne($common_where, true, [], false, 'count(*) as counts');
  96. if (!empty($mch_counts)) $data['store_coupon_nums'] = $store_counts['counts'];
  97. }
  98. return $this->success('操作成功', $data);
  99. }
  100. public function actionMyCouponList()
  101. {
  102. $params = \Yii::$app->request->get();
  103. $res = Yii::$app->services->validate->validate($params, [
  104. [['from_type', 'coupon_status'], 'required'],
  105. [['page', 'limit'], 'safe'],
  106. ]);
  107. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  108. $coupon_status = $params['coupon_status'] ?? 1;
  109. $from_type = $params['from_type'] ?? 1;
  110. $common_where = $this->getCommonWhere($coupon_status);
  111. $page_list = [];
  112. switch ($from_type) {
  113. case 1:
  114. case 3:
  115. $where = $common_where;
  116. $where[] = ['status' => StatusEnum::ENABLED];
  117. $select = 'id,coupon_id,start_time,end_time,is_use,is_giving,giving_status,mch_id,updated_at';
  118. if ($from_type == 1) {
  119. $where[] = ['mch_id' => ''];
  120. } else {
  121. $where[] = ['<>', 'mch_id', ''];
  122. }
  123. $params['where'] = $where;
  124. $params['order'] = 'id desc';
  125. $params['select'] = $select;
  126. $params['with'] = ['coupon' => function ($query) {
  127. $query->select('id,name,min_price,sub_price,appoint_type,is_giving,type,discount,is_giving_use, coupon_status')
  128. ->with(['goodsRelate', 'cateRelate']);
  129. }];
  130. $page_list = AddonsCouponUserRelate::listPage($params);
  131. if (!empty($page_list['list'])) {
  132. $mch_ids = array_column($page_list['list'], 'mch_id');
  133. $mch_list = [];
  134. if(MallAddons::isInstall($this->mall_id, 'Mch')) {
  135. $mch_list = Mch::lists(['where' => [['id' => $mch_ids]], 'index' => 'id']);
  136. }
  137. if ($params['coupon_status'] == 4) {
  138. $user_coupon_ids = array_column($page_list['list'], 'id');
  139. $share_logs = AddonsCouponShareLog::lists(['where' => [['user_coupon_id' => $user_coupon_ids], ['giving_status' => 2]], 'index' => 'user_coupon_id', 'select' => 'user_coupon_id,receive_user_id']);
  140. $receive_user_ids = array_column($share_logs, 'receive_user_id');
  141. $receive_user_list = User::lists(['where' => [['id' => $receive_user_ids]], 'index' => 'id', 'select' => 'id,mobile,nickname']);
  142. }
  143. // 通过转增得到的ids
  144. $giftedIds = AddonsCoupon::getGiftedList($this->mall_id, $page_list['list'], 1);
  145. foreach ($page_list['list'] as &$item) {
  146. $item['mch_name'] = '';
  147. $item['logo_img'] = '';
  148. $item['store_id'] = '';
  149. $item['give_name'] = '';
  150. $item['give_mobile'] = '';
  151. $item['from_type'] = $from_type;
  152. $item['coupon']['goods_ids'] = '';
  153. $item['coupon']['cate_ids'] = '';
  154. $item['use_time'] = $item['updated_at'];
  155. if (isset($mch_list[$item['mch_id']])) {
  156. $item['mch_name'] = $mch_list[$item['mch_id']]['store_name'];
  157. $item['logo_img'] = $mch_list[$item['mch_id']]['logo_img'];
  158. }
  159. $item['coupon']['is_goods_single'] = 0;
  160. if (count($item['coupon']['goodsRelate']) == 1) $item['coupon']['is_goods_single'] = 1;
  161. foreach ($item['coupon']['goodsRelate'] as $goodsRelate) {
  162. $item['coupon']['goods_ids'] .= $goodsRelate['goods_id'] . ',';
  163. }
  164. foreach ($item['coupon']['cateRelate'] as $cateRelate) {
  165. $item['coupon']['cate_ids'] .= $cateRelate['cate_id'] . ',';
  166. }
  167. $item['coupon']['goods_ids'] = trim($item['coupon']['goods_ids'], ',');
  168. $item['coupon']['cate_ids'] = trim($item['coupon']['cate_ids'], ',');
  169. unset($item['coupon']['goodsRelate']);
  170. unset($item['coupon']['cateRelate']);
  171. if ($params['coupon_status'] == 4) {
  172. if ($item['is_giving']) {
  173. $item['give_time'] = $item['updated_at'];
  174. }
  175. if (isset($share_logs[$item['id']])) {
  176. $receive_user_id = $share_logs[$item['id']]['receive_user_id'];
  177. if (isset($receive_user_list) && isset($receive_user_list[$receive_user_id])) {
  178. $item['give_name'] = $receive_user_list[$receive_user_id]['nickname'];
  179. $item['give_mobile'] = $receive_user_list[$receive_user_id]['mobile'];
  180. }
  181. }
  182. }
  183. $item['is_show_use_btn'] = true;
  184. // 返回是否显示使用按钮
  185. if (!empty($item['coupon']['is_giving']) && !empty($item['coupon']['is_giving_use']) && !in_array($item['id'], $giftedIds)) {
  186. $item['is_show_use_btn'] = false;
  187. }
  188. }
  189. }
  190. break;
  191. case 2:
  192. $params['where'] = $common_where;
  193. $params['order'] = 'id desc';
  194. $select = 'id,coupon_id,start_time,end_time,is_use,is_giving,giving_status,store_id,from_type,updated_at, coupon_status';
  195. $params['select'] = $select;
  196. $page_list = StoreCouponUserRelate::listPage($params);
  197. if (!empty($page_list['list'])) {
  198. $platform_coupon_ids = $store_coupon_ids = [];
  199. foreach ($page_list['list'] as &$item) {
  200. switch ($item['from_type']) {
  201. case 1:
  202. $platform_coupon_ids[] = $item['coupon_id'];
  203. break;
  204. case 2:
  205. $store_coupon_ids[] = $item['coupon_id'];
  206. break;
  207. }
  208. }
  209. $platform_coupon_list = AddonsCoupon::lists(['where' => [['id' => $platform_coupon_ids]], 'index' => 'id', 'select' => 'id,name,min_price,sub_price,appoint_type,is_giving,type,discount,is_giving_use', 'with' => ['storeGoodsRelate']]);
  210. $store_coupon_list = StoreCoupon::lists(['where' => [['id' => $store_coupon_ids]], 'with' => ['goodsRelate', 'cateRelate'], 'index' => 'id', 'select' => 'id,name,min_price,sub_price,appoint_type,is_giving,type,discount,exchange_voucher_type']);
  211. $store_ids = array_column($page_list['list'], 'store_id');
  212. $store_list = Store::lists(['where' => [['id' => $store_ids]], 'index' => 'id']);
  213. if ($params['coupon_status'] == 4) {
  214. $user_coupon_ids = array_column($page_list['list'], 'id');
  215. $share_logs = StoreCouponShareLog::lists(['where' => [['user_coupon_id' => $user_coupon_ids]], 'index' => 'user_coupon_id', 'select' => 'user_coupon_id,receive_user_id']);
  216. $receive_user_ids = array_column($share_logs, 'receive_user_id');
  217. $receive_user_list = User::lists(['where' => [['id' => $receive_user_ids]], 'index' => 'id', 'select' => 'id,mobile,nickname']);
  218. }
  219. foreach ($page_list['list'] as &$item) {
  220. $item['store_name'] = '';
  221. $item['logo_img'] = '';
  222. $item['give_name'] = '';
  223. $item['give_mobile'] = '';
  224. $item['use_time'] = $item['updated_at'];
  225. if (isset($store_list[$item['store_id']])) {
  226. $item['store_name'] = $store_list[$item['store_id']]['store_name'];
  227. $item['logo_img'] = $store_list[$item['store_id']]['logo_img'];
  228. }
  229. if ($params['coupon_status'] == 4) {
  230. if ($item['is_giving']) {
  231. $item['give_time'] = $item['updated_at'];
  232. }
  233. if (isset($share_logs[$item['id']])) {
  234. $receive_user_id = $share_logs[$item['id']]['receive_user_id'];
  235. if (isset($receive_user_list) && isset($receive_user_list[$receive_user_id])) {
  236. $item['give_name'] = $receive_user_list[$receive_user_id]['nickname'];
  237. $item['give_mobile'] = $receive_user_list[$receive_user_id]['mobile'];
  238. }
  239. }
  240. }
  241. $item['is_show_use_btn'] = true;
  242. $item['coupon'] = [];
  243. switch ($item['from_type']) {
  244. case 1:
  245. if (isset($platform_coupon_list[$item['coupon_id']])) {
  246. $item['coupon'] = $platform_coupon_list[$item['coupon_id']];
  247. $item['coupon']['is_goods_single'] = 0;
  248. if (count($item['coupon']['storeGoodsRelate']) == 1) $item['coupon']['is_goods_single'] = 1;
  249. foreach ($item['coupon']['storeGoodsRelate'] as $goodsRelate) {
  250. $item['coupon']['goods_ids'] .= $goodsRelate['goods_id'] . ',';
  251. }
  252. $item['coupon']['goods_ids'] = trim($item['coupon']['goods_ids'], ',');
  253. unset($item['coupon']['storeGoodsRelate']);
  254. // 存储平台优惠券,用于查询这个优惠券是否允许显示
  255. $platformCouponUserRelate[] = &$item;
  256. }
  257. !isset($item['coupon']['goods_ids']) && $item['coupon']['goods_ids'] = '';
  258. $item['coupon']['cate_ids'] = '';
  259. break;
  260. case 2:
  261. if (isset($store_coupon_list[$item['coupon_id']])) {
  262. $item['coupon'] = $store_coupon_list[$item['coupon_id']];
  263. }
  264. foreach ($item['coupon']['goodsRelate'] as $goodsRelate) {
  265. $item['coupon']['goods_ids'] .= $goodsRelate['goods_id'] . ',';
  266. }
  267. foreach ($item['coupon']['cateRelate'] as $cateRelate) {
  268. $item['coupon']['cate_ids'] .= $cateRelate['cate_id'] . ',';
  269. }
  270. $item['coupon']['goods_ids'] = trim($item['coupon']['goods_ids'], ',');
  271. $item['coupon']['cate_ids'] = trim($item['coupon']['cate_ids'], ',');
  272. unset($item['coupon']['goodsRelate']);
  273. unset($item['coupon']['cateRelate']);
  274. break;
  275. }
  276. }
  277. // 通过转增得到的ids
  278. if (!empty($platformCouponUserRelate)) {
  279. $giftedIds = AddonsCoupon::getGiftedList($this->mall_id, $platformCouponUserRelate, 2);
  280. array_walk($platformCouponUserRelate, function (&$item) use ($giftedIds) {
  281. // 返回是否显示使用按钮
  282. if (!empty($item['coupon']['is_giving']) && !empty($item['coupon']['is_giving_use']) && !in_array($item['id'], $giftedIds)) {
  283. $item['is_show_use_btn'] = false;
  284. }
  285. });
  286. }
  287. }
  288. break;
  289. }
  290. $settingService = new SettingService(['mall_id' => $this->mall_id]);
  291. $config = $settingService->getSetting();
  292. $page_list = array_merge($page_list, $config);
  293. return $this->success('操作成功', $page_list);
  294. }
  295. public function actionDetail()
  296. {
  297. $params = \Yii::$app->request->post();
  298. $res = Yii::$app->services->validate->validate($params, [
  299. [['from_type', 'coupon_id'], 'required'],
  300. [['store_id', 'user_coupon_id'], 'safe'],
  301. ]);
  302. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  303. $select = 'id,name,min_price,sub_price,appoint_type,rule,expire_type,expire_day,effect_days,begin_at ,end_at';
  304. switch ($params['from_type']) {
  305. case 1:
  306. case 3:
  307. $coupon = AddonsCoupon::getOne([['id' => $params['coupon_id']], 'mall_id' => $this->mall_id], true, false, $select);
  308. if (!empty($params['user_coupon_id'])) {
  309. if (!empty($params['store_id'])) {
  310. $coupon_user_relate = StoreCouponUserRelate::findOne(['id' => $params['user_coupon_id']]);
  311. } else {
  312. $coupon_user_relate = AddonsCouponUserRelate::findOne(['id' => $params['user_coupon_id']]);
  313. }
  314. }
  315. break;
  316. case 2:
  317. $coupon = StoreCoupon::getOne([['id' => $params['coupon_id']], 'mall_id' => $this->mall_id], true, false, $select);
  318. if (!empty($params['user_coupon_id'])) $coupon_user_relate = StoreCouponUserRelate::findOne(['id' => $params['user_coupon_id']]);
  319. break;
  320. }
  321. $coupon['start_time'] = $coupon['begin_at'];
  322. $coupon['end_time'] = $coupon['end_at'];
  323. if (!empty($coupon_user_relate)) {
  324. $coupon['start_time'] = $coupon_user_relate['start_time'];
  325. $coupon['end_time'] = $coupon_user_relate['end_time'];
  326. }
  327. return $this->success('操作成功', $coupon);
  328. }
  329. public function actionDetailList()
  330. {
  331. $params = \Yii::$app->request->get();
  332. $res = Yii::$app->services->validate->validate($params, [
  333. [['from_type', 'coupon_id'], 'required'],
  334. [['longitude', 'latitude', 'page', 'limit'], 'safe'],
  335. ]);
  336. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  337. $select = 'id,name,min_price,sub_price,appoint_type,rule,type';
  338. $coupon = [];
  339. $goods_page_list = [];
  340. $store_page_list = [];
  341. $mch_page_list = [];
  342. $isInstallStore = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE);
  343. switch ($params['from_type']) {
  344. case 1:
  345. case 3:
  346. $select .= ',mch_id,store_id';
  347. $with = ['goodsRelate', 'cateRelate'];
  348. if ($isInstallStore) {
  349. $with[] = 'storeGoodsRelate';
  350. }
  351. $coupon = AddonsCoupon::getOne([['id' => $params['coupon_id']], 'mall_id' => $this->mall_id], true, $with, false, $select);
  352. switch ($coupon['appoint_type']) {
  353. case 1:
  354. $cate_ids = [];
  355. foreach ($coupon['cateRelate'] as $cateRelate) {
  356. $cate_ids[] = $cateRelate['cate_id'];
  357. }
  358. $goods_cate_relate_list = GoodsCateRelate::lists(['where' => [['cate_id' => $cate_ids]]]);
  359. $goods_ids = array_column($goods_cate_relate_list, 'goods_id');
  360. $params['where'][] = ['id' => $goods_ids, 'status' => StatusEnum::ENABLED, 'is_on_sale' => 1];
  361. $goods_page_list = Goods::listPage($params);
  362. break;
  363. case 2:
  364. $goods_ids = [];
  365. foreach ($coupon['goodsRelate'] as $goodsRelate) {
  366. $goods_ids[] = $goodsRelate['goods_id'];
  367. }
  368. $params['where'][] = ['id' => $goods_ids, 'status' => StatusEnum::ENABLED, 'is_on_sale' => 1];
  369. $params['select'] = 'id,goods_name,cover_pic,price,original_price';
  370. $goods_page_list = Goods::listPage($params);
  371. break;
  372. case 4:
  373. $store_ids = explode(',', $coupon['store_id']);
  374. $params['where'][] = ['id' => $store_ids];
  375. $params['select'] = 'id,store_name,logo_img,longitude,latitude,sales_volume,praise';
  376. $store_page_list = Store::listPage($params);
  377. if (!empty($store_page_list['list'])) {
  378. foreach ($store_page_list['list'] as &$value) {
  379. $distance = (new LocationHelper())->getDistanceByGeo($params['latitude'], $params['longitude'], $value['latitude'], $value['longitude']);
  380. $value['distance'] = $distance;
  381. }
  382. }
  383. break;
  384. case 5:
  385. $mch_id = explode(',', $coupon['mch_id']);
  386. $params['where'][] = ['id' => $mch_id];
  387. $params['select'] = 'id,store_name,logo_img,sales_volume,praise';
  388. $mch_page_list = Mch::listPage($params);
  389. break;
  390. case AddonsCouponEnum::APPOINT_TYPE6:
  391. if (!$isInstallStore) {
  392. break;
  393. }
  394. $params['where'][] = ['id' => array_column($coupon['storeGoodsRelate'] ?? [], 'goods_id'), 'status' => StatusEnum::ENABLED, 'check_status' => 1, 'is_on_sale' => 1];
  395. $params['select'] = 'id,store_id,goods_name,cover_pic,price,original_price';
  396. $goods_page_list = StoreGoods::listPage($params);
  397. if ($coupon['type'] == AddonsCouponEnum::EXCHANGE) {
  398. foreach ($goods_page_list['list'] as &$value) {
  399. $value['price'] = 0;
  400. }
  401. }
  402. break;
  403. }
  404. break;
  405. case 2:
  406. $select .= ',store_id, exchange_voucher_type';
  407. $coupon = StoreCoupon::getOne([['id' => $params['coupon_id']], 'mall_id' => $this->mall_id], true, ['goodsRelate', 'cateRelate'], false, $select);
  408. switch ($coupon['appoint_type']) {
  409. case 1:
  410. $cate_ids = [];
  411. foreach ($coupon['cateRelate'] as $cateRelate) {
  412. $cate_ids[] = $cateRelate['cate_id'];
  413. }
  414. $goods_cate_relate_list = StoreGoodsCateRelate::lists(['where' => [['cate_id' => $cate_ids]]]);
  415. $goods_ids = array_column($goods_cate_relate_list, 'goods_id');
  416. $params['where'][] = ['id' => $goods_ids, 'status' => StatusEnum::ENABLED, 'check_status' => 1, 'is_on_sale' => 1];
  417. $params['select'] = 'id,store_id,goods_name,cover_pic,price,original_price';
  418. $goods_page_list = StoreGoods::listPage($params);
  419. break;
  420. case 2:
  421. $goods_ids = [];
  422. foreach ($coupon['goodsRelate'] as $goodsRelate) {
  423. $goods_ids[] = $goodsRelate['goods_id'];
  424. }
  425. $params['where'][] = ['id' => $goods_ids, 'status' => StatusEnum::ENABLED, 'check_status' => 1, 'is_on_sale' => 1];
  426. $params['select'] = 'id,store_id,goods_name,cover_pic,price,original_price';
  427. $goods_page_list = StoreGoods::listPage($params);
  428. if ($coupon['type'] == StoreEnum::EXCHANGE) {
  429. $goods_attr_relates = StoreGoodsAttr::find()->where(['goods_id' => $goods_ids, 'status' => StatusEnum::ENABLED])->select('goods_id, id')->indexBy('goods_id')->asArray()->all();
  430. foreach ($goods_page_list['list'] as &$value) {
  431. if ($coupon['exchange_voucher_type'] == StoreEnum::ALL_DEDUCTION) {
  432. $value['price'] = 0;
  433. } else {
  434. $value['price'] = bcsub($value['price'], $coupon['sub_price'], 2);
  435. if ($value['price'] < 0) {
  436. $value['price'] = 0;
  437. }
  438. }
  439. $value['attr_id'] = $goods_attr_relates[$value['id']]['id'] ?? 0;
  440. }
  441. }
  442. break;
  443. case 3:
  444. $params['where'][] = ['id' => $coupon['store_id']];
  445. $params['select'] = 'id,store_name,logo_img,longitude,latitude,sales_volume,praise';
  446. $store_page_list = Store::listPage($params);
  447. if (!empty($store_page_list['list'])) {
  448. foreach ($store_page_list['list'] as &$value) {
  449. $distance = (new LocationHelper())->getDistanceByGeo($params['latitude'], $params['longitude'], $value['latitude'], $value['longitude']);
  450. $value['distance'] = bcdiv($distance, 1000, 1);
  451. }
  452. }
  453. break;
  454. }
  455. break;
  456. }
  457. unset($coupon['goodsRelate']);
  458. unset($coupon['cateRelate']);
  459. unset($coupon['storeGoodsRelate']);
  460. return $this->success('操作成功', ['coupon' => $coupon, 'goods_page_list' => $goods_page_list, 'store_page_list' => $store_page_list, 'mch_page_list' => $mch_page_list]);
  461. }
  462. public function actionCenter()
  463. {
  464. $params = \Yii::$app->request->get();
  465. $res = Yii::$app->services->validate->validate($params, [
  466. [['from_type',], 'required'],
  467. [['page', 'limit', 'appoint_type'], 'safe'],
  468. ]);
  469. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  470. $where = [
  471. ['mall_id' => $this->mall_id],
  472. ['status' => StatusEnum::ENABLED],
  473. ['is_public_receive' => StatusEnum::ENABLED],
  474. ['coupon_status' => AddonsCouponEnum::STATUS_RECEIVING],
  475. ];
  476. $params['order'] = 'id desc';
  477. switch ($params['from_type']) {
  478. case 1:
  479. $where[] = ['mch_id' => ''];
  480. $class = AddonsCoupon::class;
  481. break;
  482. case 2:
  483. $class = StoreCoupon::class;
  484. break;
  485. case 3:
  486. $where[] = ['<>', 'mch_id', ''];
  487. $class = AddonsCoupon::class;
  488. break;
  489. }
  490. $params['where'] = $where;
  491. $list = $class::listPage($params, false, true);
  492. return $this->success('操作成功', $list);
  493. }
  494. /**
  495. * @return mixed|void|null
  496. */
  497. public function actionJumpCheck()
  498. {
  499. $params = Yii::$app->request->get();
  500. $res = Yii::$app->services->validate->validate($params, [
  501. [['user_coupon_id', 'goods_id', 'from_type'], 'required'],
  502. [['user_coupon_id', 'goods_id', 'from_type'], 'integer'],
  503. ]);
  504. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  505. $params['from_type'] = $params['from_type'] == 2 ? 2 : 1;
  506. if (!AddonsCoupon::jumpCheck($this->getCommonWhere(2), $this->mall_id, $this->user_id, $params)) {
  507. $this->error(AddonsCoupon::getStaticError());
  508. return;
  509. }
  510. $this->success();
  511. }
  512. }