| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- namespace hd\models\goods;
- use hd\models\BaseForm;
- /**
- * 商品更新表单模型
- */
- class UpdateForm extends BaseForm
- {
- /**
- * 商品ID
- * @var int
- */
- public $id;
- /**
- * 商品名称
- * @var string
- */
- public $name;
- /**
- * 商品价格
- * @var float
- */
- public $price;
- /**
- * 价格类型
- * @var int
- */
- public $priceType;
- /**
- * 已售数量
- * @var int
- */
- public $sold;
- /**
- * 花朵数量
- * @var int
- */
- public $flowerNum;
- /**
- * 重量
- * @var float
- */
- public $weight;
- /**
- * 自动涨价
- * @var int
- */
- public $autoRise;
- /**
- * 商品图片
- * @var array
- */
- public $shopImg;
- /**
- * 商品封面图
- * @var string
- */
- public $cover;
- /**
- * 库存状态
- * @var int
- */
- public $stockSet;
- /**
- * 商品库存
- * @var int
- */
- public $stock;
- /**
- * 商品分类ID列表
- * @var array
- */
- public $categoryIdList;
- /**
- * 商品状态
- * @var int
- */
- public $status;
- /**
- * 删除状态
- * @var int
- */
- public $delStatus;
- /**
- * 商品子项数量
- * @var int
- */
- public $setItemNum;
- /**
- * 是否需要发货
- * @var int
- */
- public $needSend;
- /**
- * 运费类型
- * @var int
- */
- public $freightType;
- public $useCaseIdList;
- public $specEnabled;
- public $specList;
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- // 必填字段验证
- [['id'], 'required', 'message' => '商品ID不能为空'],
- [['name'], 'required', 'message' => '请填写商品名称'],
- [['categoryIdList'], 'required', 'message' => '请选择商品分类'],
- // 数据类型验证
- [['id', 'status', 'delStatus', 'setItemNum'], 'integer'],
- [['name', 'cover'], 'string'],
- [['price'], 'number', 'min' => 0, 'message' => '商品价格必须大于或等于0'],
- [['sold'], 'integer', 'min' => 0, 'message' => '已售数量必须大于或等于0'],
- ['flowerNum', 'integer', 'min' => 0, 'message' => '花朵数量必须大于或等于0'],
- ['weight', 'number', 'min' => 0, 'message' => '重量必须大于或等于0'],
- [['autoRise'], 'in', 'range' => [0, 1]],
- [['stockSet'], 'in', 'range' => [0, 1]],
- [['stock'], 'integer', 'min' => 0, 'message' => '库存不能小于0'],
- [['priceType'], 'in', 'range' => [0, 1]],
- [['delStatus'], 'in', 'range' => [0, 1]],
- [['needSend'], 'in', 'range' => [0, 1]],
- [['freightType'], 'in', 'range' => [0, 1]],
- [['status'], 'in', 'range' => [0, 1]],
- // 数组类型验证
- [['shopImg'], 'validateShopImg'],
- [['categoryIdList'], 'validateCategoryIdList'],
- // 默认值设置
- ['flowerNum', 'default', 'value' => 0],
- ['weight', 'default', 'value' => 1],//当没有重量时,默认给1千克
- ['setItemNum', 'default', 'value' => 0],// 在更新时,没传值,会为 NULL 而导致报错
- ['status', 'default', 'value' => 1],
- ['delStatus', 'default', 'value' => 0],
- ['categoryIdList', 'default', 'value' => []],
- ['stock', 'default', 'value' => 0],
- ['useCaseIdList', 'default', 'value' => []],
- ['specEnabled', 'default', 'value' => 0],
- [['specEnabled'], 'in', 'range' => [0, 1]],
- [['useCaseIdList'], 'validateUseCaseIdList'],
- [['specList'], 'validateSpecList'],
- ];
- }
- /**
- * 验证商品图片
- * @param string $attribute 属性名
- * @param array $params 参数
- */
- public function validateShopImg($attribute, $params)
- {
- if (!empty($this->$attribute) && !is_array($this->$attribute)) {
- $this->addError($attribute, '商品图片格式不正确');
- }
- }
- /**
- * 验证分类ID列表
- * @param string $attribute 属性名
- * @param array $params 参数
- */
- public function validateCategoryIdList($attribute, $params)
- {
- if (!empty($this->$attribute) && !is_array($this->$attribute)) {
- $this->addError($attribute, '商品分类格式不正确');
- }
- }
- public function validateUseCaseIdList($attribute) {
- if (!is_array($this->$attribute)) $this->addError($attribute, '使用场景格式不正确');
- }
- public function validateSpecList($attribute)
- {
- if ((int)$this->specEnabled !== 1) {
- $this->$attribute = [];
- return;
- }
- $list = $this->$attribute;
- if (is_string($list)) {
- $list = json_decode($list, true);
- }
- if (!is_array($list) || empty($list)) {
- $this->addError($attribute, '请至少填写一个规格');
- return;
- }
- $formatList = [];
- foreach ($list as $index => $spec) {
- $name = '规格' . ($index + 1);
- if (!is_array($spec)) {
- $this->addError($attribute, $name . '格式错误');
- return;
- }
- $specName = trim($spec['specName'] ?? '');
- if ($specName === '') {
- $this->addError($attribute, '请填写' . $name . '名称');
- return;
- }
- $priceType = $spec['priceType'] ?? null;
- if ($priceType === null || !in_array((int)$priceType, [0, 1], true)) {
- $this->addError($attribute, $name . '价格类型错误');
- return;
- }
- $price = $spec['price'] ?? 0;
- if ((int)$priceType === 1 && (!is_numeric($price) || $price <= 0)) {
- $this->addError($attribute, '请填写' . $name . '价格');
- return;
- }
- $stockSet = $spec['stockSet'] ?? 0;
- if (!in_array((int)$stockSet, [0, 1], true)) {
- $this->addError($attribute, $name . '库存类型错误');
- return;
- }
- $stock = $spec['stock'] ?? 0;
- if ((int)$stockSet === 1 && (!is_numeric($stock) || (int)$stock < 0 || (int)$stock != $stock)) {
- $this->addError($attribute, $name . '库存数量错误');
- return;
- }
- $flowerNum = $spec['flowerNum'] ?? 0;
- if (!is_numeric($flowerNum) || (int)$flowerNum < 0 || (int)$flowerNum != $flowerNum) {
- $this->addError($attribute, $name . '花支数错误');
- return;
- }
- $weight = $spec['weight'] ?? 1;
- if (!is_numeric($weight) || $weight < 0) {
- $this->addError($attribute, $name . '重量错误');
- return;
- }
- $sold = $spec['sold'] ?? 0;
- if (!is_numeric($sold) || (int)$sold < 0 || (int)$sold != $sold) {
- $this->addError($attribute, $name . '已售数量错误');
- return;
- }
- $formatList[] = [
- 'id' => (int)($spec['id'] ?? 0),
- 'specName' => $specName,
- 'priceType' => (int)$priceType,
- 'price' => (int)$priceType === 1 ? (float)$price : 0,
- 'stockSet' => (int)$stockSet,
- 'stock' => (int)$stockSet === 1 ? (int)$stock : 9999,
- 'flowerNum' => (int)$flowerNum,
- 'weight' => (float)$weight,
- 'sold' => (int)$sold,
- 'shopImg' => is_array($spec['shopImg'] ?? null) ? $spec['shopImg'] : [],
- 'cover' => trim($spec['cover'] ?? ''),
- ];
- }
- $this->$attribute = $formatList;
- $firstSpec = $formatList[0];
- $this->priceType = $firstSpec['priceType'];
- $this->price = $firstSpec['price'];
- $this->stockSet = $firstSpec['stockSet'];
- $this->stock = $firstSpec['stock'];
- $this->flowerNum = $firstSpec['flowerNum'];
- $this->weight = $firstSpec['weight'];
- $this->sold = $firstSpec['sold'];
- }
- /**
- * 获取属性标签
- * @return array
- */
- public function attributeLabels()
- {
- return [
- 'id' => '商品ID',
- 'name' => '商品名称',
- 'price' => '商品价格',
- 'priceType' => '价格类型',
- 'flowerNum' => '花朵数量',
- 'shopImg' => '商品图片',
- 'cover' => '商品封面图',
- 'stock' => '商品库存',
- 'categoryIdList' => '商品分类',
- 'status' => '商品状态',
- 'delStatus' => '删除状态',
- 'setItemNum' => '商品子项数量',
- 'itemList' => '商品子项列表',
- ];
- }
- }
|