ShopGoodsForm.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 $pvSplit;
  24. public $goodsNo;
  25. public $unit;
  26. public $taxRate;
  27. public $cover;
  28. public $sellPrice;
  29. public $sellPriceStandard;
  30. public $marketPrice;
  31. public $pricePv;
  32. //public $point;
  33. public $storeNums;
  34. public $content;
  35. public $sort;
  36. public $status;
  37. public $categoryType;
  38. private $_model;
  39. public function init() {
  40. parent::init();
  41. $this->adminOperateLogger = new AdminOperate([
  42. 'fetchClass' => ShopGoods::class,
  43. ]);
  44. }
  45. /**
  46. * @inheritdoc
  47. */
  48. public function rules()
  49. {
  50. return [
  51. [['id','sellDiscount','giftType','sellType','goodsNo', 'goodsName', 'unit', 'sellPrice', 'marketPrice', 'pricePv', 'storeNums', 'content', 'sort','status','cover'], 'trim'],
  52. [['goodsName','sellDiscount','giftType','goodsNo', 'storeNums','sellPrice','marketPrice','pricePv', 'sort','status', 'categoryType', 'sellPriceStandard'], 'required'],
  53. [['id'], 'required', 'on'=>'edit'],
  54. [['id'], 'exist', 'targetClass'=>ShopGoods::class, 'targetAttribute'=>'ID'],
  55. [['sellPrice','marketPrice','pricePv', 'sellPriceStandard'], 'price'],
  56. [['id'], 'initModel'],
  57. [['selectedIds'], 'isSelected'],
  58. [['sort'], 'isSort'],
  59. [['sellDiscount'], 'isDiscount'],
  60. ];
  61. }
  62. public function attributeLabels()
  63. {
  64. return [
  65. 'selectedIds' => 'ID',
  66. 'id' => 'Product ID',
  67. 'goodsName' => 'Product Name', // 商品名称
  68. 'sellDiscount' => 'Discount Rate', // 会员折扣
  69. 'giftType' => 'Product Type', // 商品类型
  70. 'categoryType' => 'Product Category', // 商品分类
  71. 'sellType' => 'SellType', // 出售方式
  72. 'pvSplit' => 'PV Split', // pv分期
  73. 'goodsNo' => 'Product Code', // 产品编号
  74. 'unit' => 'Unit', // 单位
  75. 'cover' => 'Cover',// 封面
  76. 'sellPrice' => 'Price', // 销售价格
  77. 'sellPriceStandard' => 'US price',
  78. 'marketPrice' => 'Market Price', // 市场价格
  79. 'pricePv' => 'BV', // 销售PV
  80. //'point' => '兑换积分',
  81. 'storeNums' => 'Inventory', // 库存
  82. 'content' => 'Content',
  83. 'listOrder' => 'Order',// 排序
  84. 'taxRate' => 'Tax',// 税率
  85. ];
  86. }
  87. /**
  88. * 指定场景
  89. * @return array
  90. */
  91. public function scenarios()
  92. {
  93. $parentScenarios = parent::scenarios();
  94. $customScenarios = [
  95. 'add' => ['goodsName','sellDiscount','giftType', 'sellType','goodsNo','unit','sellPrice','marketPrice','pricePv','storeNums', 'content','sort','cover', 'categoryType', 'sellPriceStandard', 'taxRate', 'taxAmount','pvSplit'],
  96. 'edit' => ['id','goodsName','sellDiscount','giftType', 'sellType','goodsNo','unit','sellPrice','marketPrice','pricePv', 'storeNums', 'content','sort','cover', 'categoryType', 'sellPriceStandard', 'taxRate','pvSplit'],
  97. 'changeStatus' => ['selectedIds', 'status'],
  98. ];
  99. return array_merge($parentScenarios, $customScenarios);
  100. }
  101. /**
  102. * 初始化model
  103. * @param $attributes
  104. */
  105. public function initModel($attributes) {
  106. $this->_model = ShopGoods::findOne(['ID' => $this->id]);
  107. if (!$this->_model) {
  108. $this->addError($attributes, 'The data does not exist'); // 数据不存在
  109. }
  110. }
  111. /**
  112. * 前置数据填充和校验.
  113. * @return bool
  114. */
  115. public function beforeValidate()
  116. {
  117. if($this->categoryType){
  118. // 处理sellType
  119. $categoryType = array_column(ShopGoods::CATEGORY_TYPE, NULL, 'id');
  120. $sellType = $categoryType[$this->categoryType]['sell_type'] ?? [];
  121. // print_r($formModel);
  122. // print_r($this);
  123. // exit;
  124. if (!$sellType) {
  125. $this->addError('add', '商品购买方式错误');
  126. } else {
  127. foreach ($sellType as $item) {
  128. if (!in_array($item['id'], array_keys(ShopGoods::SALE_TYPE))) {
  129. $this->addError('add', '商品购买方式错误');
  130. break;
  131. }
  132. }
  133. // 购买方式格式化为','分割的方式
  134. $this->sellType = implode(',', array_column($sellType, 'id'));
  135. // 如果是普通商品,有PV,旅游、名车、豪宅商品没有PV
  136. if ($this->categoryType != 1) {
  137. $this->pricePv = 0;
  138. }
  139. }
  140. }
  141. return parent::beforeValidate();
  142. }
  143. /**
  144. * 批量数据
  145. * @param $attributes
  146. */
  147. public function isSelected($attributes) {
  148. if (!$this->selectedIds) {
  149. $this->addError($attributes, 'A piece of data must be selected'); // 必须选择一条数据
  150. }
  151. if (!is_array($this->selectedIds)) {
  152. $this->selectedIds = [$this->selectedIds];
  153. }
  154. }
  155. /**
  156. * 排序需大于等于1
  157. * @param $attributes
  158. */
  159. public function isSort($attributes) {
  160. if ($this->sort < 1) {
  161. $this->addError($attributes, '排序请填写大于等于1的数字');
  162. }
  163. }
  164. // 折扣为0-1
  165. public function isDiscount($attributes) {
  166. if ($this->sellDiscount < 0 || $this->sellDiscount > 1 || !is_numeric($this->sellDiscount)) {
  167. $this->addError($attributes, '折扣请填写0-1之间的数');
  168. }
  169. }
  170. /**
  171. * 添加
  172. * @return ShopGoods|null
  173. * @throws \yii\db\Exception
  174. */
  175. public function add() {
  176. if (!$this->validate()) {
  177. return null;
  178. }
  179. $transaction = \Yii::$app->db->beginTransaction();
  180. try {
  181. // 添加商品
  182. $shopGoods = new ShopGoods();
  183. $shopGoods->GOODS_NAME = $this->goodsName;
  184. //$shopGoods->TYPE = $this->type;
  185. $shopGoods->SELL_DISCOUNT = $this->sellDiscount;
  186. $shopGoods->GIFT_TYPE = implode(',',$this->giftType);
  187. // $shopGoods->SELL_TYPE = implode(',',$this->sellType);
  188. $shopGoods->SELL_TYPE = $this->sellType;
  189. $shopGoods->PV_SPLIT = $this->pvSplit;
  190. $shopGoods->GOODS_NO = $this->goodsNo;
  191. $shopGoods->UNIT = $this->unit ? $this->unit : '个';
  192. $shopGoods->COVER = $this->cover ? $this->cover : '';
  193. $shopGoods->SELL_PRICE_STANDARD = $this->sellPriceStandard;
  194. $shopGoods->SELL_PRICE = $this->sellPrice;
  195. $shopGoods->MARKET_PRICE = $this->marketPrice;
  196. $shopGoods->PRICE_PV = $this->pricePv;
  197. //$shopGoods->POINT = $this->point;
  198. $shopGoods->CONTENT = $this->content;
  199. $shopGoods->STORE_NUMS = $this->storeNums;
  200. $shopGoods->TAX_RATE = $this->taxRate;
  201. $shopGoods->SORT = $this->sort;
  202. $shopGoods->CATE_ID = '1';
  203. $shopGoods->CREATED_AT = Date::nowTime();
  204. $shopGoods->CATEGORY_TYPE = $this->categoryType;
  205. if (!$shopGoods->save()) {
  206. throw new Exception(Form::formatErrorsForApi($shopGoods->getErrors()));
  207. }
  208. $transaction->commit();
  209. } catch (Exception $e) {
  210. $transaction->rollBack();
  211. $this->addError('add', $e->getMessage());
  212. return null;
  213. }
  214. return $shopGoods;
  215. }
  216. /**
  217. * 编辑商品
  218. * @return null
  219. * @throws \yii\db\Exception
  220. */
  221. public function edit() {
  222. if (!$this->validate()) {
  223. return null;
  224. }
  225. $transaction = \Yii::$app->db->beginTransaction();
  226. try {
  227. $model = $this->_model;
  228. $model->GOODS_NAME = $this->goodsName;
  229. $model->TYPE = 0;
  230. $model->SELL_DISCOUNT = $this->sellDiscount;
  231. $model->GIFT_TYPE = implode(',',$this->giftType);
  232. // $model->SELL_TYPE = implode(',',$this->sellType);
  233. $model->SELL_TYPE = $this->sellType;
  234. $model->PV_SPLIT = $this->pvSplit;
  235. $model->GOODS_NO = $this->goodsNo;
  236. $model->UNIT = $this->unit ? $this->unit : '个';
  237. $model->COVER = $this->cover ? $this->cover : '';
  238. $model->SELL_PRICE_STANDARD = $this->sellPriceStandard;
  239. $model->SELL_PRICE = $this->sellPrice;
  240. $model->MARKET_PRICE = $this->marketPrice;
  241. $model->PRICE_PV = $this->pricePv;
  242. $model->TAX_RATE = $this->taxRate;
  243. //$model->POINT = $this->point;
  244. $model->CONTENT = $this->content;
  245. $model->STORE_NUMS = $this->storeNums;
  246. $model->SORT = $this->sort;
  247. $model->UPDATED_AT = Date::nowTime();
  248. $model->CATEGORY_TYPE = $this->categoryType;
  249. if (!$model->save()) {
  250. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  251. }
  252. $transaction->commit();
  253. } catch (Exception $e) {
  254. $transaction->rollBack();
  255. $this->addError('edit', $e->getMessage());
  256. return null;
  257. }
  258. return $model;
  259. }
  260. /**
  261. * 上下架
  262. * @return null|static
  263. * @throws \yii\db\Exception
  264. */
  265. public function changeStatus() {
  266. if (!$this->validate()) {
  267. return null;
  268. }
  269. $db = \Yii::$app->db;
  270. $transaction = $db->beginTransaction();
  271. try {
  272. foreach ($this->selectedIds as $select) {
  273. $oneGoods = ShopGoods::findOne(['ID' => $select]);
  274. //判断状态
  275. if (($msg = ShopGoods::chkAuditStatus($oneGoods->STATUS, $this->status)) != '') {
  276. throw new Exception($msg);
  277. }
  278. $oneGoods->STATUS = $this->status;
  279. $oneGoods->UPDATED_AT = Date::nowTime();
  280. if (!$oneGoods->save()) {
  281. throw new Exception(Form::formatErrorsForApi($oneGoods->getErrors()));
  282. }
  283. }
  284. $transaction->commit();
  285. } catch (Exception $e) {
  286. $transaction->rollBack();
  287. $this->addError('changeStatus', $e->getMessage());
  288. return null;
  289. }
  290. return ['status' => $this->status];
  291. }
  292. /**
  293. * 异步更新商品属性表
  294. * @return bool
  295. */
  296. public function updateAsync(){
  297. // 汇率
  298. $exchangeRate = floatval(Cache::getSystemConfig()['exchangeRate']['VALUE'] ?? 0);
  299. // 商品列表
  300. $goods = ShopGoods::findAll(['STATUS' => 1]);
  301. $transaction = \Yii::$app->db->beginTransaction();
  302. try {
  303. foreach ($goods as $good) {
  304. $content = [
  305. 'id' => $good->ID,
  306. 'exchangeRate' => $exchangeRate,
  307. 'priceStandard' => $good->SELL_PRICE_STANDARD,
  308. 'originSellPrice' => $good->SELL_PRICE,
  309. 'modernSellPrice' => $good->SELL_PRICE_STANDARD * $exchangeRate,
  310. ];
  311. $good->SELL_PRICE = $good->SELL_PRICE_STANDARD * $exchangeRate;
  312. $good->UPDATED_AT = Date::nowTime();
  313. if (!$good->save()) {
  314. throw new Exception(Form::formatErrorsForApi($good->getErrors()));
  315. }
  316. $item = json_encode($content);
  317. echo "商品 {$item} 更新成功!" . PHP_EOL;
  318. }
  319. $transaction->commit();
  320. } catch (Exception $e) {
  321. $transaction->rollBack();
  322. $this->addError('update', $e->getMessage());
  323. return false;
  324. }
  325. return true;
  326. }
  327. }