ShopGoodsForm.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. if($this->categoryType){
  110. // 处理sellType
  111. $categoryType = array_column(ShopGoods::CATEGORY_TYPE, NULL, 'id');
  112. $sellType = $categoryType[$this->categoryType]['sell_type'] ?? [];
  113. // print_r($formModel);
  114. // print_r($this);
  115. // exit;
  116. if (!$sellType) {
  117. $this->addError('add', '商品购买方式错误');
  118. } else {
  119. foreach ($sellType as $item) {
  120. if (!in_array($item['id'], array_keys(ShopGoods::SALE_TYPE))) {
  121. $this->addError('add', '商品购买方式错误');
  122. break;
  123. }
  124. }
  125. // 购买方式格式化为','分割的方式
  126. $this->sellType = implode(',', array_column($sellType, 'id'));
  127. // 如果是普通商品,有PV,旅游、名车、豪宅商品没有PV
  128. if ($this->categoryType != 1) {
  129. $this->pricePv = 0;
  130. }
  131. }
  132. }
  133. return parent::beforeValidate();
  134. }
  135. /**
  136. * 批量数据
  137. * @param $attributes
  138. */
  139. public function isSelected($attributes) {
  140. if (!$this->selectedIds) {
  141. $this->addError($attributes, '必须选择一条数据');
  142. }
  143. if (!is_array($this->selectedIds)) {
  144. $this->selectedIds = [$this->selectedIds];
  145. }
  146. }
  147. /**
  148. * 排序需大于等于1
  149. * @param $attributes
  150. */
  151. public function isSort($attributes) {
  152. if ($this->sort < 1) {
  153. $this->addError($attributes, '排序请填写大于等于1的数字');
  154. }
  155. }
  156. // 折扣为0-1
  157. public function isDiscount($attributes) {
  158. if ($this->sellDiscount < 0 || $this->sellDiscount > 1 || !is_numeric($this->sellDiscount)) {
  159. $this->addError($attributes, '折扣请填写0-1之间的数');
  160. }
  161. }
  162. /**
  163. * 添加
  164. * @return ShopGoods|null
  165. * @throws \yii\db\Exception
  166. */
  167. public function add() {
  168. if (!$this->validate()) {
  169. return null;
  170. }
  171. $transaction = \Yii::$app->db->beginTransaction();
  172. try {
  173. // 添加商品
  174. $shopGoods = new ShopGoods();
  175. $shopGoods->GOODS_NAME = $this->goodsName;
  176. //$shopGoods->TYPE = $this->type;
  177. $shopGoods->SELL_DISCOUNT = $this->sellDiscount;
  178. $shopGoods->GIFT_TYPE = implode(',',$this->giftType);
  179. $shopGoods->SELL_TYPE = implode(',',$this->sellType);
  180. // $shopGoods->SELL_TYPE = $this->sellType;
  181. $shopGoods->GOODS_NO = $this->goodsNo;
  182. $shopGoods->UNIT = $this->unit ? $this->unit : '个';
  183. $shopGoods->COVER = $this->cover ? $this->cover : '';
  184. $shopGoods->SELL_PRICE = $this->sellPrice;
  185. $shopGoods->MARKET_PRICE = $this->marketPrice;
  186. $shopGoods->PRICE_PV = $this->pricePv;
  187. //$shopGoods->POINT = $this->point;
  188. $shopGoods->CONTENT = $this->content;
  189. $shopGoods->STORE_NUMS = $this->storeNums;
  190. $shopGoods->SORT = $this->sort;
  191. $shopGoods->CATE_ID = '1';
  192. $shopGoods->CREATED_AT = Date::nowTime();
  193. $shopGoods->CATEGORY_TYPE = $this->categoryType;
  194. if (!$shopGoods->save()) {
  195. throw new Exception(Form::formatErrorsForApi($shopGoods->getErrors()));
  196. }
  197. $transaction->commit();
  198. } catch (Exception $e) {
  199. $transaction->rollBack();
  200. $this->addError('add', $e->getMessage());
  201. return null;
  202. }
  203. return $shopGoods;
  204. }
  205. /**
  206. * 编辑商品
  207. * @return null
  208. * @throws \yii\db\Exception
  209. */
  210. public function edit() {
  211. if (!$this->validate()) {
  212. return null;
  213. }
  214. $transaction = \Yii::$app->db->beginTransaction();
  215. try {
  216. $model = $this->_model;
  217. $model->GOODS_NAME = $this->goodsName;
  218. $model->TYPE = 0;
  219. $model->SELL_DISCOUNT = $this->sellDiscount;
  220. $model->GIFT_TYPE = implode(',',$this->giftType);
  221. $model->SELL_TYPE = implode(',',$this->sellType);
  222. // $model->SELL_TYPE = $this->sellType;
  223. $model->GOODS_NO = $this->goodsNo;
  224. $model->UNIT = $this->unit ? $this->unit : '个';
  225. $model->COVER = $this->cover ? $this->cover : '';
  226. $model->SELL_PRICE = $this->sellPrice;
  227. $model->MARKET_PRICE = $this->marketPrice;
  228. $model->PRICE_PV = $this->pricePv;
  229. //$model->POINT = $this->point;
  230. $model->CONTENT = $this->content;
  231. $model->STORE_NUMS = $this->storeNums;
  232. $model->SORT = $this->sort;
  233. $model->UPDATED_AT = Date::nowTime();
  234. $model->CATEGORY_TYPE = $this->categoryType;
  235. if (!$model->save()) {
  236. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  237. }
  238. $transaction->commit();
  239. } catch (Exception $e) {
  240. $transaction->rollBack();
  241. $this->addError('edit', $e->getMessage());
  242. return null;
  243. }
  244. return $model;
  245. }
  246. /**
  247. * 上下架
  248. * @return null|static
  249. * @throws \yii\db\Exception
  250. */
  251. public function changeStatus() {
  252. if (!$this->validate()) {
  253. return null;
  254. }
  255. $db = \Yii::$app->db;
  256. $transaction = $db->beginTransaction();
  257. try {
  258. foreach ($this->selectedIds as $select) {
  259. $oneGoods = ShopGoods::findOne(['ID' => $select]);
  260. //判断状态
  261. if (($msg = ShopGoods::chkAuditStatus($oneGoods->STATUS, $this->status)) != '') {
  262. throw new Exception($msg);
  263. }
  264. $oneGoods->STATUS = $this->status;
  265. $oneGoods->UPDATED_AT = Date::nowTime();
  266. if (!$oneGoods->save()) {
  267. throw new Exception(Form::formatErrorsForApi($oneGoods->getErrors()));
  268. }
  269. }
  270. $transaction->commit();
  271. } catch (Exception $e) {
  272. $transaction->rollBack();
  273. $this->addError('changeStatus', $e->getMessage());
  274. return null;
  275. }
  276. return ['status' => $this->status];
  277. }
  278. }