DecPackageForm.php 4.9 KB

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