ShopGoodsForm.php 8.6 KB

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