| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Coupon;
- use app\models\CouponAutoSend;
- use app\models\SaasCoupon;
- use app\models\User;
- use app\models\Store;
- use app\models\SaasUser;
- use app\models\UserCoupon;
- use yii\base\Model;
- class CouponForm extends Model
- {
- public $id;
- public $store_id;
- public $name;
- public $desc;
- public $pic_url;
- public $discount_type;
- public $min_price;
- public $sub_price;
- public $discount;
- public $discount_max_price;
- public $expire_type;
- public $expire_day;
- public $begin_time;
- public $end_time;
- public $is_delete;
- public $total_count;
- public $is_join;
- public $is_alipay_voucher = 0;
- public $sort;
- public $cat_id_list;
- public $appoint_type;
- public $is_integral;
- public $integral;
- public $price;
- public $total_num;
- public $user_num;
- public $rule;
- public $goods_id_list;
- public $mch_id;
- public $is_join_welfare;
- public $is_give;
- public $is_business;
- public $business_type;
- //搜索
- public $search_key;
- public $search_is_show;
- public $search_shop_name;
- public $status;
- const SCENARIO_ADD = 'add';
- const SCENARIO_EDIT = 'edit';
- const SCENARIO_DEL = 'del';
- const SCENARIO_LIST = 'list';
- const DISCOUNT_TYPE_TEXT = [
- Coupon::DISCOUNT_TYPE_ONE => '折扣',
- Coupon::DISCOUNT_TYPE_TWO => '满减'
- ];
- const EXPIRE_TYPE_TEXT = [
- Coupon::EXPIRE_TYPE_DAY => '天数',
- Coupon::IS_INTEGRAL_YES => '指定时间'
- ];
- const IS_JOIN_TEXT = [
- Coupon::IS_JOIN_YES => '加入',
- Coupon::IS_JOIN_NO => '不加入'
- ];
- const APPOINT_TYPE_TEXT = [
- Coupon::APPOINT_TYPE_CAT => '商品分类',
- Coupon::APPOINT_TYPE_GOODS => '商品',
- Coupon::APPOINT_TYPE_FACE => '当面付'
- ];
- const IS_INTEGRAL_TEXT = [
- Coupon::IS_INTEGRAL_YES => '加入',
- Coupon::IS_INTEGRAL_NO => '不加入'
- ];
- const USER_PLATFORM_TYPE = [
- User::USER_FROM_WECHAT => '微信小程序',
- User::USER_FROM_ALIPAY => '支付宝',
- User::USER_FROM_OFFICIAL_ACCOUNTS => '公众号',
- User::USER_FROM_APP => 'app微信',
- User::USER_FROM_PHONE => '手机号注册',
- User::USER_FROM_BYTEDANCE => '抖音',
- ];
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['status', 'store_id', 'discount_type', 'expire_type', 'expire_day', 'begin_time', 'end_time', 'is_delete', 'total_count', 'is_join', 'sort', 'appoint_type', 'is_integral', 'integral', 'total_num', 'user_num', 'mch_id', 'is_join_welfare', 'is_give','is_business','business_type','is_alipay_voucher'], 'integer'],
- [['min_price', 'sub_price', 'discount', 'price'], 'number'],
- [['name', 'cat_id_list', 'goods_id_list' ,'search_shop_name'], 'string', 'max' => 255],
- [['desc'], 'string', 'max' => 2000],
- [['pic_url'], 'string', 'max' => 2048],
- [['rule'], 'string', 'max' => 1000],
- ['is_delete', 'default', 'value' => Coupon::IS_DELETE_NO],
- [['store_id', 'name', 'discount_type', 'min_price', 'expire_type', 'is_join', 'sort', 'appoint_type', 'rule'], 'required', 'on' => [self::SCENARIO_ADD, self::SCENARIO_EDIT]],
- [['id'], 'required', 'on' => [self::SCENARIO_DEL, self::SCENARIO_EDIT]],
- [['discount_max_price'], 'safe'],
- ['search_key', 'string', 'on' => self::SCENARIO_LIST]
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'store_id' => 'Store ID',
- 'name' => '优惠券名称',
- 'desc' => '优惠券描述',
- 'pic_url' => '缩略图',
- 'discount_type' => '优惠券类型:1=折扣,2=满减',
- 'min_price' => '最低消费金额',
- 'sub_price' => '优惠金额',
- 'discount' => '折扣率',
- 'expire_type' => '到期类型:1=领取后N天过期,2=指定有效期',
- 'expire_day' => '有效天数,expire_type=1时',
- 'begin_time' => '有效期开始时间',
- 'end_time' => '有效期结束时间',
- 'is_delete' => '是否删除',
- 'total_count' => '发放总数量',
- 'is_join' => '是否加入领券中心 1--不加入领券中心 2--加入领券中心',
- 'sort' => '排序按降序排列',
- 'cat_id_list' => 'Cat Id List',
- 'appoint_type' => 'Appoint Type',
- 'is_integral' => '是否加入积分商城 1--不加入 2--加入',
- 'integral' => '兑换需要积分数量',
- 'price' => '售价',
- 'total_num' => '积分商城发放总数',
- 'user_num' => '每人限制兑换数量',
- 'rule' => '使用说明',
- 'goods_id_list' => 'Goods Id List',
- 'mch_id' => '入驻商id',
- 'is_give' => '是否可转增',
- 'is_business' => '是否是商盟',
- 'business_type' => '展示位置',
- ];
- }
- public function scenarios()
- {
- $scenarios = parent::scenarios();
- return $scenarios;
- }
- public function saveCoupon()
- {
- if ($this->validate()) {
- //$t = \Yii::$app->db->beginTransaction();
- $model = Coupon::findOne(['id' => $this->id]) ?: new Coupon();
- $model->attributes = $this->attributes;
- $model->is_business = $this->is_business ?: 0;
- $model->mch_id = get_mch_id();
- if (!$model->save()) {
- //$t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $model->getErrorSummary(false)[0]
- ];
- }
- //$t->commit();
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- }
- //获取店铺信息
- private function getStoreInfoById($store_id){
- $storeInfo = Store::find()->where(['id'=>$store_id,'is_delete'=>0])->asArray()->one();
- if($storeInfo && $storeInfo['name']){
- return $storeInfo['name'];
- }else{
- return "店铺暂无名称";
- }
- }
- public function searchCoupon()
- {
- $query = Coupon::find()->alias('c')->leftJoin(['uc' => UserCoupon::find()->where(['store_id' => $this->store_id, 'is_delete' => 0])->select('coupon_id, count(`coupon_id`) send_num')->groupBy('coupon_id')], 'c.id = uc.coupon_id');
- //is_business 商盟优惠券不展示在店铺优惠券列表
- $query->where(['c.is_delete' => Coupon::IS_DELETE_NO])->orderBy("c.sort desc,c.id desc");
-
- if($this->search_shop_name){
- $query->leftJoin(['s' => Store::tableName()], 's.id=c.store_id')->andWhere(['like', 's.name', $this->search_shop_name]);
- }
- if($this->store_id){
- $query->andWhere(['c.store_id'=>$this->store_id]);
- }
- if($this->is_join){
- $query->andWhere(['c.is_join'=>$this->is_join]);
- }
- if($this->is_alipay_voucher){
- $query->andWhere(['c.is_alipay_voucher'=>$this->is_alipay_voucher]);
- }
- if($this->is_business){
- $query->andWhere(['c.is_business'=>$this->is_business]);
- }else{
- $query->andWhere(['c.is_business'=>0]);
- }
- if($this->business_type){
- $query->andWhere(['c.business_type'=>$this->business_type]);
- }
- // 搜索
- if ($this->search_key) {
- $query->andWhere(['like', 'c.name', $this->search_key]);
- }
- // 已结束
- $arr = $storeName = [];
- if ($this->status == 2) {
- $query->andWhere(['or',
- ['and',
- ['<','c.end_time',time()],
- ['<>','c.total_count', -1]
- ],[
- '>', 'uc.send_num', 'c.total_count'
- ]
- ]);
- $list = $query->asArray()->all();
- foreach ($list as $val) {
- $val['coupon_status'] = 1;
- if($this->is_business){
- //获取店铺名称
- if(isset($storeName[$val['store_id']]) && $storeName[$val['store_id']] ){
- $val['store_name'] = $storeName[$val['store_id']];
- }else{
- $store_name = $this->getStoreInfoById($val['store_id']);
- $storeName[$val['store_id']] = $store_name;
- $val['store_name'] = $store_name;
- }
- }
- if ($val['total_count'] - self::getUsedCount($val['id'], $this->is_business) == 0) {
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- $val['updated_at'] = date('Y-m-d H:i:s', $val['updated_at']);
- $val['begin_time'] = date('Y-m-d', $val['begin_time']);
- $val['end_time'] = date('Y-m-d', $val['end_time']);
- $val['discount_type_text'] = self::DISCOUNT_TYPE_TEXT[$val['discount_type']];
- $val['is_join_text'] = self::IS_JOIN_TEXT[$val['is_join']];
- // 优惠券剩余
- if ($val['total_count'] == -1) {
- $val['surplus_count'] = -1;
- } else {
- $val['surplus_count'] = (int)$val['total_count'] - (int)self::getUsedCount($val['id'], $this->is_business);
- }
- // 已领
- $val['receive_count'] = (int)self::getUsedCount($val['id'], $this->is_business);
- $val['coupon_status'] = 2;
- $val['cat_list'] = CatForm::getCatListById(json_decode($val['cat_id_list']));
- $val['goods_list'] = GoodsForm::getGoodsListById(json_decode($val['goods_id_list']));
- $arr[] = $val;
- }
- }
- $count = count($arr);
- $data = array_slice($arr, (get_params('pageNo') - 1) * 10, 10);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'is_join_saas' => intval(\Yii::$app->isSaas()),
- 'data' => $data,
- 'pageNo' => intval(get_params('pageNo')),
- 'totalCount' => $count,
- ]
- ];
- } else if ($this->status == 1) {
- $list = $query->asArray()->all();
- foreach ($list as $val) {
- if($this->is_business){
- //获取店铺名称
- if(isset($storeName[$val['store_id']]) && $storeName[$val['store_id']] ){
- $val['store_name'] = $storeName[$val['store_id']];
- }else{
- $store_name = $this->getStoreInfoById($val['store_id']);
- $storeName[$val['store_id']] = $store_name;
- $val['store_name'] = $store_name;
- }
- }
- if ($val['total_count'] == -1 || $val['total_count'] - self::getUsedCount($val['id'], $this->is_business) > 0) {
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- $val['updated_at'] = date('Y-m-d H:i:s', $val['updated_at']);
- $val['begin_time'] = date('Y-m-d', $val['begin_time']);
- $val['end_time'] = date('Y-m-d', $val['end_time']);
- $val['discount_type_text'] = self::DISCOUNT_TYPE_TEXT[$val['discount_type']];
- $val['is_join_text'] = self::IS_JOIN_TEXT[$val['is_join']];
- // 优惠券剩余
- $val['coupon_status'] = 1;
- // 优惠券剩余
- if ($val['total_count'] == -1) {
- $val['surplus_count'] = -1;
- } else {
- $val['surplus_count'] = (int)$val['total_count'] - (int)self::getUsedCount($val['id'], $this->is_business);
- }
- // 已领
- $val['receive_count'] = (int)self::getUsedCount($val['id'], $this->is_business);
- $val['cat_list'] = CatForm::getCatListById(json_decode($val['cat_id_list']));
- $val['goods_list'] = GoodsForm::getGoodsListById(json_decode($val['goods_id_list']));
- $arr[] = $val;
- }
- }
- foreach ($arr as $k => $v) {
- if (strtotime($v['begin_time']) > time()) {
- // 删除不符合条件的数组下标
- unset($arr[$k]);
- }
- }
- $count = count($arr);
- $data = array_slice($arr, (input_params('pageNo') - 1) * input_params('pageSize', 10), input_params('pageSize', 10));
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'is_join_saas' => intval(\Yii::$app->isSaas()),
- 'data' => $data,
- 'pageNo' => intval(get_params('pageNo')),
- 'totalCount' => $count,
- ]
- ];
- } else if ($this->status == 0) {
- $query->andWhere(['>', 'c.begin_time', time()]);
- $list = pagination_make($query);
- foreach ($list['list'] as &$val) {
- if($this->is_business){
- //获取店铺名称
- if(isset($storeName[$val['store_id']]) && $storeName[$val['store_id']] ){
- $val['store_name'] = $storeName[$val['store_id']];
- }else{
- $store_name = $this->getStoreInfoById($val['store_id']);
- $storeName[$val['store_id']] = $store_name;
- $val['store_name'] = $store_name;
- }
- }
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- $val['updated_at'] = date('Y-m-d H:i:s', $val['updated_at']);
- $val['begin_time'] = date('Y-m-d', $val['begin_time']);
- $val['end_time'] = date('Y-m-d', $val['end_time']);
- $val['discount_type_text'] = self::DISCOUNT_TYPE_TEXT[$val['discount_type']];
- $val['is_join_text'] = self::IS_JOIN_TEXT[$val['is_join']];
- $val['coupon_status'] = 0;
- // 优惠券剩余
- if ($val['total_count'] == -1) {
- $val['surplus_count'] = -1;
- } else {
- $val['surplus_count'] = (int)$val['total_count'] - (int)self::getUsedCount($val['id'], $this->is_business);
- }
- // 已领
- $val['receive_count'] = (int)self::getUsedCount($val['id'], $this->is_business);
- $val['cat_list'] = CatForm::getCatListById(json_decode($val['cat_id_list']));
- $val['goods_list'] = GoodsForm::getGoodsListById(json_decode($val['goods_id_list']));
- }
- } else {
- $list = pagination_make($query);
- foreach ($list['list'] as &$val) {
- if($this->is_business){
- //获取店铺名称
- if(isset($storeName[$val['store_id']]) && $storeName[$val['store_id']] ){
- $val['store_name'] = $storeName[$val['store_id']];
- }else{
- $store_name = $this->getStoreInfoById($val['store_id']);
- $storeName[$val['store_id']] = $store_name;
- $val['store_name'] = $store_name;
- }
- }
- // if ($val['total_count'] == -1 || $val['total_count'] - self::getUsedCount($val['id'], $this->is_business) > 0) {
- // $val['coupon_status'] = 1;//进行中
- // } elseif (($val['total_count'] !== -1 && $val['total_count'] - self::getUsedCount($val['id'], $this->is_business) == 0) || time() > $val['end_time'])
- // {
- // $val['coupon_status'] = 2;//已结束
- // } elseif ($val['begin_time'] > time()) {
- // $val['coupon_status'] = 0;//未开始
- // }
- $val['coupon_status'] = 1;
- if (intval($val['expire_type']) === 1) {//领取后N天过期
- if (intval($val['total_count']) !== -1 && $val['total_count'] - self::getUsedCount($val['id'], $this->is_business) <= 0) {//进行中的未设置未不限制 并且已经领取完毕
- $val['coupon_status'] = 2;//已结束
- }
- } else {//指定有效期
- if (intval($val['begin_time']) > time()) {
- $val['coupon_status'] = 0;//未开始
- }
- if (intval($val['end_time']) < time()) {
- $val['coupon_status'] = 2;//已结束
- }
- if (intval($val['coupon_status']) === 1) {//进行中
- if (intval($val['total_count']) !== -1 && $val['total_count'] - self::getUsedCount($val['id'], $this->is_business) <= 0) {//进行中的未设置未不限制 并且已经领取完毕
- $val['coupon_status'] = 2;//已结束
- }
- }
- }
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- $val['updated_at'] = date('Y-m-d H:i:s', $val['updated_at']);
- $val['begin_time'] = date('Y-m-d', $val['begin_time']);
- $val['end_time'] = date('Y-m-d', $val['end_time']);
- $val['discount_type_text'] = self::DISCOUNT_TYPE_TEXT[$val['discount_type']];
- $val['is_join_text'] = self::IS_JOIN_TEXT[$val['is_join']];
- // 优惠券剩余
- if ($val['total_count'] == -1) {
- $val['surplus_count'] = -1;
- } else {
- $val['surplus_count'] = (int)$val['total_count'] - (int)self::getUsedCount($val['id'], $this->is_business);
- }
- // 已领
- $val['receive_count'] = (int)self::getUsedCount($val['id'], $this->is_business);
- $val['cat_list'] = CatForm::getCatListById(json_decode($val['cat_id_list']));
- $val['goods_list'] = GoodsForm::getGoodsListById(json_decode($val['goods_id_list']));
- }
- foreach ($list['list'] as $k => $v) {
- if (strtotime($v['begin_time']) > time()) {
- // 删除不符合条件的数组下标
- $list['list'][$k]['coupon_status'] = 0;
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'is_join_saas' => intval(\Yii::$app->isSaas()),
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount']
- ]
- ];
- }
- public function delCoupon()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- if ($this->scenario !== self::SCENARIO_DEL) {
- return [
- 'code' => 1,
- 'msg' => '删除失败'
- ];
- }
- $model = Coupon::findOne(['id' => $this->id]);
- $model->is_delete = Coupon::IS_DELETE_YES;
- if (!$model || !$model->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $model->getErrorSummary(false)[0]
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '删除成功'
- ];
- // 所有输入数据都有效 all inputs are valid
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- }
- public static function getCouponList()
- {
- return Coupon::find()->where(['is_delete' => Coupon::IS_DELETE_NO, 'store_id' => get_store_id()])->orderBy('sort desc, id desc')->select('*')->asArray()->all();
- }
- /**
- * Undocumented function
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-01-13
- * @desc: 优惠券发放
- * @param [type] $user_id
- * @param [type] $coupon_id
- * @param integer $coupon_auto_send_id
- * @param integer $type
- */
- public static function userAddCoupon($user_id, $coupon_id, $coupon_auto_send_id = 0, $type = 0)
- {
- $user = User::findOne($user_id);
- if (!$user) {
- return [
- 'code' => 1,
- 'msg' => '用户不存在'
- ];
- }
- $coupon = Coupon::findOne([
- 'id' => $coupon_id,
- 'is_delete' => Coupon::IS_DELETE_NO,
- ]);
- if (!$coupon) {
- return [
- 'code' => 1,
- 'msg' => '优惠券不存在'
- ];
- }
- $receive_count = UserCoupon::find()->where(['coupon_id' => $coupon_id, 'is_delete' => UserCoupon::IS_DELETE_NO, 'store_id' => $user->store_id])->andWhere(['!=', 'type', 3])->count();
- if ($coupon->total_count != -1 && $receive_count >= $coupon->total_count) {
- return [
- 'code' => 1,
- 'msg' => '优惠券已发放完'
- ];
- }
- $user_coupon = new UserCoupon();
- if ($type == UserCoupon::TYPE_GET) {
- $res = UserCoupon::find()->where(['store_id' => $user->store_id, 'is_delete' => UserCoupon::IS_DELETE_NO, 'type' => UserCoupon::TYPE_GET, 'user_id' => $user_id, 'coupon_id' => $coupon_id])->exists();
- if ($res) {
- return [
- 'code' => 0,
- 'msg' => '领取成功'
- ];
- }
- }
- if ($coupon_auto_send_id) {
- $coupon_auto_send = CouponAutoSend::findOne([
- 'store_id' => $user->store_id,
- 'status' => 1,
- 'id' => $coupon_auto_send_id,
- 'is_delete' => CouponAutoSend::IS_DELETE_NO
- ]);
- if (!$coupon_auto_send) {
- return [
- 'code' => 1,
- 'msg' => '自动发放不存在'
- ];
- }
- $user_coupon->coupon_auto_send_id = $coupon_auto_send->id;
- $type = $user_coupon::TYPE_AUTO;
- }
- $user_coupon->type = $type;
- $user_coupon->store_id = $user->store_id;
- $user_coupon->user_id = $user->id;
- $user_coupon->coupon_id = $coupon->id;
- if ($coupon->expire_type == $coupon::EXPIRE_TYPE_DAY) {
- $user_coupon->begin_time = time();
- $user_coupon->end_time = time() + max(0, 86400 * $coupon->expire_day);
- } elseif ($coupon->expire_type == $coupon::EXPIRE_TYPE_DATE) {
- $user_coupon->begin_time = $coupon->begin_time;
- $user_coupon->end_time = $coupon->end_time;
- if (time() > $user_coupon->end_time) {
- return [
- 'code' => 1,
- 'msg' => '优惠券已过期'
- ];
- }
- }
- $user_coupon->is_expire = UserCoupon::IS_DELETE_NO;
- $user_coupon->is_use = UserCoupon::IS_USE_NO;
- $user_coupon->is_delete = UserCoupon::IS_DELETE_NO;
- if (!$user_coupon->save()) {
- return [
- 'code' => 1,
- 'msg' => '发放失败'
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '发放成功',
- 'data' => $coupon,
- ];
- }
- /**
- * 优惠券发放记录
- * @return array
- */
- public function CouponSendRecord()
- {
- if ($this->id) {
- // 优惠券详情
- $couponDetail = Coupon::find()->where(['id' => $this->id, 'store_id' => $this->store_id])->asArray()->one();
- // 已领取数量
- $couponDetail['receive_count'] = (int)self::getUsedCount($this->id);
- // 剩余数量
- $couponDetail['surplus_count'] = $couponDetail['total_count'] == -1 ? '无限制' : (int)$couponDetail['total_count'] - (int)(self::getUsedCount($this->id));
- // 已结束
- if ((int)$couponDetail['total_count'] - (int)self::getUsedCount($this->id) <= 0) {
- $couponDetail['coupon_status'] = 2;
- }
- // 进行中
- if ((int)$couponDetail['total_count'] - (int)self::getUsedCount($this->id) > 0 || $couponDetail['total_count'] == -1) {
- $couponDetail['coupon_status'] = 1;
- }
- // 未开始
- if ($couponDetail['begin_time'] > time()) {
- $couponDetail['coupon_status'] = 0;
- }
- $couponDetail['event_text'] = CouponAutoSendForm::EVENT_TEXT;
- $couponDetail['begin_time'] = date('Y-m-d H:i:s', $couponDetail['begin_time']);
- $couponDetail['end_time'] = date('Y-m-d H:i:s', $couponDetail['end_time']);
- $arr = [];
- // 优惠券发放规则
- $couponRule = CouponAutoSend::find()
- ->where(['store_id' => $this->store_id, 'is_delete' => CouponAutoSend::IS_DELETE_NO])
- ->andWhere(['<>', 'coupon_id', ''])
- ->asArray()->all();
- foreach ($couponRule as &$val) {
- // 获取符合条件的id集合
- if (in_array($this->id, json_decode($val['coupon_id']))) {
- $arr[] = $val['id'];
- }
- }
- $couponList = CouponAutoSend::find()->where(['in', 'id', $arr, 'store_id' => $this->store_id, 'is_delete' => CouponAutoSend::IS_DELETE_NO])->asArray()->all();
- $query = UserCoupon::find()->alias('uc')->leftJoin(['u' => User::tableName()], 'u.id=uc.user_id')
- ->where(['uc.store_id' => $this->store_id, 'uc.is_delete' => UserCoupon::IS_DELETE_NO, 'uc.coupon_id' => $this->id])->andWhere(['!=', 'uc.type', 3])->groupBy('uc.user_id');
- $query->select(['uc.*', 'u.binding', 'u.avatar_url', 'u.nickname', 'u.platform']);
- $list = pagination_make($query);
- if(count($list['list']) > 0 ){
- foreach ($list['list'] as &$val) {
- $sassuser = SaasUser::find()->where(['mobile' => $val['binding']])->select('name, avatar')->asArray()->one();
- if($sassuser){
- $val['avatar_url'] = !empty($sassuser['avatar']) ? $sassuser['avatar'] : $val['avatar_url'];
- $val['nickname'] = !empty($sassuser['name']) ? $sassuser['name'] : $val['nickname'];
- }
- if($val['platform']) $val['platform'] = self::USER_PLATFORM_TYPE[$val['platform']];
- // 已领
- $val['receive_count'] = UserCoupon::find()->where(['user_id' => $val['user_id'], 'is_delete' => UserCoupon::IS_DELETE_NO, 'store_id' => $this->store_id, 'coupon_id' => $val['coupon_id']])->count();
- // 已使用
- $val['use_count'] = UserCoupon::find()->where(['user_id' => $val['user_id'], 'is_delete' => UserCoupon::IS_DELETE_NO, 'store_id' => $this->store_id, 'is_use' => UserCoupon::IS_USE_YES, 'coupon_id' => $val['coupon_id']])->count();
- }
- }
- $data = [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount'],
- 'couponDetail' => $couponDetail,
- 'couponList' => $couponList,
- ];
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data,
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '参数错误',
- ];
- }
- }
- /**
- * Undocumented function
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-01-13
- * @desc: 获取已领取的优惠券数
- * @param [int] $coupon_id
- */
- public static function getUsedCount($coupon_id, $is_business = 0)
- {
- if ($is_business) {
- $model = SaasCoupon::find();
- } else {
- $model = UserCoupon::find();
- }
- return $model->where(['coupon_id' => $coupon_id, 'is_delete' => UserCoupon::IS_DELETE_NO, 'store_id' => get_store_id()])->andWhere(['!=', 'type', 3])->count();
- }
- }
|