DecPackageForm.php 4.5 KB

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