DecPackageForm.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. public $storenums;
  23. /**
  24. * @inheritdoc
  25. */
  26. public function rules()
  27. {
  28. return [
  29. [['id', 'packageName','packageNo', 'amount', 'amountPv', 'levelId','packageContent','status'], 'trim'],
  30. [['packageName','packageNo', 'amount', 'amountPv', 'levelId','packageContent','status'], 'required'],
  31. [['id'], 'required', 'on'=>'edit'],
  32. [['id'], 'exist', 'targetClass'=>DeclarationPackage::class, 'targetAttribute'=>'ID'],
  33. [['amount','amountPv'], 'price'],
  34. [['selectedIds'], 'isSelected'],
  35. [['storenums'],'number'],
  36. ];
  37. }
  38. public function attributeLabels()
  39. {
  40. return [
  41. 'selectedIds' => '套餐IDs',
  42. 'id' => '套餐ID',
  43. 'packageName' => '套餐名称',
  44. 'packageNo' => '套餐编号',
  45. 'amount' => '套餐金额',
  46. 'amountPv' => '套餐BV',
  47. 'levelId' => '所属报单级别',
  48. 'packageContent' => '套餐详情',
  49. 'storenums' => '套餐库存',
  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','storenums'],
  61. 'edit' => ['id','packageName','packageNo','amount','amountPv', 'levelId', 'packageContent','storenums'],
  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. if($this->scenario == 'add'){
  87. $model = new DeclarationPackage();
  88. $model->CREATE_ADMIN = Admin::getAdminNameById(\Yii::$app->user->id);
  89. $model->CREATED_AT = Date::nowTime();
  90. } elseif($this->scenario == 'edit') {
  91. $model = DeclarationPackage::findOne(['ID'=>$this->id]);
  92. $model->UPDATE_ADMIN = Admin::getAdminNameById(\Yii::$app->user->id);
  93. $model->UPDATED_AT = Date::nowTime();
  94. } else {
  95. $this->addError('id', '提交场景不存在');
  96. return null;
  97. }
  98. $model->PACKAGE_NAME = $this->packageName;
  99. $model->PACKAGE_NO = $this->packageNo;
  100. $model->AMOUNT = $this->amount;
  101. $model->PV = $this->amountPv;
  102. $model->LEVEL_ID = $this->levelId;
  103. $model->PACKAGE_CONTENT = $this->packageContent;
  104. $model->STORE_NUMS = $this->storenums;
  105. if($model->save()){
  106. return $model;
  107. } else {
  108. $this->addErrors($model->getErrors());
  109. return null;
  110. }
  111. }
  112. /**
  113. * 上下架
  114. * @return null|static
  115. * @throws \yii\db\Exception
  116. */
  117. public function changeStatus() {
  118. if (!$this->validate()) {
  119. return null;
  120. }
  121. $db = \Yii::$app->db;
  122. $transaction = $db->beginTransaction();
  123. try {
  124. foreach ($this->selectedIds as $select) {
  125. $oneGoods = DeclarationPackage::findOne(['ID' => $select]);
  126. //判断状态
  127. if (($msg = DeclarationPackage::chkAuditStatus($oneGoods->STATUS, $this->status)) != '') {
  128. throw new Exception($msg);
  129. }
  130. $oneGoods->STATUS = $this->status;
  131. $oneGoods->UPDATED_AT = Date::nowTime();
  132. if (!$oneGoods->save()) {
  133. throw new Exception(Form::formatErrorsForApi($oneGoods->getErrors()));
  134. }
  135. }
  136. $transaction->commit();
  137. } catch (Exception $e) {
  138. $transaction->rollBack();
  139. $this->addError('changeStatus', $e->getMessage());
  140. return null;
  141. }
  142. return ['status' => $this->status];
  143. }
  144. }