ShopGoodsForm.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Date;
  5. use common\helpers\Form;
  6. use common\libs\logging\operate\AdminOperate;
  7. use common\models\ShopGoods;
  8. use yii\base\Exception;
  9. /**
  10. * Login form
  11. */
  12. class ShopGoodsForm extends Model
  13. {
  14. public $selectedIds;
  15. public $id;
  16. public $goodsName;
  17. public $type;
  18. public $sellDiscount;
  19. public $giftType;
  20. public $sellType;
  21. public $goodsNo;
  22. public $unit;
  23. public $cover;
  24. public $sellPrice;
  25. public $marketPrice;
  26. public $pricePv;
  27. //public $point;
  28. public $storeNums;
  29. public $content;
  30. public $sort;
  31. public $status;
  32. public $categoryType;
  33. private $_model;
  34. public function init() {
  35. parent::init();
  36. $this->adminOperateLogger = new AdminOperate([
  37. 'fetchClass' => ShopGoods::class,
  38. ]);
  39. }
  40. /**
  41. * @inheritdoc
  42. */
  43. public function rules()
  44. {
  45. return [
  46. [['id','sellDiscount','giftType','sellType','goodsNo', 'goodsName', 'unit', 'sellPrice', 'marketPrice', 'pricePv', 'storeNums', 'content', 'sort','status','cover'], 'trim'],
  47. [['goodsName','sellDiscount','giftType','goodsNo', 'storeNums','sellPrice','marketPrice','pricePv', 'sort','status','cover', 'categoryType'], 'required'],
  48. [['id'], 'required', 'on'=>'edit'],
  49. [['id'], 'exist', 'targetClass'=>ShopGoods::class, 'targetAttribute'=>'ID'],
  50. [['sellPrice','marketPrice','pricePv'], 'price'],
  51. [['id'], 'initModel'],
  52. [['selectedIds'], 'isSelected'],
  53. [['sort'], 'isSort'],
  54. [['sellDiscount'], 'isDiscount'],
  55. ];
  56. }
  57. public function attributeLabels()
  58. {
  59. return [
  60. 'selectedIds' => '商品ID',
  61. 'id' => '产品ID',
  62. 'goodsName' => '商品名称',
  63. 'sellDiscount' => '会员折扣',
  64. 'giftType' => '商品类型',
  65. 'categoryType' => '商品分类',
  66. 'sellType' => '出售方式',
  67. 'goodsNo' => '产品编号',
  68. 'unit' => '单位',
  69. 'cover' => '封面',
  70. 'sellPrice' => '销售价格',
  71. 'marketPrice' => '市场价格',
  72. 'pricePv' => '销售PV',
  73. //'point' => '兑换积分',
  74. 'storeNums' => '库存',
  75. 'content' => '产品详情',
  76. 'listOrder' => '排序',
  77. ];
  78. }
  79. /**
  80. * 指定场景
  81. * @return array
  82. */
  83. public function scenarios()
  84. {
  85. $parentScenarios = parent::scenarios();
  86. $customScenarios = [
  87. 'add' => ['goodsName','sellDiscount','giftType', 'sellType','goodsNo','unit','sellPrice','marketPrice','pricePv','storeNums', 'content','sort','cover', 'categoryType'],
  88. 'edit' => ['id','goodsName','sellDiscount','giftType', 'sellType','goodsNo','unit','sellPrice','marketPrice','pricePv', 'storeNums', 'content','sort','cover', 'categoryType'],
  89. 'changeStatus' => ['selectedIds', 'status'],
  90. ];
  91. return array_merge($parentScenarios, $customScenarios);
  92. }
  93. /**
  94. * 初始化model
  95. * @param $attributes
  96. */
  97. public function initModel($attributes) {
  98. $this->_model = ShopGoods::findOne(['ID' => $this->id]);
  99. if (!$this->_model) {
  100. $this->addError($attributes, '数据不存在');
  101. }
  102. }
  103. /**
  104. * 前置数据填充和校验.
  105. * @return bool
  106. */
  107. public function beforeValidate()
  108. {
  109. // 处理sellType
  110. $categoryType = array_column(ShopGoods::CATEGORY_TYPE, NULL, 'id');
  111. $sellType = $categoryType[$this->categoryType]['sell_type'] ?? [];
  112. if (!$sellType) {
  113. $this->addError('add', '商品购买方式错误');
  114. } else {
  115. foreach ($sellType as $item) {
  116. if (!in_array($item['id'], array_keys(ShopGoods::SALE_TYPE))) {
  117. $this->addError('add', '商品购买方式错误');
  118. break;
  119. }
  120. }
  121. // 购买方式格式化为','分割的方式
  122. $this->sellType = implode(',', array_column($sellType, 'id'));
  123. // 如果是普通商品,有PV,旅游、名车、豪宅商品没有PV
  124. if ($this->categoryType != 1) {
  125. $this->pricePv = 0;
  126. }
  127. }
  128. return parent::beforeValidate();
  129. }
  130. /**
  131. * 批量数据
  132. * @param $attributes
  133. */
  134. public function isSelected($attributes) {
  135. if (!$this->selectedIds) {
  136. $this->addError($attributes, '必须选择一条数据');
  137. }
  138. if (!is_array($this->selectedIds)) {
  139. $this->selectedIds = [$this->selectedIds];
  140. }
  141. }
  142. /**
  143. * 排序需大于等于1
  144. * @param $attributes
  145. */
  146. public function isSort($attributes) {
  147. if ($this->sort < 1) {
  148. $this->addError($attributes, '排序请填写大于等于1的数字');
  149. }
  150. }
  151. // 折扣为0-1
  152. public function isDiscount($attributes) {
  153. if ($this->sellDiscount < 0 || $this->sellDiscount > 1 || !is_numeric($this->sellDiscount)) {
  154. $this->addError($attributes, '折扣请填写0-1之间的数');
  155. }
  156. }
  157. /**
  158. * 添加
  159. * @return ShopGoods|null
  160. * @throws \yii\db\Exception
  161. */
  162. public function add() {
  163. if (!$this->validate()) {
  164. return null;
  165. }
  166. $transaction = \Yii::$app->db->beginTransaction();
  167. try {
  168. // 添加商品
  169. $shopGoods = new ShopGoods();
  170. $shopGoods->GOODS_NAME = $this->goodsName;
  171. //$shopGoods->TYPE = $this->type;
  172. $shopGoods->SELL_DISCOUNT = $this->sellDiscount;
  173. $shopGoods->GIFT_TYPE = implode(',',$this->giftType);
  174. // $shopGoods->SELL_TYPE = implode(',',$this->sellType);
  175. $shopGoods->SELL_TYPE = $this->sellType;
  176. $shopGoods->GOODS_NO = $this->goodsNo;
  177. $shopGoods->UNIT = $this->unit ? $this->unit : '个';
  178. $shopGoods->COVER = $this->cover ? $this->cover : '';
  179. $shopGoods->SELL_PRICE = $this->sellPrice;
  180. $shopGoods->MARKET_PRICE = $this->marketPrice;
  181. $shopGoods->PRICE_PV = $this->pricePv;
  182. //$shopGoods->POINT = $this->point;
  183. $shopGoods->CONTENT = $this->content;
  184. $shopGoods->STORE_NUMS = $this->storeNums;
  185. $shopGoods->SORT = $this->sort;
  186. $shopGoods->CATE_ID = '1';
  187. $shopGoods->CREATED_AT = Date::nowTime();
  188. $shopGoods->CATEGORY_TYPE = $this->categoryType;
  189. if (!$shopGoods->save()) {
  190. throw new Exception(Form::formatErrorsForApi($shopGoods->getErrors()));
  191. }
  192. $transaction->commit();
  193. } catch (Exception $e) {
  194. $transaction->rollBack();
  195. $this->addError('add', $e->getMessage());
  196. return null;
  197. }
  198. return $shopGoods;
  199. }
  200. /**
  201. * 编辑商品
  202. * @return null
  203. * @throws \yii\db\Exception
  204. */
  205. public function edit() {
  206. if (!$this->validate()) {
  207. return null;
  208. }
  209. $transaction = \Yii::$app->db->beginTransaction();
  210. try {
  211. $model = $this->_model;
  212. $model->GOODS_NAME = $this->goodsName;
  213. $model->TYPE = 0;
  214. $model->SELL_DISCOUNT = $this->sellDiscount;
  215. $model->GIFT_TYPE = implode(',',$this->giftType);
  216. // $model->SELL_TYPE = implode(',',$this->sellType);
  217. $model->SELL_TYPE = $this->sellType;
  218. $model->GOODS_NO = $this->goodsNo;
  219. $model->UNIT = $this->unit ? $this->unit : '个';
  220. $model->COVER = $this->cover ? $this->cover : '';
  221. $model->SELL_PRICE = $this->sellPrice;
  222. $model->MARKET_PRICE = $this->marketPrice;
  223. $model->PRICE_PV = $this->pricePv;
  224. //$model->POINT = $this->point;
  225. $model->CONTENT = $this->content;
  226. $model->STORE_NUMS = $this->storeNums;
  227. $model->SORT = $this->sort;
  228. $model->UPDATED_AT = Date::nowTime();
  229. $model->CATEGORY_TYPE = $this->categoryType;
  230. if (!$model->save()) {
  231. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  232. }
  233. $transaction->commit();
  234. } catch (Exception $e) {
  235. $transaction->rollBack();
  236. $this->addError('edit', $e->getMessage());
  237. return null;
  238. }
  239. return $model;
  240. }
  241. /**
  242. * 上下架
  243. * @return null|static
  244. * @throws \yii\db\Exception
  245. */
  246. public function changeStatus() {
  247. if (!$this->validate()) {
  248. return null;
  249. }
  250. $db = \Yii::$app->db;
  251. $transaction = $db->beginTransaction();
  252. try {
  253. foreach ($this->selectedIds as $select) {
  254. $oneGoods = ShopGoods::findOne(['ID' => $select]);
  255. //判断状态
  256. if (($msg = ShopGoods::chkAuditStatus($oneGoods->STATUS, $this->status)) != '') {
  257. throw new Exception($msg);
  258. }
  259. $oneGoods->STATUS = $this->status;
  260. $oneGoods->UPDATED_AT = Date::nowTime();
  261. if (!$oneGoods->save()) {
  262. throw new Exception(Form::formatErrorsForApi($oneGoods->getErrors()));
  263. }
  264. }
  265. $transaction->commit();
  266. } catch (Exception $e) {
  267. $transaction->rollBack();
  268. $this->addError('changeStatus', $e->getMessage());
  269. return null;
  270. }
  271. return ['status' => $this->status];
  272. }
  273. }