ShopGoodsForm.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Cache;
  5. use common\helpers\Date;
  6. use common\helpers\Form;
  7. use common\helpers\LoggerTool;
  8. use common\libs\logging\operate\AdminOperate;
  9. use common\models\ShopGoods;
  10. use yii\base\Exception;
  11. /**
  12. * Login form
  13. */
  14. class ShopGoodsForm extends Model
  15. {
  16. public $selectedIds;
  17. public $id;
  18. public $goodsName;
  19. public $type;
  20. public $sellDiscount;
  21. public $giftType;
  22. public $sellType;
  23. public $goodsNo;
  24. public $unit;
  25. public $cover;
  26. public $sellPrice;
  27. public $sellPriceStandard;
  28. public $marketPrice;
  29. public $pricePv;
  30. //public $point;
  31. public $storeNums;
  32. public $content;
  33. public $sort;
  34. public $status;
  35. public $categoryType;
  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', 'categoryType', 'sellPriceStandard'], 'required'],
  51. [['id'], 'required', 'on'=>'edit'],
  52. [['id'], 'exist', 'targetClass'=>ShopGoods::class, 'targetAttribute'=>'ID'],
  53. [['sellPrice','marketPrice','pricePv', 'sellPriceStandard'], 'price'],
  54. [['id'], 'initModel'],
  55. [['selectedIds'], 'isSelected'],
  56. [['sort'], 'isSort'],
  57. [['sellDiscount'], 'isDiscount'],
  58. ];
  59. }
  60. public function attributeLabels()
  61. {
  62. return [
  63. 'selectedIds' => '商品ID',
  64. 'id' => '产品ID',
  65. 'goodsName' => '商品名称',
  66. 'sellDiscount' => '会员折扣',
  67. 'giftType' => '商品类型',
  68. 'categoryType' => '商品分类',
  69. 'sellType' => '出售方式',
  70. 'goodsNo' => '产品编号',
  71. 'unit' => '单位',
  72. 'cover' => '封面',
  73. 'sellPrice' => '销售价格',
  74. 'sellPriceStandard' => '标准价格',
  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', 'sellPriceStandard'],
  92. 'edit' => ['id','goodsName','sellDiscount','giftType', 'sellType','goodsNo','unit','sellPrice','marketPrice','pricePv', 'storeNums', 'content','sort','cover', 'categoryType', 'sellPriceStandard'],
  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. // 处理sellType
  114. $categoryType = array_column(ShopGoods::CATEGORY_TYPE, NULL, 'id');
  115. $sellType = $categoryType[$this->categoryType]['sell_type'] ?? [];
  116. if (!$sellType) {
  117. $this->addError('add', '商品购买方式错误');
  118. } else {
  119. foreach ($sellType as $item) {
  120. if (!in_array($item['id'], array_keys(ShopGoods::SALE_TYPE))) {
  121. $this->addError('add', '商品购买方式错误');
  122. break;
  123. }
  124. }
  125. // 购买方式格式化为','分割的方式
  126. $this->sellType = implode(',', array_column($sellType, 'id'));
  127. // 如果是普通商品,有PV,旅游积分、车房积分商品没有PV
  128. if ($this->categoryType != 1) {
  129. $this->pricePv = 0;
  130. }
  131. }
  132. return parent::beforeValidate();
  133. }
  134. /**
  135. * 批量数据
  136. * @param $attributes
  137. */
  138. public function isSelected($attributes) {
  139. if (!$this->selectedIds) {
  140. $this->addError($attributes, '必须选择一条数据');
  141. }
  142. if (!is_array($this->selectedIds)) {
  143. $this->selectedIds = [$this->selectedIds];
  144. }
  145. }
  146. /**
  147. * 排序需大于等于1
  148. * @param $attributes
  149. */
  150. public function isSort($attributes) {
  151. if ($this->sort < 1) {
  152. $this->addError($attributes, '排序请填写大于等于1的数字');
  153. }
  154. }
  155. // 折扣为0-1
  156. public function isDiscount($attributes) {
  157. if ($this->sellDiscount < 0 || $this->sellDiscount > 1 || !is_numeric($this->sellDiscount)) {
  158. $this->addError($attributes, '折扣请填写0-1之间的数');
  159. }
  160. }
  161. /**
  162. * 添加
  163. * @return ShopGoods|null
  164. * @throws \yii\db\Exception
  165. */
  166. public function add() {
  167. if (!$this->validate()) {
  168. return null;
  169. }
  170. $transaction = \Yii::$app->db->beginTransaction();
  171. try {
  172. // 添加商品
  173. $shopGoods = new ShopGoods();
  174. $shopGoods->GOODS_NAME = $this->goodsName;
  175. //$shopGoods->TYPE = $this->type;
  176. $shopGoods->SELL_DISCOUNT = $this->sellDiscount;
  177. $shopGoods->GIFT_TYPE = implode(',',$this->giftType);
  178. // $shopGoods->SELL_TYPE = implode(',',$this->sellType);
  179. $shopGoods->SELL_TYPE = $this->sellType;
  180. $shopGoods->GOODS_NO = $this->goodsNo;
  181. $shopGoods->UNIT = $this->unit ? $this->unit : '个';
  182. $shopGoods->COVER = $this->cover ? $this->cover : '';
  183. $shopGoods->SELL_PRICE_STANDARD = $this->sellPriceStandard;
  184. $shopGoods->SELL_PRICE = $this->sellPrice;
  185. $shopGoods->MARKET_PRICE = $this->marketPrice;
  186. $shopGoods->PRICE_PV = $this->pricePv;
  187. //$shopGoods->POINT = $this->point;
  188. $shopGoods->CONTENT = $this->content;
  189. $shopGoods->STORE_NUMS = $this->storeNums;
  190. $shopGoods->SORT = $this->sort;
  191. $shopGoods->CATE_ID = '1';
  192. $shopGoods->CREATED_AT = Date::nowTime();
  193. $shopGoods->CATEGORY_TYPE = $this->categoryType;
  194. if (!$shopGoods->save()) {
  195. throw new Exception(Form::formatErrorsForApi($shopGoods->getErrors()));
  196. }
  197. $transaction->commit();
  198. } catch (Exception $e) {
  199. $transaction->rollBack();
  200. $this->addError('add', $e->getMessage());
  201. return null;
  202. }
  203. return $shopGoods;
  204. }
  205. /**
  206. * 编辑商品
  207. * @return null
  208. * @throws \yii\db\Exception
  209. */
  210. public function edit() {
  211. if (!$this->validate()) {
  212. return null;
  213. }
  214. $transaction = \Yii::$app->db->beginTransaction();
  215. try {
  216. $model = $this->_model;
  217. $model->GOODS_NAME = $this->goodsName;
  218. $model->TYPE = 0;
  219. $model->SELL_DISCOUNT = $this->sellDiscount;
  220. $model->GIFT_TYPE = implode(',',$this->giftType);
  221. // $model->SELL_TYPE = implode(',',$this->sellType);
  222. $model->SELL_TYPE = $this->sellType;
  223. $model->GOODS_NO = $this->goodsNo;
  224. $model->UNIT = $this->unit ? $this->unit : '个';
  225. $model->COVER = $this->cover ? $this->cover : '';
  226. $model->SELL_PRICE_STANDARD = $this->sellPriceStandard;
  227. $model->SELL_PRICE = $this->sellPrice;
  228. $model->MARKET_PRICE = $this->marketPrice;
  229. $model->PRICE_PV = $this->pricePv;
  230. //$model->POINT = $this->point;
  231. $model->CONTENT = $this->content;
  232. $model->STORE_NUMS = $this->storeNums;
  233. $model->SORT = $this->sort;
  234. $model->UPDATED_AT = Date::nowTime();
  235. $model->CATEGORY_TYPE = $this->categoryType;
  236. if (!$model->save()) {
  237. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  238. }
  239. $transaction->commit();
  240. } catch (Exception $e) {
  241. $transaction->rollBack();
  242. $this->addError('edit', $e->getMessage());
  243. return null;
  244. }
  245. return $model;
  246. }
  247. /**
  248. * 上下架
  249. * @return null|static
  250. * @throws \yii\db\Exception
  251. */
  252. public function changeStatus() {
  253. if (!$this->validate()) {
  254. return null;
  255. }
  256. $db = \Yii::$app->db;
  257. $transaction = $db->beginTransaction();
  258. try {
  259. foreach ($this->selectedIds as $select) {
  260. $oneGoods = ShopGoods::findOne(['ID' => $select]);
  261. //判断状态
  262. if (($msg = ShopGoods::chkAuditStatus($oneGoods->STATUS, $this->status)) != '') {
  263. throw new Exception($msg);
  264. }
  265. $oneGoods->STATUS = $this->status;
  266. $oneGoods->UPDATED_AT = Date::nowTime();
  267. if (!$oneGoods->save()) {
  268. throw new Exception(Form::formatErrorsForApi($oneGoods->getErrors()));
  269. }
  270. }
  271. $transaction->commit();
  272. } catch (Exception $e) {
  273. $transaction->rollBack();
  274. $this->addError('changeStatus', $e->getMessage());
  275. return null;
  276. }
  277. return ['status' => $this->status];
  278. }
  279. /**
  280. * 异步更新商品属性表
  281. * @return bool
  282. */
  283. public function updateAsync(){
  284. // 汇率
  285. $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0);
  286. // 商品列表
  287. $goods = ShopGoods::findAll(['STATUS' => 1]);
  288. $transaction = \Yii::$app->db->beginTransaction();
  289. try{
  290. foreach ($goods as $good) {
  291. $model = clone $good;
  292. $model->SELL_PRICE = $good->SELL_PRICE_STANDARD * $exchangeRate;
  293. $model->MARKET_PRICE = $good->SELL_PRICE_STANDARD * $exchangeRate;
  294. $model->UPDATED_AT = Date::nowTime();
  295. if (!$model->save()) {
  296. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  297. }
  298. }
  299. $transaction->commit();
  300. } catch (Exception $e){
  301. $transaction->rollBack();
  302. $this->addError('update', $e->getMessage());
  303. return false;
  304. }
  305. return true;
  306. }
  307. }