ShopGoodsForm.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. $this->sellType = $this->type == '1' ? implode(',', array_keys(ShopGoods::LOCAL_PAY)) : '5';
  141. }
  142. /**
  143. * 添加
  144. * @return ShopGoods|null
  145. * @throws \yii\db\Exception
  146. */
  147. public function add() {
  148. if (!$this->validate()) {
  149. return null;
  150. }
  151. $transaction = \Yii::$app->db->beginTransaction();
  152. try {
  153. // 添加商品
  154. $shopGoods = new ShopGoods();
  155. $shopGoods->GOODS_NAME = $this->goodsName;
  156. //$shopGoods->TYPE = $this->type;
  157. $shopGoods->SELL_DISCOUNT = $this->sellDiscount;
  158. $shopGoods->GIFT_TYPE = implode(',',$this->giftType);
  159. // $shopGoods->SELL_TYPE = implode(',',$this->sellType);
  160. $shopGoods->SELL_TYPE = $this->sellType;
  161. $shopGoods->GOODS_NO = $this->goodsNo;
  162. $shopGoods->UNIT = $this->unit ?: '个';
  163. $shopGoods->COVER = $this->cover ?: '';
  164. $shopGoods->SELL_PRICE = $this->sellPrice;
  165. $shopGoods->MARKET_PRICE = $this->marketPrice;
  166. $shopGoods->PRICE_PV = $this->pricePv;
  167. //$shopGoods->POINT = $this->point;
  168. $shopGoods->CONTENT = $this->content;
  169. $shopGoods->STORE_NUMS = $this->storeNums;
  170. $shopGoods->SORT = $this->sort;
  171. $shopGoods->CATE_ID = $this->type;
  172. $shopGoods->CREATED_AT = Date::nowTime();
  173. if (!$shopGoods->save()) {
  174. throw new Exception(Form::formatErrorsForApi($shopGoods->getErrors()));
  175. }
  176. $transaction->commit();
  177. } catch (Exception $e) {
  178. $transaction->rollBack();
  179. $this->addError('add', $e->getMessage());
  180. return null;
  181. }
  182. return $shopGoods;
  183. }
  184. /**
  185. * 编辑商品
  186. * @return null
  187. * @throws \yii\db\Exception
  188. */
  189. public function edit() {
  190. if (!$this->validate()) {
  191. return null;
  192. }
  193. $transaction = \Yii::$app->db->beginTransaction();
  194. try {
  195. $model = $this->_model;
  196. $model->GOODS_NAME = $this->goodsName;
  197. $model->TYPE = 0;
  198. $model->CATE_ID = $this->type ?? $this->_model->CATE_ID;
  199. $model->SELL_DISCOUNT = $this->sellDiscount;
  200. $model->GIFT_TYPE = implode(',',$this->giftType);
  201. // $model->SELL_TYPE = implode(',',$this->sellType);
  202. $model->SELL_TYPE = $this->sellType;
  203. $model->GOODS_NO = $this->goodsNo;
  204. $model->UNIT = $this->unit ? $this->unit : '个';
  205. $model->COVER = $this->cover ? $this->cover : '';
  206. $model->SELL_PRICE = $this->sellPrice;
  207. $model->MARKET_PRICE = $this->marketPrice;
  208. $model->PRICE_PV = $this->pricePv;
  209. //$model->POINT = $this->point;
  210. $model->CONTENT = $this->content;
  211. $model->STORE_NUMS = $this->storeNums;
  212. $model->SORT = $this->sort;
  213. $model->UPDATED_AT = Date::nowTime();
  214. $model->STATUS_DATE = $this->statusdate;
  215. $model->GOODS_STATUS_DATE = $this->goodsstatusdate;
  216. $model->GOODS_DATE = $this->goodsdate / 1000;
  217. if (!$model->save()) {
  218. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  219. }
  220. $transaction->commit();
  221. } catch (Exception $e) {
  222. $transaction->rollBack();
  223. $this->addError('edit', $e->getMessage());
  224. return null;
  225. }
  226. return $model;
  227. }
  228. /**
  229. * 上下架
  230. * @return null|static
  231. * @throws \yii\db\Exception
  232. */
  233. public function changeStatus() {
  234. if (!$this->validate()) {
  235. return null;
  236. }
  237. $db = \Yii::$app->db;
  238. $transaction = $db->beginTransaction();
  239. try {
  240. foreach ($this->selectedIds as $select) {
  241. $oneGoods = ShopGoods::findOne(['ID' => $select]);
  242. //判断状态
  243. if (($msg = ShopGoods::chkAuditStatus($oneGoods->STATUS, $this->status)) != '') {
  244. throw new Exception($msg);
  245. }
  246. $oneGoods->STATUS = $this->status;
  247. $oneGoods->UPDATED_AT = Date::nowTime();
  248. if (!$oneGoods->save()) {
  249. throw new Exception(Form::formatErrorsForApi($oneGoods->getErrors()));
  250. }
  251. }
  252. $transaction->commit();
  253. } catch (Exception $e) {
  254. $transaction->rollBack();
  255. $this->addError('changeStatus', $e->getMessage());
  256. return null;
  257. }
  258. return ['status' => $this->status];
  259. }
  260. }