DecPackageForm.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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' => 'US price',
  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, 'A piece of data must be selected'); // 必须选择一条数据
  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. $model->PACKAGE_CONTENT = $this->packageContent;
  109. $model->AMOUNT_STANDARD = $this->amountStandard;
  110. $model->STORE_NUMS = $this->storenums;
  111. if($model->save()){
  112. return $model;
  113. } else {
  114. $this->addErrors($model->getErrors());
  115. return null;
  116. }
  117. }
  118. /**
  119. * 上下架
  120. * @return null|static
  121. * @throws \yii\db\Exception
  122. */
  123. public function changeStatus() {
  124. if (!$this->validate()) {
  125. return null;
  126. }
  127. $db = \Yii::$app->db;
  128. $transaction = $db->beginTransaction();
  129. try {
  130. foreach ($this->selectedIds as $select) {
  131. $oneGoods = DeclarationPackage::findOne(['ID' => $select]);
  132. //判断状态
  133. if (($msg = DeclarationPackage::chkAuditStatus($oneGoods->STATUS, $this->status)) != '') {
  134. throw new Exception($msg);
  135. }
  136. $oneGoods->STATUS = $this->status;
  137. $oneGoods->UPDATED_AT = Date::nowTime();
  138. if (!$oneGoods->save()) {
  139. throw new Exception(Form::formatErrorsForApi($oneGoods->getErrors()));
  140. }
  141. }
  142. $transaction->commit();
  143. } catch (Exception $e) {
  144. $transaction->rollBack();
  145. $this->addError('changeStatus', $e->getMessage());
  146. return null;
  147. }
  148. return ['status' => $this->status];
  149. }
  150. }