ShopGoodsForm.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 $taxRate;
  26. public $cover;
  27. public $sellPrice;
  28. public $sellPriceStandard;
  29. public $marketPrice;
  30. public $pricePv;
  31. //public $point;
  32. public $storeNums;
  33. public $content;
  34. public $sort;
  35. public $status;
  36. public $categoryType;
  37. private $_model;
  38. public function init() {
  39. parent::init();
  40. $this->adminOperateLogger = new AdminOperate([
  41. 'fetchClass' => ShopGoods::class,
  42. ]);
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. public function rules()
  48. {
  49. return [
  50. [['id','sellDiscount','giftType','sellType','goodsNo', 'goodsName', 'unit', 'sellPrice', 'marketPrice', 'pricePv', 'storeNums', 'content', 'sort','status','cover'], 'trim'],
  51. [['goodsName','sellDiscount','giftType','goodsNo', 'storeNums','sellPrice','marketPrice','pricePv', 'sort','status', 'categoryType', 'sellPriceStandard'], 'required'],
  52. [['id'], 'required', 'on'=>'edit'],
  53. [['id'], 'exist', 'targetClass'=>ShopGoods::class, 'targetAttribute'=>'ID'],
  54. [['sellPrice','marketPrice','pricePv', 'sellPriceStandard'], 'price'],
  55. [['id'], 'initModel'],
  56. [['selectedIds'], 'isSelected'],
  57. [['sort'], 'isSort'],
  58. [['sellDiscount'], 'isDiscount'],
  59. ];
  60. }
  61. public function attributeLabels()
  62. {
  63. return [
  64. 'selectedIds' => 'ID',
  65. 'id' => 'Product ID',
  66. 'goodsName' => 'Product Name', // 商品名称
  67. 'sellDiscount' => 'Discount Rate', // 会员折扣
  68. 'giftType' => 'Product Type', // 商品类型
  69. 'categoryType' => 'Product Category', // 商品分类
  70. 'sellType' => 'SellType', //出售方式
  71. 'goodsNo' => 'Product Code', // 产品编号
  72. 'unit' => 'Unit', // 单位
  73. 'cover' => 'Cover',// 封面
  74. 'sellPrice' => 'Price', // 销售价格
  75. 'sellPriceStandard' => 'US price',
  76. 'marketPrice' => 'Market Price', // 市场价格
  77. 'pricePv' => 'BV', // 销售PV
  78. //'point' => '兑换积分',
  79. 'storeNums' => 'Inventory', // 库存
  80. 'content' => 'Content',
  81. 'listOrder' => 'Order',// 排序
  82. 'taxRate' => 'Tax',// 税率
  83. ];
  84. }
  85. /**
  86. * 指定场景
  87. * @return array
  88. */
  89. public function scenarios()
  90. {
  91. $parentScenarios = parent::scenarios();
  92. $customScenarios = [
  93. 'add' => ['goodsName','sellDiscount','giftType', 'sellType','goodsNo','unit','sellPrice','marketPrice','pricePv','storeNums', 'content','sort','cover', 'categoryType', 'sellPriceStandard', 'taxRate', 'taxAmount'],
  94. 'edit' => ['id','goodsName','sellDiscount','giftType', 'sellType','goodsNo','unit','sellPrice','marketPrice','pricePv', 'storeNums', 'content','sort','cover', 'categoryType', 'sellPriceStandard', 'taxRate'],
  95. 'changeStatus' => ['selectedIds', 'status'],
  96. ];
  97. return array_merge($parentScenarios, $customScenarios);
  98. }
  99. /**
  100. * 初始化model
  101. * @param $attributes
  102. */
  103. public function initModel($attributes) {
  104. $this->_model = ShopGoods::findOne(['ID' => $this->id]);
  105. if (!$this->_model) {
  106. $this->addError($attributes, '数据不存在');
  107. }
  108. }
  109. /**
  110. * 前置数据填充和校验.
  111. * @return bool
  112. */
  113. public function beforeValidate()
  114. {
  115. if($this->categoryType){
  116. // 处理sellType
  117. $categoryType = array_column(ShopGoods::CATEGORY_TYPE, NULL, 'id');
  118. $sellType = $categoryType[$this->categoryType]['sell_type'] ?? [];
  119. // print_r($formModel);
  120. // print_r($this);
  121. // exit;
  122. if (!$sellType) {
  123. $this->addError('add', '商品购买方式错误');
  124. } else {
  125. foreach ($sellType as $item) {
  126. if (!in_array($item['id'], array_keys(ShopGoods::SALE_TYPE))) {
  127. $this->addError('add', '商品购买方式错误');
  128. break;
  129. }
  130. }
  131. // 购买方式格式化为','分割的方式
  132. $this->sellType = implode(',', array_column($sellType, 'id'));
  133. // 如果是普通商品,有PV,旅游、名车、豪宅商品没有PV
  134. if ($this->categoryType != 1) {
  135. $this->pricePv = 0;
  136. }
  137. }
  138. }
  139. return parent::beforeValidate();
  140. }
  141. /**
  142. * 批量数据
  143. * @param $attributes
  144. */
  145. public function isSelected($attributes) {
  146. if (!$this->selectedIds) {
  147. $this->addError($attributes, '必须选择一条数据');
  148. }
  149. if (!is_array($this->selectedIds)) {
  150. $this->selectedIds = [$this->selectedIds];
  151. }
  152. }
  153. /**
  154. * 排序需大于等于1
  155. * @param $attributes
  156. */
  157. public function isSort($attributes) {
  158. if ($this->sort < 1) {
  159. $this->addError($attributes, '排序请填写大于等于1的数字');
  160. }
  161. }
  162. // 折扣为0-1
  163. public function isDiscount($attributes) {
  164. if ($this->sellDiscount < 0 || $this->sellDiscount > 1 || !is_numeric($this->sellDiscount)) {
  165. $this->addError($attributes, '折扣请填写0-1之间的数');
  166. }
  167. }
  168. /**
  169. * 添加
  170. * @return ShopGoods|null
  171. * @throws \yii\db\Exception
  172. */
  173. public function add() {
  174. if (!$this->validate()) {
  175. return null;
  176. }
  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->TAX_RATE = $this->taxRate;
  198. $shopGoods->SORT = $this->sort;
  199. $shopGoods->CATE_ID = '1';
  200. $shopGoods->CREATED_AT = Date::nowTime();
  201. $shopGoods->CATEGORY_TYPE = $this->categoryType;
  202. if (!$shopGoods->save()) {
  203. throw new Exception(Form::formatErrorsForApi($shopGoods->getErrors()));
  204. }
  205. $transaction->commit();
  206. } catch (Exception $e) {
  207. $transaction->rollBack();
  208. $this->addError('add', $e->getMessage());
  209. return null;
  210. }
  211. return $shopGoods;
  212. }
  213. /**
  214. * 编辑商品
  215. * @return null
  216. * @throws \yii\db\Exception
  217. */
  218. public function edit() {
  219. if (!$this->validate()) {
  220. return null;
  221. }
  222. $transaction = \Yii::$app->db->beginTransaction();
  223. try {
  224. $model = $this->_model;
  225. $model->GOODS_NAME = $this->goodsName;
  226. $model->TYPE = 0;
  227. $model->SELL_DISCOUNT = $this->sellDiscount;
  228. $model->GIFT_TYPE = implode(',',$this->giftType);
  229. // $model->SELL_TYPE = implode(',',$this->sellType);
  230. $model->SELL_TYPE = $this->sellType;
  231. $model->GOODS_NO = $this->goodsNo;
  232. $model->UNIT = $this->unit ? $this->unit : '个';
  233. $model->COVER = $this->cover ? $this->cover : '';
  234. $model->SELL_PRICE_STANDARD = $this->sellPriceStandard;
  235. $model->SELL_PRICE = $this->sellPrice;
  236. $model->MARKET_PRICE = $this->marketPrice;
  237. $model->PRICE_PV = $this->pricePv;
  238. $model->TAX_RATE = $this->taxRate;
  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. $content = [
  301. 'id' => $good->ID,
  302. 'exchangeRate' => $exchangeRate,
  303. 'priceStandard' => $good->SELL_PRICE_STANDARD,
  304. 'originSellPrice' => $good->SELL_PRICE,
  305. 'modernSellPrice' => $good->SELL_PRICE_STANDARD * $exchangeRate,
  306. ];
  307. $good->SELL_PRICE = $good->SELL_PRICE_STANDARD * $exchangeRate;
  308. $good->UPDATED_AT = Date::nowTime();
  309. if (!$good->save()) {
  310. throw new Exception(Form::formatErrorsForApi($good->getErrors()));
  311. }
  312. $item = json_encode($content);
  313. echo "商品 {$item} 更新成功!" . PHP_EOL;
  314. }
  315. $transaction->commit();
  316. } catch (Exception $e) {
  317. $transaction->rollBack();
  318. $this->addError('update', $e->getMessage());
  319. return false;
  320. }
  321. return true;
  322. }
  323. }