| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\models\Cat;
- use app\models\Coupon;
- use app\models\CouponAutoSend;
- use app\models\Goods;
- use app\models\Mch;
- use app\models\MchCat;
- use app\models\SaasUser;
- use app\models\User;
- use app\models\UserCoupon;
- use yii\base\BaseObject;
- use yii\base\Model;
- use app\modules\admin\models\mochat\MochatForm;
- class CouponForm extends Model
- {
- public $store_id;
- public $user_id;
- public $status;
- public $id;
- public $coupon_id;
- public $page = 1;
- public function rules()
- {
- return [
- [['status', 'id', 'coupon_id', 'user_id', 'page'], 'integer'],
- ];
- }
- /**
- * 优惠券列表
- * @return array
- */
- public function search()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- $query = UserCoupon::find()->alias('uc')->leftJoin(['c' => Coupon::tableName()], 'uc.coupon_id=c.id')
- ->leftJoin(['cas' => CouponAutoSend::tableName()], 'cas.id=uc.coupon_auto_send_id')
- ->where(['uc.user_id' => $this->user_id]);
- if ($this->status == 0) {
- $query->andWhere([
- 'uc.is_delete' => 0,
- ]);
- }
- if ($this->status == 1) {
- $query->andWhere([
- 'uc.is_delete' => 0,
- 'uc.is_use' => 0,
- 'uc.is_expire' => 0,
- ]);
- }
- if ($this->status == 2) {
- $query->andWhere([
- 'uc.is_delete' => 0,
- 'uc.is_use' => 1,
- 'uc.is_expire' => 0,
- ]);
- }
- if ($this->status == 3) {
- $query->andWhere([
- 'uc.is_delete' => 0,
- 'uc.is_expire' => 1,
- ]);
- }
- if ($this->status == 0) {
- $query->orderBy('uc.is_expire ASC, uc.is_use ASC, uc.created_at DESC');
- } else {
- $query->orderBy('uc.created_at DESC');
- }
- $page = $this->page;
- $limit = 10;
- $query->offset(($page - 1) * $limit);
- $query->limit($limit);
- $list = $query
- ->select('c.id coupon_id, uc.id user_coupon_id,c.sub_price,c.mch_id,c.min_price,uc.begin_time,uc.end_time,uc.is_use,
- uc.is_expire,cas.event,uc.type,c.appoint_type,c.cat_id_list,c.goods_id_list,c.name,c.rule,c.desc,c.discount,c.discount_max_price,c.discount_type,c.expire_type,c.expire_day,c.is_give,uc.give_user_id')->asArray()->all();
- $events = [
- 0 => '平台发放',
- 1 => '分享红包',
- 2 => '购物返券',
- 3 => '领券中心',
- 4 => '积分兑换',
- ];
- foreach ($list as $i => $item) {
- $list[$i]['can_send'] = $item['is_give'];
- if ($item['is_use'] || $item['is_expire']) {
- $list[$i]['can_send'] = 0;
- }
- $list[$i]['status'] = 0;
- if ($item['is_use']) {
- $list[$i]['status'] = 1;
- }
- if ($item['is_expire']) {
- $list[$i]['status'] = 2;
- }
- $list[$i]['begintime'] = date('Y.m.d', $item['begin_time']);
- $list[$i]['endtime'] = date('Y.m.d', $item['end_time']);
- $list[$i]['min_price_desc'] = $item['min_price'] == 0 ? '无门槛' : '满' . $item['min_price'] . '元可用';
- if (!$item['event']) {
- if ($item['type'] == 2) {
- $list[$i]['event'] = $item['event'] = 3;
- } elseif ($item['type'] == 0) {
- $list[$i]['event'] = $item['event'] = 0;
- } else {
- $list[$i]['event'] = $item['event'] = 4;
- }
- }
- $list[$i]['event_desc'] = $events[$item['event']];
- if ($item['appoint_type'] == 1 && $item['cat_id_list'] !== 'null' && $item['cat_id_list'] !== '[]') {
- $list[$i]['cat'] = Cat::find()->select('id, name')->where(['store_id' => $this->store_id, 'is_delete' => 0,
- 'id' => json_decode($item['cat_id_list'])])->asArray()->all();
- $cat_list = [];
- foreach ($list[$i]['cat'] as $value) {
- $cat_list[] = $value['name'];
- }
- $list[$i]['content'] = "适用范围:仅限分类:".implode('、', $cat_list)."使用";
- $list[$i]['goods'] = [];
- } elseif ($item['appoint_type'] == 2 && $item['goods_id_list'] !== 'null' && $item['goods_id_list'] !== null) {
- $list[$i]['goods'] = Goods::find()->select('id, name, price, cover_pic')->where(['store_id' => $this->store_id, 'is_delete' => 0, 'id' => json_decode($item['goods_id_list'])])->asArray()->all();
- $list[$i]['content'] = "适用范围:指定商品使用";
- $list[$i]['cat'] = [];
- } else {
- $list[$i]['goods'] = [];
- $list[$i]['cat'] = [];
- }
- $list[$i]['give_user_info'] = [];
- if ($item['give_user_id']) {
- $list[$i]['give_user_info'] = User::find()->where(['u.id' => $item['give_user_id']])->alias('u')->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')->select('su.avatar,su.name')->asArray()->one();
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'list' => $list,
- ],
- ];
- }
- /**
- * 优惠券列表
- * @return array
- */
- public function saasSearch()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- $query = UserCoupon::find()->alias('uc')->leftJoin(['c' => Coupon::tableName()], 'uc.coupon_id=c.id')
- ->leftJoin(['cas' => CouponAutoSend::tableName()], 'cas.id=uc.coupon_auto_send_id')
- ->where(['uc.user_id' => $this->user_id]);
- if ($this->status == 0) {
- $query->andWhere([
- 'uc.is_delete' => 0,
- 'uc.is_use' => 0,
- 'uc.is_expire' => 0,
- ]);
- }
- if ($this->status == 1) {
- $query->andWhere([
- 'uc.is_delete' => 0,
- 'uc.is_use' => 1,
- 'uc.is_expire' => 0,
- ]);
- }
- if ($this->status == 2) {
- $query->andWhere([
- 'uc.is_delete' => 0,
- 'uc.is_use' => 0,
- 'uc.is_expire' => 1,
- ]);
- }
- if ($this->status == 3) {
- $query->andWhere([
- 'uc.is_delete' => 0,
- 'uc.type' => 4, // 转赠
- ]);
- }
- $page = $this->page;
- $limit = 10;
- $query->offset(($page - 1) * $limit);
- $query->limit($limit);
- $list = $query->orderBy('uc.created_at DESC')
- ->select('c.*, uc.id user_coupon_id,uc.begin_time,uc.end_time,uc.is_use,uc.is_expire,cas.event,uc.type')->asArray()->all();
- $events = [
- 0 => '平台发放',
- 1 => '分享红包',
- 2 => '购物返券',
- 3 => '领券中心',
- 4 => '积分兑换',
- 5 => '福利中心',
- 6 => '转赠'
- ];
- foreach ($list as $i => $item) {
- // TODO: store 优惠券所在店铺信息
- $list[$i]['store'] = [
- 'store_id' => $item['store_id'],
- 'name' => 'ceshi_name',
- 'logo' => 'https://quanqudao.we10.cn/web/v1/statics/shop/img/shop-logo.png'
- ];
- $list[$i]['status'] = 0;
- if ($item['is_use']) {
- $list[$i]['status'] = 1;
- }
- if ($item['is_expire']) {
- $list[$i]['status'] = 2;
- }
- $list[$i]['content'] = "适用范围:全场通用";
- if ($item['appoint_type'] == 1 && $item['cat_id_list'] !== 'null') {
- $list[$i]['cat'] = Cat::find()->select('id, name')->where(['is_delete' => 0, 'id' => json_decode($item['cat_id_list'])])->asArray()->all();
- $cat_list = [];
- foreach ($list[$i]['cat'] as $value) {
- $cat_list[] = $value['name'];
- }
- $list[$i]['content'] = "适用范围:仅限分类:".implode('、', $cat_list)."使用";
- $list[$i]['goods'] = [];
- } elseif ($item['appoint_type'] == 2 && $item['goods_id_list'] !== 'null') {
- $list[$i]['goods'] = Goods::find()->select('id')->where(['store_id' => $item['store_id'], 'is_delete' => 0, 'id' => json_decode($item['goods_id_list'])])->asArray()->all();
- $list[$i]['cat'] = [];
- $list[$i]['content'] = "指定商品使用 点击查看指定商品";
- } else {
- $list[$i]['goods'] = [];
- $list[$i]['cat'] = [];
- }
- $list[$i]['min_price_desc'] = $item['min_price'] == 0 ? '无门槛' : '满' . $item['min_price'] . '元可用';
- $list[$i]['begin_time'] = date('Y.m.d H:i', $item['begin_time']);
- $list[$i]['end_time'] = date('Y.m.d H:i', $item['end_time']);
- if (!$item['event']) {
- if ($item['type'] == 2) {
- $list[$i]['event'] = $item['event'] = 3;
- } elseif ($item['type'] == 0) {
- $list[$i]['event'] = $item['event'] = 0;
- } elseif ($item['type'] == 3) {
- $list[$i]['event'] = $item['event'] = 5;
- } elseif ($item['type'] == 4) {
- $list[$i]['event'] = $item['event'] = 6;
- } else {
- $list[$i]['event'] = $item['event'] = 4;
- }
- }
- // TODO: store 信息
- $list[$i]['event_desc'] = $events[$item['event']];
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'list' => $list,
- ],
- ];
- }
- /**
- * 优惠券详情
- * @return array
- */
- public function detail()
- {
- if ($this->coupon_id) {
- $list = Coupon::find()->alias('c')
- ->where([
- 'c.is_delete' => 0,
- 'c.is_join' => 2,
- 'c.store_id' => $this->store_id,
- 'c.id' => $this->coupon_id,
- ])
- ->leftJoin(UserCoupon::tableName() . ' uc', "uc.coupon_id=c.id and uc.user_id ={$this->user_id}
- and uc.type = 2 and uc.is_delete=0")
- ->select(['c.*', '(case when isnull(uc.id) then 0 else 1 end) as is_receive'])
- ->asArray()
- ->one();
- $coupon = $list;
- };
- if ($this->id) {
- $list = UserCoupon::find()
- ->where([
- 'store_id' => $this->store_id,
- 'is_delete' => 0,
- 'id' => $this->id,
- ])->with('coupon')->asArray()->one();
- if ($coupon = $list['coupon']) {
- $list['min_price'] = $coupon['min_price'];
- $list['sub_price'] = $coupon['sub_price'];
- $list['appoint_type'] = $coupon['appoint_type'];
- $list['rule'] = $coupon['rule'];
- $list['expire_type'] = 2;
- $list['status'] = 0;
- if ($list['is_use']) {
- $list['status'] = 1;
- }
- if ($list['is_expire']) {
- $list['status'] = 2;
- }
- if ($list['give_user_id']) {
- $list['status'] = 3;
- }
- }
- }
- if ($list) {
- $list['can_send'] = $coupon['is_give'];
- if ($list['is_use'] || $list['is_expire']) {
- $list['can_send'] = 0;
- }
- $list['sub_price'] = $coupon['sub_price'];
- $list['min_price'] = $coupon['min_price'];
- $list['min_price_desc'] = $coupon['min_price'] == 0 ? '无门槛' : '满' . $coupon['min_price'] . '元可用';
- $list['begin_time'] = date('Y.m.d', $list['begin_time']);
- $list['end_time'] = date('Y.m.d', $list['end_time']);
- if ($list['appoint_type'] == 1 && $coupon['cat_id_list'] !== 'null' && $coupon['cat_id_list'] !== '[]') {
- $list['cat'] = Cat::find()->select('id, name')->where(['store_id' => $this->store_id, 'is_delete' => 0,
- 'id' => json_decode($coupon['cat_id_list'])])->asArray()->all();
- $cat_list = [];
- foreach ($list['cat'] as $value) {
- $cat_list[] = $value['name'];
- }
- $list['content'] = "适用范围:仅限分类:".implode('、', $cat_list)."使用";
- $list['goods'] = [];
- } elseif ($list['appoint_type'] == 2 && $coupon['goods_id_list'] !== 'null' && $coupon['goods_id_list'] !== null) {
- $list['goods'] = Goods::find()->select('id, name, price, cover_pic')->where(['store_id' => $this->store_id, 'is_delete' => 0, 'id' => json_decode($coupon['goods_id_list'])])->asArray()->all();
- $list['content'] = "适用范围:指定商品使用";
- $list['cat'] = [];
- } else {
- $list['goods'] = [];
- $list['cat'] = [];
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'list' => $list,
- ],
- ];
- } else {
- return [
- 'code' => 0,
- 'msg' => '不存在',
- 'data' => [
- 'list' => [],
- ],
- ];
- }
- }
- /**
- * 优惠券详情
- * @return array
- */
- public function saasDetail()
- {
- if ($this->coupon_id) {
- $list = Coupon::find()->alias('c')
- ->where([
- 'c.is_delete' => 0,
- 'c.is_join' => 2,
- 'c.store_id' => $this->store_id,
- 'c.id' => $this->coupon_id,
- ])
- ->leftJoin(UserCoupon::tableName() . ' uc', "uc.coupon_id=c.id and uc.user_id ={$this->user_id}
- and uc.type = 2 and uc.is_delete=0")
- ->select(['c.*', '(case when isnull(uc.id) then 0 else 1 end) as is_receive'])
- ->asArray()
- ->one();
- $coupon = $list;
- };
- if ($this->id) {
- $list = UserCoupon::find()
- ->where([
- 'store_id' => $this->store_id,
- 'is_delete' => 0,
- 'id' => $this->id,
- 'user_id' => $this->user_id,
- ])->with('coupon')->asArray()->one();
- if ($coupon = $list['coupon']) {
- $list['min_price'] = $coupon['min_price'];
- $list['sub_price'] = $coupon['sub_price'];
- $list['appoint_type'] = $coupon['appoint_type'];
- $list['rule'] = $coupon['rule'];
- $list['expire_type'] = 2;
- $list['status'] = 0;
- if ($list['is_use']) {
- $list['status'] = 1;
- }
- if ($list['is_expire']) {
- $list['status'] = 2;
- }
- }
- }
- if ($list) {
- if ($coupon['sub_price'] >= 100) {
- $list['sub_price'] = (int)$coupon['sub_price'];
- }
- if ($coupon['min_price'] >= 100) {
- $list['min_price'] = (int)$coupon['min_price'];
- }
- $list['min_price_desc'] = $coupon['min_price'] == 0 ? '无门槛' : '满' . $coupon['min_price'] . '元可用';
- $list['begin_time'] = date('Y.m.d H:i', $list['begin_time']);
- $list['end_time'] = date('Y.m.d H:i', $list['end_time']);
- if ($list['appoint_type'] == 1) {
- $list['cat'] = Cat::find()->select('id,name')->where(['store_id'=>$this->store_id,'is_delete'=>0,
- 'id'=>json_decode($coupon['cat_id_list'])])->asArray()->all();
- $list['goods'] = [];
- } elseif ($list['appoint_type'] == 2) {
- $list['goods'] = json_decode($coupon['goods_id_list']);
- $list['cat'] = [];
- } else {
- $list['goods'] = [];
- $list['cat'] = [];
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'list' => $list,
- ],
- ];
- } else {
- return [
- 'code' => 0,
- 'msg' => '不存在',
- 'data' => [
- 'list' => [],
- ],
- ];
- }
- }
- /**
- * 分享页面送优惠券
- * @return array
- */
- public function saveCoupon()
- {
- $coupon_auto_send_list = CouponAutoSend::find()->where([
- 'store_id' => $this->store_id,
- 'is_delete' => 0,
- 'event' => 1,
- ])->all();
- $count = 0;
- $coupon_list = [];
- foreach ($coupon_auto_send_list as $coupon_auto_send) {
- if (Coupon::userAddCoupon($this->user_id, $coupon_auto_send->coupon_id, $coupon_auto_send->id)) {
- $count++;
- $coupon = Coupon::find()->select('name,discount_type,min_price,sub_price,discount,discount_max_price,expire_type,
- expire_day,begin_time,end_time,goods_id_list,appoint_type')->where(['id' => $coupon_auto_send->coupon_id])
- ->asArray()->one();
- if ($coupon['expire_type'] == 1) {
- $coupon['desc'] = "本券有效期为发放后{$coupon['expire_day']}天内";
- } else {
- if($coupon['end_time'] <= time()) {
- continue;
- }
- $coupon['desc'] = "本券有效期" . date('Y-m-d H:i:s', $coupon['begin_time']) . "至"
- . date('Y-m-d H:i:s', $coupon['end_time']);
- }
- if (count($coupon['goods_id_list']) > 0 && $coupon['appoint_type'] == 2) {
- $goodIds = implode(",", json_decode($coupon['goods_id_list']));
- $coupon['url'] = '/other/list/list?goods_id=' . $goodIds;
- } else {
- $coupon['url'] = '/other/list/list';
- }
- $coupon_list[] = $coupon;
- }
- }
- if ($count == 0) {
- return [
- 'code' => 1,
- 'msg' => '没有发放优惠券',
- ];
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'list' => $coupon_list,
- ],
- ];
- }
- /**
- * 领取优惠券
- * @return array
- */
- public function sendCoupon()
- {
- $coupon = Coupon::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'id' => $this->id])->asArray()->one();
- if (!$coupon) {
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- $coupon['type'] = 2;
- $user_coupon = UserCoupon::find()
- ->where(['store_id' => $this->store_id , 'coupon_id' => $this->id, 'user_id' => $this->user_id, 'type' => 2,
- 'is_delete' => 0])->exists();
- if ($user_coupon) {
- $coupon_list[] = $coupon;
- $coupon_list_new = Coupon::getList();
- return [
- 'code' => 1,
- 'msg' => '已领取',
- 'data'=> [
- 'coupon_list'=>$coupon_list_new
- ]
- ];
- }
- $coupon_count = UserCoupon::find()->where(['store_id' => $this->store_id, 'is_delete' => 0,
- 'coupon_id' => $coupon['id']])->andWhere(['!=', 'type', 3])->count();
- if ($coupon['total_count'] != -1 && $coupon['total_count'] <= $coupon_count) {
- return [
- 'code' => 1,
- 'msg' => '优惠券已领完'
- ];
- }
- $res = new UserCoupon();
- $res->user_id = $this->user_id;
- $res->store_id = $this->store_id;
- $res->coupon_id = $this->id;
- $res->coupon_auto_send_id = 0;
- $res->type = 2;
- $res->is_use = 0;
- $res->is_expire = 0;
- $res->is_delete = 0;
- $res->created_at = time();
- if ($coupon['expire_type'] == 1) {
- $res->begin_time = time();
- $res->end_time = time() + max(0, 86400 * $coupon['expire_day']);
- } elseif ($coupon['expire_type'] == 2) {
- $res->begin_time = $coupon['begin_time'];
- $res->end_time = $coupon['end_time'];
- }
- $subStock = Coupon::subStock($this->id);
- if($subStock['code']){
- return $subStock;
- }
- if ($res->save()) {
- MochatForm::sendMsg(1, $this->store_id, MochatForm::MSG_TYPE_GET_COUPON, get_saas_user_id(), ['coupon_id' => $this->id]);
- $coupon_list[] = $coupon;
- $coupon_list_new = Coupon::getList();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'list' => $coupon_list,
- 'coupon_list' => $coupon_list_new
- ],
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- }
- /**
- * 领取优惠券
- * @return array
- */
- public function sendSaasCoupon()
- {
- $coupon = Coupon::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'id' => $this->id])->asArray()->one();
- if (!$coupon) {
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- $coupon['type'] = 3;
- $user_coupon = UserCoupon::find()
- ->where(['store_id' => $this->store_id, 'coupon_id' => $this->id, 'user_id' => $this->user_id, 'type' => 3,
- 'is_delete' => 0])->exists();
- if ($user_coupon) {
- $coupon_list_new = Coupon::getList();
- return [
- 'code' => 1,
- 'msg' => '已领取',
- 'data'=> [
- 'coupon_list' => $coupon_list_new
- ]
- ];
- }
- $coupon_count = UserCoupon::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'coupon_id' => $coupon['id'],
- 'type' => 3])->count();
- if ($coupon['total_count'] != -1 && $coupon['total_count'] <= $coupon_count) {
- return [
- 'code' => 1,
- 'msg' => '优惠券已领完'
- ];
- }
- $res = new UserCoupon();
- $res->user_id = $this->user_id;
- $res->store_id = $this->store_id;
- $res->coupon_id = $this->id;
- $res->coupon_auto_send_id = 0;
- $res->type = 3;
- $res->is_use = 0;
- $res->is_expire = 0;
- $res->is_delete = 0;
- $res->created_at = time();
- if ($coupon['expire_type'] == 1) {
- $res->begin_time = time();
- $res->end_time = time() + max(0, 86400 * $coupon['expire_day']);
- } elseif ($coupon['expire_type'] == 2) {
- $res->begin_time = $coupon['begin_time'];
- $res->end_time = $coupon['end_time'];
- }
- if ($res->save()) {
- $coupon_list[] = $coupon;
- $coupon_list_new = Coupon::getList();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'list' => $coupon_list,
- 'coupon_list' => $coupon_list_new
- ],
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- }
- /**
- * 支付成功送优惠券
- * @return array
- */
- public function paySaveCoupon()
- {
- $coupon_auto_send_list = CouponAutoSend::find()->where([
- 'store_id' => $this->store_id,
- 'is_delete' => 0,
- 'event' => 2,
- ])->all();
- $count = 0;
- $coupon_list = [];
- foreach ($coupon_auto_send_list as $coupon_auto_send) {
- if (Coupon::userAddCoupon($this->user_id, $coupon_auto_send->coupon_id, $coupon_auto_send->id)) {
- $count++;
- $coupon = Coupon::find()->select('name,discount_type,min_price,sub_price,discount,discount_max_price,expire_type,
- expire_day,begin_time,end_time')->where(['id' => $coupon_auto_send->coupon_id])->asArray()->one();
- if ($coupon['expire_type'] == 1) {
- $coupon['desc'] = "本券有效期为发放后{$coupon['expire_day']}天内";
- } else {
- $coupon['desc'] = "本券有效期" . date('Y-m-d H:i:s', $coupon['begin_time']) . "至"
- . date('Y-m-d H:i:s', $coupon['end_time']);
- }
- $coupon_list[] = $coupon;
- }
- }
- if ($count == 0) {
- return [
- 'code' => 1,
- 'msg' => '没有发放优惠券',
- ];
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'list' => $coupon_list,
- ],
- ];
- }
- }
|