| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- namespace common\models\forms;
- use backendApi\modules\v1\models\Admin;
- use common\components\Model;
- use common\helpers\Cache;
- use common\helpers\Date;
- use common\models\DeclarationPackage;
- use yii\base\Exception;
- /**
- * Login form
- */
- class DecPackageForm extends Model
- {
- public $selectedIds;
- public $id;
- public $packageName;
- public $packageNo;
- public $amount;
- public $amountStandard;
- public $amountPv;
- public $levelId;
- public $packageContent;
- public $status;
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['id', 'packageName','packageNo', 'amount', 'amountPv', 'levelId','packageContent','status'], 'trim'],
- [['packageName','packageNo', 'amount', 'amountPv', 'levelId','packageContent','status', 'amountStandard'], 'required'],
- [['id'], 'required', 'on'=>'edit'],
- [['id'], 'exist', 'targetClass'=>DeclarationPackage::class, 'targetAttribute'=>'ID'],
- [['amount','amountPv'], 'price'],
- [['selectedIds'], 'isSelected'],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'selectedIds' => '套餐IDs',
- 'id' => '套餐ID',
- 'packageName' => '套餐名称',
- 'packageNo' => '套餐编号',
- 'amount' => '套餐金额',
- 'amountStandard' => '标准价格',
- 'amountPv' => '套餐BV',
- 'levelId' => '所属报单级别',
- 'packageContent' => '套餐详情',
- ];
- }
- /**
- * 指定场景
- * @return array
- */
- public function scenarios()
- {
- $parentScenarios = parent::scenarios();
- $customScenarios = [
- 'add' => ['packageName','packageNo','amount','amountPv','levelId', 'packageContent', 'amountStandard'],
- 'edit' => ['id','packageName','packageNo','amount','amountPv', 'levelId', 'packageContent', 'amountStandard'],
- 'changeStatus' => ['selectedIds', 'status'],
- ];
- return array_merge($parentScenarios, $customScenarios);
- }
- /**
- * 批量数据
- * @param $attributes
- */
- public function isSelected($attributes) {
- if (!$this->selectedIds) {
- $this->addError($attributes, '必须选择一条数据');
- }
- if (!is_array($this->selectedIds)) {
- $this->selectedIds = [$this->selectedIds];
- }
- }
- /**
- * 编辑
- * @return DeclarationPackage|null|static
- */
- public function edit(){
- if(!$this->validate()){
- return null;
- }
- // 汇率:TODO:可优化为指定是否需要根据汇率生成
- $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0);
- if($this->scenario == 'add'){
- $model = new DeclarationPackage();
- $model->CREATE_ADMIN = Admin::getAdminNameById(\Yii::$app->user->id);
- $model->CREATED_AT = Date::nowTime();
- } elseif($this->scenario == 'edit') {
- $model = DeclarationPackage::findOne(['ID'=>$this->id]);
- $model->UPDATE_ADMIN = Admin::getAdminNameById(\Yii::$app->user->id);
- $model->UPDATED_AT = Date::nowTime();
- } else {
- $this->addError('id', '提交场景不存在');
- return null;
- }
- $model->PACKAGE_NAME = $this->packageName;
- $model->PACKAGE_NO = $this->packageNo;
- $model->AMOUNT = $this->amount;
- $model->PV = $this->amountPv;
- $model->LEVEL_ID = $this->levelId;
- $model->PACKAGE_CONTENT = $this->packageContent;
- $model->AMOUNT_STANDARD = $this->amountStandard;
- if($model->save()){
- return $model;
- } else {
- $this->addErrors($model->getErrors());
- return null;
- }
- }
- /**
- * 上下架
- * @return null|static
- * @throws \yii\db\Exception
- */
- public function changeStatus() {
- if (!$this->validate()) {
- return null;
- }
- $db = \Yii::$app->db;
- $transaction = $db->beginTransaction();
- try {
- foreach ($this->selectedIds as $select) {
- $oneGoods = DeclarationPackage::findOne(['ID' => $select]);
- //判断状态
- if (($msg = DeclarationPackage::chkAuditStatus($oneGoods->STATUS, $this->status)) != '') {
- throw new Exception($msg);
- }
- $oneGoods->STATUS = $this->status;
- $oneGoods->UPDATED_AT = Date::nowTime();
- if (!$oneGoods->save()) {
- throw new Exception(Form::formatErrorsForApi($oneGoods->getErrors()));
- }
- }
- $transaction->commit();
- } catch (Exception $e) {
- $transaction->rollBack();
- $this->addError('changeStatus', $e->getMessage());
- return null;
- }
- return ['status' => $this->status];
- }
- }
|