DefaultController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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 common\enums\AddonsEnum;
  11. use common\enums\StatusEnum;
  12. use common\helpers\ApiCode;
  13. use common\models\goods\GoodsCateRelate;
  14. use Yii;
  15. use api\controllers\OnAuthController;
  16. use common\helpers\ArrayHelper;
  17. use common\models\goods\Goods;
  18. use common\models\goods\GoodsCate;
  19. use common\models\user\User;
  20. /**
  21. * 默认控制器
  22. *
  23. * Class DefaultController
  24. * @package addons\Coupon\api\modules\v1\controllers
  25. */
  26. class DefaultController extends OnAuthController
  27. {
  28. public $modelClass = '';
  29. /**
  30. * 不用进行登录验证的方法
  31. *
  32. * 例如: ['index', 'update', 'create', 'view', 'delete']
  33. * 默认全部需要验证
  34. *
  35. * @var array
  36. */
  37. protected $authOptional = ['coupon-list'];
  38. /**
  39. * @desc:优惠券列表--商品详情页优惠券列表
  40. * @Author: hua
  41. * @Date: 2021/12/24
  42. * @Time: 15:35
  43. * @Copyright: copyright (c) 2021 广东七件事集团
  44. * @return mixed|void
  45. */
  46. public function actionCouponList()
  47. {
  48. if (\Yii::$app->request->isGet) {
  49. $params = \Yii::$app->request->get();
  50. $res = Yii::$app->services->validate->validate($params, [
  51. [['goods_id'], 'required'],
  52. ]);
  53. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  54. $goods_id = $params['goods_id'];
  55. $coupon_goods_relate_list = AddonsCouponGoodsRelate::lists(['where'=>[['goods_id'=>$goods_id]],'select'=>'coupon_id']);
  56. // dd($coupon_goods_relate_list);
  57. $goods_relate_coupon_id_arr = array_column($coupon_goods_relate_list,'coupon_id');
  58. $goods_cate_relate_list = GoodsCateRelate::lists(['where'=>[['goods_id'=>$goods_id]],'select'=>'cate_id']);
  59. $cate_id_arr = array_column($goods_cate_relate_list,'cate_id');
  60. $coupon_cate_relate_list = AddonsCouponCatRelate::lists(['where'=>[['cate_id'=>$cate_id_arr]],'select'=>'coupon_id']);
  61. $cate_relate_coupon_id_arr = array_column($coupon_cate_relate_list,'coupon_id');
  62. $where[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'is_public_receive' => 1, 'coupon_status' => AddonsCouponEnum::STATUS_RECEIVING, 'store_id' => 0,'mch_id'=>0];
  63. $where[] = ['>', 'total_count' ,0];
  64. $where[] = [
  65. 'or',
  66. ['appoint_type' => AddonsCouponEnum::APPOINT_TYPE3],
  67. ['appoint_type' => AddonsCouponEnum::APPOINT_TYPE2,'id'=>$goods_relate_coupon_id_arr],
  68. ['appoint_type' => AddonsCouponEnum::APPOINT_TYPE1,'id'=>$cate_relate_coupon_id_arr],
  69. ];
  70. $params['where'] = $where;
  71. $params['select'] = 'id,store_id,type,begin_at,end_at,min_price,sub_price,discount,discount_limit,coupon_status,name,is_receive_limit,receive_count,is_member,total_count,expire_type,expire_day,effect_days';
  72. $params['order'] = 'created_at desc';
  73. $list = AddonsCoupon::listPage($params);
  74. if (!empty($list['list'])) {
  75. $coupon_ids = array_column($list['list'],'id');
  76. if(!empty($this->user_id)){
  77. $coupon_user_relate_list = AddonsCouponUserRelate::lists([
  78. 'where'=>
  79. [
  80. ['coupon_id'=>$coupon_ids],
  81. ['user_id'=>$this->user_id],
  82. ['status'=>StatusEnum::ENABLED],
  83. ['or', ['is_giving' => 0], ['is_giving' => 1, 'giving_status' => AddonsCouponEnum::GIVING_STATUS1]],
  84. ],
  85. 'select'=>'count(id) as counts,coupon_id,start_time,end_time',
  86. 'group'=>'coupon_id',
  87. 'index'=>'coupon_id'
  88. ]);
  89. }
  90. $coupon_ids = array_column($list['list'],'id');
  91. $member_levels = AddonsCouponMemberRelate::find()
  92. ->select('member_level')
  93. ->where(['mall_id' => $this->mall_id, 'coupon_id' => $coupon_ids])
  94. ->asArray()
  95. ->column();
  96. $rows = [];
  97. foreach ($list['list'] as $item){
  98. $item['is_receive'] = 0;
  99. $item['counts'] = 0;
  100. if (1 == $item['is_member']) {
  101. // 限制会员等级领取
  102. if (!in_array($this->user->level, $member_levels)){
  103. continue;
  104. }
  105. }
  106. $item['text'] = '';
  107. if(!empty($coupon_user_relate_list[$item['id']])){
  108. $item['counts'] = $coupon_user_relate_list[$item['id']]['counts'];
  109. if (1 == $item['is_receive_limit'] && $item['counts'] >= $item['receive_count']) $item['is_receive'] = 1;
  110. $item['begin_at'] = $coupon_user_relate_list[$item['id']]['start_time'];
  111. $item['end_at'] = $coupon_user_relate_list[$item['id']]['end_time'];
  112. }else{
  113. switch ($item['expire_type']) {
  114. case 1:
  115. $item['text'] = "领取后" . $item['expire_day'] . "天过期";
  116. break;
  117. case 3:
  118. $item['text'] = "领取后," . $item['effect_days'] . '天后生效,' . $item['expire_day'] . "天过期";
  119. break;
  120. }
  121. }
  122. // 剩余可领取数量
  123. if (1 == $item['is_receive_limit']) {
  124. $item['remainder_count'] = $item['receive_count'] - $item['counts'];
  125. } else {
  126. $item['remainder_count'] = $item['total_count'];
  127. }
  128. $rows[]=$item;
  129. }
  130. $list['list'] = $rows;
  131. }
  132. $list['coupon_status_map'] = AddonsCouponEnum::getCouponStatusMap();
  133. return $this->success('操作成功', $list);
  134. }
  135. }
  136. /**
  137. * @name:
  138. * @msg: 会员优惠券列表(我的优惠券列表)
  139. * @param {*}
  140. * @return {*}
  141. */
  142. public function actionUserCouponList()
  143. {
  144. if (!\Yii::$app->request->isGet) {
  145. return $this->error('请求方式错误');
  146. }
  147. $user_id = $this->user_id;
  148. $page = \Yii::$app->request->get('page') ?? 1;
  149. $limit = 20;
  150. $status = \Yii::$app->request->get('status');
  151. if (!in_array($status, [2, 1])) {
  152. return $this->error('参数错误');
  153. }
  154. $where[] = ['mall_id' => $this->mall_id, 'user_id' => $user_id, 'store_id' => 0];
  155. if ($status == 1) {
  156. // 未失效
  157. $where[] = ['is_use' => 0, 'is_force_failure' => 0, 'status' => StatusEnum::ENABLED, 'coupon_status' => AddonsCouponEnum::STATUS_RECEIVING];
  158. $where[] = ['>', 'end_time', time()];
  159. $where[] = [
  160. 'or',
  161. ['is_giving' => 0],
  162. ['is_giving' => 1, 'giving_status' => [0, 1]],
  163. ];
  164. } else {
  165. // 失效
  166. $where[] = [
  167. 'or',
  168. ['status' => StatusEnum::DISABLED],
  169. ['is_giving' => 1, 'giving_status' => 2],
  170. ['is_force_failure' => 1],
  171. ['is_use' => 1],
  172. ['>', 'coupon_status', AddonsCouponEnum::STATUS_RECEIVING],
  173. ['<=', 'end_time', time()],
  174. ];
  175. }
  176. // $coupon_ids = AddonsCoupon::find()->select('id')->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->column();
  177. // $where[] = ['in', 'coupon_id', $coupon_ids];
  178. $params = [
  179. 'select' => 'id as user_coupon_id,coupon_id,coupon_status,is_use,is_giving,giving_status,user_id,start_time,end_time',
  180. 'where' => $where,
  181. 'page' => $page,
  182. 'limit' => $limit,
  183. 'order' => 'created_at desc,id desc'
  184. ];
  185. $user_coupon_list = AddonsCouponUserRelate::listPage($params);
  186. // $user_coupon_list = AddonsCouponUserRelate::getStaticError();
  187. // dd($user_coupon_list);
  188. if (!empty($user_coupon_list['list'])) {
  189. $coupon_ids = array_values(array_flip(array_flip(array_column($user_coupon_list['list'], 'coupon_id'))));
  190. // dd($coupon_ids);
  191. $coupons = AddonsCoupon::find()
  192. ->select('id,store_id,mch_id,name,type,discount,discount_limit,min_price,sub_price,expire_type,expire_day,begin_at,end_at,rule,is_original_use,is_giving as can_share,appoint_type')
  193. ->where(['id' => $coupon_ids])
  194. ->asArray()
  195. ->indexBy('id')
  196. ->all();
  197. // dd($coupons);
  198. $good_relates = AddonsCouponGoodsRelate::find()->select('coupon_id,goods_id')->where(['coupon_id' => $coupon_ids])->asArray()->all();
  199. $relate_goodids = [];
  200. if (!empty($good_relates)) {
  201. foreach ($good_relates as $relate) {
  202. $relate_goodids[$relate['coupon_id']][] = $relate['goods_id'];
  203. }
  204. }
  205. foreach ($user_coupon_list['list'] as &$lv) {
  206. $coupon = $coupons[$lv['coupon_id']] ?? [];
  207. if(!empty($coupon['mch_id'])){
  208. $coupon['mch_id'] = explode(',',$coupon['mch_id']);
  209. }else{
  210. $coupon['mch_id'] = [];
  211. }
  212. if(!empty($coupon['store_id'])) {
  213. $coupon['store_id'] = explode(',',$coupon['store_id']);
  214. }else{
  215. $coupon['store_id'] = [];
  216. }
  217. $coupon = array_merge($coupon, $lv);
  218. $coupon_info = !empty($coupon) ? AddonsCoupon::parseCouponInfo($coupon, true) : [];
  219. // dd($coupon_info);
  220. $lv = array_merge($coupon, $coupon_info);
  221. $lv['relate_goodids'] = $relate_goodids[$lv['coupon_id']] ?? [];
  222. }
  223. }
  224. $config = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_COUPON, 'basic');
  225. $user_coupon_list['give_type'] = empty($config['give_type'])?'1':$config['give_type'];
  226. return $this->success('操作成功', $user_coupon_list);
  227. }
  228. /**
  229. * @name:
  230. * @msg: 领取优惠券
  231. * @param {integer} $type 类型类型,1:平台的优惠券,2:好友/商城私发 赠送的优惠券
  232. * @param {integer} $receive_type 领取场景,1 => 商城发放, 2 => 购买商品奖励, 3 => 商品详情处领取, 4 => 领券中心领取, 5 => 签到, 6 => 好友转赠, 7 => 云库存-提货到线下奖励, 8 => 升级礼包赠送, 9 => 九宫格中奖, 10 => 刮刮卡中奖
  233. * @param {integer} $coupon_id 优惠券id
  234. * @param {integer} $coupon_share_log_id 优惠券分享记录id
  235. * @return {*}
  236. */
  237. public function actionReceive()
  238. {
  239. $params = \Yii::$app->request->post();
  240. $res = Yii::$app->services->validate->validate($params, [
  241. [['type', 'receive_type', 'coupon_id'], 'required'],
  242. [['coupon_share_log_id'], 'safe'],
  243. ]);
  244. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  245. $params['user_id'] = $this->user_id;
  246. $params['user'] = $this->user;
  247. $params['num'] = 1;//数量
  248. $params['mall_id'] = $this->mall_id;
  249. $params['receive_platform'] = $this->platform??'';
  250. if($params['type']==1){
  251. //领取平台优惠券 -- 商品详情页公开领取
  252. $coupon_user_relate = AddonsCouponUserRelate::setData($params);
  253. if($coupon_user_relate==false){
  254. return $this->error(AddonsCouponUserRelate::getStaticError());
  255. }
  256. $user_coupon_id = $coupon_user_relate['id']??0;
  257. } else {
  258. if ($params['receive_type'] == 6 && empty($params['coupon_share_log_id'])) {
  259. // 扫描后台生成的海报进行领取(没有coupon_share_log_id),商城私发的优惠券也是走公开领取的逻辑
  260. $coupon_user_relate = AddonsCouponUserRelate::setData($params);
  261. if ($coupon_user_relate === false) return $this->error(AddonsCouponUserRelate::getStaticError());
  262. $user_coupon_id = $coupon_user_relate['id']??0;
  263. } elseif ($params['receive_type'] == 6 && !empty($params['coupon_share_log_id'])) {
  264. // 领取转赠优惠券 -- 好友转赠
  265. $res = AddonsCouponUserRelate::receiveByGive($params);
  266. $user_coupon_id = $res === false ? 0 : $res->id;
  267. } else {
  268. return $this->error('参数错误');
  269. }
  270. }
  271. if ($res == false) return $this->error(AddonsCouponUserRelate::getStaticError());
  272. return $this->success('领取成功', ['user_coupon_id' => $user_coupon_id]);
  273. }
  274. /**
  275. * 转送优惠券--老版本的接口,已废弃
  276. * @return mixed|void
  277. * @throws \yii\db\Exception
  278. */
  279. /*
  280. public function actionTransfer()
  281. {
  282. $params = \Yii::$app->request->post();
  283. $user_id = $this->user_id;
  284. $res = Yii::$app->services->validate->validate($params, [
  285. [['user_coupon_id', 'receive_user_id'], 'required'],
  286. ['receive_user_id', function ($attribute) use ($user_id) {
  287. if ($user_id == $this->$attribute) $this->addError($attribute, '不能转送给自己');
  288. }]
  289. ]);
  290. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  291. $where = [];
  292. $where[] = ['id' => $params['user_coupon_id']];
  293. $where[] = ['user_id' => $user_id];
  294. $where[] = ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]];
  295. $user_coupon_model = AddonsCouponUserRelate::getOne($where);
  296. if (empty($user_coupon_model)) return $this->error( '您没有该优惠券');
  297. if ($user_coupon_model->is_use) return $this->error( '您的优惠券已经使用了');
  298. $where = [];
  299. $where[] = ['id' => $user_coupon_model->coupon_id];
  300. $where[] = ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]];
  301. $res = AddonsCoupon::getOne($where);
  302. if (empty($res)) return $this->error( '该优惠券不存在');
  303. $params['end_time'] =$user_coupon_model->end_time;
  304. $res = AddonsCouponUserRelate::transfer($params);
  305. if ($res === false) return $this->error(AddonsCouponUserRelate::getStaticError());
  306. return $this->success('操作成功', []);
  307. }
  308. */
  309. /**
  310. * @name:
  311. * @msg: 转赠优惠券
  312. * @param {*}
  313. * @return {*}
  314. */
  315. public function actionCouponGive()
  316. {
  317. if (!\Yii::$app->request->isPost) {
  318. return $this->error('请求方式错误');
  319. }
  320. $coupon_id = \Yii::$app->request->post('coupon_id');
  321. $user_coupon_id = \Yii::$app->request->post('user_coupon_id');
  322. if (empty($coupon_id) || empty($user_coupon_id)) {
  323. return $this->error('参数错误');
  324. }
  325. // $coupon = AddonsCoupon::findOne(['mall_id' => $this->mall_id, 'id' => $coupon_id, 'coupon_status' => 2, 'status' => StatusEnum::ENABLED]);
  326. $coupon = AddonsCoupon::findOne(['mall_id' => $this->mall_id, 'id' => $coupon_id]);
  327. if (empty($coupon)) {
  328. return $this->error('优惠券不存在或者已经失效');
  329. }
  330. if (1 != $coupon->is_giving) {
  331. return $this->error('优惠券不可转赠');
  332. }
  333. $wheres = [
  334. 'mall_id' => $this->mall_id,
  335. 'id' => $user_coupon_id,
  336. 'user_id' => $this->user_id,
  337. 'is_force_failure' => 0,
  338. 'is_use' => 0,
  339. 'status' => StatusEnum::ENABLED,
  340. 'coupon_status' => AddonsCouponEnum::STATUS_RECEIVING
  341. ];
  342. $user_coupon = AddonsCouponUserRelate::find()
  343. ->where($wheres)
  344. ->andWhere(['or', ['is_giving' => 0], ['is_giving' => 1, 'giving_status' => [0, 1]]])
  345. ->one();
  346. if (empty($user_coupon)) {
  347. return $this->error('当前优惠券不可转赠');
  348. }
  349. $params = [
  350. 'mall_id' => $this->mall_id,
  351. 'coupon_id' => $coupon_id,
  352. 'user_coupon_id' => $user_coupon_id,
  353. 'giving_user_id' => $this->user_id,
  354. ];
  355. $res = AddonsCouponShareLog::setData($params);
  356. if (!$res) {
  357. return $this->error(AddonsCouponShareLog::getStaticError());
  358. } else {
  359. return $this->success('转赠成功', ['coupon_share_log_id' => $res->id]);
  360. }
  361. }
  362. /**
  363. * @name:
  364. * @msg: 获取优惠券可以使用的商品列表
  365. * @param {*}
  366. * @return {*}
  367. */
  368. public function actionCanUseCouponList()
  369. {
  370. if (!\Yii::$app->request->isGet) {
  371. return $this->error('请求方式错误');
  372. }
  373. $page = \Yii::$app->request->get('page') ?? 1;
  374. $limit = \Yii::$app->request->get('page_size') ?? $this->pageSize;
  375. // 排序字段, sales、price、created_at
  376. $sort_type = \Yii::$app->request->get('sort_type') ?? 'id';
  377. // 排序方式,升序/降序:asc、desc
  378. $order_by = \Yii::$app->request->get('order_by') ?? 'desc';
  379. $good_name = \Yii::$app->request->get('good_name');
  380. $coupon_id = \Yii::$app->request->get('coupon_id');
  381. if (!empty($sort_type)) {
  382. if (!in_array($sort_type, ['sales', 'price', 'created_at', 'id'])) {
  383. return $this->error('参数错误');
  384. }
  385. if ('sales' == $sort_type) $sort_type = 'sales_num';
  386. } else {
  387. $sort_type = 'id';
  388. }
  389. if (!empty($order_by)) {
  390. if (!in_array($order_by, ['asc', 'desc'])) {
  391. return $this->error('参数错误');
  392. }
  393. } else {
  394. $order_by = 'desc';
  395. }
  396. if (empty($coupon_id)) {
  397. return $this->error('参数错误: coupon_id');
  398. }
  399. $coupon_info = AddonsCoupon::findOne(['mall_id' => $this->mall_id, 'id' => $coupon_id, 'status' => 1, 'coupon_status' => 2]);
  400. if (empty($coupon_info)) {
  401. return $this->error('优惠券异常');
  402. }
  403. $where[] = ['mall_id' => $this->mall_id, 'is_on_sale' => 1, 'status' => 1];
  404. if (1 == $coupon_info->appoint_type) {
  405. // 指定分类使用
  406. $cates = $coupon_info->cateRelate;
  407. $cate_ids = array_column($cates, 'cate_id');
  408. // dd($cate_ids);
  409. $good_ids = GoodsCateRelate::find()->select('goods_id')->where(['cate_id' => $cate_ids])->asArray()->column();
  410. $where[] = ['id' => $good_ids];
  411. } elseif (2 == $coupon_info->appoint_type) {
  412. // 指定商品可用
  413. $gw_data = $coupon_info->goodsRelate;
  414. $good_ids = array_column($gw_data, 'goods_id');
  415. // dd($good_ids);
  416. $where[] = ['id' => $good_ids];
  417. } elseif (3 == $coupon_info->appoint_type) {
  418. // 全部商品可用,不做处理
  419. }elseif (4 == $coupon_info->appoint_type) {
  420. // 指定门店
  421. $where[] = ['id' => []];
  422. } elseif (5 == $coupon_info->appoint_type) {
  423. // 指定商户
  424. $where[] = ['mch_id' => $coupon_info['mch_id']];
  425. }else {
  426. return $this->error('参数错误:appoint_type');
  427. }
  428. if (!empty($good_name)) {
  429. $where[] = ['like', 'goods_name', $good_name];
  430. }
  431. // dd($where);
  432. $params = [
  433. 'select' => 'id as good_id,price,goods_name,cover_pic,original_price,mch_id',
  434. 'where' => $where,
  435. 'order' => $sort_type . ' ' . $order_by,
  436. 'page' => $page,
  437. 'limit' => $limit,
  438. ];
  439. // dd($params);
  440. $list = Goods::listPage($params);
  441. // $list = Goods::getStaticError();
  442. // dd($list);
  443. $list['coupon_info'] = AddonsCoupon::parseCouponInfo(ArrayHelper::toArray($coupon_info));
  444. return $this->success('操作成功', $list);
  445. }
  446. /**
  447. * @name:
  448. * @msg: 优惠券信息
  449. * @param {integer} $coupon_id 优惠券 id
  450. * @return {*}
  451. */
  452. public function actionCouponInfo()
  453. {
  454. if (!\Yii::$app->request->isGet) {
  455. return $this->error('请求方式错误');
  456. }
  457. $user_id = $this->user_id;
  458. $coupon_id = \Yii::$app->request->get('coupon_id');
  459. $coupon_share_log_id = \Yii::$app->request->get('coupon_share_log_id');
  460. if (empty($coupon_id)) {
  461. return $this->error('参数错误');
  462. }
  463. $coupon = AddonsCoupon::find()->where(['id' => $coupon_id, 'mall_id' => $this->mall_id])->asArray()->one();
  464. if (empty($coupon)) return $this->error('优惠券不存在');
  465. if (!empty($coupon_share_log_id)) {
  466. // 转赠优惠券
  467. $coupon_share = AddonsCouponShareLog::findOne(['id' => $coupon_share_log_id, 'mall_id' => $this->mall_id]);
  468. if (empty($coupon_share)) return $this->error('未找到相应的分享记录');
  469. if ($coupon_share->coupon_id != $coupon_id) return $this->error('数据异常');
  470. $user_id = $coupon_share->giving_user_id;
  471. $user_coupon_info = AddonsCouponUserRelate::find()
  472. ->select('start_time,end_time,coupon_status,is_use,is_giving,giving_status,user_id')
  473. ->where(['id' => $coupon_share['user_coupon_id'], 'mall_id' => $this->mall_id, 'user_id' => $user_id, 'is_giving' => 1])
  474. ->orderBy('id desc')
  475. ->limit(1)
  476. ->asArray()
  477. ->one();
  478. // 返回这张券的领取人id
  479. $coupon['receive_user_id'] = $coupon_share['receive_user_id'];
  480. // 返回领取者coupon_user_relate表的id
  481. $coupon['get_user_coupon_id'] = $coupon_share['get_user_coupon_id'];
  482. // 是否已失效
  483. $coupon['is_lapse'] = in_array($user_coupon_info['coupon_status'], [AddonsCouponEnum::STATUS_EXPIRED, AddonsCouponEnum::STATUS_FAILURE]) ||
  484. $user_coupon_info['is_use'] == 1 ||
  485. $user_coupon_info['giving_status'] == AddonsCouponEnum::GIVING_STATUS2;
  486. // 返回分享链接的这张券已经被领取,领取人的那张券有没有失效
  487. $coupon['is_lapse_by_latest_user_coupon'] = false;
  488. if ($coupon['get_user_coupon_id'] && $coupon_share['receive_user_id'] == $this->user_id) {
  489. $latest_user_coupon_info = AddonsCouponUserRelate::find()
  490. ->select('start_time,end_time,coupon_status,is_use,is_giving,giving_status,user_id')
  491. ->where(['id' => $coupon['get_user_coupon_id'], 'mall_id' => $this->mall_id])
  492. ->limit(1)
  493. ->asArray()
  494. ->one();
  495. // 是否已失效
  496. $coupon['is_lapse_by_latest_user_coupon'] = in_array($latest_user_coupon_info['coupon_status'], [AddonsCouponEnum::STATUS_EXPIRED, AddonsCouponEnum::STATUS_FAILURE]) ||
  497. $latest_user_coupon_info['is_use'] == 1 ||
  498. $latest_user_coupon_info['giving_status'] == AddonsCouponEnum::GIVING_STATUS2;
  499. }
  500. } else {
  501. // 非转赠优惠券
  502. $user_id = 0;
  503. $mall_log = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.logo');
  504. $mall_name = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.name');
  505. if (empty($mall_log)) {
  506. $mall_log = \Yii::$app->request->hostInfo . '/resources/img/mall/mall_logo_default.jpg';
  507. }
  508. if (empty($mall_name)) $mall_name = 'qimall商城';
  509. $user_coupon_info = AddonsCouponUserRelate::find()
  510. ->select('start_time,end_time')
  511. ->where(['coupon_id' => $coupon_id, 'mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'is_giving' => 0])
  512. ->orderBy('id desc')
  513. ->limit(1)
  514. ->asArray()
  515. ->one();
  516. }
  517. if (!empty($user_coupon_info)) {
  518. $coupon = array_merge($coupon, $user_coupon_info);
  519. $coupon = array_merge($coupon, AddonsCoupon::parseCouponInfo($coupon, true));
  520. } else {
  521. $coupon = array_merge($coupon, AddonsCoupon::parseCouponInfo($coupon));
  522. }
  523. $user = User::findOne(['id' => $user_id, 'mall_id' => $this->mall_id]);
  524. if (empty($user)) {
  525. $coupon_picture = $mall_log;
  526. $coupon_user_name = $mall_name;
  527. } else {
  528. $coupon_picture = $user->avatar_url ?: \Yii::$app->request->hostInfo . '/resources/img/mall/default_avatar_url.png';
  529. $coupon_user_name = ($user->nickname ?: $user->username) ?: '会员';
  530. }
  531. $coupon['coupon_picture'] = $coupon_picture;
  532. $coupon['give_user_name'] = $coupon_user_name;
  533. $couponLink = [];
  534. switch($coupon['appoint_type']){
  535. case AddonsCouponEnum::APPOINT_TYPE1: // 指定分类
  536. $couponLinkList = AddonsCouponCatRelate::find()->where(['coupon_id' => $coupon['id']])->select('cate_id')->column();
  537. if(count($couponLinkList) == 1){
  538. $cate_id = GoodsCate::find()->where(['id' => $couponLinkList[0]])->select('id')->column();
  539. $cateList = self::getAncestors($cate_id);
  540. foreach($cateList as $v){
  541. array_push($couponLink, $v['id']);
  542. }
  543. }
  544. break;
  545. case AddonsCouponEnum::APPOINT_TYPE2: // 指定商品
  546. $couponLink = AddonsCouponGoodsRelate::find()->where(['coupon_id' => $coupon['id']])->select('goods_id')->column();
  547. break;
  548. case AddonsCouponEnum::APPOINT_TYPE4: // 指定门店
  549. $couponLink = explode(',',$coupon['store_id']);
  550. break;
  551. case AddonsCouponEnum::APPOINT_TYPE5: // 指定商户
  552. $couponLink = explode(',',$coupon['mch_id']);
  553. break;
  554. }
  555. $coupon['couponLink'] = $couponLink;
  556. return $this->success('操作成功', ['coupon_info' => $coupon]);
  557. }
  558. public static function getAncestors($childId)
  559. {
  560. $result = [];
  561. $category = GoodsCate::findOne(['id' => $childId]);
  562. if ($category && $category->parent_id != 0) {
  563. // 如果不是顶级节点,则继续向上查找
  564. $result[] = $category;
  565. $result = array_merge(self::getAncestors($category->parent_id), $result);
  566. } else if ($category && $category->parent_id == 0) {
  567. // 如果是顶级节点,直接加入结果
  568. $result[] = $category;
  569. }
  570. return $result;
  571. }
  572. /**
  573. * @name:
  574. * @msg: 优惠券列表
  575. * @param {integer} $coupon_id 优惠券 id
  576. * @return {*}
  577. */
  578. public function actionGetCouponList(){
  579. $params['where'][] = ['=','mall_id',$this->mall_id];
  580. $params['where'][] = ['=','status',StatusEnum::ENABLED];
  581. $params['where'][] = ['=','is_public_receive',StatusEnum::ENABLED];
  582. $params['where'][] = ['=','coupon_status',AddonsCouponEnum::STATUS_RECEIVING];
  583. $params['where'][] = ['store_id'=>''];
  584. $params['with'] = ['statistics'];
  585. $params['order'] = 'id desc';
  586. $limit = \Yii::$app->request->post('limit');
  587. $params['limit'] = $limit;
  588. $list = AddonsCoupon::listPage($params,false,true);
  589. $config = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_COUPON, 'basic');
  590. $list['images'] = json_decode($config['images'],true);
  591. return $this->success('操作成功', $list);
  592. }
  593. //开屏优惠券列表
  594. public function actionOpenScreenCouponList()
  595. {
  596. $where[] = ['mall_id' => $this->mall_id];
  597. $where[] = ['coupon_status' => 2];
  598. $where[] = ['is_open_screen' => 1];
  599. $where[] = ['status' => StatusEnum::ENABLED];
  600. $where[] = [ 'store_id'=>''];
  601. $params['order'] = 'id desc';
  602. $params['where'] = $where;
  603. $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';
  604. $platform_coupon_list = AddonsCoupon::lists($params);
  605. $platform_coupon_list = $this->formatCouponList($platform_coupon_list);
  606. $list = $platform_coupon_list;
  607. return $this->success('success', $list);
  608. }
  609. protected function formatCouponList($coupon_list)
  610. {
  611. $coupon_ids = array_column($coupon_list, 'id');
  612. $relate_list = AddonsCouponUserRelate::lists([
  613. 'where' => [
  614. ['coupon_id' => $coupon_ids],
  615. ['user_id' => $this->user_id]
  616. ],
  617. 'index' => 'coupon_id'
  618. ]);
  619. $list = [];
  620. foreach ($coupon_list as &$item) {
  621. $item['coupon_id'] = $item['id'];
  622. $item['is_receive'] = $item['is_use'] = $item['user_coupon_id'] = 0;
  623. if (isset($relate_list[$item['id']])) {
  624. $item['is_receive'] = 1;
  625. $item['is_use'] = $relate_list[$item['id']]['is_use'];
  626. $item['user_coupon_id'] = $relate_list[$item['id']]['id'];
  627. }else{
  628. $list[]=$item;
  629. }
  630. }
  631. return $list;
  632. }
  633. /**
  634. * 获取用户信息
  635. * @return array|mixed
  636. */
  637. public function actionGetUser()
  638. {
  639. $params = \Yii::$app->request->get();
  640. $res = Yii::$app->services->validate->validate($params, [
  641. [['keyword'], 'required', 'message' => 'keyword必传'],
  642. ]);
  643. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  644. $where[] = ['mall_id' => $this->mall_id];
  645. $where[] = ['status' => StatusEnum::ENABLED];
  646. $where[] = [
  647. 'or',
  648. ['mobile' => $params['keyword']],
  649. ['id' => $params['keyword']],
  650. ];
  651. $user = User::getOne($where, true, [], false);
  652. if (empty($user)) return $this->error('暂无此用户');
  653. if($user['id']==$this->user_id) return $this->error('不能赠送给自己');
  654. return $this->success(ApiCode::CODE_SUCCESS_TITLE, $user);
  655. }
  656. public function actionGiveCoupon(){
  657. try {
  658. $params = \Yii::$app->request->post();
  659. $res = Yii::$app->services->validate->validate($params, [
  660. [['user_coupon_id','receive_user_id','coupon_id'], 'required'],
  661. ]);
  662. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  663. $params['user_id'] = $this->user_id;
  664. $params['num'] = 1;//数量
  665. $params['mall_id'] = $this->mall_id;
  666. $params['receive_platform'] = $this->platform??'';
  667. AddonsCouponUserRelate::giveCouponBy2($params);
  668. return $this->success('success',[]);
  669. }catch (\Exception $e){
  670. \Yii::error($e->getMessage().$e->getFile().$e->getLine());
  671. return $this->error($e->getMessage());
  672. }
  673. }
  674. }