ShopGoodsForm.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 $categoryType;
  33. public $statusdate;
  34. public $goodsstatusdate;
  35. public $goodsdate;
  36. private $_model;
  37. public function init() {
  38. parent::init();
  39. $this->adminOperateLogger = new AdminOperate([
  40. 'fetchClass' => ShopGoods::class,
  41. ]);
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. public function rules()
  47. {
  48. return [
  49. [['id','sellDiscount','giftType','sellType','goodsNo', 'goodsName', 'unit', 'sellPrice', 'marketPrice', 'pricePv', 'storeNums', 'content', 'sort','status','cover'], 'trim'],
  50. [['goodsName','sellDiscount','giftType','goodsNo', 'storeNums','sellPrice','marketPrice','pricePv', 'sort','status','cover', 'categoryType'], 'required'],
  51. [['id'], 'required', 'on'=>'edit'],
  52. [['id'], 'exist', 'targetClass'=>ShopGoods::class, 'targetAttribute'=>'ID'],
  53. [['sellPrice','marketPrice','pricePv'], 'price'],
  54. [['id'], 'initModel'],
  55. [['selectedIds'], 'isSelected'],
  56. [['sort'], 'isSort'],
  57. [['sellDiscount'], 'isDiscount'],
  58. [['statusdate','goodsstatusdate'],'match','pattern'=>'/^[0-1]{1,1}$/']
  59. ];
  60. }
  61. public function attributeLabels()
  62. {
  63. return [
  64. 'selectedIds' => '商品ID',
  65. 'id' => '产品ID',
  66. 'goodsName' => '商品名称',
  67. 'sellDiscount' => '会员折扣',
  68. 'giftType' => '商品类型',
  69. 'categoryType' => '商品分类',
  70. 'sellType' => '出售方式',
  71. 'goodsNo' => '产品编号',
  72. 'unit' => '单位',
  73. 'cover' => '封面',
  74. 'sellPrice' => '销售价格',
  75. 'marketPrice' => '市场价格',
  76. 'pricePv' => '销售PV',
  77. //'point' => '兑换积分',
  78. 'storeNums' => '库存',
  79. 'content' => '产品详情',
  80. 'listOrder' => '排序',
  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', 'categoryType'],
  92. 'edit' => ['id','goodsName','sellDiscount','giftType', 'sellType','goodsNo','unit','sellPrice','marketPrice','pricePv', 'storeNums', 'content','sort','cover', 'categoryType','statusdate','goodsstatusdate','goodsdate'],
  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. * @return bool
  110. */
  111. public function beforeValidate()
  112. {
  113. if($this->categoryType){
  114. // 处理sellType
  115. $categoryType = array_column(ShopGoods::CATEGORY_TYPE, NULL, 'id');
  116. $sellType = $categoryType[$this->categoryType]['sell_type'] ?? [];
  117. // print_r($formModel);
  118. // print_r($this);
  119. // exit;
  120. if (!$sellType) {
  121. $this->addError('add', '商品购买方式错误');
  122. } else {
  123. foreach ($sellType as $item) {
  124. if (!in_array($item['id'], array_keys(ShopGoods::SALE_TYPE))) {
  125. $this->addError('add', '商品购买方式错误');
  126. break;
  127. }
  128. }
  129. // 购买方式格式化为','分割的方式
  130. $this->sellType = implode(',', array_column($sellType, 'id'));
  131. // 如果是普通商品,有PV,旅游、名车、豪宅商品没有PV
  132. if ($this->categoryType != 1) {
  133. $this->pricePv = 0;
  134. }
  135. }
  136. }
  137. return parent::beforeValidate();
  138. }
  139. /**
  140. * 批量数据
  141. * @param $attributes
  142. */
  143. public function isSelected($attributes) {
  144. if (!$this->selectedIds) {
  145. $this->addError($attributes, '必须选择一条数据');
  146. }
  147. if (!is_array($this->selectedIds)) {
  148. $this->selectedIds = [$this->selectedIds];
  149. }
  150. }
  151. /**
  152. * 排序需大于等于1
  153. * @param $attributes
  154. */
  155. public function isSort($attributes) {
  156. if ($this->sort < 1) {
  157. $this->addError($attributes, '排序请填写大于等于1的数字');
  158. }
  159. }
  160. // 折扣为0-1
  161. public function isDiscount($attributes) {
  162. if ($this->sellDiscount < 0 || $this->sellDiscount > 1 || !is_numeric($this->sellDiscount)) {
  163. $this->addError($attributes, '折扣请填写0-1之间的数');
  164. }
  165. }
  166. /**
  167. * 添加
  168. * @return ShopGoods|null
  169. * @throws \yii\db\Exception
  170. */
  171. public function add() {
  172. if (!$this->validate()) {
  173. return null;
  174. }
  175. $transaction = \Yii::$app->db->beginTransaction();
  176. try {
  177. // 添加商品
  178. $shopGoods = new ShopGoods();
  179. $shopGoods->GOODS_NAME = $this->goodsName;
  180. //$shopGoods->TYPE = $this->type;
  181. $shopGoods->SELL_DISCOUNT = $this->sellDiscount;
  182. $shopGoods->GIFT_TYPE = implode(',',$this->giftType);
  183. // $shopGoods->SELL_TYPE = implode(',',$this->sellType);
  184. $shopGoods->SELL_TYPE = $this->sellType;
  185. $shopGoods->GOODS_NO = $this->goodsNo;
  186. $shopGoods->UNIT = $this->unit ? $this->unit : '个';
  187. $shopGoods->COVER = $this->cover ? $this->cover : '';
  188. $shopGoods->SELL_PRICE = $this->sellPrice;
  189. $shopGoods->MARKET_PRICE = $this->marketPrice;
  190. $shopGoods->PRICE_PV = $this->pricePv;
  191. //$shopGoods->POINT = $this->point;
  192. $shopGoods->CONTENT = $this->content;
  193. $shopGoods->STORE_NUMS = $this->storeNums;
  194. $shopGoods->SORT = $this->sort;
  195. $shopGoods->CATE_ID = '1';
  196. $shopGoods->CREATED_AT = Date::nowTime();
  197. $shopGoods->CATEGORY_TYPE = $this->categoryType;
  198. if (!$shopGoods->save()) {
  199. throw new Exception(Form::formatErrorsForApi($shopGoods->getErrors()));
  200. }
  201. $transaction->commit();
  202. } catch (Exception $e) {
  203. $transaction->rollBack();
  204. $this->addError('add', $e->getMessage());
  205. return null;
  206. }
  207. return $shopGoods;
  208. }
  209. /**
  210. * 编辑商品
  211. * @return null
  212. * @throws \yii\db\Exception
  213. */
  214. public function edit() {
  215. if (!$this->validate()) {
  216. return null;
  217. }
  218. $transaction = \Yii::$app->db->beginTransaction();
  219. try {
  220. $model = $this->_model;
  221. $model->GOODS_NAME = $this->goodsName;
  222. $model->TYPE = 0;
  223. $model->SELL_DISCOUNT = $this->sellDiscount;
  224. $model->GIFT_TYPE = implode(',',$this->giftType);
  225. // $model->SELL_TYPE = implode(',',$this->sellType);
  226. $model->SELL_TYPE = $this->sellType;
  227. $model->GOODS_NO = $this->goodsNo;
  228. $model->UNIT = $this->unit ? $this->unit : '个';
  229. $model->COVER = $this->cover ? $this->cover : '';
  230. $model->SELL_PRICE = $this->sellPrice;
  231. $model->MARKET_PRICE = $this->marketPrice;
  232. $model->PRICE_PV = $this->pricePv;
  233. //$model->POINT = $this->point;
  234. $model->CONTENT = $this->content;
  235. $model->STORE_NUMS = $this->storeNums;
  236. $model->SORT = $this->sort;
  237. $model->UPDATED_AT = Date::nowTime();
  238. $model->CATEGORY_TYPE = $this->categoryType;
  239. $model->STATUS_DATE = $this->statusdate;
  240. $model->GOODS_STATUS_DATE = $this->goodsstatusdate;
  241. $model->GOODS_DATE = $this->goodsdate / 1000;
  242. if (!$model->save()) {
  243. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  244. }
  245. $transaction->commit();
  246. } catch (Exception $e) {
  247. $transaction->rollBack();
  248. $this->addError('edit', $e->getMessage());
  249. return null;
  250. }
  251. return $model;
  252. }
  253. /**
  254. * 上下架
  255. * @return null|static
  256. * @throws \yii\db\Exception
  257. */
  258. public function changeStatus() {
  259. if (!$this->validate()) {
  260. return null;
  261. }
  262. $db = \Yii::$app->db;
  263. $transaction = $db->beginTransaction();
  264. try {
  265. foreach ($this->selectedIds as $select) {
  266. $oneGoods = ShopGoods::findOne(['ID' => $select]);
  267. //判断状态
  268. if (($msg = ShopGoods::chkAuditStatus($oneGoods->STATUS, $this->status)) != '') {
  269. throw new Exception($msg);
  270. }
  271. $oneGoods->STATUS = $this->status;
  272. $oneGoods->UPDATED_AT = Date::nowTime();
  273. if (!$oneGoods->save()) {
  274. throw new Exception(Form::formatErrorsForApi($oneGoods->getErrors()));
  275. }
  276. }
  277. $transaction->commit();
  278. } catch (Exception $e) {
  279. $transaction->rollBack();
  280. $this->addError('changeStatus', $e->getMessage());
  281. return null;
  282. }
  283. return ['status' => $this->status];
  284. }
  285. }