DeclarationLoopForm.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. use common\models\DeclarationPackage;
  9. use common\helpers\Date;
  10. use common\models\ShopGoods;
  11. /**
  12. * Login form
  13. */
  14. class DeclarationLoopForm extends Model
  15. {
  16. public $data;
  17. /**
  18. * @inheritdoc
  19. */
  20. public function rules()
  21. {
  22. return [
  23. [['data'], 'required'],
  24. [['data'], 'formatData'],
  25. [['data'], 'isData', 'on'=>['canDec', 'notFull']],
  26. ];
  27. }
  28. public function attributeLabels()
  29. {
  30. return [
  31. 'data' => '数据',
  32. ];
  33. }
  34. /**
  35. * 指定场景
  36. * @return array
  37. */
  38. public function scenarios()
  39. {
  40. $parentScenarios = parent::scenarios();
  41. $customScenarios = [
  42. 'userDec' => ['data'],
  43. 'canDec' => ['data'],
  44. 'notFull' => ['data'],
  45. ];
  46. return array_merge($parentScenarios, $customScenarios);
  47. }
  48. /**
  49. * 格式化提交的数据
  50. * @param $attribute
  51. */
  52. public function formatData($attribute){
  53. //$this->data = Json::decode($this->data);
  54. if(!is_array($this->data)){
  55. $this->addError($attribute, '数据格式错误');
  56. }
  57. }
  58. /**
  59. * 循环校验数据是否合格
  60. * @param $attribute
  61. */
  62. public function isData($attribute){
  63. $model = new DeclarationForm();
  64. $model->scenario = $this->scenario;
  65. $model->allData = $this->data;
  66. foreach ($this->data as $value){
  67. if(is_array($value)){
  68. foreach($value as $key=>$decFormData){
  69. $model->$key = $decFormData;
  70. }
  71. if(!$model->validate()){
  72. $this->addErrors($model->getErrors());
  73. }
  74. $model->type = null;
  75. $model->decSn = null;
  76. $model->userId = null;
  77. $model->toUserId = null;
  78. $model->decPv = null;
  79. $model->insertUserName = null;
  80. $model->insertUserIdCard = null;
  81. $model->conUserName = null;
  82. $model->recUserName = null;
  83. $model->location = null;
  84. } else {
  85. $this->addError($attribute, '报单数据格式错误');
  86. }
  87. }
  88. }
  89. /**
  90. * 报单
  91. * @return bool
  92. * @throws \yii\db\Exception
  93. */
  94. public function add(){
  95. $startTime = microtime(true);
  96. if(!$this->validate()){
  97. return null;
  98. }
  99. $db = \Yii::$app->db;
  100. $transaction = $db->beginTransaction();
  101. try{
  102. // 所有的首购单会员ID以备点位绑定使用
  103. $allZcUserIds = [];
  104. $zcUserIdCard = null;
  105. $model = new DeclarationForm();
  106. $model->scenario = $this->scenario;
  107. $model->allData = $this->data;
  108. foreach ($this->data as $value){
  109. /**
  110. * 2022-04-29
  111. * York
  112. * 获取商品套餐信息
  113. */
  114. if (isset($value['packageId']) && $value['packageId']){
  115. $packagedata = DeclarationPackage::findOneAsArray('ID=:ID', [':ID' => $value['packageId']]);
  116. //var_dump($packagedata['ID']);
  117. if($packagedata['STORE_NUMS']>0){
  118. $data = DeclarationPackage::find()->where(['ID'=> $packagedata['ID'] ])->one();
  119. $goods_store_nums = $data->STORE_NUMS - 1;
  120. $data->STORE_NUMS = $goods_store_nums;
  121. $data->update();
  122. // var_dump('输出一下数量');
  123. // var_dump($goods_store_nums);
  124. //库存为0下架套餐
  125. if ($goods_store_nums <= 0){
  126. $data->STATUS = 0;
  127. $data->UPDATED_AT = Date::nowTime();
  128. // var_dump('购买后套餐库存为0 下架拉');
  129. }
  130. }else{
  131. throw new Exception($packagedata['PACKAGE_NAME'].'库存不足');
  132. }
  133. }
  134. if (count($value['goodsId']) > 0 && (count($value['goodsId']) == count($value['goodsNum']))){
  135. for ($i=0;$i<count($value['goodsId']);$i++){
  136. $goods = ShopGoods::findOneAsArray('ID=:ID',[':ID'=> $value['goodsId'][$i]]);
  137. if ($goods['STATUS'] == 1 ){
  138. if($goods['STORE_NUMS'] >= $value['goodsNum'][$i]){
  139. $data = ShopGoods::find()->where(['ID' => $value['goodsId'][$i]])->one();
  140. $goods_store_nums = $data->STORE_NUMS - $value['goodsNum'][$i];
  141. $data->STORE_NUMS = $goods_store_nums;
  142. $data->update();
  143. if($goods_store_nums <= 0){
  144. $data->STATUS = 0;
  145. $data->UPDATED_AT = Date::nowTime();
  146. $data->update();
  147. }
  148. }else{
  149. throw new Exception($goods['GOODS_NAME'].'商品库存不足');
  150. }
  151. }else{
  152. throw new Exception($goods['GOODS_NAME'].'商品已下架');
  153. }
  154. }
  155. }
  156. if(is_array($value)){
  157. foreach($value as $key=>$decFormData){
  158. $model->$key = $decFormData;
  159. }
  160. // 把首购单的几个会员归集到一个数组里,将来绑定到一起
  161. if( $model->type == 'ZC'){
  162. $allZcUserIds[] = null;
  163. if($zcUserIdCard != null){
  164. if($model->insertUserIdCard != $zcUserIdCard){
  165. throw new Exception('批量报单会员必须是同一身份证');
  166. }
  167. } else {
  168. $zcUserIdCard = $model->insertUserIdCard;
  169. }
  170. }
  171. if(!$model->add($this->data)){
  172. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  173. }
  174. // $model->type = null;
  175. // $model->decSn = null;
  176. // $model->userId = null;
  177. // $model->toUserId = null;
  178. // $model->decPv = null;
  179. // $model->insertUserName = null;
  180. // $model->insertUserIdCard = null;
  181. // $model->conUserName = null;
  182. // $model->recUserName = null;
  183. // $model->location = null;
  184. } else {
  185. throw new Exception('报单数据格式错误');
  186. }
  187. }
  188. // 把这几个首购单的注册用户点位绑定到一起
  189. // UserBind::zcBind($allZcUserIds, $zcUserIdCard);
  190. // if((microtime(true) - $startTime) >= 20) {
  191. // throw new Exception('服务端报单超时');
  192. // }
  193. $transaction->commit();
  194. } catch (\Exception $e){
  195. $transaction->rollBack();
  196. $this->addError('add', $e->getMessage());
  197. return null;
  198. }
  199. return true;
  200. }
  201. /**
  202. * RPC服务校验数据
  203. * @param $data
  204. * @return array
  205. */
  206. public static function rpcIsData($data){
  207. $result = [
  208. 'error' => false,
  209. 'message' => '数据可报单',
  210. ];
  211. $formModel = new self();
  212. $formModel->scenario = 'canDec';
  213. $formModel->data = $data;
  214. if(!$formModel->validate()){
  215. $result['error'] = true;
  216. $result['message'] = Form::formatErrorsForApi($formModel->getErrors());
  217. }
  218. return $result;
  219. }
  220. }