UpdateForm.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace hd\models\goods;
  3. use hd\models\BaseForm;
  4. /**
  5. * 商品更新表单模型
  6. */
  7. class UpdateForm extends BaseForm
  8. {
  9. /**
  10. * 商品ID
  11. * @var int
  12. */
  13. public $id;
  14. /**
  15. * 商品名称
  16. * @var string
  17. */
  18. public $name;
  19. /**
  20. * 商品价格
  21. * @var float
  22. */
  23. public $price;
  24. /**
  25. * 价格类型
  26. * @var int
  27. */
  28. public $priceType;
  29. /**
  30. * 已售数量
  31. * @var int
  32. */
  33. public $sold;
  34. /**
  35. * 花朵数量
  36. * @var int
  37. */
  38. public $flowerNum;
  39. /**
  40. * 重量
  41. * @var float
  42. */
  43. public $weight;
  44. /**
  45. * 自动涨价
  46. * @var int
  47. */
  48. public $autoRise;
  49. /**
  50. * 商品图片
  51. * @var array
  52. */
  53. public $shopImg;
  54. /**
  55. * 商品封面图
  56. * @var string
  57. */
  58. public $cover;
  59. /**
  60. * 库存状态
  61. * @var int
  62. */
  63. public $stockSet;
  64. /**
  65. * 商品库存
  66. * @var int
  67. */
  68. public $stock;
  69. /**
  70. * 商品分类ID列表
  71. * @var array
  72. */
  73. public $categoryIdList;
  74. /**
  75. * 商品状态
  76. * @var int
  77. */
  78. public $status;
  79. /**
  80. * 删除状态
  81. * @var int
  82. */
  83. public $delStatus;
  84. /**
  85. * 商品子项数量
  86. * @var int
  87. */
  88. public $setItemNum;
  89. /**
  90. * 是否需要发货
  91. * @var int
  92. */
  93. public $needSend;
  94. /**
  95. * 运费类型
  96. * @var int
  97. */
  98. public $freightType;
  99. public $useCaseIdList;
  100. public $specEnabled;
  101. public $specList;
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function rules()
  106. {
  107. return [
  108. // 必填字段验证
  109. [['id'], 'required', 'message' => '商品ID不能为空'],
  110. [['name'], 'required', 'message' => '请填写商品名称'],
  111. [['categoryIdList'], 'required', 'message' => '请选择商品分类'],
  112. // 数据类型验证
  113. [['id', 'status', 'delStatus', 'setItemNum'], 'integer'],
  114. [['name', 'cover'], 'string'],
  115. [['price'], 'number', 'min' => 0, 'message' => '商品价格必须大于或等于0'],
  116. [['sold'], 'integer', 'min' => 0, 'message' => '已售数量必须大于或等于0'],
  117. ['flowerNum', 'integer', 'min' => 0, 'message' => '花朵数量必须大于或等于0'],
  118. ['weight', 'number', 'min' => 0, 'message' => '重量必须大于或等于0'],
  119. [['autoRise'], 'in', 'range' => [0, 1]],
  120. [['stockSet'], 'in', 'range' => [0, 1]],
  121. [['stock'], 'integer', 'min' => 0, 'message' => '库存不能小于0'],
  122. [['priceType'], 'in', 'range' => [0, 1]],
  123. [['delStatus'], 'in', 'range' => [0, 1]],
  124. [['needSend'], 'in', 'range' => [0, 1]],
  125. [['freightType'], 'in', 'range' => [0, 1]],
  126. [['status'], 'in', 'range' => [0, 1]],
  127. // 数组类型验证
  128. [['shopImg'], 'validateShopImg'],
  129. [['categoryIdList'], 'validateCategoryIdList'],
  130. // 默认值设置
  131. ['flowerNum', 'default', 'value' => 0],
  132. ['weight', 'default', 'value' => 1],//当没有重量时,默认给1千克
  133. ['setItemNum', 'default', 'value' => 0],// 在更新时,没传值,会为 NULL 而导致报错
  134. ['status', 'default', 'value' => 1],
  135. ['delStatus', 'default', 'value' => 0],
  136. ['categoryIdList', 'default', 'value' => []],
  137. ['stock', 'default', 'value' => 0],
  138. ['useCaseIdList', 'default', 'value' => []],
  139. ['specEnabled', 'default', 'value' => 0],
  140. [['specEnabled'], 'in', 'range' => [0, 1]],
  141. [['useCaseIdList'], 'validateUseCaseIdList'],
  142. [['specList'], 'validateSpecList'],
  143. ];
  144. }
  145. /**
  146. * 验证商品图片
  147. * @param string $attribute 属性名
  148. * @param array $params 参数
  149. */
  150. public function validateShopImg($attribute, $params)
  151. {
  152. if (!empty($this->$attribute) && !is_array($this->$attribute)) {
  153. $this->addError($attribute, '商品图片格式不正确');
  154. }
  155. }
  156. /**
  157. * 验证分类ID列表
  158. * @param string $attribute 属性名
  159. * @param array $params 参数
  160. */
  161. public function validateCategoryIdList($attribute, $params)
  162. {
  163. if (!empty($this->$attribute) && !is_array($this->$attribute)) {
  164. $this->addError($attribute, '商品分类格式不正确');
  165. }
  166. }
  167. public function validateUseCaseIdList($attribute) {
  168. if (!is_array($this->$attribute)) $this->addError($attribute, '使用场景格式不正确');
  169. }
  170. public function validateSpecList($attribute)
  171. {
  172. if ((int)$this->specEnabled !== 1) {
  173. $this->$attribute = [];
  174. return;
  175. }
  176. $list = $this->$attribute;
  177. if (is_string($list)) {
  178. $list = json_decode($list, true);
  179. }
  180. if (!is_array($list) || empty($list)) {
  181. $this->addError($attribute, '请至少填写一个规格');
  182. return;
  183. }
  184. $formatList = [];
  185. foreach ($list as $index => $spec) {
  186. $name = '规格' . ($index + 1);
  187. if (!is_array($spec)) {
  188. $this->addError($attribute, $name . '格式错误');
  189. return;
  190. }
  191. $specName = trim($spec['specName'] ?? '');
  192. if ($specName === '') {
  193. $this->addError($attribute, '请填写' . $name . '名称');
  194. return;
  195. }
  196. $priceType = $spec['priceType'] ?? null;
  197. if ($priceType === null || !in_array((int)$priceType, [0, 1], true)) {
  198. $this->addError($attribute, $name . '价格类型错误');
  199. return;
  200. }
  201. $price = $spec['price'] ?? 0;
  202. if ((int)$priceType === 1 && (!is_numeric($price) || $price <= 0)) {
  203. $this->addError($attribute, '请填写' . $name . '价格');
  204. return;
  205. }
  206. $stockSet = $spec['stockSet'] ?? 0;
  207. if (!in_array((int)$stockSet, [0, 1], true)) {
  208. $this->addError($attribute, $name . '库存类型错误');
  209. return;
  210. }
  211. $stock = $spec['stock'] ?? 0;
  212. if ((int)$stockSet === 1 && (!is_numeric($stock) || (int)$stock < 0 || (int)$stock != $stock)) {
  213. $this->addError($attribute, $name . '库存数量错误');
  214. return;
  215. }
  216. $flowerNum = $spec['flowerNum'] ?? 0;
  217. if (!is_numeric($flowerNum) || (int)$flowerNum < 0 || (int)$flowerNum != $flowerNum) {
  218. $this->addError($attribute, $name . '花支数错误');
  219. return;
  220. }
  221. $weight = $spec['weight'] ?? 1;
  222. if (!is_numeric($weight) || $weight < 0) {
  223. $this->addError($attribute, $name . '重量错误');
  224. return;
  225. }
  226. $sold = $spec['sold'] ?? 0;
  227. if (!is_numeric($sold) || (int)$sold < 0 || (int)$sold != $sold) {
  228. $this->addError($attribute, $name . '已售数量错误');
  229. return;
  230. }
  231. $formatList[] = [
  232. 'id' => (int)($spec['id'] ?? 0),
  233. 'specName' => $specName,
  234. 'priceType' => (int)$priceType,
  235. 'price' => (int)$priceType === 1 ? (float)$price : 0,
  236. 'stockSet' => (int)$stockSet,
  237. 'stock' => (int)$stockSet === 1 ? (int)$stock : 9999,
  238. 'flowerNum' => (int)$flowerNum,
  239. 'weight' => (float)$weight,
  240. 'sold' => (int)$sold,
  241. 'shopImg' => is_array($spec['shopImg'] ?? null) ? $spec['shopImg'] : [],
  242. 'cover' => trim($spec['cover'] ?? ''),
  243. ];
  244. }
  245. $this->$attribute = $formatList;
  246. $firstSpec = $formatList[0];
  247. $this->priceType = $firstSpec['priceType'];
  248. $this->price = $firstSpec['price'];
  249. $this->stockSet = $firstSpec['stockSet'];
  250. $this->stock = $firstSpec['stock'];
  251. $this->flowerNum = $firstSpec['flowerNum'];
  252. $this->weight = $firstSpec['weight'];
  253. $this->sold = $firstSpec['sold'];
  254. }
  255. /**
  256. * 获取属性标签
  257. * @return array
  258. */
  259. public function attributeLabels()
  260. {
  261. return [
  262. 'id' => '商品ID',
  263. 'name' => '商品名称',
  264. 'price' => '商品价格',
  265. 'priceType' => '价格类型',
  266. 'flowerNum' => '花朵数量',
  267. 'shopImg' => '商品图片',
  268. 'cover' => '商品封面图',
  269. 'stock' => '商品库存',
  270. 'categoryIdList' => '商品分类',
  271. 'status' => '商品状态',
  272. 'delStatus' => '删除状态',
  273. 'setItemNum' => '商品子项数量',
  274. 'itemList' => '商品子项列表',
  275. ];
  276. }
  277. }