121], [['activities_number'], 'string', 'max' => 11], [['min_value', 'max_value'], 'string', 'max' => 191], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => '商户id', 'activities_name' => '活动名称', 'activities_number' => '活动编号', 'start_at' => '活动开始时间', 'end_at' => '活动结束时间', 'reward_type' => '奖品类型', 'min_value' => '数额最小值', 'max_value' => '数额最大值', 'sum_total' => '发放总数', 'get_number' => '领取人数', 'surplus_num' => '剩余个数', 'detail_show' => '商品详情页', 'index_show' => '首页的商品列表', 'created_at' => 'Created At', 'updated_at' => 'Updated At', ]; } public static function labelText($type = '') { $text = ''; if(!empty($type)){ switch ($type) { case self::INTEGRAL: $text = '激活积分'; break; case self::CONTRIBUTION: $text = '贡献值'; break; case self::RED_PACKET: $text = '红包'; break; case self::BALANCE: $text = '余额'; break; } } return $text; } public static function setData($mall_id, array $params) { try { if(empty($params['id'])){//新增 $model = new self(); $model->load($params, ''); $model->surplus_num = $model->sum_total; $model->mall_id = $mall_id; $goods_ids_arr = array_column($params['goods'],'id'); if(!$model->save()){ throw new Exception($model->getErrorMessage()); } $model->activities_number = rand(1000,1999).$model->id; if(!$model->save()){ throw new Exception($model->getErrorMessage()); } //保存到关系表 foreach ($goods_ids_arr as $v){ $relationModel = new RandomRedPacketRelation(); $relationModel->mall_id = $mall_id; $relationModel->goods_id = $v; $relationModel->packet_id = $model->id; if(!$relationModel->save()){ throw new Exception($relationModel->getErrorMessage()); } } return $model; }else{ $model = self::findOne(['id'=>$params['id']]); $model->load($params, ''); //根据修改后的红包总数 - 领取人数 = 剩余个数 $surplus_num = $model->sum_total - $model->get_number; if($surplus_num < 0){ $surplus_num = 0; } $model->surplus_num = $surplus_num; if(!$model->save()){ throw new Exception($model->getErrorMessage()); } if(!empty($params['goods'])){ $goods_ids_arr = array_column($params['goods'],'id'); //删除 $delRelation = RandomRedPacketRelation::deleteAll(['packet_id'=>$model->id]); if(!$delRelation){ throw new Exception("修改失败"); } //保存到关系表 foreach ($goods_ids_arr as $v){ $relationModel = new RandomRedPacketRelation(); $relationModel->mall_id = $mall_id; $relationModel->goods_id = $v; $relationModel->packet_id = $model->id; if(!$relationModel->save()){ throw new Exception($relationModel->getErrorMessage()); } } } return $model; } }catch (\Exception $e){ self::setStaticError($e->getMessage()); return false; } } }