| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use Yii;
- use yii\db\ActiveRecord;
- use yii\behaviors\TimestampBehavior;
- use app\models\WechatConfig;
- use app\models\Option;
- use app\modules\admin\models\mochat\MochatForm;
- /**
- * This is the model class for table "{{%coupon}}".
- *
- * @property int $id
- * @property int $store_id
- * @property string $name 优惠券名称
- * @property string $desc 优惠券描述
- * @property string|null $pic_url 缩略图
- * @property int $discount_type 优惠券类型:1=折扣,2=满减
- * @property float $min_price 最低消费金额
- * @property float $sub_price 优惠金额
- * @property float $discount 折扣率
- * @property int $expire_type 到期类型:1=领取后N天过期,2=指定有效期
- * @property int $expire_day 有效天数,expire_type=1时
- * @property int $begin_time 有效期开始时间
- * @property int $end_time 有效期结束时间
- * @property int $created_at 添加时间
- * @property int $is_delete 是否删除
- * @property int $total_count 发放总数量
- * @property int $is_join 是否加入领券中心 1--不加入领券中心 2--加入领券中心
- * @property int $is_alipay_voucher
- * @property int|null $sort 排序按升序排列
- * @property string|null $cat_id_list
- * @property int|null $appoint_type
- * @property int $is_integral 是否加入积分商城 1--不加入 2--加入
- * @property int $integral 兑换需要积分数量
- * @property float $price 售价
- * @property int $total_num 积分商城发放总数
- * @property int $user_num 每人限制兑换数量
- * @property string|null $rule 使用说明
- * @property string|null $goods_id_list
- * @property int|null $mch_id 入驻商id
- * @property int|null $updated_at
- * @property int $is_join_welfare 是否加入到福利中心
- * @property int $is_give
- */
- class Coupon extends \yii\db\ActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%coupon}}';
- }
- const IS_DELETE_YES = 1;//已删除
- const IS_DELETE_NO = 0;//未删除
- const IS_JOIN_YES = 2; //加入领券中心
- const IS_JOIN_NO = 1;//不加入领券中心
- const DISCOUNT_TYPE_ONE = 1;//折扣类型
- const DISCOUNT_TYPE_TWO = 2; //满减类型
- const EXPIRE_TYPE_DAY = 1;//领取后n天过期
- const EXPIRE_TYPE_DATE = 2;//指定有效期
- const IS_INTEGRAL_YES = 2;//加入积分商城
- const IS_INTEGRAL_NO = 1;//不加入积分商城
- const APPOINT_TYPE_CAT = 1;//商品分类
- const APPOINT_TYPE_GOODS = 2;//商品
- const APPOINT_TYPE_FACE = 3;//当面付
- /**
- * saas 加入福利中心
- */
- const IS_JOIN_WELFARE_YES = 1;
- /**
- * saas 不加入福利中心
- */
- const IS_JOIN_WELFARE_NO = 0;
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class,
- 'attributes' => [
- ActiveRecord::EVENT_BEFORE_INSERT => ['updated_at', 'created_at'],
- ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at'
- ]
- ]
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['store_id', 'name'], 'required'],
- [['store_id', 'discount_type', 'updated_at', 'expire_type', 'expire_day', 'begin_time', 'end_time', 'created_at', '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'], 'string', 'max' => 255],
- [['desc'], 'string', 'max' => 2000],
- [['pic_url'], 'string', 'max' => 2048],
- [['rule'], 'string', 'max' => 1000],
- ['discount', 'default', 'value' => 10],
- ['is_integral', 'default', 'value' => 1],
- ['integral', 'default', 'value' => 0],
- ['price', 'default', 'value' => 0],
- ['total_num', 'default', 'value' => 0],
- ['expire_day', 'default', 'value' => 0],
- ['user_num', 'default', 'value' => 0],
- ['sub_price', 'default', 'value' => 0],
- ['total_count', 'default', 'value' => -1],
- [['begin_time', 'end_time'], 'default', 'value' => 0],
- [['is_alipay_voucher'], 'safe']
- ];
- }
- /**
- * {@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' => '有效期结束时间',
- 'created_at' => '添加时间',
- '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',
- 'updated_at' => 'Update Time',
- 'is_join_welfare' => '是否加入福利中心 1--不加入福利中心 2--加入福利中心',
- 'is_give' => '是否可转增',
- 'is_business' => '是否是商盟',
- 'business_type' => '展示位置',
- ];
- }
- /**
- * @param int $mch_id
- * @param int $limit
- * @param int $all
- * @return array
- */
- public static function getList($mch_id = 0, $limit = 0, $all = 0, $goods_id_list = [], $coupon_id = 0)
- {
- $user_id = get_user_id();
- $store_id = get_store_id();
- //商盟优惠券不展示在领取列表
- $coupon_form = Coupon::find()->alias('c')->where([
- 'c.is_delete' => 0, 'c.is_join' => 2, 'c.is_business' => 0,'c.store_id' => $store_id
- ])->andWhere(['!=', 'c.total_count', 0])
- ->leftJoin(UserCoupon::tableName() . ' uc', "uc.coupon_id=c.id and uc.user_id ={$user_id} and uc.type != 3 and uc.is_delete=0")
- ->select(['c.*', '(case when isnull(uc.id) then 0 else 1 end) as is_receive']);
-
- if (empty($all)) {
- $coupon_form->andWhere([
- 'c.mch_id' => $mch_id
- ]);
- }
- if (!empty($coupon_id)) {
- $coupon_form->andWhere([
- 'c.id' => $coupon_id
- ]);
- MochatForm::sendMsg(1, $store_id, MochatForm::MSG_TYPE_VIEW_COUPON, get_saas_user_id(), ['coupon_id' => $coupon_id]);
- }
- if ($limit > 0) {
- $coupon_form->limit($limit);
- }
- $coupon_list = $coupon_form->orderBy('is_receive ASC, sort DESC, id DESC')->asArray()->all();
- // \Yii::error([__METHOD__, $coupon_id, empty($coupon_id), $coupon_form->createCommand()->getRawSql()]);
- $new_list = [];
- foreach ($coupon_list as $index => $value) {
- $coupon_list[$index]['mch_id'] = $value['mch_id'] ? $value['mch_id'] : 0;
- $coupon_list[$index]['desc'] = !empty($value['desc']) ? $value['desc'] : '';
- if ($value['min_price'] >= 100) {
- $coupon_list[$index]['min_price'] = (int)$value['min_price'];
- }
- if ($value['sub_price'] >= 100) {
- $coupon_list[$index]['sub_price'] = (int)$value['sub_price'];
- }
- $coupon_list[$index]['begintime'] = date('Y.m.d', $value['begin_time']);
- $coupon_list[$index]['endtime'] = date('Y.m.d', $value['end_time']);
- if ($value['mch_id'] > 0) {
- $coupon_list[$index]['content'] = "适用范围:". Mch::findOne($value['mch_id'])->name;
- } else {
- $coupon_list[$index]['content'] = "适用范围:全场通用";
- }
- if ($value['appoint_type'] == 1 && $value['cat_id_list'] !== 'null' && $value['cat_id_list'] !== '[]') {
- if ($value['mch_id'] > 0) {
- $coupon_list[$index]['cat'] = MchCat::find()->select('id, name')->where(['is_delete'=>0, 'id' => json_decode($value['cat_id_list'])])->asArray()->all();
- }else {
- $coupon_list[$index]['cat'] = Cat::find()->select('id, name')->where(['store_id' => $store_id,'is_delete' => 0, 'id' => json_decode($value['cat_id_list'])])->asArray()->all();
- }
- $cat_list = [];
- foreach ($coupon_list[$index]['cat'] as $item) {
- $cat_list[] = $item['name'];
- }
- $coupon_list[$index]['content'] = "适用范围:仅限分类 ".implode('、', $cat_list)."使用";
- $coupon_list[$index]['goods'] = [];
- } elseif ($value['appoint_type'] == 2 && $value['goods_id_list'] !== 'null' && $value['goods_id_list'] !== null) {
- $coupon_list[$index]['goods'] = Goods::find()->select('id,name,price,cover_pic')->where(['store_id' => $store_id, 'is_delete' => 0, 'id' => json_decode($value['goods_id_list'])])->asArray()->all();
- $coupon_list[$index]['cat'] = [];
- $coupon_list[$index]['content'] = "适用范围:指定商品使用";
- } else {
- $coupon_list[$index]['goods'] = [];
- $coupon_list[$index]['cat'] = [];
- }
- if ($value['is_receive'] == 0) {
- $coupon_list[$index]['receive_content'] = '立即领取';
- } else {
- $coupon_list[$index]['receive_content'] = '已领取';
- }
- $coupon_count = UserCoupon::find()->where([
- 'store_id' => $store_id, 'is_delete' => 0, 'coupon_id' => $value['id'], 'type' => 2
- ])->count();
- if ($value['total_count'] > $coupon_count || $value['total_count'] == -1) {
- if ($value['expire_type'] == 2) {
- if (strtotime(date('Y-m-d 23:59:59', $value['end_time'])) >= time()) {
- $new_list[] = $coupon_list[$index];
- }
- } else {
- $new_list[] = $coupon_list[$index];
- }
- }
- }
- if (empty($goods_id_list)) {
- return $new_list;
- }
- $list = [];
- foreach ($new_list as $value) {
- if ($value['is_receive'] == 1) {
- continue; // 已领取的跳过
- }
- if ($value['appoint_type'] == 1 && $value['cat_id_list'] !== 'null' && $value['cat_id_list'] !== '[]') {
- continue; // 适用于分类的跳过
- }
- if ($value['appoint_type'] == 2 && $value['goods_id_list'] !== 'null' && $value['goods_id_list'] !== null) {
- // 判断该优惠券是否指定商品,如果指定但是给定的goods_id_list中的商品id不存在,则跳过
- $idList = json_decode($value['goods_id_list']);
- $is = false;
- foreach ($goods_id_list as $v) {
- if (in_array($v, $idList)) {
- $is = true;
- break;
- }
- }
- if (!$is) {
- continue;
- }
- }
- $list[] = $value;
- }
- return $list;
-
- }
- /**
- * @param int $limit
- * @param int $all
- * @return array
- */
- public static function getSaasList($limit = 0)
- {
- $user_id = get_user_id();
- $store_id = get_store_id();
- $coupon_form = Coupon::find()->alias('c')->where([
- 'c.is_delete' => 0,
- ])->andWhere(['!=', 'c.total_count', 0])
- ->leftJoin(UserCoupon::tableName() . ' uc', "uc.coupon_id=c.id and uc.user_id ={$user_id} and (uc.type = 3 or uc.type = 4) and uc.is_delete=0")
- ->select(['c.*', '(case when isnull(uc.id) then 0 else 1 end) as is_receive']);
- if ($limit > 0) {
- $coupon_form->limit($limit);
- }
- $coupon_list = $coupon_form->orderBy('is_receive ASC, sort ASC, id DESC')->asArray()->all();
- $new_list = [];
- foreach ($coupon_list as $index => $value) {
- // TODO: store 信息
- $coupon_list[$index]['store_id'] = $value['store_id'] ? $value['store_id'] : 0;
- $coupon_list[$index]['desc'] = !empty($value['desc']) ? $value['desc'] : '';
- if ($value['min_price'] >= 100) {
- $coupon_list[$index]['min_price'] = (int)$value['min_price'];
- }
- if ($value['sub_price'] >= 100) {
- $coupon_list[$index]['sub_price'] = (int)$value['sub_price'];
- }
- $coupon_list[$index]['begintime'] = date('Y.m.d', $value['begin_time']);
- $coupon_list[$index]['endtime'] = date('Y.m.d', $value['end_time']);
- $coupon_list[$index]['content'] = "适用范围:全场通用";
- if ($value['appoint_type'] == 1 && $value['cat_id_list'] !== 'null') {
- $coupon_list[$index]['cat'] = Cat::find()->select('id, name')->where(['is_delete' => 0, 'id' => json_decode($value['cat_id_list'])])->asArray()->all();
- $cat_list = [];
- foreach ($coupon_list[$index]['cat'] as $item) {
- $cat_list[] = $item['name'];
- }
- $coupon_list[$index]['content'] = "适用范围:仅限分类:".implode('、', $cat_list)."使用";
- $coupon_list[$index]['goods'] = [];
- } elseif ($value['appoint_type'] == 2 && $value['goods_id_list'] !== 'null') {
- $coupon_list[$index]['goods'] = Goods::find()->select('id')->where(['store_id' => $store_id, 'is_delete' => 0, 'id' => json_decode($value['goods_id_list'])])->asArray()->all();
- $coupon_list[$index]['cat'] = [];
- $coupon_list[$index]['content'] = "指定商品使用 点击查看指定商品";
- } else {
- $coupon_list[$index]['goods'] = [];
- $coupon_list[$index]['cat'] = [];
- }
- if ($value['is_receive'] == 0) {
- $coupon_list[$index]['receive_content'] = '立即领取';
- } else {
- $coupon_list[$index]['receive_content'] = '已领取';
- }
- $coupon_count = UserCoupon::find()->where([
- 'is_delete' => 0, 'coupon_id' => $value['id'], 'type' => 3
- ])->count();
- if ($value['total_count'] > $coupon_count || $value['total_count'] == -1) {
- if ($value['expire_type'] == 2) {
- if ($value['end_time'] >= time()) {
- $new_list[] = $coupon_list[$index];
- }
- } else {
- $new_list[] = $coupon_list[$index];
- }
- }
- }
- return $new_list;
- }
- /**
- * @param int $limit
- * @param int $all
- * @return array
- */
- public static function getAllianceList($page = 1, $pageSize = 20, $goods_id_list=[], $store_id = 0)
- {
- $saas_id = get_saas_user_id();
- $user_id = User::find()->where(['binding' => get_saas_user()->mobile, 'is_delete' => 0])->select('id')->column();
- // $user_id = get_user_id();
- $user_id = array_merge($user_id, [0]);
- $user_id = implode(',', $user_id);
- $offset = ($page - 1) * $pageSize;
- $time = time();
- $sql = "SELECT
- c.*,
- ( CASE WHEN isnull( uc_saas.id )
- AND isnull( uc_user.id )
- THEN 0 ELSE 1 END ) AS is_receive
- FROM
- cyy_coupon c
- LEFT JOIN cyy_saas_coupon uc_saas ON c.id = uc_saas.coupon_id
- AND c.is_business in (0, 1) AND uc_saas.saas_id = {$saas_id} AND uc_saas.is_delete = 0
- LEFT JOIN cyy_user_coupon uc_user ON c.id = uc_user.coupon_id
- AND c.is_business = 0 AND uc_user.user_id in ({$user_id}) AND uc_user.is_delete = 0
- LEFT JOIN cyy_store uc_s ON c.store_id = uc_s.id
- WHERE c.is_delete = 0 AND ((c.expire_type = 2 AND c.end_time >= {$time} AND c.begin_time <= {$time}) OR c.expire_type <> 2) ";
- if ($store_id > 0) {
- $sql .= " AND c.store_id = {$store_id}";
- } else {
- $sql .= " AND uc_s.business_model != 1";
- }
- $sql .= " GROUP BY c.id ORDER BY is_receive ASC, c.sort ASC, c.id DESC";
- $count = \Yii::$app->db->createCommand($sql)->query()->count();
- $sql .= " LIMIT {$pageSize} OFFSET {$offset}";
- $coupon_list = \Yii::$app->db->createCommand($sql)->queryAll();
- // $coupon_form = Coupon::find()->alias('c')->where([
- // 'c.is_delete' => 0,'c.is_business'=>1,'c.business_type'=>1,'c.business_status'=>1,
- // ])->andWhere(['!=', 'c.total_count', 0])
- // ->leftJoin(SaasCoupon::tableName() . ' sc', "sc.coupon_id=c.id and sc.saas_id ={$saas_id} and sc.is_delete=0")
- // ->select(['c.*', '(case when isnull(sc.id) then 0 else 1 end) as is_receive']);
- // $countQuery = clone $coupon_form;
- // $count = $countQuery->count();
- // $coupon_form->offset(($page - 1) * $pageSize)->limit($pageSize);
- // $coupon_list = $coupon_form->orderBy('is_receive ASC, c.sort ASC, c.id DESC')->asArray()->all();
- $new_list = $list = [];
- foreach ($coupon_list as $index => $value) {
- // TODO: store 信息
- $coupon_list[$index]['store_id'] = $value['store_id'] ? $value['store_id'] : 0;
- $coupon_list[$index]['desc'] = !empty($value['desc']) ? $value['desc'] : '';
- if ($value['min_price'] >= 100) {
- $coupon_list[$index]['min_price'] = (int)$value['min_price'];
- }
- if ($value['sub_price'] >= 100) {
- $coupon_list[$index]['sub_price'] = (int)$value['sub_price'];
- }
- $coupon_list[$index]['begintime'] = date('Y.m.d', $value['begin_time']);
- $coupon_list[$index]['endtime'] = date('Y.m.d', $value['end_time']);
- $coupon_list[$index]['content'] = "适用范围:全场通用";
- if ($value['appoint_type'] == 1 && $value['cat_id_list'] !== 'null') {
- $coupon_list[$index]['cat'] = Cat::find()->select('id, name')->where(['is_delete' => 0, 'id' => json_decode($value['cat_id_list'])])->asArray()->all();
- $cat_list = [];
- foreach ($coupon_list[$index]['cat'] as $item) {
- $cat_list[] = $item['name'];
- }
- $coupon_list[$index]['content'] = "适用范围:仅限分类:".implode('、', $cat_list)."使用";
- $coupon_list[$index]['goods'] = [];
- } elseif ($value['appoint_type'] == 2 && $value['goods_id_list'] !== 'null') {
- $coupon_list[$index]['goods'] = Goods::find()->select('id')->where(['store_id' => $value['store_id'], 'is_delete' => 0, 'id' => json_decode($value['goods_id_list'])])->asArray()->all();
- $coupon_list[$index]['cat'] = [];
- $coupon_list[$index]['content'] = "指定商品使用 点击查看指定商品";
- } else {
- $coupon_list[$index]['goods'] = [];
- $coupon_list[$index]['cat'] = [];
- }
- if ($value['is_receive'] == 0) {
- $coupon_list[$index]['receive_content'] = '立即领取';
- } else {
- $coupon_list[$index]['receive_content'] = '已领取';
- }
- if($coupon_list[$index]['store_id'] >0 ){
- $coupon_list[$index]['storeInfo'] = Store::find()->where(['id'=>$coupon_list[$index]['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'=>$coupon_list[$index]['store_id'],'is_delete'=>0,'type'=>1])->asArray()->one();
- if(isset($coupon_list[$index]['storeInfo']['id']) && $coupon_list[$index]['storeInfo']['id']>0){
- if($wechatInfo){
- $coupon_list[$index]['storeInfo']['wechat_app_id'] = $wechatInfo['app_id'];
- }
- $alipayInfo = Option::get('alipay_config',$coupon_list[$index]['store_id'],'alipay');
- if($alipayInfo && $alipayInfo['value']){
- $alipayInfo = json_decode($alipayInfo['value'],true);
- $coupon_list[$index]['storeInfo']['alipay_app_id'] = $alipayInfo['app_id'];
- }
- }
- }
- $coupon_count = SaasCoupon::find()->where([
- 'is_delete' => 0, 'coupon_id' => $value['id'], 'type' => 2
- ])->count();
- if ($value['total_count'] > $coupon_count || $value['total_count'] == -1) {
- if ($value['expire_type'] == 2) {
- if ($value['end_time'] >= time()) {
- $new_list[] = $coupon_list[$index];
- }
- } else {
- $new_list[] = $coupon_list[$index];
- }
- }else{
- $new_list[] = $coupon_list[$index];
- }
- }
- if (!empty($goods_id_list)) {
- foreach ($new_list as $value) {
- if ($value['is_receive'] == 1) {
- continue; // 已领取的跳过
- }
- if ($value['appoint_type'] == 1 && $value['cat_id_list'] !== 'null' && $value['cat_id_list'] !== '[]') {
- continue; // 适用于分类的跳过
- }
- if ($value['appoint_type'] == 2 && $value['goods_id_list'] !== 'null' && $value['goods_id_list'] !== null) {
- // 判断该优惠券是否指定商品,如果指定但是给定的goods_id_list中的商品id不存在,则跳过
- $idList = json_decode($value['goods_id_list']);
- $is = false;
- foreach ($goods_id_list as $v) {
- if (in_array($v, $idList)) {
- $is = true;
- break;
- }
- }
- if (!$is) {
- continue;
- }
- }
- $list[] = $value;
- }
- }else{
- $list = $new_list;
- }
- $data = [
- 'list' => $list,
- 'page' => $page,
- 'total_count'=> $count
- ];
- return $data;
- }
- /**
- * 给用户发放优惠券
- * @param integer $user_id 用户id
- * @param integer $coupon_id 优惠券id
- * @param integer $coupon_auto_send_id 自动发放id
- * @param integer $type 领券类型
- * @return boolean | integer
- */
- public static function userAddCoupon($user_id, $coupon_id, $coupon_auto_send_id = 0, $type = 0)
- {
- $user = User::findOne($user_id);
- if (!$user) {
- return false;
- }
- $coupon = Coupon::findOne([
- 'id' => $coupon_id,
- 'is_delete' => 0,
- ]);
- if (!$coupon) {
- return false;
- }
- if ($coupon->total_count == 0) {
- return false;
- }
- $user_coupon = new UserCoupon();
- if ($type == 2) {
- $res = UserCoupon::find()->where(['is_delete'=>0,'type'=>2,'user_id'=>$user_id,'coupon_id'=>$coupon_id])->exists();
- if ($res) {
- return false;
- }
- }
- if ($coupon_auto_send_id) {
- $coupon_auto_send = CouponAutoSend::findOne([
- 'id' => $coupon_auto_send_id,
- 'is_delete' => 0,
- ]);
- if (!$coupon_auto_send) {
- return false;
- }
- if ($coupon_auto_send->send_times != 0) {
- $send_count = UserCoupon::find()->where([
- 'coupon_auto_send_id' => $coupon_auto_send->id,
- 'user_id' => $user->id,
- ])->count();
- if ($send_count && $send_count >= $coupon_auto_send->send_times) {
- return false;
- }
- }
- $user_coupon->coupon_auto_send_id = $coupon_auto_send->id;
- $type = 1;
- }
- $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 == 1) {
- $user_coupon->begin_time = time();
- $user_coupon->end_time = time() + max(0, 86400 * $coupon->expire_day);
- } elseif ($coupon->expire_type == 2) {
- $user_coupon->begin_time = $coupon->begin_time;
- $user_coupon->end_time = $coupon->end_time;
- }
- $user_coupon->is_expire = 0;
- $user_coupon->is_use = 0;
- $user_coupon->is_delete = 0;
- $user_coupon->created_at = time();
- $user_coupon->save();
- return $user_coupon->id;
- }
- public function getCount()
- {
- return UserCoupon::find()->where(['coupon_id'=>$this->id,'is_delete'=>0,'type'=>2])->count();
- }
- }
|