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, ], ]; } }