| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models;
- use app\models\WechatConfig;
- use app\models\Option;
- use app\models\Coupon;
- use app\models\CouponAutoSend;
- use app\models\Goods;
- use app\models\SaasUser;
- use app\models\SaasCoupon;
- use app\models\User;
- use app\models\Cat;
- use app\models\Store;
- use app\models\UserCoupon;
- use app\modules\admin\models\mochat\MochatForm;
- use yii\base\BaseObject;
- use yii\base\Model;
- class CouponForm extends Model
- {
- public $send_type;
- public $saas_id;
- public $status;
- public $id;
- public $coupon_id;
- public $store_id;
- public function rules()
- {
- return [
- [['status', 'id', 'coupon_id', 'saas_id','send_type', 'store_id'], 'number'],
- ];
- }
- /**
- * 优惠券列表
- * @return array
- */
- public function search()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- $query = SaasCoupon::find()->alias('sc')->leftJoin(['c' => Coupon::tableName()], 'sc.coupon_id=c.id')
- ->where(['sc.saas_id' => $this->saas_id]);
- if ($this->status == 0) {
- $query->andWhere([
- 'sc.is_delete' => 0,
- ]);
- }
- if ($this->status == 1) {
- $query->andWhere([
- 'sc.is_delete' => 0,
- 'sc.is_use' => 0,
- 'sc.is_expire' => 0,
- ]);
- }
- if ($this->status == 2) {
- $query->andWhere([
- 'sc.is_delete' => 0,
- 'sc.is_use' => 1,
- 'sc.is_expire' => 0,
- ]);
- }
- if ($this->status == 3) {
- $query->andWhere([
- 'sc.is_delete' => 0,
- 'sc.is_expire' => 1,
- ]);
- }
- if ($this->status == 0) {
- $query->orderBy('sc.is_expire ASC, sc.is_use ASC, sc.created_at DESC');
- } else {
- $query->orderBy('sc.created_at DESC');
- }
- $list = $query
- ->limit(50)
- ->select('c.id coupon_id, sc.id user_coupon_id,c.sub_price,c.mch_id,c.min_price,sc.begin_time,sc.end_time,sc.is_use,
- sc.is_expire,sc.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,sc.give_user_id,c.store_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(['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(['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'] = SaasUser::find()->where(['id'=>$item['give_user_id']])->select('avatar,name')->asArray()->one();
- //$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();
- }
- if($item['store_id'] > 0){
- $list[$i]['storeInfo'] = Store::find()->where(['id'=>$item['store_id'],'is_delete'=>0])->select(['id', 'name', 'logo', 'coordinate', 'created_at', 'category_id', 'tags', 'sales', 'rank', 'per_spend','business_model','address'])->asArray()->one();
- $wechatInfo = WechatConfig::find()->where(['store_id'=>$item['store_id'],'is_delete'=>0,'type'=>1])->asArray()->one();
- if(isset($list[$i]['storeInfo']['id']) && $list[$i]['storeInfo']['id']>0){
- if($wechatInfo){
- $list[$i]['storeInfo']['wechat_app_id'] = $wechatInfo['app_id'];
- }
- $alipayInfo = Option::get('alipay_config',$item['store_id'],'alipay');
- if($alipayInfo && $alipayInfo['value']){
- $alipayInfo = json_decode($alipayInfo['value'],true);
- $list[$i]['storeInfo']['alipay_app_id'] = $alipayInfo['app_id'];
- }
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'list' => $list,
- ],
- ];
- }
- /**
- * 领取优惠券
- * @return array
- */
- public function sendSaasCoupon()
- {
- $coupon = Coupon::find()->where(['is_delete' => 0, 'id' => $this->id])->asArray()->one(); // ,'is_business'=>1, 'business_type'=>$this->send_type 领取正常商城优惠券修改
- if (!$coupon) {
- return [
- 'code' => 1,
- 'msg' => '优惠券不存在'
- ];
- }
- $saas_coupon = SaasCoupon::find() ->where(['coupon_id' => $this->id, 'saas_id' => $this->saas_id, 'type' => $this->send_type, 'is_delete' => 0])->exists();
- if ($saas_coupon) {
- return [
- 'code' => 1,
- 'msg' => '已领取',
- ];
- }
- $coupon_count = SaasCoupon::find()->where(['is_delete' => 0, 'coupon_id' => $this->id ])->count();
- if ($coupon['total_count'] != -1 && $coupon['total_count'] <= $coupon_count) {
- return [
- 'code' => 1,
- 'msg' => '优惠券已领完'
- ];
- }
- $res = new SaasCoupon();
- $res->saas_id = $this->saas_id;
- $res->store_id = $coupon['store_id'];
- $res->coupon_id = $this->id;
- $res->coupon_auto_send_id = 0;
- $res->type = $this->send_type;
- $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();
- if ($coupon['expire_day'] != 0) {
- $res->end_time = time() + max(0, 86400 * $coupon['expire_day']);
- } else {
- $res->end_time = 0; //永久有效
- }
- } elseif ($coupon['expire_type'] == 2) {
- $res->begin_time = $coupon['begin_time'];
- $res->end_time = $coupon['end_time'];
- }
- if ($res->save()) {
- return [
- 'code' => 0,
- 'msg' => 'success'
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- }
- /**
- * 优惠券详情
- * @return array
- */
- public function detail()
- {
- $id = 0;
- if ($this->coupon_id) {
- $user_ids = User::find()->where(['binding' => get_saas_user()->mobile])->select('id')->column();
- $UserCoupon = UserCoupon::findOne(['coupon_id' => $this->coupon_id, 'user_id' => $user_ids]);
- $list = Coupon::find()->alias('c')
- ->where([
- 'c.is_delete' => 0,
- 'c.is_join' => 2,
- 'c.id' => $this->coupon_id,
- ])
- ->leftJoin(UserCoupon::tableName() . ' uc', "uc.coupon_id=c.id and uc.user_id ={$UserCoupon->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 = SaasCoupon::find()
- ->where([
- '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 sendCoupon()
- {
- $coupon = Coupon::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'id' => $this->id])->asArray()->one();
- if (!$coupon) {
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- $user_id = 0;
- if ($this->saas_id) {
- $saasUser = SaasUser::findOne($this->saas_id);
- $user = User::findOne(['binding' => $saasUser->mobile, 'store_id' => $this->store_id]);
- $coupon['type'] = 2;
- $user_coupon = UserCoupon::find()
- ->where(['store_id' => $this->store_id , 'coupon_id' => $this->id, 'user_id' => $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 = $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'];
- }
- if ($res->save()) {
- MochatForm::sendMsg(1, $this->store_id, MochatForm::MSG_TYPE_GET_COUPON, $this->saas_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 [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- }
|