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(); } }