DecPackageForm.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace common\models\forms;
  3. use backendApi\modules\v1\models\Admin;
  4. use common\components\Model;
  5. use common\helpers\Cache;
  6. use common\helpers\Date;
  7. use common\models\DeclarationPackage;
  8. use yii\base\Exception;
  9. /**
  10. * Login form
  11. */
  12. class DecPackageForm extends Model
  13. {
  14. public $selectedIds;
  15. public $id;
  16. public $packageName;
  17. public $packageNo;
  18. public $amount;
  19. public $amountStandard;
  20. public $amountPv;
  21. public $levelId;
  22. public $packageContent;
  23. public $status;
  24. public $storenums;
  25. /**
  26. * @inheritdoc
  27. */
  28. public function rules()
  29. {
  30. return [
  31. [['id', 'packageName','packageNo', 'amount', 'amountPv', 'levelId','packageContent','status'], 'trim'],
  32. [['packageName','packageNo', 'amount', 'amountPv', 'levelId','packageContent','status', 'amountStandard'], 'required'],
  33. [['id'], 'required', 'on'=>'edit'],
  34. [['id'], 'exist', 'targetClass'=>DeclarationPackage::class, 'targetAttribute'=>'ID'],
  35. [['amount','amountPv'], 'price'],
  36. [['selectedIds'], 'isSelected'],
  37. [['storenums'],'number'],
  38. ];
  39. }
  40. public function attributeLabels()
  41. {
  42. return [
  43. 'selectedIds' => '套餐IDs',
  44. 'id' => '套餐ID',
  45. 'packageName' => '套餐名称',
  46. 'packageNo' => '套餐编号',
  47. 'amount' => '套餐金额',
  48. 'amountStandard' => '标准价格',
  49. 'amountPv' => '套餐BV',
  50. 'levelId' => '所属报单级别',
  51. 'packageContent' => '套餐详情',
  52. 'storenums' => '套餐库存',
  53. ];
  54. }
  55. /**
  56. * 指定场景
  57. * @return array
  58. */
  59. public function scenarios()
  60. {
  61. $parentScenarios = parent::scenarios();
  62. $customScenarios = [
  63. 'add' => ['packageName','packageNo','amount','amountPv','levelId', 'packageContent', 'amountStandard','storenums'],
  64. 'edit' => ['id','packageName','packageNo','amount','amountPv', 'levelId', 'packageContent', 'amountStandard','storenums'],
  65. 'changeStatus' => ['selectedIds', 'status'],
  66. ];
  67. return array_merge($parentScenarios, $customScenarios);
  68. }
  69. /**
  70. * 批量数据
  71. * @param $attributes
  72. */
  73. public function isSelected($attributes) {
  74. if (!$this->selectedIds) {
  75. $this->addError($attributes, '必须选择一条数据');
  76. }
  77. if (!is_array($this->selectedIds)) {
  78. $this->selectedIds = [$this->selectedIds];
  79. }
  80. }
  81. /**
  82. * 编辑
  83. * @return DeclarationPackage|null|static
  84. */
  85. public function edit(){
  86. if(!$this->validate()){
  87. return null;
  88. }
  89. // 汇率:TODO:可优化为指定是否需要根据汇率生成
  90. $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0);
  91. if($this->scenario == 'add'){
  92. $model = new DeclarationPackage();
  93. $model->CREATE_ADMIN = Admin::getAdminNameById(\Yii::$app->user->id);
  94. $model->CREATED_AT = Date::nowTime();
  95. } elseif($this->scenario == 'edit') {
  96. $model = DeclarationPackage::findOne(['ID'=>$this->id]);
  97. $model->UPDATE_ADMIN = Admin::getAdminNameById(\Yii::$app->user->id);
  98. $model->UPDATED_AT = Date::nowTime();
  99. } else {
  100. $this->addError('id', '提交场景不存在');
  101. return null;
  102. }
  103. $model->PACKAGE_NAME = $this->packageName;
  104. $model->PACKAGE_NO = $this->packageNo;
  105. $model->AMOUNT = $this->amount;
  106. $model->PV = $this->amountPv;
  107. $model->LEVEL_ID = $this->levelId;
  108. <<<<<<< HEAD
  109. $model->PACKAGE_CONTENT = $this->packageContent;
  110. $model->AMOUNT_STANDARD = $this->amountStandard;
  111. =======
  112. $model->PACKAGE_CONTENT = $this->packageContent;
  113. $model->STORE_NUMS = $this->storenums;
  114. >>>>>>> feature/york-1590-orderkucun-0614
  115. if($model->save()){
  116. return $model;
  117. } else {
  118. $this->addErrors($model->getErrors());
  119. return null;
  120. }
  121. }
  122. /**
  123. * 上下架
  124. * @return null|static
  125. * @throws \yii\db\Exception
  126. */
  127. public function changeStatus() {
  128. if (!$this->validate()) {
  129. return null;
  130. }
  131. $db = \Yii::$app->db;
  132. $transaction = $db->beginTransaction();
  133. try {
  134. foreach ($this->selectedIds as $select) {
  135. $oneGoods = DeclarationPackage::findOne(['ID' => $select]);
  136. //判断状态
  137. if (($msg = DeclarationPackage::chkAuditStatus($oneGoods->STATUS, $this->status)) != '') {
  138. throw new Exception($msg);
  139. }
  140. $oneGoods->STATUS = $this->status;
  141. $oneGoods->UPDATED_AT = Date::nowTime();
  142. if (!$oneGoods->save()) {
  143. throw new Exception(Form::formatErrorsForApi($oneGoods->getErrors()));
  144. }
  145. }
  146. $transaction->commit();
  147. } catch (Exception $e) {
  148. $transaction->rollBack();
  149. $this->addError('changeStatus', $e->getMessage());
  150. return null;
  151. }
  152. return ['status' => $this->status];
  153. }
  154. }