ShopGoodsForm.php 7.5 KB

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