UserCouponController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. namespace addons\Coupon\backend\controllers;
  3. use addons\Coupon\common\enums\AddonsCouponEnum;
  4. use addons\Coupon\common\models\AddonsCoupon;
  5. use addons\Coupon\common\models\AddonsCouponShareLog;
  6. use addons\Coupon\common\models\AddonsCouponUseLog;
  7. use addons\Store\common\models\StoreCouponUseLog;
  8. use addons\Coupon\common\models\AddonsCouponUserRelate;
  9. use addons\Store\common\models\StoreCouponUserRelate;
  10. use common\enums\StatusEnum;
  11. use common\models\user\User;
  12. use Yii;
  13. /**
  14. * 默认控制器
  15. *
  16. * Class UserCouponController
  17. * @package addons\Coupon\backend\controllers
  18. */
  19. class UserCouponController extends BaseController
  20. {
  21. public $enableCsrfValidation = false;
  22. /**
  23. * 首页
  24. *
  25. * @return string
  26. */
  27. public function actionIndex()
  28. {
  29. if (\Yii::$app->request->isAjax) {
  30. if (\Yii::$app->request->isGet) {
  31. $params = \Yii::$app->request->get();
  32. //连表查询
  33. $params['join'] = [
  34. ['inner join', User::tableName() . ' as qu', 'qu.id = acu.user_id'],
  35. ['left join', AddonsCoupon::tableName() . ' as ac', 'ac.id = acu.coupon_id']
  36. ];
  37. $params['alias'] = 'acu';
  38. $params['where'][] = ['ac.mall_id' => $this->mall_id];
  39. $params['where'][] = ['acu.status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  40. // $params['where'][] = ['ac.status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  41. if (!empty($params['keyword'])) $params['where'][] = ['like', 'ac.name', $params['keyword']];
  42. if (!empty($params['user_id'])) $params['where'][] = ['=', 'acu.user_id', $params['user_id']];
  43. if (!empty($params['mobile'])) $params['where'][] = ['=', 'qu.mobile', $params['mobile']];
  44. if (!empty($params['status'])) {
  45. switch($params['status']) {
  46. case 1: // 已过期
  47. $params['where'][] = ['=', 'acu.coupon_status', 3];
  48. $params['where'][] = ['=', 'acu.is_giving', 0];
  49. break;
  50. case 2: // 已领取
  51. $params['where'][] = ['=', 'acu.coupon_status', 2];
  52. $params['where'][] = ['=', 'acu.is_giving', 0];
  53. break;
  54. case 3: // 转增中
  55. $params['where'][] = ['=', 'giving_status', 1];
  56. $params['where'][] = ['=', 'acu.is_giving', 1];
  57. break;
  58. case 4: // 已转增
  59. $params['where'][] = ['=', 'giving_status', 2];
  60. $params['where'][] = ['=', 'acu.is_giving', 1];
  61. break;
  62. default:
  63. break;
  64. }
  65. }
  66. $params['order'] = 'acu.id desc';
  67. $params['select'] = 'ac.id as coupon_id,ac.name,ac.type,ac.discount,ac.min_price,ac.sub_price,ac.sort,ac.expire_type,ac.expire_day,
  68. ac.begin_at,ac.end_at,ac.appoint_type coupon_appoint_type,ac.rule,ac.is_member,ac.is_giving,ac.coupon_status,ac.discount_limit,ac.is_original_use,acu.*';
  69. $list = AddonsCouponUserRelate::listPage($params);
  70. // var_dump($list);exit;
  71. if (!empty($list['list'])) {
  72. $user_id_arr = array_column($list['list'], 'user_id');
  73. $params = [];
  74. $params['where'] = [['id' => $user_id_arr]];
  75. $params['select'] = 'id,avatar_url,nickname';
  76. $user_list = User::lists($params);
  77. $user_nickname_arr = array_column($user_list, 'nickname', 'id');
  78. $user_avatar_url_arr = array_column($user_list, 'avatar_url', 'id');
  79. $user_coupon_ids = array_column($list['list'], 'id');
  80. $user_coupon_use_logs = AddonsCouponUseLog::find()
  81. ->select('user_coupon_id,created_at')
  82. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'user_coupon_id' => $user_coupon_ids])
  83. ->asArray()
  84. ->indexBy('user_coupon_id')
  85. ->all();
  86. $get_user_coupon_ids = array_column($list['list'],'id');
  87. //获取转赠记录
  88. $AddonsCouponShareLogs = AddonsCouponShareLog::lists([
  89. 'where'=>[
  90. ['user_coupon_id'=>$get_user_coupon_ids]
  91. ],
  92. 'index'=>'get_user_coupon_id',
  93. ]);
  94. $receive_temp_arr = [];
  95. foreach ($AddonsCouponShareLogs as $val){
  96. if(isset($receive_temp_arr[$val['user_coupon_id']])&&$receive_temp_arr[$val['user_coupon_id']]['giving_status']==2)continue;
  97. $receive_temp_arr[$val['user_coupon_id']] = $val;
  98. }
  99. $receive_user_ids = array_column($receive_temp_arr,'receive_user_id');
  100. $params = [];
  101. $params['where'] = [['id' => $receive_user_ids]];
  102. $params['select'] = 'id,nickname';
  103. $params['index'] = 'id';
  104. $receive_user_list = User::lists($params);
  105. foreach ($list['list'] as &$v) {
  106. $v = array_merge($v, AddonsCoupon::parseCouponInfo($v, true));
  107. $v['nickname'] = isset($user_nickname_arr[$v['user_id']]) ? $user_nickname_arr[$v['user_id']] : '';
  108. $v['avatar_url'] = isset($user_avatar_url_arr[$v['user_id']]) ? $user_avatar_url_arr[$v['user_id']] : '';
  109. if($v['expire_type']==2){
  110. $v['is_failure'] = $v['end_at']<time()?"0":"1";
  111. }else{
  112. $v['is_failure'] = $v['end_time']<time()?"0":"1";
  113. }
  114. $log = $user_coupon_use_logs[$v['id']] ?? [];
  115. $v['used_time'] = $log['created_at'] ?? 0;
  116. $v['giving_user'] = '';
  117. if($v['giving_status']==2){
  118. if(isset($receive_temp_arr[$v['id']])){
  119. $AddonsCouponShareLog = $receive_temp_arr[$v['id']];
  120. $receive_user_id = $AddonsCouponShareLog['receive_user_id'];
  121. if(isset($receive_user_list[$receive_user_id])){
  122. $v['giving_user'].='受赠者ID:' .$receive_user_id;
  123. }
  124. }
  125. }
  126. // 兑换券
  127. if ($v['type'] == AddonsCouponEnum::EXCHANGE) {
  128. $v['content'] = '全额抵扣';
  129. $v['min_price'] = 0;
  130. }
  131. // 优惠券的appoint_type被qimall_addons_coupon_user_relate表的appoint_type覆盖,这里将其重新覆盖
  132. $v['appoint_type'] = $v['coupon_appoint_type'];
  133. }
  134. }
  135. return $this->success('操作成功', $list);
  136. }
  137. }
  138. return $this->render($this->action->id);
  139. }
  140. // public function actionReceivedList()
  141. // {
  142. // return $this->render('detail-list');
  143. // }
  144. // public function actionUsedList()
  145. // {
  146. // return $this->render('detail-list');
  147. // }
  148. /**
  149. * @name:
  150. * @msg: 优惠券被领取/使用详情
  151. * @param {*}
  152. * @return {*}
  153. */
  154. public function actionUserCouponDetail()
  155. {
  156. if (!\Yii::$app->request->isAjax) {
  157. return $this->render('/user-coupon/detail-list');
  158. }
  159. if (!\Yii::$app->request->isGet) {
  160. return $this->error('请求方式错误');
  161. }
  162. $coupon_id = \Yii::$app->request->get('coupon_id');
  163. $coupon = AddonsCoupon::getOne([['id' => $coupon_id]]);
  164. // 1:已领取,2:已使用
  165. $status = \Yii::$app->request->get('status');
  166. $page = \Yii::$app->request->get('page') ?? 1;
  167. $limit = \Yii::$app->request->get('page_size') ?? $this->pageSize;
  168. if (empty($status)) {
  169. return $this->error('参数错误');
  170. }
  171. $user_id = \Yii::$app->request->get('user_id');
  172. // 会员昵称/手机号
  173. $keyword = \Yii::$app->request->get('keyword');
  174. // 使用时间
  175. $start_time = \Yii::$app->request->get('start_time');
  176. $end_time = \Yii::$app->request->get('end_time');
  177. $where[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
  178. if (!empty($coupon_id)) {
  179. $where[] = ['coupon_id' => $coupon_id];
  180. }
  181. if (2 == $status) {
  182. $where[] = ['is_use' => 1];
  183. }
  184. if (!empty($user_id)) {
  185. $where[] = ['user_id' => $user_id];
  186. }
  187. if (!empty($start_time)) {
  188. $where[] = ['>=', 'start_time', $start_time];
  189. }
  190. if (!empty($end_time)) {
  191. $where[] = ['<=', 'end_time', $end_time];
  192. }
  193. if (!empty($keyword)) {
  194. $user_ids = User::find()
  195. ->select('id')
  196. ->where(['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]])
  197. ->asArray()
  198. ->column();
  199. $where[] = ['user_id' => $user_ids];
  200. }
  201. if (!empty($coupon->store_id)) {
  202. $model = StoreCouponUserRelate::class;
  203. $where[] = ['from_type' => 1];
  204. } else {
  205. $model = AddonsCouponUserRelate::class;
  206. }
  207. $params = [
  208. 'alias' => 'uc',
  209. 'select' => 'id,user_id,coupon_id,is_use,is_giving,giving_status,receive_type,is_force_failure,receive_platform,created_at,start_time as begin_at,end_time as end_at,coupon_status',
  210. 'where' => $where,
  211. 'page' => $page,
  212. 'limit' => $limit,
  213. 'order' => 'created_at desc',
  214. ];
  215. $list = $model::listPage($params);
  216. if (!empty($list['list'])) {
  217. $user_coupon_ids = array_values(array_unique(array_column($list['list'], 'id')));
  218. $coupon_use_model = $coupon->store_id ? StoreCouponUseLog::class : AddonsCouponUseLog::class;
  219. $coupon_used_logs = $coupon_use_model::find()
  220. ->where(['user_coupon_id' => $user_coupon_ids, 'status' => 1, 'mall_id' => $this->mall_id])
  221. ->asArray()
  222. ->indexBy('user_coupon_id')
  223. ->all();
  224. $user_ids = array_values(array_unique(array_column($list['list'], 'user_id')));
  225. $user_query = User::find()
  226. ->select('id,nickname,mobile,avatar_url')
  227. ->where(['id' => $user_ids, 'status' => StatusEnum::ENABLED]);
  228. if (!empty($keyword)) {
  229. $user_query->andWhere(['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]]);
  230. }
  231. $user_arrs = $user_query->asArray()->indexBy('id')->all();
  232. $coupons = AddonsCoupon::find()
  233. ->select('id,name')
  234. ->where(['id' => array_column($list['list'], 'coupon_id')])
  235. ->asArray()
  236. ->indexBy('id')
  237. ->all();
  238. foreach ($list['list'] as &$lv) {
  239. $lv['order_id'] = $coupon_used_logs[$lv['id']]['order_id'] ?? 0;
  240. $user = $user_arrs[$lv['user_id']] ?? [];
  241. $lv['nickname'] = $user['nickname'] ?? '';
  242. $lv['mobile'] = $user['mobile'] ?? '';
  243. $lv['avatar_url'] = $user['avatar_url'];
  244. if (empty($user['avatar_url'])) $lv['avatar_url'] = \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  245. $coupon = $coupons[$lv['coupon_id']] ?? [];
  246. $lv['name'] = $coupon['name'] ?? '';
  247. }
  248. }
  249. // $list = UserCoupon::getError();dd($list);
  250. if (empty($coupon_id)) $coupon_id = $list['list'][0]['coupon_id'] ?? 0;
  251. $coupon_data = AddonsCoupon::find()
  252. ->select('id,type,store_id,name,begin_at,end_at,discount,discount_limit,min_price,sub_price,coupon_status,is_original_use,rule,expire_type,expire_day,effect_days')
  253. ->where(['mall_id' => $this->mall_id, 'id' => $coupon_id, 'status' => 1])
  254. ->asArray()
  255. ->one();
  256. $coupon_info = !empty($coupon_data) ? AddonsCoupon::parseCouponInfo($coupon_data) : [];
  257. $coupon_info = array_merge($coupon_data, $coupon_info);
  258. return $this->success('操作成功', ['list' => $list, 'coupon_info' => $coupon_info,'send_from_map'=>AddonsCouponEnum::getSendFromMap()]);
  259. }
  260. /**
  261. * @name:
  262. * @msg: 停止单个用户用券
  263. * @param {*}
  264. * @return {*}
  265. */
  266. public function actionForceFailure()
  267. {
  268. if (!\Yii::$app->request->isGet) {
  269. return $this->error('请求方式错误');
  270. }
  271. $coupon_id = \Yii::$app->request->get('coupon_id');
  272. $user_id = \Yii::$app->request->get('user_id');
  273. if (empty($coupon_id) || empty($user_id)) {
  274. return $this->error('参数错误');
  275. }
  276. $coupon = AddonsCoupon::getOne([['id' => $coupon_id]]);
  277. $model = $coupon->store_id ? StoreCouponUserRelate::class : AddonsCouponUserRelate::class;
  278. $user_coupon = $model::find()
  279. ->where(['mall_id' => $this->mall_id, 'coupon_id' => $coupon_id, 'user_id' => $user_id, 'is_use' => 0, 'is_giving' => 0, 'status' => 1, 'is_force_failure' => 0])
  280. ->limit(1)
  281. ->one();
  282. if (empty($user_coupon)) {
  283. return $this->error('无法停止该用户用券');
  284. }
  285. $user_coupon->is_force_failure = 1;
  286. $user_coupon->updated_at = time();
  287. // $user_coupon->status = 0;
  288. $user_coupon->coupon_status = 4;
  289. if ($user_coupon->save()) {
  290. return $this->success('操作成功');
  291. } else {
  292. return $this->error('操作失败');
  293. }
  294. }
  295. /**
  296. * @name:
  297. * @msg: 停止全部用户用券
  298. * @param {*}
  299. * @return {*}
  300. */
  301. public function actionForceFailureAll()
  302. {
  303. if (!\Yii::$app->request->isGet) {
  304. return $this->error('请求方式错误');
  305. }
  306. $coupon_id = \Yii::$app->request->get('coupon_id');
  307. if (empty($coupon_id)) {
  308. return $this->error('参数错误');
  309. }
  310. $user_coupons = AddonsCouponUserRelate::findAll(['mall_id' => $this->mall_id, 'coupon_id' => $coupon_id, 'is_use' => 0, 'is_giving' => 0, 'status' => 1, 'is_force_failure' => 0]);
  311. if (!empty($user_coupons)) {
  312. $res = AddonsCouponUserRelate::updateAll(
  313. ['is_force_failure' => 1, 'updated_at' => time(), 'coupon_status' => 4],
  314. ['mall_id' => $this->mall_id, 'coupon_id' => $coupon_id, 'is_use' => 0, 'is_giving' => 0, 'status' => 1, 'is_force_failure' => 0]
  315. );
  316. if ($res === false) {
  317. return $this->error('操作失败');
  318. }
  319. }
  320. return $this->success('操作成功');
  321. }
  322. /**
  323. * @notes:操作处理
  324. * @return mixed|void|null
  325. * @throws \yii\db\Exception
  326. * @author: lun
  327. * @Time: 2022/10/27 9:46
  328. */
  329. public function actionHandle()
  330. {
  331. $request = Yii::$app->request;
  332. if ($request->isAjax) {
  333. if ($request->isGet) {
  334. $params = Yii::$app->request->get();
  335. // 检查提交的参数
  336. $res = Yii::$app->services->validate->validate($params,[
  337. [['id'], 'required'],
  338. [['id','status'], 'integer'],
  339. ]);
  340. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  341. if (!isset($params['status'])) unset($params['status']);
  342. $params['mall_id'] = $this->mall_id;
  343. $res = AddonsCouponUserRelate::handle($params);
  344. if (!$res) return $this->error('修改失败:msg=' . AddonsCouponUserRelate::getStaticError());
  345. return $this->success('修改成功');
  346. }
  347. }
  348. }
  349. public function actionDelBatch()
  350. {
  351. $request = Yii::$app->request;
  352. if ($request->isAjax) {
  353. if ($request->isPost) {
  354. $params = Yii::$app->request->post();
  355. // 检查提交的参数
  356. $res = Yii::$app->services->validate->validate($params,[
  357. [['id'], 'required'],
  358. ]);
  359. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  360. $params['mall_id'] = $this->mall_id;
  361. $res = AddonsCouponUserRelate::updateAll(['status'=>StatusEnum::DELETE],['id'=>$params['id'],'mall_id'=>$this->mall_id]);
  362. if (!$res) return $this->error('批量删除失败:msg=' . AddonsCouponUserRelate::getStaticError());
  363. return $this->success('批量删除成功');
  364. }
  365. }
  366. }
  367. /**
  368. * 转赠记录
  369. *
  370. * @return void
  371. */
  372. public function actionTransLog(int $id)
  373. {
  374. // ID
  375. $ids[] = $id;
  376. // 来源
  377. $from_id = $id;
  378. while ($from = AddonsCouponShareLog::find()->where(['get_user_coupon_id' => $from_id])->select([
  379. 'id', 'user_coupon_id'
  380. ])->one()) {
  381. $from_id = $from->user_coupon_id;
  382. $ids[] = $from_id;
  383. }
  384. // 赠送
  385. $give_id = $id;
  386. while ($give = AddonsCouponShareLog::find()->where(['user_coupon_id' => $give_id])->andWhere(['=', 'giving_status', 2])->select([
  387. 'id', 'get_user_coupon_id'
  388. ])->one()) {
  389. $give_id = $give->get_user_coupon_id;
  390. $ids[] = $give_id;
  391. }
  392. $list = AddonsCouponUserRelate::find()->with(['user' => function ($query) {
  393. $query->select(['id', 'avatar_url', 'nickname', 'mobile', 'username']);
  394. }, 'coupon' => function ($query) {
  395. $query->select(['id', 'name', 'appoint_type']);
  396. }])->where(['id' => $ids])->orderBy('id asc')->asArray()->all();
  397. $list = array_map(function ($item) {
  398. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  399. return $item;
  400. }, $list);
  401. return $this->success('ok', $list);
  402. }
  403. /**
  404. * 用户优惠券失效
  405. * @return void
  406. */
  407. public function actionTransfer()
  408. {
  409. $coupon_id = Yii::$app->request->get('id');
  410. if (empty($coupon_id)) {
  411. return $this->error('请选择要失效的优惠券');
  412. }
  413. $couponModel = AddonsCouponUserRelate::findOne(['id' => $coupon_id]);
  414. if (empty($couponModel)) {
  415. return $this->error('优惠券不存在');
  416. }
  417. $couponModel->coupon_status = 4;
  418. if (!$couponModel->save()) {
  419. return $this->error('优惠券失效失败');
  420. }
  421. return $this->success('优惠券失效成功');
  422. }
  423. }