BaDeclarationLoopForm.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Form;
  5. use common\models\UserBind;
  6. use Yii;
  7. use yii\base\Exception;
  8. use yii\helpers\Json;
  9. use common\models\DeclarationPackage;
  10. use common\helpers\Date;
  11. use common\models\ShopGoods;
  12. /**
  13. * Login form
  14. */
  15. class BaDeclarationLoopForm extends Model
  16. {
  17. public $data;
  18. /**
  19. * @inheritdoc
  20. */
  21. public function rules()
  22. {
  23. return [
  24. [['data'], 'required'],
  25. [['data'], 'formatData'],
  26. [['data'], 'isData', 'on'=>['canDec', 'notFull']],
  27. ];
  28. }
  29. public function attributeLabels()
  30. {
  31. return [
  32. 'data' => 'Data',// 数据
  33. ];
  34. }
  35. /**
  36. * 指定场景
  37. * @return array
  38. */
  39. public function scenarios()
  40. {
  41. $parentScenarios = parent::scenarios();
  42. $customScenarios = [
  43. 'userDec' => ['data'],
  44. 'canDec' => ['data'],
  45. 'notFull' => ['data'],
  46. ];
  47. return array_merge($parentScenarios, $customScenarios);
  48. }
  49. /**
  50. * 格式化提交的数据
  51. * @param $attribute
  52. */
  53. public function formatData($attribute){
  54. //$this->data = Json::decode($this->data);
  55. if(!is_array($this->data)){
  56. $this->addError($attribute, Yii::t('app', 'dataFormatError'));
  57. }
  58. }
  59. /**
  60. * 循环校验数据是否合格
  61. * @param $attribute
  62. */
  63. public function isData($attribute){
  64. $model = new BaDeclarationForm();
  65. $model->scenario = $this->scenario;
  66. $model->allData = $this->data;
  67. foreach ($this->data as $value){
  68. if(is_array($value)){
  69. foreach($value as $key=>$decFormData){
  70. $model->$key = $decFormData;
  71. }
  72. if(!$model->validate()){
  73. $this->addErrors($model->getErrors());
  74. }
  75. $model->type = null;
  76. $model->decSn = null;
  77. $model->userId = null;
  78. $model->toUserId = null;
  79. $model->decPv = null;
  80. $model->insertUserName = null;
  81. $model->insertUserIdCard = null;
  82. $model->conUserName = null;
  83. $model->recUserName = null;
  84. $model->location = null;
  85. } else {
  86. $this->addError($attribute, Yii::t('app', 'reportFormatIncorrect'));
  87. }
  88. }
  89. }
  90. /**
  91. * 报单
  92. * @return bool
  93. * @throws \yii\db\Exception
  94. */
  95. public function add(){
  96. $startTime = microtime(true);
  97. if(!$this->validate()){
  98. return null;
  99. }
  100. $db = \Yii::$app->db;
  101. $transaction = $db->beginTransaction();
  102. try{
  103. // 所有的首购单会员ID以备点位绑定使用
  104. $allZcUserIds = [];
  105. $zcUserIdCard = null;
  106. $model = new BaDeclarationForm();
  107. $model->scenario = $this->scenario;
  108. $model->allData = $this->data;
  109. foreach ($this->data as $value){
  110. if (count($value['goodsId']) > 0 && (count($value['goodsId']) == count($value['goodsNum']))){
  111. for ($i=0;$i<count($value['goodsId']);$i++){
  112. $goods = ShopGoods::findOneAsArray('ID=:ID',[':ID'=> $value['goodsId'][$i]]);
  113. if (!$goods) {
  114. throw new Exception(Yii::t('app', 'productsDoesSoldOut'));
  115. }
  116. if ($goods['STATUS'] == 1 ){
  117. if($goods['STORE_NUMS'] >= $value['goodsNum'][$i]){
  118. $data = ShopGoods::find()->where(['ID' => $value['goodsId'][$i]])->one();
  119. $goods_store_nums = $data->STORE_NUMS - $value['goodsNum'][$i];
  120. $data->STORE_NUMS = $goods_store_nums;
  121. $data->update();
  122. if($goods_store_nums <= 0){
  123. $data->STATUS = 0;
  124. $data->UPDATED_AT = Date::nowTime();
  125. $data->update();
  126. }
  127. }else{
  128. throw new Exception($goods['GOODS_NAME']. Yii::t('app', 'insufficientInventory'));
  129. }
  130. }else{
  131. throw new Exception($goods['GOODS_NAME']. Yii::t('app', 'soldOut'));
  132. }
  133. }
  134. }
  135. if(is_array($value)){
  136. foreach($value as $key=>$decFormData){
  137. $model->$key = $decFormData;
  138. }
  139. // 把首购单的几个会员归集到一个数组里,将来绑定到一起
  140. if( $model->type == 'ZC'){
  141. $allZcUserIds[] = null;
  142. if($zcUserIdCard != null){
  143. if($model->insertUserIdCard != $zcUserIdCard){
  144. throw new Exception(Yii::t('app', 'bulkDeclarationNotSames'));
  145. }
  146. } else {
  147. $zcUserIdCard = $model->insertUserIdCard;
  148. }
  149. }
  150. if(!$model->add($this->data)){
  151. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  152. }
  153. } else {
  154. throw new Exception(Yii::t('app', 'reportFormatIncorrect'));
  155. }
  156. }
  157. $transaction->commit();
  158. } catch (\Exception $e){
  159. $transaction->rollBack();
  160. $this->addError('add', $e->getMessage());
  161. return null;
  162. }
  163. return true;
  164. }
  165. /**
  166. * RPC服务校验数据
  167. * @param $data
  168. * @return array
  169. */
  170. public static function rpcIsData($data){
  171. $result = [
  172. 'error' => false,
  173. 'message' => 'Data can be reported',//数据可报单
  174. ];
  175. $formModel = new self();
  176. $formModel->scenario = 'canDec';
  177. $formModel->data = $data;
  178. if(!$formModel->validate()){
  179. $result['error'] = true;
  180. $result['message'] = Form::formatErrorsForApi($formModel->getErrors());
  181. }
  182. return $result;
  183. }
  184. }