['canDec', 'notFull']], ]; } public function attributeLabels() { return [ 'data' => 'Data',// 数据 ]; } /** * 指定场景 * @return array */ public function scenarios() { $parentScenarios = parent::scenarios(); $customScenarios = [ 'userDec' => ['data'], 'canDec' => ['data'], 'notFull' => ['data'], ]; return array_merge($parentScenarios, $customScenarios); } /** * 格式化提交的数据 * @param $attribute */ public function formatData($attribute){ if(!is_array($this->data)){ $this->addError($attribute, 'Data format error');// 数据格式错误 } } /** * 循环校验数据是否合格 * @param $attribute */ public function isData($attribute){ $model = new DeclarationForm(); $model->scenario = $this->scenario; $model->allData = $this->data; foreach ($this->data as $value){ if(is_array($value)){ foreach($value as $key=>$decFormData){ $model->$key = $decFormData; } if(!$model->validate()){ $this->addErrors($model->getErrors()); } $model->type = null; $model->decSn = null; $model->userId = null; $model->toUserId = null; $model->decPv = null; $model->insertUserName = null; $model->insertUserIdCard = null; $model->conUserName = null; $model->recUserName = null; $model->bottomUserName = null; $model->location = null; $model->decType = null; } else { $this->addError($attribute, Yii::t('app', 'reportFormatIncorrect')); } } } /** * 报单 * @return bool * @throws \yii\db\Exception */ public function add() { if(!$this->validate()){ return null; } $result = null; $db = \Yii::$app->db; $transaction = $db->beginTransaction(); try{ // 所有的首购单会员ID以备点位绑定使用 $zcUserIdCard = null; $model = new ApproachDeclarationForm(); $model->scenario = $this->scenario; $model->allData = $this->data; foreach ($this->data as $value) { // 套餐报单 if (isset($value['packageId']) && $value['packageId']){ $packagedata = DeclarationPackage::findOneAsArray('ID=:ID', [':ID' => $value['packageId']]); if (!$packagedata) { throw new Exception(Yii::t('app', 'productsDoesSoldOut')); } if($packagedata['STORE_NUMS']>0){ $data = DeclarationPackage::find()->where(['ID'=> $packagedata['ID'] ])->one(); $goods_store_nums = $data->STORE_NUMS - 1; $data->STORE_NUMS = $goods_store_nums; $data->update(); //库存为0下架套餐 if ($goods_store_nums <= 0){ $data->STATUS = 0; $data->UPDATED_AT = Date::nowTime(); } }else{ throw new Exception($packagedata['PACKAGE_NAME'] . Yii::t('app', 'insufficientInventory')); } } // 普通商品报单 if (count($value['goodsId']) > 0 && (count($value['goodsId']) == count($value['goodsNum']))) { for ($i = 0; $i < count($value['goodsId']) ;$i++) { $goods = ShopGoods::findOneAsArray('ID=:ID',[':ID'=> $value['goodsId'][$i]]); if (!$goods) { throw new Exception(Yii::t('app', 'productsDoesSoldOut')); } $goodsNature = ShopGoodsNature::findOneAsArray('GOODS_ID=:GOODS_ID AND COUNTRY_ID=:COUNTRY_ID', [':GOODS_ID' => $value['goodsId'][$i], ':COUNTRY_ID' => $value['countryId']]); if (!$goodsNature) { throw new Exception(Yii::t('app', 'productsDoesSoldOut')); } if($goods['INSTALMENT']>0){ // 分期的商品 if($value['goodsNum'][$i]>1){ // 只能购买一个 throw new Exception(Yii::t('app', 'allowOnlyOne')); } // 分期的总期数 $instalment = intval(Cache::getSystemConfig()['instalment']['VALUE'] ?? 3); // 分期商品的期数不能大于总分期数限制 if (intval($goods['INSTALMENT']) > $instalment) { throw new Exception(Yii::t('app', 'instalmentGoodsNoError')); } } if($goods['INSTALMENT']>1){ // 不允许购买“非第一期”的商品 throw new Exception(Yii::t('app', 'canNotBuy')); } if ($goods['STATUS'] == 1 ){ if($goods['STORE_NUMS'] >= $value['goodsNum'][$i]) { // 减库存 $data = ShopGoods::find()->where(['ID' => $value['goodsId'][$i]])->one(); $goods_store_nums = $data->STORE_NUMS - $value['goodsNum'][$i]; $data->STORE_NUMS = $goods_store_nums; $data->update(); if($goods_store_nums <= 0){ $data->STATUS = 0; $data->UPDATED_AT = Date::nowTime(); $data->update(); } } else { throw new Exception($goods['GOODS_NAME'] . Yii::t('app', 'insufficientInventory')); } }else{ throw new Exception($goods['GOODS_NAME'] . Yii::t('app', 'soldOut')); } } } if (is_array($value)) { foreach($value as $key=>$decFormData){ $model->$key = $decFormData; } // 把首购单的几个会员归集到一个数组里,将来绑定到一起 if( $model->type == 'ZC'){ if($zcUserIdCard != null){ if($model->insertUserIdCard != $zcUserIdCard){ throw new Exception(Yii::t('app', 'bulkDeclarationNotSames')); } } else { $zcUserIdCard = $model->insertUserIdCard; } } $result = $model->add($this->data); if(!$result){ throw new Exception(Form::formatErrorsForApi($model->getErrors())); } } else { throw new Exception(Yii::t('app', 'reportFormatIncorrect')); } } $transaction->commit(); return $result; } catch (\Exception $e){ $transaction->rollBack(); $this->addError('add', $e->getMessage() . '; ' . $e->getLine() . '; ' . $e->getFile()); return null; } } /** * RPC服务校验数据 * @param $data * @return array */ public static function rpcIsData($data){ $result = [ 'error' => false, 'message' => 'Data can be reported',//数据可报单 ]; $formModel = new self(); $formModel->scenario = 'canDec'; $formModel->data = $data; if(!$formModel->validate()){ $result['error'] = true; $result['message'] = Form::formatErrorsForApi($formModel->getErrors()); } return $result; } }