96 lines
2.8 KiB
PHP
96 lines
2.8 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @author Any
|
|
* @description KISS
|
|
* @date 2020-11-23
|
|
* @version 1.0.0
|
|
*
|
|
* _____LOG_____
|
|
*
|
|
*/
|
|
namespace app\modules\admin\models\coupon;
|
|
|
|
use app\models\Banner;
|
|
use app\models\Coupon;
|
|
use app\models\Model;
|
|
use app\models\User;
|
|
use app\models\UserCoupon;
|
|
use app\modules\admin\models\AdminModel;
|
|
|
|
class UserCouponEditForm extends AdminModel
|
|
{
|
|
public $coupon_id;
|
|
public $user_type;
|
|
public $user_list;
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['coupon_id','user_type'], 'required'],
|
|
[['user_list',], 'safe'],
|
|
[['coupon_id','user_type'], 'integer'],
|
|
];
|
|
}
|
|
|
|
public function attributeLabels()
|
|
{
|
|
return [
|
|
'coupon_id' => '卡券',
|
|
'user_type' => '发放用户',
|
|
'user_list' => '选择用户',
|
|
];
|
|
}
|
|
|
|
public function edit()
|
|
{
|
|
if(!$this->validate()){
|
|
return $this->getModelError();
|
|
}
|
|
|
|
$coupon = Coupon::findOne(['is_delete' => 0,'id' => $this->coupon_id]);
|
|
if($coupon == null){
|
|
return ['code' => 1,'msg' => '卡券不存在或已被清理'];
|
|
}
|
|
if($coupon->end_time <= time()){
|
|
return ['code' => 1,'msg' => '卡券已过有效时间'];
|
|
}
|
|
|
|
if($this->user_type == '1' && empty($this->user_list)){
|
|
return ['code' => 1,'msg' =>'您已选择指定用户,请添加发放用户'];
|
|
}
|
|
|
|
if($this->user_type == '0'){
|
|
$user_ids = User::find()->where(['is_delete' => 0])->andWhere(['!=','id',1])->select('id')->column();
|
|
}else{
|
|
if(empty($this->user_list)){
|
|
return ['code' => 1,'msg' =>'您已选择指定用户,请添加发放用户'];
|
|
}
|
|
$user_ids = array_column($this->user_list,'id');
|
|
}
|
|
|
|
if(empty($user_ids)){
|
|
return ['code' => 1,'msg' =>'请检查系统内是否存在用户或是否添加发放用户'];
|
|
}
|
|
$array = [];
|
|
foreach ($user_ids as $item){
|
|
array_push($array,[$item,$this->coupon_id,0,time(),time(),0,0,1]);
|
|
}
|
|
$key = ['user_id','coupon_id','status','created_at','updated_at','is_delete','deleted_at','is_admin_send'];
|
|
$chunk = array_chunk($array,300);
|
|
$success_count = 0;
|
|
$error_count = 0;
|
|
foreach ($chunk as $index => $value){
|
|
$r = \Yii::$app->db->createCommand()->batchInsert(UserCoupon::tableName(),$key, $value)->execute();
|
|
if(!$r){
|
|
$error_count += count($value);
|
|
}else{
|
|
$success_count += count($value);
|
|
}
|
|
}
|
|
return ['code' => 0,'msg' =>"成功发放{$success_count}条,发放失败{$error_count}条"];
|
|
|
|
}
|
|
|
|
|
|
} |