ShopGoodsForm.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 $statusdate;
  33. public $goodsstatusdate;
  34. public $goodsdate;
  35. private $_model;
  36. public function init() {
  37. parent::init();
  38. $this->adminOperateLogger = new AdminOperate([
  39. 'fetchClass' => ShopGoods::class,
  40. ]);
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function rules()
  46. {
  47. return [
  48. [['id','sellDiscount','giftType','sellType','goodsNo', 'goodsName', 'unit', 'sellPrice', 'marketPrice', 'pricePv', 'storeNums', 'content', 'sort','status','cover'], 'trim'],
  49. [['goodsName','sellDiscount','giftType','goodsNo', 'storeNums','sellPrice','marketPrice','pricePv', 'sort','status', 'type'], 'required'],
  50. [['id'], 'required', 'on'=>'edit'],
  51. [['id'], 'exist', 'targetClass'=>ShopGoods::class, 'targetAttribute'=>'ID'],
  52. [['sellPrice','marketPrice','pricePv'], 'price'],
  53. [['id'], 'initModel'],
  54. [['selectedIds'], 'isSelected'],
  55. [['sort'], 'isSort'],
  56. [['sellDiscount'], 'isDiscount'],
  57. [['statusdate','goodsstatusdate'],'match','pattern'=>'/^[0-1]{1,1}$/'],
  58. [['type'], 'isType'],
  59. ];
  60. }
  61. public function attributeLabels()
  62. {
  63. return [
  64. 'selectedIds' => '商品ID',
  65. 'id' => '产品ID',
  66. 'goodsName' => '商品名称',
  67. 'sellDiscount' => '会员折扣',
  68. 'giftType' => '商品类型',
  69. 'sellType' => '出售方式',
  70. 'goodsNo' => '产品编号',
  71. 'unit' => '单位',
  72. 'cover' => '封面',
  73. 'sellPrice' => '销售价格',
  74. 'marketPrice' => '市场价格',
  75. 'pricePv' => '销售PV',
  76. //'point' => '兑换积分',
  77. 'storeNums' => '库存',
  78. 'content' => '产品详情',
  79. 'listOrder' => '排序',
  80. 'type' => '商品来源',
  81. ];
  82. }
  83. /**
  84. * 指定场景
  85. * @return array
  86. */
  87. public function scenarios()
  88. {
  89. $parentScenarios = parent::scenarios();
  90. $customScenarios = [
  91. 'add' => ['goodsName','sellDiscount','giftType', 'sellType','goodsNo','unit','sellPrice','marketPrice','pricePv','storeNums', 'content','sort','cover', 'type'],
  92. 'edit' => ['id','goodsName','sellDiscount','giftType', 'sellType','goodsNo','unit','sellPrice','marketPrice','pricePv', 'storeNums', 'content','sort','cover','statusdate','goodsstatusdate','goodsdate', 'type'],
  93. 'changeStatus' => ['selectedIds', 'status'],
  94. ];
  95. return array_merge($parentScenarios, $customScenarios);
  96. }
  97. /**
  98. * 初始化model
  99. * @param $attributes
  100. */
  101. public function initModel($attributes) {
  102. $this->_model = ShopGoods::findOne(['ID' => $this->id]);
  103. if (!$this->_model) {
  104. $this->addError($attributes, '数据不存在');
  105. }
  106. }
  107. /**
  108. * 批量数据
  109. * @param $attributes
  110. */
  111. public function isSelected($attributes) {
  112. if (!$this->selectedIds) {
  113. $this->addError($attributes, '必须选择一条数据');
  114. }
  115. if (!is_array($this->selectedIds)) {
  116. $this->selectedIds = [$this->selectedIds];
  117. }
  118. }
  119. /**
  120. * 排序需大于等于1
  121. * @param $attributes
  122. */
  123. public function isSort($attributes) {
  124. if ($this->sort < 1) {
  125. $this->addError($attributes, '排序请填写大于等于1的数字');
  126. }
  127. }
  128. // 折扣为0-1
  129. public function isDiscount($attributes) {
  130. if ($this->sellDiscount < 0 || $this->sellDiscount > 1 || !is_numeric($this->sellDiscount)) {
  131. $this->addError($attributes, '折扣请填写0-1之间的数');
  132. }
  133. }
  134. /**
  135. * 根据商品类型处理支付类型
  136. * @param $attributes
  137. */
  138. public function isType() {
  139. $this->sellType = $this->type == '1' ? implode(',', array_keys(ShopGoods::LOCAL_PAY)) : implode(',', array_keys(ShopGoods::ONLINE_PAY));
  140. }
  141. /**
  142. * 添加
  143. * @return ShopGoods|null
  144. * @throws \yii\db\Exception
  145. */
  146. public function add() {
  147. if (!$this->validate()) {
  148. return null;
  149. }
  150. $transaction = \Yii::$app->db->beginTransaction();
  151. try {
  152. // 添加商品
  153. $shopGoods = new ShopGoods();
  154. $shopGoods->GOODS_NAME = $this->goodsName;
  155. //$shopGoods->TYPE = $this->type;
  156. $shopGoods->SELL_DISCOUNT = $this->sellDiscount;
  157. $shopGoods->GIFT_TYPE = implode(',',$this->giftType);
  158. // $shopGoods->SELL_TYPE = implode(',',$this->sellType);
  159. $shopGoods->SELL_TYPE = $this->sellType;
  160. $shopGoods->GOODS_NO = $this->goodsNo;
  161. $shopGoods->UNIT = $this->unit ?: '个';
  162. $shopGoods->COVER = $this->cover ?: '';
  163. $shopGoods->SELL_PRICE = $this->sellPrice;
  164. $shopGoods->MARKET_PRICE = $this->marketPrice;
  165. $shopGoods->PRICE_PV = $this->pricePv;
  166. //$shopGoods->POINT = $this->point;
  167. $shopGoods->CONTENT = $this->content;
  168. $shopGoods->STORE_NUMS = $this->storeNums;
  169. $shopGoods->SORT = $this->sort;
  170. $shopGoods->CATE_ID = $this->type;
  171. $shopGoods->CREATED_AT = Date::nowTime();
  172. if (!$shopGoods->save()) {
  173. throw new Exception(Form::formatErrorsForApi($shopGoods->getErrors()));
  174. }
  175. $transaction->commit();
  176. } catch (Exception $e) {
  177. $transaction->rollBack();
  178. $this->addError('add', $e->getMessage());
  179. return null;
  180. }
  181. return $shopGoods;
  182. }
  183. /**
  184. * 编辑商品
  185. * @return null
  186. * @throws \yii\db\Exception
  187. */
  188. public function edit() {
  189. if (!$this->validate()) {
  190. return null;
  191. }
  192. $transaction = \Yii::$app->db->beginTransaction();
  193. try {
  194. $model = $this->_model;
  195. $model->GOODS_NAME = $this->goodsName;
  196. $model->TYPE = 0;
  197. $model->CATE_ID = $this->type ?? $this->_model->CATE_ID;
  198. $model->SELL_DISCOUNT = $this->sellDiscount;
  199. $model->GIFT_TYPE = implode(',',$this->giftType);
  200. // $model->SELL_TYPE = implode(',',$this->sellType);
  201. $model->SELL_TYPE = $this->sellType;
  202. $model->GOODS_NO = $this->goodsNo;
  203. $model->UNIT = $this->unit ? $this->unit : '个';
  204. $model->COVER = $this->cover ? $this->cover : '';
  205. $model->SELL_PRICE = $this->sellPrice;
  206. $model->MARKET_PRICE = $this->marketPrice;
  207. $model->PRICE_PV = $this->pricePv;
  208. //$model->POINT = $this->point;
  209. $model->CONTENT = $this->content;
  210. $model->STORE_NUMS = $this->storeNums;
  211. $model->SORT = $this->sort;
  212. $model->UPDATED_AT = Date::nowTime();
  213. $model->STATUS_DATE = $this->statusdate;
  214. $model->GOODS_STATUS_DATE = $this->goodsstatusdate;
  215. $model->GOODS_DATE = $this->goodsdate / 1000;
  216. if (!$model->save()) {
  217. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  218. }
  219. $transaction->commit();
  220. } catch (Exception $e) {
  221. $transaction->rollBack();
  222. $this->addError('edit', $e->getMessage());
  223. return null;
  224. }
  225. return $model;
  226. }
  227. /**
  228. * 上下架
  229. * @return null|static
  230. * @throws \yii\db\Exception
  231. */
  232. public function changeStatus() {
  233. if (!$this->validate()) {
  234. return null;
  235. }
  236. $db = \Yii::$app->db;
  237. $transaction = $db->beginTransaction();
  238. try {
  239. foreach ($this->selectedIds as $select) {
  240. $oneGoods = ShopGoods::findOne(['ID' => $select]);
  241. //判断状态
  242. if (($msg = ShopGoods::chkAuditStatus($oneGoods->STATUS, $this->status)) != '') {
  243. throw new Exception($msg);
  244. }
  245. $oneGoods->STATUS = $this->status;
  246. $oneGoods->UPDATED_AT = Date::nowTime();
  247. if (!$oneGoods->save()) {
  248. throw new Exception(Form::formatErrorsForApi($oneGoods->getErrors()));
  249. }
  250. }
  251. $transaction->commit();
  252. } catch (Exception $e) {
  253. $transaction->rollBack();
  254. $this->addError('changeStatus', $e->getMessage());
  255. return null;
  256. }
  257. return ['status' => $this->status];
  258. }
  259. }