1,'number'=>1]] const COUPON_LIST = 'coupon_list'; const COUPON_SEND = 'coupon_send'; 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 => '不加入' ]; public function rules(): array { return [ [['store_id', 'user_id', 'number', 'md_id'], 'integer', 'on' => [self::COUPON_SEND, self::COUPON_LIST]], ['keyword', 'string', 'on' => [self::COUPON_LIST]], ['store_id', 'required', 'on' => [self::COUPON_LIST, self::COUPON_SEND]], [['user_id', 'send_coupons'], 'required', 'on' => [self::COUPON_SEND]], [['send_coupons'], 'safe', 'on' => [self::COUPON_SEND]] ]; } public function attributeLabels(): array { return [ 'store_id' => '商城ID', 'user_id' => '用户ID', 'send_coupons' => '要发放的优惠券信息', 'number' => '优惠券数量', 'keyword' => '搜索关键词' ]; } public function couponList(): array { $query = Coupon::find()->alias('c'); //is_business 商盟优惠券不展示在店铺优惠券列表 $query->where(['c.is_delete' => Coupon::IS_DELETE_NO])->orderBy("c.sort desc,c.id desc"); if ($this->store_id) { $query->andWhere(['c.store_id' => $this->store_id]); } $query->andWhere(['c.is_alipay_voucher' => 0]); $query->andWhere(['c.is_business' => 0]); // 搜索 if ($this->keyword) { $query->andWhere(['like', 'c.name', $this->keyword]); } $list = pagination_make($query); foreach ($list['list'] as &$val) { $val['coupon_status'] = 1; if (intval($val['expire_type']) === 1) {//领取后N天过期 } else {//指定有效期 if (intval($val['begin_time']) > time()) { $val['coupon_status'] = 0;//未开始 } if (intval($val['end_time']) < time()) { $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']]; // 已领 $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 sendCoupon(): array { try { $this->send_coupons = json_decode($this->send_coupons, TRUE); if (count($this->send_coupons) <= 0) { throw new \Exception('请选择要发放的优惠券'); } $senNum = 0; foreach ($this->send_coupons as $coupon) { for ($i = 0; $i < $coupon['number']; $i++) { $sendRes = CouponForm::userAddCoupon($this->user_id, $coupon['coupon_id']); $user = User::findOne($this->user_id); // 生成赠送优惠券的记录 CashierActionLog::setLog($this->store_id,get_user_id(),CashierActionLog::SEND_COUPON,'给'.$user->nickname.'用户赠送优惠券一张', $this->md_id); if ($sendRes['code'] == 0) { $senNum++; } } } if ($senNum == 0) { return ['code' => 1, 'msg' => '优惠券发放失败']; } else { return ['code' => 0, 'msg' => '已成功发放' . $senNum . '张优惠券']; } } catch (\Exception $e) { return ['code' => 1, 'msg' => $e->getMessage()]; } } }