| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715 |
- <?php
- namespace addons\Coupon\api\modules\v1\controllers;
- use addons\Coupon\common\enums\AddonsCouponEnum;
- use addons\Coupon\common\models\AddonsCoupon;
- use addons\Coupon\common\models\AddonsCouponCatRelate;
- use addons\Coupon\common\models\AddonsCouponGoodsRelate;
- use addons\Coupon\common\models\AddonsCouponMemberRelate;
- use addons\Coupon\common\models\AddonsCouponShareLog;
- use addons\Coupon\common\models\AddonsCouponUserRelate;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\helpers\ApiCode;
- use common\models\goods\GoodsCateRelate;
- use Yii;
- use api\controllers\OnAuthController;
- use common\helpers\ArrayHelper;
- use common\models\goods\Goods;
- use common\models\goods\GoodsCate;
- use common\models\user\User;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\Coupon\api\modules\v1\controllers
- */
- class DefaultController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['coupon-list'];
- /**
- * @desc:优惠券列表--商品详情页优惠券列表
- * @Author: hua
- * @Date: 2021/12/24
- * @Time: 15:35
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionCouponList()
- {
- if (\Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['goods_id'], 'required'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $goods_id = $params['goods_id'];
- $coupon_goods_relate_list = AddonsCouponGoodsRelate::lists(['where'=>[['goods_id'=>$goods_id]],'select'=>'coupon_id']);
- // dd($coupon_goods_relate_list);
- $goods_relate_coupon_id_arr = array_column($coupon_goods_relate_list,'coupon_id');
- $goods_cate_relate_list = GoodsCateRelate::lists(['where'=>[['goods_id'=>$goods_id]],'select'=>'cate_id']);
- $cate_id_arr = array_column($goods_cate_relate_list,'cate_id');
- $coupon_cate_relate_list = AddonsCouponCatRelate::lists(['where'=>[['cate_id'=>$cate_id_arr]],'select'=>'coupon_id']);
- $cate_relate_coupon_id_arr = array_column($coupon_cate_relate_list,'coupon_id');
- $where[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'is_public_receive' => 1, 'coupon_status' => AddonsCouponEnum::STATUS_RECEIVING, 'store_id' => 0,'mch_id'=>0];
- $where[] = ['>', 'total_count' ,0];
- $where[] = [
- 'or',
- ['appoint_type' => AddonsCouponEnum::APPOINT_TYPE3],
- ['appoint_type' => AddonsCouponEnum::APPOINT_TYPE2,'id'=>$goods_relate_coupon_id_arr],
- ['appoint_type' => AddonsCouponEnum::APPOINT_TYPE1,'id'=>$cate_relate_coupon_id_arr],
- ];
- $params['where'] = $where;
- $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';
- $params['order'] = 'created_at desc';
- $list = AddonsCoupon::listPage($params);
- if (!empty($list['list'])) {
- $coupon_ids = array_column($list['list'],'id');
- if(!empty($this->user_id)){
- $coupon_user_relate_list = AddonsCouponUserRelate::lists([
- 'where'=>
- [
- ['coupon_id'=>$coupon_ids],
- ['user_id'=>$this->user_id],
- ['status'=>StatusEnum::ENABLED],
- ['or', ['is_giving' => 0], ['is_giving' => 1, 'giving_status' => AddonsCouponEnum::GIVING_STATUS1]],
- ],
- 'select'=>'count(id) as counts,coupon_id,start_time,end_time',
- 'group'=>'coupon_id',
- 'index'=>'coupon_id'
- ]);
- }
- $coupon_ids = array_column($list['list'],'id');
- $member_levels = AddonsCouponMemberRelate::find()
- ->select('member_level')
- ->where(['mall_id' => $this->mall_id, 'coupon_id' => $coupon_ids])
- ->asArray()
- ->column();
- $rows = [];
- foreach ($list['list'] as $item){
- $item['is_receive'] = 0;
- $item['counts'] = 0;
- if (1 == $item['is_member']) {
- // 限制会员等级领取
- if (!in_array($this->user->level, $member_levels)){
- continue;
- }
- }
- $item['text'] = '';
- if(!empty($coupon_user_relate_list[$item['id']])){
- $item['counts'] = $coupon_user_relate_list[$item['id']]['counts'];
- if (1 == $item['is_receive_limit'] && $item['counts'] >= $item['receive_count']) $item['is_receive'] = 1;
- $item['begin_at'] = $coupon_user_relate_list[$item['id']]['start_time'];
- $item['end_at'] = $coupon_user_relate_list[$item['id']]['end_time'];
- }else{
- switch ($item['expire_type']) {
- case 1:
- $item['text'] = "领取后" . $item['expire_day'] . "天过期";
- break;
- case 3:
- $item['text'] = "领取后," . $item['effect_days'] . '天后生效,' . $item['expire_day'] . "天过期";
- break;
- }
- }
- // 剩余可领取数量
- if (1 == $item['is_receive_limit']) {
- $item['remainder_count'] = $item['receive_count'] - $item['counts'];
- } else {
- $item['remainder_count'] = $item['total_count'];
- }
- $rows[]=$item;
- }
- $list['list'] = $rows;
- }
- $list['coupon_status_map'] = AddonsCouponEnum::getCouponStatusMap();
- return $this->success('操作成功', $list);
- }
- }
- /**
- * @name:
- * @msg: 会员优惠券列表(我的优惠券列表)
- * @param {*}
- * @return {*}
- */
- public function actionUserCouponList()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $user_id = $this->user_id;
- $page = \Yii::$app->request->get('page') ?? 1;
- $limit = 20;
- $status = \Yii::$app->request->get('status');
- if (!in_array($status, [2, 1])) {
- return $this->error('参数错误');
- }
- $where[] = ['mall_id' => $this->mall_id, 'user_id' => $user_id, 'store_id' => 0];
- if ($status == 1) {
- // 未失效
- $where[] = ['is_use' => 0, 'is_force_failure' => 0, 'status' => StatusEnum::ENABLED, 'coupon_status' => AddonsCouponEnum::STATUS_RECEIVING];
- $where[] = ['>', 'end_time', time()];
- $where[] = [
- 'or',
- ['is_giving' => 0],
- ['is_giving' => 1, 'giving_status' => [0, 1]],
- ];
- } else {
- // 失效
- $where[] = [
- 'or',
- ['status' => StatusEnum::DISABLED],
- ['is_giving' => 1, 'giving_status' => 2],
- ['is_force_failure' => 1],
- ['is_use' => 1],
- ['>', 'coupon_status', AddonsCouponEnum::STATUS_RECEIVING],
- ['<=', 'end_time', time()],
- ];
- }
- // $coupon_ids = AddonsCoupon::find()->select('id')->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->column();
- // $where[] = ['in', 'coupon_id', $coupon_ids];
- $params = [
- 'select' => 'id as user_coupon_id,coupon_id,coupon_status,is_use,is_giving,giving_status,user_id,start_time,end_time',
- 'where' => $where,
- 'page' => $page,
- 'limit' => $limit,
- 'order' => 'created_at desc,id desc'
- ];
- $user_coupon_list = AddonsCouponUserRelate::listPage($params);
- // $user_coupon_list = AddonsCouponUserRelate::getStaticError();
- // dd($user_coupon_list);
- if (!empty($user_coupon_list['list'])) {
- $coupon_ids = array_values(array_flip(array_flip(array_column($user_coupon_list['list'], 'coupon_id'))));
- // dd($coupon_ids);
- $coupons = AddonsCoupon::find()
- ->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')
- ->where(['id' => $coupon_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- // dd($coupons);
- $good_relates = AddonsCouponGoodsRelate::find()->select('coupon_id,goods_id')->where(['coupon_id' => $coupon_ids])->asArray()->all();
- $relate_goodids = [];
- if (!empty($good_relates)) {
- foreach ($good_relates as $relate) {
- $relate_goodids[$relate['coupon_id']][] = $relate['goods_id'];
- }
- }
- foreach ($user_coupon_list['list'] as &$lv) {
- $coupon = $coupons[$lv['coupon_id']] ?? [];
- if(!empty($coupon['mch_id'])){
- $coupon['mch_id'] = explode(',',$coupon['mch_id']);
- }else{
- $coupon['mch_id'] = [];
- }
- if(!empty($coupon['store_id'])) {
- $coupon['store_id'] = explode(',',$coupon['store_id']);
- }else{
- $coupon['store_id'] = [];
- }
- $coupon = array_merge($coupon, $lv);
- $coupon_info = !empty($coupon) ? AddonsCoupon::parseCouponInfo($coupon, true) : [];
- // dd($coupon_info);
- $lv = array_merge($coupon, $coupon_info);
- $lv['relate_goodids'] = $relate_goodids[$lv['coupon_id']] ?? [];
- }
- }
- $config = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_COUPON, 'basic');
- $user_coupon_list['give_type'] = empty($config['give_type'])?'1':$config['give_type'];
- return $this->success('操作成功', $user_coupon_list);
- }
- /**
- * @name:
- * @msg: 领取优惠券
- * @param {integer} $type 类型类型,1:平台的优惠券,2:好友/商城私发 赠送的优惠券
- * @param {integer} $receive_type 领取场景,1 => 商城发放, 2 => 购买商品奖励, 3 => 商品详情处领取, 4 => 领券中心领取, 5 => 签到, 6 => 好友转赠, 7 => 云库存-提货到线下奖励, 8 => 升级礼包赠送, 9 => 九宫格中奖, 10 => 刮刮卡中奖
- * @param {integer} $coupon_id 优惠券id
- * @param {integer} $coupon_share_log_id 优惠券分享记录id
- * @return {*}
- */
- public function actionReceive()
- {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['type', 'receive_type', 'coupon_id'], 'required'],
- [['coupon_share_log_id'], 'safe'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $params['user_id'] = $this->user_id;
- $params['user'] = $this->user;
- $params['num'] = 1;//数量
- $params['mall_id'] = $this->mall_id;
- $params['receive_platform'] = $this->platform??'';
- if($params['type']==1){
- //领取平台优惠券 -- 商品详情页公开领取
- $coupon_user_relate = AddonsCouponUserRelate::setData($params);
- if($coupon_user_relate==false){
- return $this->error(AddonsCouponUserRelate::getStaticError());
- }
- $user_coupon_id = $coupon_user_relate['id']??0;
- } else {
- if ($params['receive_type'] == 6 && empty($params['coupon_share_log_id'])) {
- // 扫描后台生成的海报进行领取(没有coupon_share_log_id),商城私发的优惠券也是走公开领取的逻辑
- $coupon_user_relate = AddonsCouponUserRelate::setData($params);
- if ($coupon_user_relate === false) return $this->error(AddonsCouponUserRelate::getStaticError());
- $user_coupon_id = $coupon_user_relate['id']??0;
- } elseif ($params['receive_type'] == 6 && !empty($params['coupon_share_log_id'])) {
- // 领取转赠优惠券 -- 好友转赠
- $res = AddonsCouponUserRelate::receiveByGive($params);
- $user_coupon_id = $res === false ? 0 : $res->id;
- } else {
- return $this->error('参数错误');
- }
- }
- if ($res == false) return $this->error(AddonsCouponUserRelate::getStaticError());
- return $this->success('领取成功', ['user_coupon_id' => $user_coupon_id]);
- }
- /**
- * 转送优惠券--老版本的接口,已废弃
- * @return mixed|void
- * @throws \yii\db\Exception
- */
- /*
- public function actionTransfer()
- {
- $params = \Yii::$app->request->post();
- $user_id = $this->user_id;
- $res = Yii::$app->services->validate->validate($params, [
- [['user_coupon_id', 'receive_user_id'], 'required'],
- ['receive_user_id', function ($attribute) use ($user_id) {
- if ($user_id == $this->$attribute) $this->addError($attribute, '不能转送给自己');
- }]
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $where = [];
- $where[] = ['id' => $params['user_coupon_id']];
- $where[] = ['user_id' => $user_id];
- $where[] = ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]];
- $user_coupon_model = AddonsCouponUserRelate::getOne($where);
- if (empty($user_coupon_model)) return $this->error( '您没有该优惠券');
- if ($user_coupon_model->is_use) return $this->error( '您的优惠券已经使用了');
- $where = [];
- $where[] = ['id' => $user_coupon_model->coupon_id];
- $where[] = ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]];
- $res = AddonsCoupon::getOne($where);
- if (empty($res)) return $this->error( '该优惠券不存在');
- $params['end_time'] =$user_coupon_model->end_time;
- $res = AddonsCouponUserRelate::transfer($params);
- if ($res === false) return $this->error(AddonsCouponUserRelate::getStaticError());
- return $this->success('操作成功', []);
- }
- */
- /**
- * @name:
- * @msg: 转赠优惠券
- * @param {*}
- * @return {*}
- */
- public function actionCouponGive()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $coupon_id = \Yii::$app->request->post('coupon_id');
- $user_coupon_id = \Yii::$app->request->post('user_coupon_id');
- if (empty($coupon_id) || empty($user_coupon_id)) {
- return $this->error('参数错误');
- }
- // $coupon = AddonsCoupon::findOne(['mall_id' => $this->mall_id, 'id' => $coupon_id, 'coupon_status' => 2, 'status' => StatusEnum::ENABLED]);
- $coupon = AddonsCoupon::findOne(['mall_id' => $this->mall_id, 'id' => $coupon_id]);
- if (empty($coupon)) {
- return $this->error('优惠券不存在或者已经失效');
- }
- if (1 != $coupon->is_giving) {
- return $this->error('优惠券不可转赠');
- }
- $wheres = [
- 'mall_id' => $this->mall_id,
- 'id' => $user_coupon_id,
- 'user_id' => $this->user_id,
- 'is_force_failure' => 0,
- 'is_use' => 0,
- 'status' => StatusEnum::ENABLED,
- 'coupon_status' => AddonsCouponEnum::STATUS_RECEIVING
- ];
- $user_coupon = AddonsCouponUserRelate::find()
- ->where($wheres)
- ->andWhere(['or', ['is_giving' => 0], ['is_giving' => 1, 'giving_status' => [0, 1]]])
- ->one();
- if (empty($user_coupon)) {
- return $this->error('当前优惠券不可转赠');
- }
- $params = [
- 'mall_id' => $this->mall_id,
- 'coupon_id' => $coupon_id,
- 'user_coupon_id' => $user_coupon_id,
- 'giving_user_id' => $this->user_id,
- ];
- $res = AddonsCouponShareLog::setData($params);
- if (!$res) {
- return $this->error(AddonsCouponShareLog::getStaticError());
- } else {
- return $this->success('转赠成功', ['coupon_share_log_id' => $res->id]);
- }
- }
- /**
- * @name:
- * @msg: 获取优惠券可以使用的商品列表
- * @param {*}
- * @return {*}
- */
- public function actionCanUseCouponList()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $page = \Yii::$app->request->get('page') ?? 1;
- $limit = \Yii::$app->request->get('page_size') ?? $this->pageSize;
- // 排序字段, sales、price、created_at
- $sort_type = \Yii::$app->request->get('sort_type') ?? 'id';
- // 排序方式,升序/降序:asc、desc
- $order_by = \Yii::$app->request->get('order_by') ?? 'desc';
- $good_name = \Yii::$app->request->get('good_name');
- $coupon_id = \Yii::$app->request->get('coupon_id');
- if (!empty($sort_type)) {
- if (!in_array($sort_type, ['sales', 'price', 'created_at', 'id'])) {
- return $this->error('参数错误');
- }
- if ('sales' == $sort_type) $sort_type = 'sales_num';
- } else {
- $sort_type = 'id';
- }
- if (!empty($order_by)) {
- if (!in_array($order_by, ['asc', 'desc'])) {
- return $this->error('参数错误');
- }
- } else {
- $order_by = 'desc';
- }
- if (empty($coupon_id)) {
- return $this->error('参数错误: coupon_id');
- }
- $coupon_info = AddonsCoupon::findOne(['mall_id' => $this->mall_id, 'id' => $coupon_id, 'status' => 1, 'coupon_status' => 2]);
- if (empty($coupon_info)) {
- return $this->error('优惠券异常');
- }
- $where[] = ['mall_id' => $this->mall_id, 'is_on_sale' => 1, 'status' => 1];
- if (1 == $coupon_info->appoint_type) {
- // 指定分类使用
- $cates = $coupon_info->cateRelate;
- $cate_ids = array_column($cates, 'cate_id');
- // dd($cate_ids);
- $good_ids = GoodsCateRelate::find()->select('goods_id')->where(['cate_id' => $cate_ids])->asArray()->column();
- $where[] = ['id' => $good_ids];
- } elseif (2 == $coupon_info->appoint_type) {
- // 指定商品可用
- $gw_data = $coupon_info->goodsRelate;
- $good_ids = array_column($gw_data, 'goods_id');
- // dd($good_ids);
- $where[] = ['id' => $good_ids];
- } elseif (3 == $coupon_info->appoint_type) {
- // 全部商品可用,不做处理
- }elseif (4 == $coupon_info->appoint_type) {
- // 指定门店
- $where[] = ['id' => []];
- } elseif (5 == $coupon_info->appoint_type) {
- // 指定商户
- $where[] = ['mch_id' => $coupon_info['mch_id']];
- }else {
- return $this->error('参数错误:appoint_type');
- }
- if (!empty($good_name)) {
- $where[] = ['like', 'goods_name', $good_name];
- }
- // dd($where);
- $params = [
- 'select' => 'id as good_id,price,goods_name,cover_pic,original_price,mch_id',
- 'where' => $where,
- 'order' => $sort_type . ' ' . $order_by,
- 'page' => $page,
- 'limit' => $limit,
- ];
- // dd($params);
- $list = Goods::listPage($params);
- // $list = Goods::getStaticError();
- // dd($list);
- $list['coupon_info'] = AddonsCoupon::parseCouponInfo(ArrayHelper::toArray($coupon_info));
- return $this->success('操作成功', $list);
- }
- /**
- * @name:
- * @msg: 优惠券信息
- * @param {integer} $coupon_id 优惠券 id
- * @return {*}
- */
- public function actionCouponInfo()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $user_id = $this->user_id;
- $coupon_id = \Yii::$app->request->get('coupon_id');
- $coupon_share_log_id = \Yii::$app->request->get('coupon_share_log_id');
- if (empty($coupon_id)) {
- return $this->error('参数错误');
- }
- $coupon = AddonsCoupon::find()->where(['id' => $coupon_id, 'mall_id' => $this->mall_id])->asArray()->one();
- if (empty($coupon)) return $this->error('优惠券不存在');
- if (!empty($coupon_share_log_id)) {
- // 转赠优惠券
- $coupon_share = AddonsCouponShareLog::findOne(['id' => $coupon_share_log_id, 'mall_id' => $this->mall_id]);
- if (empty($coupon_share)) return $this->error('未找到相应的分享记录');
- if ($coupon_share->coupon_id != $coupon_id) return $this->error('数据异常');
- $user_id = $coupon_share->giving_user_id;
- $user_coupon_info = AddonsCouponUserRelate::find()
- ->select('start_time,end_time,coupon_status,is_use,is_giving,giving_status,user_id')
- ->where(['id' => $coupon_share['user_coupon_id'], 'mall_id' => $this->mall_id, 'user_id' => $user_id, 'is_giving' => 1])
- ->orderBy('id desc')
- ->limit(1)
- ->asArray()
- ->one();
- // 返回这张券的领取人id
- $coupon['receive_user_id'] = $coupon_share['receive_user_id'];
- // 返回领取者coupon_user_relate表的id
- $coupon['get_user_coupon_id'] = $coupon_share['get_user_coupon_id'];
- // 是否已失效
- $coupon['is_lapse'] = in_array($user_coupon_info['coupon_status'], [AddonsCouponEnum::STATUS_EXPIRED, AddonsCouponEnum::STATUS_FAILURE]) ||
- $user_coupon_info['is_use'] == 1 ||
- $user_coupon_info['giving_status'] == AddonsCouponEnum::GIVING_STATUS2;
-
- // 返回分享链接的这张券已经被领取,领取人的那张券有没有失效
- $coupon['is_lapse_by_latest_user_coupon'] = false;
- if ($coupon['get_user_coupon_id'] && $coupon_share['receive_user_id'] == $this->user_id) {
- $latest_user_coupon_info = AddonsCouponUserRelate::find()
- ->select('start_time,end_time,coupon_status,is_use,is_giving,giving_status,user_id')
- ->where(['id' => $coupon['get_user_coupon_id'], 'mall_id' => $this->mall_id])
- ->limit(1)
- ->asArray()
- ->one();
- // 是否已失效
- $coupon['is_lapse_by_latest_user_coupon'] = in_array($latest_user_coupon_info['coupon_status'], [AddonsCouponEnum::STATUS_EXPIRED, AddonsCouponEnum::STATUS_FAILURE]) ||
- $latest_user_coupon_info['is_use'] == 1 ||
- $latest_user_coupon_info['giving_status'] == AddonsCouponEnum::GIVING_STATUS2;
- }
- } else {
- // 非转赠优惠券
- $user_id = 0;
- $mall_log = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.logo');
- $mall_name = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.name');
- if (empty($mall_log)) {
- $mall_log = \Yii::$app->request->hostInfo . '/resources/img/mall/mall_logo_default.jpg';
- }
- if (empty($mall_name)) $mall_name = 'qimall商城';
- $user_coupon_info = AddonsCouponUserRelate::find()
- ->select('start_time,end_time')
- ->where(['coupon_id' => $coupon_id, 'mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'is_giving' => 0])
- ->orderBy('id desc')
- ->limit(1)
- ->asArray()
- ->one();
- }
- if (!empty($user_coupon_info)) {
- $coupon = array_merge($coupon, $user_coupon_info);
- $coupon = array_merge($coupon, AddonsCoupon::parseCouponInfo($coupon, true));
- } else {
- $coupon = array_merge($coupon, AddonsCoupon::parseCouponInfo($coupon));
- }
- $user = User::findOne(['id' => $user_id, 'mall_id' => $this->mall_id]);
- if (empty($user)) {
- $coupon_picture = $mall_log;
- $coupon_user_name = $mall_name;
- } else {
- $coupon_picture = $user->avatar_url ?: \Yii::$app->request->hostInfo . '/resources/img/mall/default_avatar_url.png';
- $coupon_user_name = ($user->nickname ?: $user->username) ?: '会员';
- }
- $coupon['coupon_picture'] = $coupon_picture;
- $coupon['give_user_name'] = $coupon_user_name;
- $couponLink = [];
- switch($coupon['appoint_type']){
- case AddonsCouponEnum::APPOINT_TYPE1: // 指定分类
- $couponLinkList = AddonsCouponCatRelate::find()->where(['coupon_id' => $coupon['id']])->select('cate_id')->column();
- if(count($couponLinkList) == 1){
- $cate_id = GoodsCate::find()->where(['id' => $couponLinkList[0]])->select('id')->column();
- $cateList = self::getAncestors($cate_id);
- foreach($cateList as $v){
- array_push($couponLink, $v['id']);
- }
- }
- break;
- case AddonsCouponEnum::APPOINT_TYPE2: // 指定商品
- $couponLink = AddonsCouponGoodsRelate::find()->where(['coupon_id' => $coupon['id']])->select('goods_id')->column();
- break;
- case AddonsCouponEnum::APPOINT_TYPE4: // 指定门店
- $couponLink = explode(',',$coupon['store_id']);
- break;
- case AddonsCouponEnum::APPOINT_TYPE5: // 指定商户
- $couponLink = explode(',',$coupon['mch_id']);
- break;
- }
- $coupon['couponLink'] = $couponLink;
- return $this->success('操作成功', ['coupon_info' => $coupon]);
- }
- public static function getAncestors($childId)
- {
- $result = [];
- $category = GoodsCate::findOne(['id' => $childId]);
- if ($category && $category->parent_id != 0) {
- // 如果不是顶级节点,则继续向上查找
- $result[] = $category;
- $result = array_merge(self::getAncestors($category->parent_id), $result);
- } else if ($category && $category->parent_id == 0) {
- // 如果是顶级节点,直接加入结果
- $result[] = $category;
- }
- return $result;
- }
- /**
- * @name:
- * @msg: 优惠券列表
- * @param {integer} $coupon_id 优惠券 id
- * @return {*}
- */
- public function actionGetCouponList(){
- $params['where'][] = ['=','mall_id',$this->mall_id];
- $params['where'][] = ['=','status',StatusEnum::ENABLED];
- $params['where'][] = ['=','is_public_receive',StatusEnum::ENABLED];
- $params['where'][] = ['=','coupon_status',AddonsCouponEnum::STATUS_RECEIVING];
- $params['where'][] = ['store_id'=>''];
- $params['with'] = ['statistics'];
- $params['order'] = 'id desc';
- $limit = \Yii::$app->request->post('limit');
- $params['limit'] = $limit;
- $list = AddonsCoupon::listPage($params,false,true);
- $config = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_COUPON, 'basic');
- $list['images'] = json_decode($config['images'],true);
- return $this->success('操作成功', $list);
- }
- //开屏优惠券列表
- public function actionOpenScreenCouponList()
- {
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['coupon_status' => 2];
- $where[] = ['is_open_screen' => 1];
- $where[] = ['status' => StatusEnum::ENABLED];
- $where[] = [ 'store_id'=>''];
- $params['order'] = 'id desc';
- $params['where'] = $where;
- $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';
- $platform_coupon_list = AddonsCoupon::lists($params);
- $platform_coupon_list = $this->formatCouponList($platform_coupon_list);
- $list = $platform_coupon_list;
- return $this->success('success', $list);
- }
- protected function formatCouponList($coupon_list)
- {
- $coupon_ids = array_column($coupon_list, 'id');
- $relate_list = AddonsCouponUserRelate::lists([
- 'where' => [
- ['coupon_id' => $coupon_ids],
- ['user_id' => $this->user_id]
- ],
- 'index' => 'coupon_id'
- ]);
- $list = [];
- foreach ($coupon_list as &$item) {
- $item['coupon_id'] = $item['id'];
- $item['is_receive'] = $item['is_use'] = $item['user_coupon_id'] = 0;
- if (isset($relate_list[$item['id']])) {
- $item['is_receive'] = 1;
- $item['is_use'] = $relate_list[$item['id']]['is_use'];
- $item['user_coupon_id'] = $relate_list[$item['id']]['id'];
- }else{
- $list[]=$item;
- }
- }
- return $list;
- }
- /**
- * 获取用户信息
- * @return array|mixed
- */
- public function actionGetUser()
- {
- $params = \Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['keyword'], 'required', 'message' => 'keyword必传'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['status' => StatusEnum::ENABLED];
- $where[] = [
- 'or',
- ['mobile' => $params['keyword']],
- ['id' => $params['keyword']],
- ];
- $user = User::getOne($where, true, [], false);
- if (empty($user)) return $this->error('暂无此用户');
- if($user['id']==$this->user_id) return $this->error('不能赠送给自己');
- return $this->success(ApiCode::CODE_SUCCESS_TITLE, $user);
- }
- public function actionGiveCoupon(){
- try {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['user_coupon_id','receive_user_id','coupon_id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['user_id'] = $this->user_id;
- $params['num'] = 1;//数量
- $params['mall_id'] = $this->mall_id;
- $params['receive_platform'] = $this->platform??'';
- AddonsCouponUserRelate::giveCouponBy2($params);
- return $this->success('success',[]);
- }catch (\Exception $e){
- \Yii::error($e->getMessage().$e->getFile().$e->getLine());
- return $this->error($e->getMessage());
- }
- }
- }
|