ShopGoodsForm.php 8.3 KB

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