ShopGoodsForm.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. 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. // 汇率
  176. $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0);
  177. $transaction = \Yii::$app->db->beginTransaction();
  178. try {
  179. // 添加商品
  180. $shopGoods = new ShopGoods();
  181. $shopGoods->GOODS_NAME = $this->goodsName;
  182. //$shopGoods->TYPE = $this->type;
  183. $shopGoods->SELL_DISCOUNT = $this->sellDiscount;
  184. $shopGoods->GIFT_TYPE = implode(',',$this->giftType);
  185. // $shopGoods->SELL_TYPE = implode(',',$this->sellType);
  186. $shopGoods->SELL_TYPE = $this->sellType;
  187. $shopGoods->GOODS_NO = $this->goodsNo;
  188. $shopGoods->UNIT = $this->unit ? $this->unit : '个';
  189. $shopGoods->COVER = $this->cover ? $this->cover : '';
  190. $shopGoods->SELL_PRICE_STANDARD = $this->sellPriceStandard;
  191. $shopGoods->SELL_PRICE = $this->sellPrice;
  192. $shopGoods->MARKET_PRICE = $this->marketPrice;
  193. $shopGoods->PRICE_PV = $this->pricePv;
  194. //$shopGoods->POINT = $this->point;
  195. $shopGoods->CONTENT = $this->content;
  196. $shopGoods->STORE_NUMS = $this->storeNums;
  197. $shopGoods->SORT = $this->sort;
  198. $shopGoods->CATE_ID = '1';
  199. $shopGoods->CREATED_AT = Date::nowTime();
  200. $shopGoods->CATEGORY_TYPE = $this->categoryType;
  201. if (!$shopGoods->save()) {
  202. throw new Exception(Form::formatErrorsForApi($shopGoods->getErrors()));
  203. }
  204. $transaction->commit();
  205. } catch (Exception $e) {
  206. $transaction->rollBack();
  207. $this->addError('add', $e->getMessage());
  208. return null;
  209. }
  210. return $shopGoods;
  211. }
  212. /**
  213. * 编辑商品
  214. * @return null
  215. * @throws \yii\db\Exception
  216. */
  217. public function edit() {
  218. if (!$this->validate()) {
  219. return null;
  220. }
  221. // 汇率
  222. $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0);
  223. $transaction = \Yii::$app->db->beginTransaction();
  224. try {
  225. $model = $this->_model;
  226. $model->GOODS_NAME = $this->goodsName;
  227. $model->TYPE = 0;
  228. $model->SELL_DISCOUNT = $this->sellDiscount;
  229. $model->GIFT_TYPE = implode(',',$this->giftType);
  230. // $model->SELL_TYPE = implode(',',$this->sellType);
  231. $model->SELL_TYPE = $this->sellType;
  232. $model->GOODS_NO = $this->goodsNo;
  233. $model->UNIT = $this->unit ? $this->unit : '个';
  234. $model->COVER = $this->cover ? $this->cover : '';
  235. $model->SELL_PRICE_STANDARD = $this->sellPriceStandard;
  236. $model->SELL_PRICE = $this->sellPrice;
  237. $model->MARKET_PRICE = $this->marketPrice;
  238. $model->PRICE_PV = $this->pricePv;
  239. //$model->POINT = $this->point;
  240. $model->CONTENT = $this->content;
  241. $model->STORE_NUMS = $this->storeNums;
  242. $model->SORT = $this->sort;
  243. $model->UPDATED_AT = Date::nowTime();
  244. $model->CATEGORY_TYPE = $this->categoryType;
  245. if (!$model->save()) {
  246. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  247. }
  248. $transaction->commit();
  249. } catch (Exception $e) {
  250. $transaction->rollBack();
  251. $this->addError('edit', $e->getMessage());
  252. return null;
  253. }
  254. return $model;
  255. }
  256. /**
  257. * 上下架
  258. * @return null|static
  259. * @throws \yii\db\Exception
  260. */
  261. public function changeStatus() {
  262. if (!$this->validate()) {
  263. return null;
  264. }
  265. $db = \Yii::$app->db;
  266. $transaction = $db->beginTransaction();
  267. try {
  268. foreach ($this->selectedIds as $select) {
  269. $oneGoods = ShopGoods::findOne(['ID' => $select]);
  270. //判断状态
  271. if (($msg = ShopGoods::chkAuditStatus($oneGoods->STATUS, $this->status)) != '') {
  272. throw new Exception($msg);
  273. }
  274. $oneGoods->STATUS = $this->status;
  275. $oneGoods->UPDATED_AT = Date::nowTime();
  276. if (!$oneGoods->save()) {
  277. throw new Exception(Form::formatErrorsForApi($oneGoods->getErrors()));
  278. }
  279. }
  280. $transaction->commit();
  281. } catch (Exception $e) {
  282. $transaction->rollBack();
  283. $this->addError('changeStatus', $e->getMessage());
  284. return null;
  285. }
  286. return ['status' => $this->status];
  287. }
  288. /**
  289. * 异步更新商品属性表
  290. * @return bool
  291. */
  292. public function updateAsync(){
  293. // 汇率
  294. $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0);
  295. // 商品列表
  296. $goods = ShopGoods::findAll(['STATUS' => 1]);
  297. $transaction = \Yii::$app->db->beginTransaction();
  298. try{
  299. foreach ($goods as $good) {
  300. $model = clone $good;
  301. $model->SELL_PRICE = $good->SELL_PRICE_STANDARD * $exchangeRate;
  302. $model->MARKET_PRICE = $good->SELL_PRICE_STANDARD * $exchangeRate;
  303. $model->UPDATED_AT = Date::nowTime();
  304. if (!$model->save()) {
  305. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  306. }
  307. }
  308. $transaction->commit();
  309. } catch (Exception $e){
  310. $transaction->rollBack();
  311. $this->addError('update', $e->getMessage());
  312. return false;
  313. }
  314. return true;
  315. }
  316. }