DeclarationLoopForm.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\base\Exception;
  7. use yii\helpers\Json;
  8. /**
  9. * Login form
  10. */
  11. class DeclarationLoopForm extends Model
  12. {
  13. public $data;
  14. /**
  15. * @inheritdoc
  16. */
  17. public function rules()
  18. {
  19. return [
  20. [['data'], 'required'],
  21. [['data'], 'formatData'],
  22. [['data'], 'isData', 'on'=>['canDec', 'notFull']],
  23. ];
  24. }
  25. public function attributeLabels()
  26. {
  27. return [
  28. 'data' => '数据',
  29. ];
  30. }
  31. /**
  32. * 指定场景
  33. * @return array
  34. */
  35. public function scenarios()
  36. {
  37. $parentScenarios = parent::scenarios();
  38. $customScenarios = [
  39. 'userDec' => ['data'],
  40. 'canDec' => ['data'],
  41. 'notFull' => ['data'],
  42. ];
  43. return array_merge($parentScenarios, $customScenarios);
  44. }
  45. /**
  46. * 格式化提交的数据
  47. * @param $attribute
  48. */
  49. public function formatData($attribute){
  50. //$this->data = Json::decode($this->data);
  51. if(!is_array($this->data)){
  52. $this->addError($attribute, '数据格式错误');
  53. }
  54. }
  55. /**
  56. * 循环校验数据是否合格
  57. * @param $attribute
  58. */
  59. public function isData($attribute){
  60. $model = new DeclarationForm();
  61. $model->scenario = $this->scenario;
  62. $model->allData = $this->data;
  63. foreach ($this->data as $value){
  64. if(is_array($value)){
  65. foreach($value as $key=>$decFormData){
  66. $model->$key = $decFormData;
  67. }
  68. if(!$model->validate()){
  69. $this->addErrors($model->getErrors());
  70. }
  71. $model->type = null;
  72. $model->decSn = null;
  73. $model->userId = null;
  74. $model->toUserId = null;
  75. $model->decPv = null;
  76. $model->insertUserName = null;
  77. $model->insertUserIdCard = null;
  78. $model->conUserName = null;
  79. $model->recUserName = null;
  80. $model->location = null;
  81. } else {
  82. $this->addError($attribute, '报单数据格式错误');
  83. }
  84. }
  85. }
  86. /**
  87. * 报单
  88. * @return bool
  89. * @throws \yii\db\Exception
  90. */
  91. public function add(){
  92. $startTime = microtime(true);
  93. if(!$this->validate()){
  94. return null;
  95. }
  96. $db = \Yii::$app->db;
  97. $transaction = $db->beginTransaction();
  98. try{
  99. // 所有的首购单会员ID以备点位绑定使用
  100. $allZcUserIds = [];
  101. $zcUserIdCard = null;
  102. $model = new DeclarationForm();
  103. $model->scenario = $this->scenario;
  104. $model->allData = $this->data;
  105. foreach ($this->data as $value){
  106. if(is_array($value)){
  107. foreach($value as $key=>$decFormData){
  108. $model->$key = $decFormData;
  109. }
  110. // 把首购单的几个会员归集到一个数组里,将来绑定到一起
  111. if( $model->type == 'ZC'){
  112. $allZcUserIds[] = null;
  113. if($zcUserIdCard != null){
  114. if($model->insertUserIdCard != $zcUserIdCard){
  115. throw new Exception('批量报单会员必须是同一身份证');
  116. }
  117. } else {
  118. $zcUserIdCard = $model->insertUserIdCard;
  119. }
  120. }
  121. if(!$model->add($this->data)){
  122. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  123. }
  124. // $model->type = null;
  125. // $model->decSn = null;
  126. // $model->userId = null;
  127. // $model->toUserId = null;
  128. // $model->decPv = null;
  129. // $model->insertUserName = null;
  130. // $model->insertUserIdCard = null;
  131. // $model->conUserName = null;
  132. // $model->recUserName = null;
  133. // $model->location = null;
  134. } else {
  135. throw new Exception('报单数据格式错误');
  136. }
  137. }
  138. // 把这几个首购单的注册用户点位绑定到一起
  139. // UserBind::zcBind($allZcUserIds, $zcUserIdCard);
  140. // if((microtime(true) - $startTime) >= 20) {
  141. // throw new Exception('服务端报单超时');
  142. // }
  143. $transaction->commit();
  144. } catch (\Exception $e){
  145. $transaction->rollBack();
  146. $this->addError('add', $e->getMessage());
  147. return null;
  148. }
  149. return true;
  150. }
  151. /**
  152. * RPC服务校验数据
  153. * @param $data
  154. * @return array
  155. */
  156. public static function rpcIsData($data){
  157. $result = [
  158. 'error' => false,
  159. 'message' => '数据可报单',
  160. ];
  161. $formModel = new self();
  162. $formModel->scenario = 'canDec';
  163. $formModel->data = $data;
  164. if(!$formModel->validate()){
  165. $result['error'] = true;
  166. $result['message'] = Form::formatErrorsForApi($formModel->getErrors());
  167. }
  168. return $result;
  169. }
  170. }