| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643 |
- <?php
- namespace addons\Store\client\controllers;
- use addons\Coupon\common\enums\AddonsCouponEnum;
- use addons\Coupon\common\models\AddonsCoupon;
- use addons\Store\common\enums\CouponEnum;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreCoupon;
- use addons\Store\common\models\StoreCouponGoodStatistics;
- use addons\Store\common\models\StoreCouponMemberRelate;
- use addons\Store\common\models\StoreCouponReward;
- use addons\Store\common\models\StoreCouponStatistics;
- use addons\Store\common\models\StoreCouponUseLog;
- use addons\Store\common\models\StoreCouponUserRelate;
- use addons\Store\common\models\StoreGoods;
- use addons\Store\common\models\StoreGoodsCate;
- use addons\Store\common\services\StoreService;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\helpers\ApiCode;
- use common\helpers\ArrayHelper;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- use common\models\user\UserLevel;
- use yii\db\Expression;
- class CouponController extends BaseController
- {
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- $where[] = ['mall_id' => $this->mall_id,'store_id'=>$this->store_id, 'status' => StatusEnum::ENABLED];
- if (!empty($params['name'])) $where[] = ['like', 'name', $params['name']];
- if (!empty($params['status'])) $where[] = ['coupon_status' => $params['status']];
- if (!empty($params['type'])) $where[] = ['type' => $params['type']];
- $params['select'] = 'id,mall_id,store_id,name,type,coupon_status,discount,discount_limit,min_price,sub_price,
- total_count,expire_type,expire_day,begin_at,end_at,appoint_type,rule,is_remind,remind_days,effect_days,
- is_receive_limit,receive_count,is_member,is_original_use,is_public_receive,is_giving,coupon_status,
- is_giving_reward,is_consume_reward,qrcode_url,poster_url,created_at';
- $params['where'] = $where;
- $params['order'] = 'id desc';
- $list = StoreCoupon::listPage($params);
- if (!empty($list['list'])) {
- $h5_domain = rtrim(\Yii::$app->services->setting->get('system.h5_url'),'/') .'/#/';
- $mall_logo = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.logo');
- if (empty($mall_logo)) \Yii::$app->request->hostInfo . '/resources/img/mall/mall_logo_default.jpg';
- $mall_logo = $this->imgBase64Encode($mall_logo);
- $mall_name = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.name');
- if (empty($mall_name)) $mall_name = '七件事商城';
- $coupon_ids = array_column($list['list'], 'id');
- $user_coupons = StoreCouponStatistics::lists(['where' => [['coupon_id' => $coupon_ids],['store_id'=>$this->store_id,]], 'select' => 'coupon_id,received_number,used_number', 'index' => 'coupon_id']);
- foreach ($list['list'] as &$lv) {
- $lv['received_number'] = $lv['used_number'] = 0;
- if (isset($user_coupons[$lv['id']])) {
- $lv['received_number'] = $user_coupons[$lv['id']]['received_number'];
- $lv['used_number'] = $user_coupons[$lv['id']]['used_number'];
- }
- $lv['page_link'] = $h5_domain . 'plugins2/Store/coupon/type?sign=' . $this->mall['mall_sign'] . '&coupon_id=' . $lv['id'] . '&coupon_share_log_id=0&from_type=2';
- $lv['poster_filename'] = substr(md5("{'poster'_{$lv['id']}_{$this->mall_id}"), 0, 16);
- $lv['qrcode_filename'] = substr(md5("{'qrcode'_{$lv['id']}_{$this->mall_id}"), 0, 16);
- $lv = array_merge($lv, StoreCoupon::parseCouponInfo($lv));
- }
- $list['mall_info']['mall_logo'] = $mall_logo;
- $list['mall_info']['mall_name'] = $mall_name;
- }
- return $this->success('操作成功', $list);
- }
- }
- return $this->render($this->action->id);
- }
- public function actionEdit()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $coupon_id = \Yii::$app->request->get('coupon_id');
- $is_copy = \Yii::$app->request->get('is_copy') ?? false;
- $coupon_info = [];
- $member_level_limits = [];
- $good_lists = [];
- $goods_id_list = [];
- $giving_rewards = [];
- $consume_rewards = [];
- $checked_categorys = [];
- $store = [];
- if (!empty($coupon_id)) {
- $coupon_info = StoreCoupon::find()
- ->select('id,mall_id,store_id,name,type,discount,discount_limit,min_price,sub_price,total_count,sort,expire_type,expire_day,begin_at,end_at,appoint_type,rule,is_remind,remind_days,effect_days,is_receive_limit,receive_count,is_member,is_original_use,
- is_public_receive,is_giving,coupon_status,is_giving_reward,is_consume_reward,
- created_at,qrcode_url,poster_url,is_open_screen,first_frame,video_url,watch_times')
- ->where(['id' => $coupon_id, 'mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED], 'coupon_status' => [1, 2, 3, 4]])
- ->one();
- if (empty($coupon_info)) return $this->error('优惠券不存在或已失效');
- if (($coupon_info->coupon_status != 1 || $coupon_info->coupon_status != 2) && $is_copy) return $this->error('该优惠券不能复制');
- if ($is_copy) $coupon_info->id = 0;
- $member_level_limits = StoreCouponMemberRelate::lists(['where' => [['mall_id' => $this->mall_id,], ['coupon_id' => $coupon_id]], 'select' => 'member_level']);
- if(!empty($member_level_limits)) $member_level_limits = array_values(array_flip(array_flip(array_column($member_level_limits, 'member_level'))));
- $good_lists =!empty( $coupon_info->goods)? $coupon_info->goods:[];
- $coupon_rewards = StoreCouponReward::lists(['where' => [['mall_id' => $this->mall_id], ['coupon_id' => $coupon_id]], 'select' => 'reward_type,balance,score,red_packet']);
- if (!empty($coupon_rewards)) {
- foreach ($coupon_rewards as $rk => $rv) {
- switch ($rv['reward_type']) {
- case 1:
- $giving_rewards = ['balance' => $rv['balance'], 'score' => $rv['score'], 'red_packet' => $rv['red_packet']];
- break;
- case 2:
- $consume_rewards = ['balance' => $rv['balance'], 'score' => $rv['score'], 'red_packet' => $rv['red_packet'],];
- break;
- }
- }
- }
- // 优惠券已经绑定的商品分类
- $checked_categorys = $coupon_info->cateRelate??[];
- if (!empty($checked_categorys)) {
- $cate_ids = array_column($checked_categorys, 'cate_id');
- foreach ($cate_ids as $cate_id) {
- $cateids[] = [$cate_id];
- }
- $checked_categorys = $cateids;
- } else {
- $checked_categorys = [];
- }
- }
- $member_levels = $member_levels = UserLevel::lists(['where' => [['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], 'select' => 'id,level,name']);
- $conditions = [['is_show' => 1, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id, 'store_id' => $this->store_id]];
- $good_category = StoreGoodsCate::getCates($conditions, [], true, 'id,name,pic,parent_id');
- $good_category = StoreGoodsCate::getTreeCate($good_category);
- $coupon_info = ArrayHelper::toArray($coupon_info);
- if (!empty($good_lists)) {
- $good_lists = ArrayHelper::toArray($good_lists);
- foreach ($good_lists as &$gv) {
- $gv['goods_id'] = $gv['id'];
- }
- $goods_id_list = array_column($good_lists, 'id');
- }
- if(!isset($coupon_info['is_open_screen'])) $coupon_info['is_open_screen'] = 0;
- return $this->success('操作成功', compact('coupon_info', 'member_levels',
- 'good_category', 'member_level_limits',
- 'good_lists', 'goods_id_list',
- 'giving_rewards', 'consume_rewards', 'checked_categorys', 'store'));
- } else {
- try {
- $post = \Yii::$app->request->post();
- if (empty($post['coupon_info']['id'])) {
- $scenarios = 'create';
- $post['coupon_info']['mall_id'] = $this->mall_id;
- $post['coupon_info']['store_id'] = $this->store_id;
- } else {
- $scenarios = 'update';
- }
- $res = StoreCoupon::setData($post, $scenarios);
- if (!$res) {
- return $this->error(StoreCoupon::getStaticError());
- }
- return $this->success('添加优惠券成功');
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- }
- } else {
- return $this->render($this->action->id);
- }
- }
-
- public function actionCouponStatistics(){
- $params = \Yii::$app->request->get();
- $coupon_id = $params['coupon_id'];
- if (empty($params['coupon_id'])) return $this->error('参数错误');
- $user_coupons = StoreCouponStatistics::find()
- ->select('total_payed_amount,total_discount_amount,use_probability,receive_user_number,
- use_order_number,total_use_coupon_payed_amount,use_user_number')
- ->where(['mall_id' => $this->mall_id, 'store_id'=>$this->store_id,'coupon_id' => $coupon_id])
- ->asArray()->one();
- if (!empty($user_coupons)) {
- foreach ($user_coupons as $uk => $uv) {
- $user_coupons[$uk] = bcmul($uv,1,2);
- }
- $user_coupons['use_probability'] = $user_coupons['use_probability'] * 100 . '%';
- }
- $coupon_info = StoreCoupon::getOne([['mall_id'=>$this->mall_id],['store_id'=>$this->store_id],'id'=>$coupon_id],true);
- $coupon_info = array_merge($coupon_info, StoreCoupon::parseCouponInfo($coupon_info));
- $where[] = ['mall_id' => $this->mall_id, 'coupon_id' => $coupon_id];
- $params = [
- 'select' => 'coupon_id,good_id,payed_number,buyed_user_number',
- 'where' => $where,
- ];
- $good_statistics = StoreCouponGoodStatistics::lists($params);
- if (!empty($good_statistics)) {
- $good_ids = array_column($good_statistics, 'good_id');
- $goods = StoreGoods::lists(['where'=>[['id'=>$good_ids]],'select'=>'id,goods_name,cover_pic','index'=>'id']);
- foreach ($good_statistics as &$lv) {
- $lv['goods_name'] = $lv['cover_pic'] = '';
- if(isset($goods[$lv['good_id']])){
- $lv['goods_name'] = $goods[$lv['good_id']]['goods_name'];
- $lv['cover_pic'] = $goods[$lv['good_id']]['cover_pic'];
- }
- }
- }
- return $this->success('操作成功', compact('user_coupons', 'good_statistics', 'coupon_info'));
- }
- /**
- * 删除优惠券
- * @return mixed|void
- * @throws \yii\db\Exception
- */
- public function actionDel()
- {
- $params = \Yii::$app->request->post();
- $res = StoreCoupon::updateAll(['status' => StatusEnum::DELETE], ['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'id' => $params['id']]);
- if ($res === false) return $this->error(StoreCoupon::getStaticError());
- return $this->success('操作成功');
- }
- public function actionUserCoupon()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- $where = [];
- $where[] = ['mall_id'=>$this->mall_id];
- $where[] =new Expression("FIND_IN_SET({$this->store_id},store_id)");
- if (!empty($params['user_id'])) $where[] = ['user_id'=>$params['user_id']];
- if (!empty($params['from_type'])) $where[] = ['from_type'=>$params['from_type']];
- if (!empty($params['keyword'])) {
- if (!empty($params['from_type'])) {
- switch ($params['from_type']){
- case 1:
- $store_coupon_list = AddonsCoupon::lists(['where'=>[['mall_id'=>$this->mall_id],['store_id'=>$this->store_id],['like','name',trim($params['keyword'])]],'select'=>'id']);
- $coupon_ids = array_column($store_coupon_list,'id');
- $where[] = ['coupon_id'=>$coupon_ids];
- break;
- case 2:
- $store_coupon_list = StoreCoupon::lists(['where'=>[['mall_id'=>$this->mall_id],['store_id'=>$this->store_id],['like','name',trim($params['keyword'])]],'select'=>'id']);
- $coupon_ids = array_column($store_coupon_list,'id');
- $where[] = ['coupon_id'=>$coupon_ids];
- break;
- }
- }
- }
- $params['where'] = $where;
- $params['order'] = 'id desc';
- $list = StoreCouponUserRelate::listPage($params);
- if(!empty($list['list'])){
- $coupon_ids = $store_coupon_ids = [];
- foreach ($list['list'] as $val){
- if($val['from_type']==1){
- $coupon_ids = array_column($list['list'],'coupon_id');
- }else{
- $store_coupon_ids = array_column($list['list'],'coupon_id');
- }
- }
- $user_id_arr = array_column($list['list'], 'user_id');
- $user_coupon_ids = array_column($list['list'], 'id');
- $user_list = User::lists(['where'=>[['id' => $user_id_arr],['mall_id'=>$this->mall_id]],'select'=>'id,avatar_url,nickname','index'=>'id']);
- $store_coupon_list = StoreCoupon::lists(['where'=>[['mall_id'=>$this->mall_id],['store_id'=>$this->store_id],['id'=>$store_coupon_ids]],'index'=>'id']);
- $coupon_list = AddonsCoupon::lists(['where'=>[
- ['mall_id'=>$this->mall_id],
- new Expression("FIND_IN_SET({$this->store_id},store_id)"),
- ['id'=>$coupon_ids]
- ],'index'=>'id']);
- $user_coupon_use_logs = StoreCouponUseLog::lists(['where'=>[['mall_id' => $this->mall_id],['status' => StatusEnum::ENABLED],['user_coupon_id' => $user_coupon_ids]],'select'=>'user_coupon_id,created_at','index'=>'user_coupon_id']);
- foreach ($list['list'] as &$item){
- $item['name']=$item['rule']='';
- $item['type'] =$item['discount'] = $item['min_price']= $item['begin_at']= $item['end_at']= $item['appoint_type']= $item['is_giving']= $item['coupon_status']
- =$item['sub_price']=$item['sort']=$item['expire_type']=$item['expire_day']=$item['is_member']=$item['discount_limit']=$item['is_original_use']=0;
- if($item['from_type']==1){
- if(isset($coupon_list[$item['coupon_id']])){
- $coupon = $coupon_list[$item['coupon_id']];
- $item = array_merge($item, AddonsCoupon::parseCouponInfo($coupon, true));
- }
- }else{
- if(isset($store_coupon_list[$item['coupon_id']])){
- $coupon = $store_coupon_list[$item['coupon_id']];
- $item = array_merge($item, StoreCoupon::parseCouponInfo($coupon, true));
- }
- }
- $item['nickname'] = $item['avatar_url'] = '';
- if(isset($user_list[$item['user_id']])){
- $item['nickname'] = $user_list[$item['user_id']]['nickname'];
- $item['avatar_url'] = $user_list[$item['user_id']]['avatar_url'];
- }
- $item['used_time'] = $user_coupon_use_logs[$item['id']]['created_at'] ?? 0;
- if($item['expire_type']==2){
- $item['is_failure'] = $item['end_at']<time()?"0":"1";
- }else{
- $item['is_failure'] = $item['end_time']<time()?"0":"1";
- }
- }
- }
- return $this->success('操作成功', $list);
- }
- }
- return $this->render($this->action->id);
- }
- public function actionSelectCoupon(){
- $params = \Yii::$app->request->post();
- $where = [];
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['store_id' => $this->store_id];
- $where[] = ['status' => [StatusEnum::ENABLED]];
- $where[] = [
- 'or',
- ['=', 'total_count', -1],
- ['>', 'total_count', 'receive'],
- ];
- if (!empty($params['keyword'])) $where[] = ['like', 'name', $params['keyword']];
- $params['where'] = $where;
- $params['order'] = 'sort asc,id desc';
- $params['select'] = 'id,name,type,discount,min_price,sub_price,expire_type,expire_day,begin_at,end_at,status,total_count';
- $params['limit'] = 30;
- $store_coupon_list = StoreCoupon::lists($params);
- //判断是否过期
- foreach ($store_coupon_list as $k => $v) {
- $store_coupon_list[$k]['from_type'] = 2;
- $store_coupon_list[$k]['type_text'] = '门店优惠券';
- $v['end_at'] < time() && $v['expire_type'] == 2 ? $store_coupon_list[$k]['is_failure'] = 1 : $store_coupon_list[$k]['is_failure'] = 0;
- }
- $list = $store_coupon_list;
- $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_COUPON);
- if($is_install){
- $params = \Yii::$app->request->post();
- $where = [];
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['store_id' => $this->store_id];
- $where[] = ['status' => [StatusEnum::ENABLED]];
- $where[] = [
- 'or',
- ['=', 'total_count', -1],
- ['>', 'total_count', 'receive'],
- ];
- if (!empty($params['keyword'])) $where[] = ['like', 'name', $params['keyword']];
- $params['where'] = $where;
- $params['order'] = 'sort asc,id desc';
- $params['select'] = 'id,name,type,discount,min_price,sub_price,expire_type,expire_day,begin_at,end_at,status,total_count';
- $params['limit'] = 30;
- $coupon_list = AddonsCoupon::lists($params);
- //判断是否过期
- foreach ($coupon_list as $k => $v) {
- $coupon_list[$k]['from_type'] = 1;
- $coupon_list[$k]['type_text'] = '平台优惠券';
- $v['end_at'] < time() && $v['expire_type'] == 2 ? $coupon_list[$k]['is_failure'] = 1 : $coupon_list[$k]['is_failure'] = 0;
- }
- $list = array_merge($list,$coupon_list);
- }
- return $this->success('操作成功', $list);
- }
- public function actionSelectCouponCompent(){
- $params = \Yii::$app->request->post();
- $where = [];
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['store_id' => $this->store_id];
- $where[] = ['status' => [StatusEnum::ENABLED]];
- $where[] = [
- 'or',
- ['=', 'total_count', -1],
- ['>', 'total_count', 'receive'],
- ];
- if (!empty($params['keyword'])) $where[] = ['like', 'name', $params['keyword']];
- $params['where'] = $where;
- $params['order'] = 'sort asc,id desc';
- $params['select'] = 'id,name,type,discount,min_price,sub_price,expire_type,expire_day,begin_at,end_at,status,total_count';
- $store_coupon_list = StoreCoupon::lists($params);
- //判断是否过期
- foreach ($store_coupon_list as $k => $v) {
- $store_coupon_list[$k]['from_type'] = 2;
- $store_coupon_list[$k]['type_text'] = '门店优惠券';
- $v['end_at'] < time() && $v['expire_type'] == 2 ? $store_coupon_list[$k]['is_failure'] = 1 : $store_coupon_list[$k]['is_failure'] = 0;
- }
- $list = $store_coupon_list;
- $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_COUPON);
- if($is_install){
- $params = \Yii::$app->request->post();
- $where = [];
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['store_id' => $this->store_id];
- $where[] = ['status' => [StatusEnum::ENABLED]];
- $where[] = [
- 'or',
- ['=', 'total_count', -1],
- ['>', 'total_count', 'receive'],
- ];
- if (!empty($params['keyword'])) $where[] = ['like', 'name', $params['keyword']];
- $params['where'] = $where;
- $params['order'] = 'sort asc,id desc';
- $params['select'] = 'id,name,type,discount,min_price,sub_price,expire_type,expire_day,begin_at,end_at,status,total_count';
- $coupon_list = AddonsCoupon::lists($params);
- //判断是否过期
- foreach ($coupon_list as $k => $v) {
- $coupon_list[$k]['from_type'] = 1;
- $coupon_list[$k]['type_text'] = '平台优惠券';
- $v['end_at'] < time() && $v['expire_type'] == 2 ? $coupon_list[$k]['is_failure'] = 1 : $coupon_list[$k]['is_failure'] = 0;
- }
- $list = array_merge($list,$coupon_list);
- }
- return $this->success('操作成功', $list);
- }
- //不用验证权限
- public function actionSearchGoods()
- {
- $params = \Yii::$app->request->get();
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['store_id' => $this->store_id];
- $where[] = ['status' => StatusEnum::ENABLED];
- $where[] = ['check_status' => 1];
- $where[] = ['is_on_sale' => 1];
- if (!empty($params['keyword'])) $where[] = ['like', 'goods_name', $params['keyword']];
- $params['select'] = 'id as goods_id,goods_name,cover_pic,sort,goods_type';
- $params['where'] = $where;
- $params['order'] = 'sort asc,id desc';
- $list = StoreGoods::listPage($params);
- return $this->success('操作成功', $list);
- }
- /**
- * 图片base64编码
- * @param string $img
- * @param bool $imgHtmlCode
- * author 江南极客
- * @return string
- */
- public function imgBase64Encode($img = '', $imgHtmlCode = true)
- {
- //如果是本地文件
- if (strpos($img, 'http') === false && !file_exists($img)) return $img;
- //获取文件内容
- $file_content = file_get_contents($img);
- if ($file_content === false) return $img;
- $imageInfo = getimagesize($img);
- $prefiex = '';
- if ($imgHtmlCode) $prefiex = 'data:' . $imageInfo['mime'] . ';base64,';
- $base64 = $prefiex . chunk_split(base64_encode($file_content));
- return $base64;
- }
- public function actionUserCouponDetail()
- {
- if (!\Yii::$app->request->isAjax) {
- return $this->render('/coupon/detail-list');
- }
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $coupon_id = \Yii::$app->request->get('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];
- }
- $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',
- 'where' => $where,
- 'page' => $page,
- 'limit' => $limit,
- 'order' => 'created_at desc',
- ];
- $list =StoreCouponUserRelate::listPage($params);
- if (!empty($list['list'])) {
- $user_coupon_ids = array_values(array_unique(array_column($list['list'], 'id')));
- $coupon_used_logs = StoreCouponUseLog::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 = StoreCoupon::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 = StoreCoupon::find()
- ->select('id,type,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) ? StoreCoupon::parseCouponInfo($coupon_data) : [];
- $coupon_info = array_merge($coupon_data, $coupon_info);
- return $this->success('操作成功', ['list' => $list, 'coupon_info' => $coupon_info,'send_from_map'=>CouponEnum::getSendFromMap()]);
- }
- 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('参数错误');
- }
- $user_coupon = StoreCouponUserRelate::find()
- ->where(['mall_id' => $this->mall_id,'store_id' => $this->store_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('操作失败');
- }
- }
- 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 = StoreCouponUserRelate::findAll(['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'coupon_id' => $coupon_id, 'is_use' => 0, 'is_giving' => 0, 'status' => 1, 'is_force_failure' => 0]);
- if (!empty($user_coupons)) {
- $res = StoreCouponUserRelate::updateAll(
- ['is_force_failure' => 1, 'updated_at' => time(), 'coupon_status' => 4],
- ['mall_id' => $this->mall_id, 'store_id' => $this->store_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('操作成功');
- }
- public function actionSetting()
- {
- $retuest = \Yii::$app->request;
- if ($retuest->isAjax) {
- if ($retuest->isPost) {
- $params = \Yii::$app->request->post();
- $res = \Yii::$app->services->validate->validate($params,[
- [['is_show_coupon'], 'safe'],
- ]);
- if($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $service = new StoreService(['mall_id' => $this->mall_id, 'store_id' => $this->store_id]);
- return $service->updateIsShowCoupon($params)
- ? $this->success('成功')
- : $this->error($service->getError());
- } else {
- // 获取配置
- $store = Store::findOne(['id'=>$this->store_id]);
- $config = [
- 'is_show_coupon'=>$store['is_show_coupon']??1,
- ];
- $this->success('保存成功', $config);
- }
- }
- return $this->render($this->action->id);
- }
- }
|