| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- <?php
- namespace addons\Coupon\backend\controllers;
- use addons\Coupon\common\enums\AddonsCouponEnum;
- use addons\Coupon\common\models\AddonsCoupon;
- use addons\Coupon\common\models\AddonsCouponShareLog;
- use addons\Coupon\common\models\AddonsCouponUseLog;
- use addons\Store\common\models\StoreCouponUseLog;
- use addons\Coupon\common\models\AddonsCouponUserRelate;
- use addons\Store\common\models\StoreCouponUserRelate;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use Yii;
- /**
- * 默认控制器
- *
- * Class UserCouponController
- * @package addons\Coupon\backend\controllers
- */
- class UserCouponController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * 首页
- *
- * @return string
- */
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- //连表查询
- $params['join'] = [
- ['inner join', User::tableName() . ' as qu', 'qu.id = acu.user_id'],
- ['left join', AddonsCoupon::tableName() . ' as ac', 'ac.id = acu.coupon_id']
- ];
- $params['alias'] = 'acu';
- $params['where'][] = ['ac.mall_id' => $this->mall_id];
- $params['where'][] = ['acu.status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- // $params['where'][] = ['ac.status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- if (!empty($params['keyword'])) $params['where'][] = ['like', 'ac.name', $params['keyword']];
- if (!empty($params['user_id'])) $params['where'][] = ['=', 'acu.user_id', $params['user_id']];
- if (!empty($params['mobile'])) $params['where'][] = ['=', 'qu.mobile', $params['mobile']];
- if (!empty($params['status'])) {
- switch($params['status']) {
- case 1: // 已过期
- $params['where'][] = ['=', 'acu.coupon_status', 3];
- $params['where'][] = ['=', 'acu.is_giving', 0];
- break;
- case 2: // 已领取
- $params['where'][] = ['=', 'acu.coupon_status', 2];
- $params['where'][] = ['=', 'acu.is_giving', 0];
- break;
- case 3: // 转增中
- $params['where'][] = ['=', 'giving_status', 1];
- $params['where'][] = ['=', 'acu.is_giving', 1];
- break;
- case 4: // 已转增
- $params['where'][] = ['=', 'giving_status', 2];
- $params['where'][] = ['=', 'acu.is_giving', 1];
- break;
- default:
- break;
- }
- }
- $params['order'] = 'acu.id desc';
- $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,
- 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.*';
- $list = AddonsCouponUserRelate::listPage($params);
- // var_dump($list);exit;
- if (!empty($list['list'])) {
- $user_id_arr = array_column($list['list'], 'user_id');
- $params = [];
- $params['where'] = [['id' => $user_id_arr]];
- $params['select'] = 'id,avatar_url,nickname';
- $user_list = User::lists($params);
- $user_nickname_arr = array_column($user_list, 'nickname', 'id');
- $user_avatar_url_arr = array_column($user_list, 'avatar_url', 'id');
- $user_coupon_ids = array_column($list['list'], 'id');
- $user_coupon_use_logs = AddonsCouponUseLog::find()
- ->select('user_coupon_id,created_at')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'user_coupon_id' => $user_coupon_ids])
- ->asArray()
- ->indexBy('user_coupon_id')
- ->all();
- $get_user_coupon_ids = array_column($list['list'],'id');
- //获取转赠记录
- $AddonsCouponShareLogs = AddonsCouponShareLog::lists([
- 'where'=>[
- ['user_coupon_id'=>$get_user_coupon_ids]
- ],
- 'index'=>'get_user_coupon_id',
- ]);
- $receive_temp_arr = [];
- foreach ($AddonsCouponShareLogs as $val){
- if(isset($receive_temp_arr[$val['user_coupon_id']])&&$receive_temp_arr[$val['user_coupon_id']]['giving_status']==2)continue;
- $receive_temp_arr[$val['user_coupon_id']] = $val;
- }
- $receive_user_ids = array_column($receive_temp_arr,'receive_user_id');
- $params = [];
- $params['where'] = [['id' => $receive_user_ids]];
- $params['select'] = 'id,nickname';
- $params['index'] = 'id';
- $receive_user_list = User::lists($params);
- foreach ($list['list'] as &$v) {
- $v = array_merge($v, AddonsCoupon::parseCouponInfo($v, true));
- $v['nickname'] = isset($user_nickname_arr[$v['user_id']]) ? $user_nickname_arr[$v['user_id']] : '';
- $v['avatar_url'] = isset($user_avatar_url_arr[$v['user_id']]) ? $user_avatar_url_arr[$v['user_id']] : '';
- if($v['expire_type']==2){
- $v['is_failure'] = $v['end_at']<time()?"0":"1";
- }else{
- $v['is_failure'] = $v['end_time']<time()?"0":"1";
- }
- $log = $user_coupon_use_logs[$v['id']] ?? [];
- $v['used_time'] = $log['created_at'] ?? 0;
- $v['giving_user'] = '';
- if($v['giving_status']==2){
- if(isset($receive_temp_arr[$v['id']])){
- $AddonsCouponShareLog = $receive_temp_arr[$v['id']];
- $receive_user_id = $AddonsCouponShareLog['receive_user_id'];
- if(isset($receive_user_list[$receive_user_id])){
- $v['giving_user'].='受赠者ID:' .$receive_user_id;
- }
- }
- }
- // 兑换券
- if ($v['type'] == AddonsCouponEnum::EXCHANGE) {
- $v['content'] = '全额抵扣';
- $v['min_price'] = 0;
- }
- // 优惠券的appoint_type被qimall_addons_coupon_user_relate表的appoint_type覆盖,这里将其重新覆盖
- $v['appoint_type'] = $v['coupon_appoint_type'];
- }
- }
- return $this->success('操作成功', $list);
- }
- }
- return $this->render($this->action->id);
- }
- // public function actionReceivedList()
- // {
- // return $this->render('detail-list');
- // }
- // public function actionUsedList()
- // {
- // return $this->render('detail-list');
- // }
- /**
- * @name:
- * @msg: 优惠券被领取/使用详情
- * @param {*}
- * @return {*}
- */
- public function actionUserCouponDetail()
- {
- if (!\Yii::$app->request->isAjax) {
- return $this->render('/user-coupon/detail-list');
- }
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $coupon_id = \Yii::$app->request->get('coupon_id');
- $coupon = AddonsCoupon::getOne([['id' => $coupon_id]]);
- // 1:已领取,2:已使用
- $status = \Yii::$app->request->get('status');
- $page = \Yii::$app->request->get('page') ?? 1;
- $limit = \Yii::$app->request->get('page_size') ?? $this->pageSize;
- if (empty($status)) {
- return $this->error('参数错误');
- }
- $user_id = \Yii::$app->request->get('user_id');
- // 会员昵称/手机号
- $keyword = \Yii::$app->request->get('keyword');
- // 使用时间
- $start_time = \Yii::$app->request->get('start_time');
- $end_time = \Yii::$app->request->get('end_time');
- $where[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
- if (!empty($coupon_id)) {
- $where[] = ['coupon_id' => $coupon_id];
- }
- if (2 == $status) {
- $where[] = ['is_use' => 1];
- }
- if (!empty($user_id)) {
- $where[] = ['user_id' => $user_id];
- }
- if (!empty($start_time)) {
- $where[] = ['>=', 'start_time', $start_time];
- }
- if (!empty($end_time)) {
- $where[] = ['<=', 'end_time', $end_time];
- }
- if (!empty($keyword)) {
- $user_ids = User::find()
- ->select('id')
- ->where(['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]])
- ->asArray()
- ->column();
- $where[] = ['user_id' => $user_ids];
- }
- if (!empty($coupon->store_id)) {
- $model = StoreCouponUserRelate::class;
- $where[] = ['from_type' => 1];
- } else {
- $model = AddonsCouponUserRelate::class;
- }
- $params = [
- 'alias' => 'uc',
- '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',
- 'where' => $where,
- 'page' => $page,
- 'limit' => $limit,
- 'order' => 'created_at desc',
- ];
- $list = $model::listPage($params);
- if (!empty($list['list'])) {
- $user_coupon_ids = array_values(array_unique(array_column($list['list'], 'id')));
- $coupon_use_model = $coupon->store_id ? StoreCouponUseLog::class : AddonsCouponUseLog::class;
- $coupon_used_logs = $coupon_use_model::find()
- ->where(['user_coupon_id' => $user_coupon_ids, 'status' => 1, 'mall_id' => $this->mall_id])
- ->asArray()
- ->indexBy('user_coupon_id')
- ->all();
- $user_ids = array_values(array_unique(array_column($list['list'], 'user_id')));
- $user_query = User::find()
- ->select('id,nickname,mobile,avatar_url')
- ->where(['id' => $user_ids, 'status' => StatusEnum::ENABLED]);
- if (!empty($keyword)) {
- $user_query->andWhere(['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]]);
- }
- $user_arrs = $user_query->asArray()->indexBy('id')->all();
- $coupons = AddonsCoupon::find()
- ->select('id,name')
- ->where(['id' => array_column($list['list'], 'coupon_id')])
- ->asArray()
- ->indexBy('id')
- ->all();
- foreach ($list['list'] as &$lv) {
- $lv['order_id'] = $coupon_used_logs[$lv['id']]['order_id'] ?? 0;
- $user = $user_arrs[$lv['user_id']] ?? [];
- $lv['nickname'] = $user['nickname'] ?? '';
- $lv['mobile'] = $user['mobile'] ?? '';
- $lv['avatar_url'] = $user['avatar_url'];
- if (empty($user['avatar_url'])) $lv['avatar_url'] = \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
- $coupon = $coupons[$lv['coupon_id']] ?? [];
- $lv['name'] = $coupon['name'] ?? '';
- }
- }
- // $list = UserCoupon::getError();dd($list);
- if (empty($coupon_id)) $coupon_id = $list['list'][0]['coupon_id'] ?? 0;
- $coupon_data = AddonsCoupon::find()
- ->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')
- ->where(['mall_id' => $this->mall_id, 'id' => $coupon_id, 'status' => 1])
- ->asArray()
- ->one();
- $coupon_info = !empty($coupon_data) ? AddonsCoupon::parseCouponInfo($coupon_data) : [];
- $coupon_info = array_merge($coupon_data, $coupon_info);
- return $this->success('操作成功', ['list' => $list, 'coupon_info' => $coupon_info,'send_from_map'=>AddonsCouponEnum::getSendFromMap()]);
- }
- /**
- * @name:
- * @msg: 停止单个用户用券
- * @param {*}
- * @return {*}
- */
- public function actionForceFailure()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $coupon_id = \Yii::$app->request->get('coupon_id');
- $user_id = \Yii::$app->request->get('user_id');
- if (empty($coupon_id) || empty($user_id)) {
- return $this->error('参数错误');
- }
- $coupon = AddonsCoupon::getOne([['id' => $coupon_id]]);
- $model = $coupon->store_id ? StoreCouponUserRelate::class : AddonsCouponUserRelate::class;
- $user_coupon = $model::find()
- ->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])
- ->limit(1)
- ->one();
- if (empty($user_coupon)) {
- return $this->error('无法停止该用户用券');
- }
- $user_coupon->is_force_failure = 1;
- $user_coupon->updated_at = time();
- // $user_coupon->status = 0;
- $user_coupon->coupon_status = 4;
- if ($user_coupon->save()) {
- return $this->success('操作成功');
- } else {
- return $this->error('操作失败');
- }
- }
- /**
- * @name:
- * @msg: 停止全部用户用券
- * @param {*}
- * @return {*}
- */
- public function actionForceFailureAll()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $coupon_id = \Yii::$app->request->get('coupon_id');
- if (empty($coupon_id)) {
- return $this->error('参数错误');
- }
- $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]);
- if (!empty($user_coupons)) {
- $res = AddonsCouponUserRelate::updateAll(
- ['is_force_failure' => 1, 'updated_at' => time(), 'coupon_status' => 4],
- ['mall_id' => $this->mall_id, 'coupon_id' => $coupon_id, 'is_use' => 0, 'is_giving' => 0, 'status' => 1, 'is_force_failure' => 0]
- );
- if ($res === false) {
- return $this->error('操作失败');
- }
- }
- return $this->success('操作成功');
- }
- /**
- * @notes:操作处理
- * @return mixed|void|null
- * @throws \yii\db\Exception
- * @author: lun
- * @Time: 2022/10/27 9:46
- */
- public function actionHandle()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- $params = Yii::$app->request->get();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($params,[
- [['id'], 'required'],
- [['id','status'], 'integer'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if (!isset($params['status'])) unset($params['status']);
- $params['mall_id'] = $this->mall_id;
- $res = AddonsCouponUserRelate::handle($params);
- if (!$res) return $this->error('修改失败:msg=' . AddonsCouponUserRelate::getStaticError());
- return $this->success('修改成功');
- }
- }
- }
- public function actionDelBatch()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isPost) {
- $params = Yii::$app->request->post();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($params,[
- [['id'], 'required'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $res = AddonsCouponUserRelate::updateAll(['status'=>StatusEnum::DELETE],['id'=>$params['id'],'mall_id'=>$this->mall_id]);
- if (!$res) return $this->error('批量删除失败:msg=' . AddonsCouponUserRelate::getStaticError());
- return $this->success('批量删除成功');
- }
- }
- }
- /**
- * 转赠记录
- *
- * @return void
- */
- public function actionTransLog(int $id)
- {
- // ID
- $ids[] = $id;
- // 来源
- $from_id = $id;
- while ($from = AddonsCouponShareLog::find()->where(['get_user_coupon_id' => $from_id])->select([
- 'id', 'user_coupon_id'
- ])->one()) {
- $from_id = $from->user_coupon_id;
- $ids[] = $from_id;
- }
- // 赠送
- $give_id = $id;
- while ($give = AddonsCouponShareLog::find()->where(['user_coupon_id' => $give_id])->andWhere(['=', 'giving_status', 2])->select([
- 'id', 'get_user_coupon_id'
- ])->one()) {
- $give_id = $give->get_user_coupon_id;
- $ids[] = $give_id;
- }
- $list = AddonsCouponUserRelate::find()->with(['user' => function ($query) {
- $query->select(['id', 'avatar_url', 'nickname', 'mobile', 'username']);
- }, 'coupon' => function ($query) {
- $query->select(['id', 'name', 'appoint_type']);
- }])->where(['id' => $ids])->orderBy('id asc')->asArray()->all();
- $list = array_map(function ($item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- return $item;
- }, $list);
- return $this->success('ok', $list);
- }
- /**
- * 用户优惠券失效
- * @return void
- */
- public function actionTransfer()
- {
- $coupon_id = Yii::$app->request->get('id');
- if (empty($coupon_id)) {
- return $this->error('请选择要失效的优惠券');
- }
- $couponModel = AddonsCouponUserRelate::findOne(['id' => $coupon_id]);
- if (empty($couponModel)) {
- return $this->error('优惠券不存在');
- }
- $couponModel->coupon_status = 4;
- if (!$couponModel->save()) {
- return $this->error('优惠券失效失败');
- }
- return $this->success('优惠券失效成功');
- }
- }
|