Files
xk-api-yii/admin/models/forms/FreeRuleEditForm.php

81 lines
2.3 KiB
PHP

<?php
namespace admin\models\forms;
use admin\exceptions\ApiException;
use common\models\oldDb\FreeDeliveryRules;
use yii\base\Model;
/**
* Created by PhpStorm.
* User: chatfeed
* Date: 2022/5/23
* Time: 6:02 PM
*/
/**
* @property \common\models\oldDb\FreeDeliveryRules $model
*/
class FreeRuleEditForm extends Model
{
public $model;
public $price;
public $detail;
public $name;
public $type;
public $status;
public function rules()
{
return [
[['type', 'status'], 'integer'],
[['price', 'status'], 'default', 'value' => 0],
['price', 'number', 'min' => 0],
['detail', 'safe'],
['name', 'required'],
];
}
public function save()
{
if (!$this->validate()) {
return ['error'=>$this->model->getErrors()];
}
if ($this->model->isNewRecord) {
$this->model->is_delete = 0;
}
$conditionList = [];
foreach ($this->detail as &$item) {
if (isset($item['condition'])) {
if (in_array($item['condition'], $conditionList)) {
throw new ApiException("同一条规则下,包邮条件不能相同");
}
$conditionList[] = $item['condition'];
$item['condition'] = trim($item['condition']);
if (!is_numeric($item['condition']) || $item['condition'] < 0 || $item['condition'] > 99999999) {
throw new ApiException("包邮条件必须大于等于0,小于99999999");
}
if (empty($this->type)) {
throw new ApiException("请选择包邮类型");
}
if (in_array($this->type, [2, 4])) {
$item['condition'] = (int)$item['condition'];
}
} else {
throw new ApiException("请设置包邮条件");
}
}
unset($item);
$this->model->detail = json_encode($this->detail);
$this->model->price = $this->price;
$this->model->name = $this->name;
$this->model->type = $this->type;
$this->model->status = $this->status;
if ($this->model->save()) {
return [];
} else {
return ['error'=>$this->model->getErrors()];
}
}
}