| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- namespace common\models\forms;
- use common\components\Model;
- use common\helpers\Form;
- use common\models\UserBind;
- use yii\base\Exception;
- use yii\helpers\Json;
- use common\models\DeclarationPackage;
- use common\helpers\Date;
- use common\models\ShopGoods;
- /**
- * Login form
- */
- class BaDeclarationLoopForm extends Model
- {
- public $data;
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['data'], 'required'],
- [['data'], 'formatData'],
- [['data'], 'isData', 'on'=>['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){
- //$this->data = Json::decode($this->data);
- if(!is_array($this->data)){
- $this->addError($attribute, 'Data format error');// 数据格式错误
- }
- }
- /**
- * 循环校验数据是否合格
- * @param $attribute
- */
- public function isData($attribute){
- $model = new BaDeclarationForm();
- $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->location = null;
- } else {
- $this->addError($attribute, 'The format of the report data is incorrect');// 报单数据格式错误
- }
- }
- }
- /**
- * 报单
- * @return bool
- * @throws \yii\db\Exception
- */
- public function add(){
- $startTime = microtime(true);
- if(!$this->validate()){
- return null;
- }
- $db = \Yii::$app->db;
- $transaction = $db->beginTransaction();
- try{
- // 所有的首购单会员ID以备点位绑定使用
- $allZcUserIds = [];
- $zcUserIdCard = null;
- $model = new BaDeclarationForm();
- $model->scenario = $this->scenario;
- $model->allData = $this->data;
- foreach ($this->data as $value){
- 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['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'].'Insufficient inventory');// 商品库存不足
- }
- }else{
- throw new Exception($goods['GOODS_NAME'].'Sold out');// 商品已下架
- }
- }
- }
- if(is_array($value)){
- foreach($value as $key=>$decFormData){
- $model->$key = $decFormData;
- }
- // 把首购单的几个会员归集到一个数组里,将来绑定到一起
- if( $model->type == 'ZC'){
- $allZcUserIds[] = null;
- if($zcUserIdCard != null){
- if($model->insertUserIdCard != $zcUserIdCard){
- throw new Exception('Bulk declaration member must be the same member'); // 批量报单会员必须是同一身份证
- }
- } else {
- $zcUserIdCard = $model->insertUserIdCard;
- }
- }
- if(!$model->add($this->data)){
- throw new Exception(Form::formatErrorsForApi($model->getErrors()));
- }
- } else {
- throw new Exception('The format of the report data is incorrect');// 报单数据格式错误
- }
- }
- $transaction->commit();
- } catch (\Exception $e){
- $transaction->rollBack();
- $this->addError('add', $e->getMessage());
- return null;
- }
- return true;
- }
- /**
- * 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;
- }
- }
|