ShopGoodsForm.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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\Countries;
  10. use common\models\CurrencyConversions;
  11. use common\models\ShopGoods;
  12. use common\models\ShopGoodsNature;
  13. use yii\base\Exception;
  14. /**
  15. * Login form
  16. */
  17. class ShopGoodsForm extends Model
  18. {
  19. public $selectedIds;
  20. public $id;
  21. public $goodsName;
  22. public $type;
  23. public $sellDiscount;
  24. public $giftType;
  25. public $sellType;
  26. public $pvSplit;
  27. public $goodsNo;
  28. public $unit;
  29. public $cover;
  30. public $sellPriceStandard;
  31. public $pricePv;
  32. public $storeNums;
  33. public $content;
  34. public $sort;
  35. public $status;
  36. public $categoryType;
  37. public $nature;
  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', 'pricePv', 'storeNums', 'content', 'sort','status','cover'], 'trim'],
  52. [['goodsName','sellDiscount','giftType','goodsNo', 'storeNums','pricePv', 'sort','status', 'categoryType', 'sellPriceStandard', 'nature'], '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. 'sellPriceStandard' => 'US price',
  77. 'pricePv' => 'BV', // 销售PV
  78. 'storeNums' => 'Inventory', // 库存
  79. 'content' => 'Content',
  80. 'listOrder' => 'Order',// 排序
  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','pricePv','storeNums', 'content','sort','cover', 'categoryType', 'sellPriceStandard', 'taxRate', 'taxAmount','pvSplit', 'nature'],
  92. 'edit' => ['id','goodsName','sellDiscount','giftType', 'sellType','goodsNo','unit','pricePv', 'storeNums', 'content','sort','cover', 'categoryType', 'sellPriceStandard', 'nature','pvSplit'],
  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, \Yii::t('ctx', 'dataDoesNotExists')); // 数据不存在
  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. if (!$sellType) {
  118. $this->addError('add', \Yii::t('ctx', 'shopBuyActionErrorNotice'));
  119. } else {
  120. foreach ($sellType as $item) {
  121. if (!in_array($item['id'], array_keys(ShopGoods::getSaleType()))) {
  122. $this->addError('add', \Yii::t('ctx', 'shopBuyActionErrorNotice'));
  123. break;
  124. }
  125. }
  126. // 购买方式格式化为','分割的方式
  127. $this->sellType = implode(',', array_column($sellType, 'id'));
  128. // 如果是普通商品,有PV,旅游、名车、豪宅商品没有PV
  129. if ($this->categoryType != 1) {
  130. $this->pricePv = 0;
  131. }
  132. }
  133. }
  134. return parent::beforeValidate();
  135. }
  136. /**
  137. * 批量数据
  138. * @param $attributes
  139. */
  140. public function isSelected($attributes) {
  141. if (!$this->selectedIds) {
  142. $this->addError($attributes, \Yii::t('ctx', 'aPieceMustBeSelected')); // 必须选择一条数据
  143. }
  144. if (!is_array($this->selectedIds)) {
  145. $this->selectedIds = [$this->selectedIds];
  146. }
  147. }
  148. /**
  149. * 排序需大于等于1
  150. * @param $attributes
  151. */
  152. public function isSort($attributes) {
  153. if ($this->sort < 1) {
  154. $this->addError($attributes, \Yii::t('ctx', 'shopSortMoreThanOne'));
  155. }
  156. }
  157. // 折扣为0-1
  158. public function isDiscount($attributes) {
  159. if ($this->sellDiscount < 0 || $this->sellDiscount > 1 || !is_numeric($this->sellDiscount)) {
  160. $this->addError($attributes, \Yii::t('ctx', 'shopDiscountZeroBetweenOne'));
  161. }
  162. }
  163. /**
  164. * 添加
  165. * @return ShopGoods|null
  166. * @throws \yii\db\Exception
  167. */
  168. public function add() {
  169. if (!$this->validate()) {
  170. return null;
  171. }
  172. $transaction = \Yii::$app->db->beginTransaction();
  173. try {
  174. // 添加商品
  175. $shopGoods = new ShopGoods();
  176. $shopGoods->GOODS_NAME = $this->goodsName;
  177. $shopGoods->SELL_DISCOUNT = $this->sellDiscount;
  178. $shopGoods->GIFT_TYPE = implode(',',$this->giftType);
  179. $shopGoods->SELL_TYPE = $this->sellType;
  180. $shopGoods->PV_SPLIT = $this->pvSplit;
  181. $shopGoods->GOODS_NO = $this->goodsNo;
  182. $shopGoods->UNIT = $this->unit ?: '个';
  183. $shopGoods->COVER = $this->cover ?: '';
  184. $shopGoods->SELL_PRICE_STANDARD = $this->sellPriceStandard;
  185. $shopGoods->PRICE_PV = $this->pricePv;
  186. $shopGoods->CONTENT = $this->content;
  187. $shopGoods->STORE_NUMS = $this->storeNums;
  188. $shopGoods->SORT = $this->sort;
  189. $shopGoods->CATE_ID = '1';
  190. $shopGoods->CREATED_AT = Date::nowTime();
  191. $shopGoods->CATEGORY_TYPE = $this->categoryType;
  192. if (!$shopGoods->save()) {
  193. throw new Exception(Form::formatErrorsForApi($shopGoods->getErrors()));
  194. }
  195. // 商品属性
  196. foreach ($this->nature as $item) {
  197. // 国家
  198. $currency = Countries::getById($item['countryId']);
  199. // 转换美元汇率
  200. $rate = CurrencyConversions::getToUSDRate($currency['LOCAL_CURRENCY_ID']);
  201. $shopGoodsNature = new ShopGoodsNature();
  202. $shopGoodsNature->GOODS_ID = $shopGoods->ID;
  203. $shopGoodsNature->COUNTRY_ID = $item['countryId'];
  204. $shopGoodsNature->LOCAL_CURRENCY_ID = $currency['LOCAL_CURRENCY_ID'];
  205. $shopGoodsNature->MARKET_PRICE = $item['sellPrice'];
  206. $shopGoodsNature->SELL_PRICE = $shopGoods->SELL_PRICE_STANDARD * $rate;
  207. $shopGoodsNature->TAX_RATE = $item['taxRate'];
  208. if (!$shopGoodsNature->save()) {
  209. $transaction->rollBack();
  210. throw new Exception(Form::formatErrorsForApi($shopGoodsNature->getErrors()));
  211. }
  212. }
  213. $transaction->commit();
  214. } catch (Exception $e) {
  215. $transaction->rollBack();
  216. $this->addError('add', $e->getMessage());
  217. return null;
  218. }
  219. return $shopGoods;
  220. }
  221. /**
  222. * 编辑商品
  223. * @return null
  224. * @throws \yii\db\Exception
  225. */
  226. public function edit() {
  227. if (!$this->validate()) {
  228. return null;
  229. }
  230. $transaction = \Yii::$app->db->beginTransaction();
  231. try {
  232. $model = $this->_model;
  233. $model->GOODS_NAME = $this->goodsName;
  234. $model->TYPE = 0;
  235. $model->SELL_DISCOUNT = $this->sellDiscount;
  236. $model->GIFT_TYPE = implode(',',$this->giftType);
  237. $model->SELL_TYPE = $this->sellType;
  238. $model->PV_SPLIT = $this->pvSplit;
  239. $model->GOODS_NO = $this->goodsNo;
  240. $model->UNIT = $this->unit ?: '个';
  241. $model->COVER = $this->cover ?: '';
  242. $model->SELL_PRICE_STANDARD = $this->sellPriceStandard;
  243. $model->PRICE_PV = $this->pricePv;
  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. // 移除属性表中商品属性
  253. ShopGoodsNature::deleteAll(['GOODS_ID' => $model->ID]);
  254. // 商品属性
  255. foreach ($this->nature as $item) {
  256. // 国家
  257. $currency = Countries::getById($item['countryId']);
  258. // 转换美元汇率
  259. $rate = CurrencyConversions::getToUSDRate($currency['LOCAL_CURRENCY_ID']);
  260. $shopGoodsNature = new ShopGoodsNature();
  261. $shopGoodsNature->GOODS_ID = $model->ID;
  262. $shopGoodsNature->COUNTRY_ID = $item['countryId'];
  263. $shopGoodsNature->LOCAL_CURRENCY_ID = $currency['LOCAL_CURRENCY_ID'];
  264. $shopGoodsNature->MARKET_PRICE = $item['sellPrice'];
  265. $shopGoodsNature->SELL_PRICE = $model->SELL_PRICE_STANDARD * $rate;
  266. $shopGoodsNature->TAX_RATE = $item['taxRate'];
  267. if (!$shopGoodsNature->save()) {
  268. $transaction->rollBack();
  269. throw new Exception(Form::formatErrorsForApi($shopGoodsNature->getErrors()));
  270. }
  271. }
  272. $transaction->commit();
  273. } catch (Exception $e) {
  274. $transaction->rollBack();
  275. $this->addError('edit', $e->getMessage());
  276. return null;
  277. }
  278. return $model;
  279. }
  280. /**
  281. * 上下架
  282. * @return null|static
  283. * @throws \yii\db\Exception
  284. */
  285. public function changeStatus() {
  286. if (!$this->validate()) {
  287. return null;
  288. }
  289. $db = \Yii::$app->db;
  290. $transaction = $db->beginTransaction();
  291. try {
  292. foreach ($this->selectedIds as $select) {
  293. $oneGoods = ShopGoods::findOne(['ID' => $select]);
  294. //判断状态
  295. if (($msg = ShopGoods::chkAuditStatus($oneGoods->STATUS, $this->status)) != '') {
  296. throw new Exception($msg);
  297. }
  298. $oneGoods->STATUS = $this->status;
  299. $oneGoods->UPDATED_AT = Date::nowTime();
  300. if (!$oneGoods->save()) {
  301. throw new Exception(Form::formatErrorsForApi($oneGoods->getErrors()));
  302. }
  303. }
  304. $transaction->commit();
  305. } catch (Exception $e) {
  306. $transaction->rollBack();
  307. $this->addError('changeStatus', $e->getMessage());
  308. return null;
  309. }
  310. return ['status' => $this->status];
  311. }
  312. /**
  313. * 异步更新商品属性表
  314. * @return bool
  315. */
  316. public function updateAsync($id){
  317. // 汇率配置
  318. $currencyConversions = CurrencyConversions::findOne(['ID' => $id]);
  319. // 商品属性
  320. $shopGoodsNature = ShopGoodsNature::findAll(['LOCAL_CURRENCY_ID' => $currencyConversions->TO_CURRENCY_ID]);
  321. $transaction = \Yii::$app->db->beginTransaction();
  322. try {
  323. foreach ($shopGoodsNature as $goodsNature) {
  324. // 商品
  325. $shopGoods = ShopGoods::findOne(['ID' => $goodsNature->GOODS_ID]);
  326. // 刷新销售价格
  327. $goodsNature->SELL_PRICE = $shopGoods->SELL_PRICE_STANDARD * $currencyConversions->RATE;
  328. if (!$goodsNature->save()) {
  329. $transaction->rollBack();
  330. throw new Exception(Form::formatErrorsForApi($goodsNature->getErrors()));
  331. }
  332. }
  333. echo "商品更新成功!" . PHP_EOL;
  334. $transaction->commit();
  335. } catch (Exception $e) {
  336. $transaction->rollBack();
  337. $this->addError('update', $e->getMessage());
  338. return false;
  339. }
  340. return true;
  341. }
  342. }