ShopGoodsForm.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. public $autoMaintenance = 0;
  39. private $_model;
  40. public function init() {
  41. parent::init();
  42. $this->adminOperateLogger = new AdminOperate([
  43. 'fetchClass' => ShopGoods::class,
  44. ]);
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. public function rules()
  50. {
  51. return [
  52. [['id','sellDiscount','giftType','sellType','goodsNo', 'goodsName', 'unit', 'pricePv', 'storeNums', 'content', 'sort','status','cover'], 'trim'],
  53. [['goodsName','sellDiscount','giftType','goodsNo', 'storeNums','pricePv', 'sort','status', 'categoryType', 'sellPriceStandard', 'nature'], 'required'],
  54. [['id'], 'required', 'on'=>'edit'],
  55. [['id'], 'exist', 'targetClass'=>ShopGoods::class, 'targetAttribute'=>'ID'],
  56. [['sellPrice','marketPrice','pricePv', 'sellPriceStandard'], 'price'],
  57. [['id'], 'initModel'],
  58. [['selectedIds'], 'isSelected'],
  59. [['sort'], 'isSort'],
  60. [['sellDiscount'], 'isDiscount'],
  61. [['sellPriceStandard'], 'isPrice'],
  62. ];
  63. }
  64. public function attributeLabels()
  65. {
  66. return [
  67. 'selectedIds' => 'ID',
  68. 'id' => 'Product ID',
  69. 'goodsName' => 'Product Name', // 商品名称
  70. 'sellDiscount' => 'Discount Rate', // 会员折扣
  71. 'giftType' => 'Product Type', // 商品类型
  72. 'categoryType' => 'Product Category', // 商品分类
  73. 'sellType' => 'SellType', // 出售方式
  74. 'pvSplit' => 'PV Split', // pv分期
  75. 'goodsNo' => 'Product Code', // 产品编号
  76. 'unit' => 'Unit', // 单位
  77. 'cover' => 'Cover',// 封面
  78. 'sellPriceStandard' => 'US price',
  79. 'pricePv' => 'BV', // 销售PV
  80. 'storeNums' => 'Inventory', // 库存
  81. 'content' => 'Content',
  82. 'listOrder' => 'Order',// 排序
  83. 'autoMaintenance' => 'Auto Maintenance',
  84. ];
  85. }
  86. /**
  87. * 指定场景
  88. * @return array
  89. */
  90. public function scenarios()
  91. {
  92. $parentScenarios = parent::scenarios();
  93. $customScenarios = [
  94. 'add' => ['goodsName','sellDiscount','giftType', 'sellType','goodsNo','unit','pricePv','storeNums', 'content','sort','cover', 'categoryType', 'sellPriceStandard', 'taxRate', 'taxAmount','pvSplit', 'nature', 'autoMaintenance'],
  95. 'edit' => ['id','goodsName','sellDiscount','giftType', 'sellType','goodsNo','unit','pricePv', 'storeNums', 'content','sort','cover', 'categoryType', 'sellPriceStandard', 'nature','pvSplit', 'autoMaintenance'],
  96. 'changeStatus' => ['selectedIds', 'status'],
  97. ];
  98. return array_merge($parentScenarios, $customScenarios);
  99. }
  100. /**
  101. * 初始化model
  102. * @param $attributes
  103. */
  104. public function initModel($attributes) {
  105. $this->_model = ShopGoods::findOne(['ID' => $this->id]);
  106. if (!$this->_model) {
  107. $this->addError($attributes, \Yii::t('ctx', 'dataDoesNotExists')); // 数据不存在
  108. }
  109. }
  110. /**
  111. * 前置数据填充和校验.
  112. * @return bool
  113. */
  114. public function beforeValidate()
  115. {
  116. if($this->categoryType){
  117. // 处理sellType
  118. $categoryType = array_column(ShopGoods::CATEGORY_TYPE, NULL, 'id');
  119. $sellType = $categoryType[$this->categoryType]['sell_type'] ?? [];
  120. if (!$sellType) {
  121. $this->addError('add', \Yii::t('ctx', 'shopBuyActionErrorNotice'));
  122. } else {
  123. foreach ($sellType as $item) {
  124. if (!in_array($item['id'], array_keys(ShopGoods::getSaleType()))) {
  125. $this->addError('add', \Yii::t('ctx', 'shopBuyActionErrorNotice'));
  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, \Yii::t('ctx', 'aPieceMustBeSelected')); // 必须选择一条数据
  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, \Yii::t('ctx', 'shopSortMoreThanOne'));
  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, \Yii::t('ctx', 'shopDiscountZeroBetweenOne'));
  164. }
  165. }
  166. public function isPrice($attributes)
  167. {
  168. if ($this->sellPriceStandard <= 0) {
  169. $this->addError($attributes, \Yii::t('ctx', 'sellPriceStandardMustGreaterThanZero'));
  170. }
  171. }
  172. /**
  173. * 添加
  174. * @return ShopGoods|null
  175. * @throws \yii\db\Exception
  176. */
  177. public function add() {
  178. if (!$this->validate()) {
  179. return null;
  180. }
  181. $transaction = \Yii::$app->db->beginTransaction();
  182. try {
  183. // 添加商品
  184. $shopGoods = new ShopGoods();
  185. $shopGoods->GOODS_NAME = $this->goodsName;
  186. $shopGoods->SELL_DISCOUNT = $this->sellDiscount;
  187. $shopGoods->GIFT_TYPE = implode(',',$this->giftType);
  188. $shopGoods->SELL_TYPE = $this->sellType;
  189. $shopGoods->PV_SPLIT = $this->pvSplit;
  190. $shopGoods->GOODS_NO = $this->goodsNo;
  191. $shopGoods->UNIT = $this->unit ?: '个';
  192. $shopGoods->COVER = $this->cover ?: '';
  193. $shopGoods->SELL_PRICE_STANDARD = $this->sellPriceStandard;
  194. $shopGoods->PRICE_PV = $this->pricePv;
  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. $shopGoods->AUTO_MAINTENANCE = $this->autoMaintenance ?? 0;
  202. if (!$shopGoods->save()) {
  203. throw new Exception(Form::formatErrorsForApi($shopGoods->getErrors()));
  204. }
  205. // 商品属性
  206. foreach ($this->nature as $item) {
  207. // 国家
  208. $country = Countries::getById($item['ID']);
  209. $shopGoodsNature = new ShopGoodsNature();
  210. $shopGoodsNature->GOODS_ID = $shopGoods->ID;
  211. $shopGoodsNature->COUNTRY_ID = $item['ID'];
  212. $shopGoodsNature->LOCAL_CURRENCY_ID = $country['LOCAL_CURRENCY_ID'];
  213. $shopGoodsNature->SELL_PRICE = round($item['sellPrice']);
  214. $shopGoodsNature->MARKET_PRICE = round($item['marketPrice']);
  215. $shopGoodsNature->TAX_RATE = $item['taxRate'];
  216. if (!$shopGoodsNature->save()) {
  217. $transaction->rollBack();
  218. throw new Exception(Form::formatErrorsForApi($shopGoodsNature->getErrors()));
  219. }
  220. }
  221. $transaction->commit();
  222. } catch (Exception $e) {
  223. $transaction->rollBack();
  224. $this->addError('add', $e->getMessage());
  225. return null;
  226. }
  227. return $shopGoods;
  228. }
  229. /**
  230. * 编辑商品
  231. * @return null
  232. * @throws \yii\db\Exception
  233. */
  234. public function edit() {
  235. if (!$this->validate()) {
  236. return null;
  237. }
  238. $transaction = \Yii::$app->db->beginTransaction();
  239. try {
  240. $model = $this->_model;
  241. $model->GOODS_NAME = $this->goodsName;
  242. $model->TYPE = 0;
  243. $model->SELL_DISCOUNT = $this->sellDiscount;
  244. $model->GIFT_TYPE = implode(',',$this->giftType);
  245. $model->SELL_TYPE = $this->sellType;
  246. $model->PV_SPLIT = $this->pvSplit;
  247. $model->GOODS_NO = $this->goodsNo;
  248. $model->UNIT = $this->unit ?: '个';
  249. $model->COVER = $this->cover ?: '';
  250. $model->SELL_PRICE_STANDARD = $this->sellPriceStandard;
  251. $model->PRICE_PV = $this->pricePv;
  252. $model->CONTENT = $this->content;
  253. $model->STORE_NUMS = $this->storeNums;
  254. $model->SORT = $this->sort;
  255. $model->UPDATED_AT = Date::nowTime();
  256. $model->CATEGORY_TYPE = $this->categoryType;
  257. $model->AUTO_MAINTENANCE = $this->autoMaintenance ?? 0;
  258. if (!$model->save()) {
  259. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  260. }
  261. ShopGoodsNature::deleteAll(['GOODS_ID' => $model->ID]);
  262. // 商品属性
  263. foreach ($this->nature as $item) {
  264. $country = Countries::getById($item['ID']);
  265. $shopGoodsNature = new ShopGoodsNature();
  266. $shopGoodsNature->GOODS_ID = $model->ID;
  267. $shopGoodsNature->COUNTRY_ID = $item['ID'];
  268. $shopGoodsNature->LOCAL_CURRENCY_ID = $country['LOCAL_CURRENCY_ID'];
  269. $shopGoodsNature->SELL_PRICE = round($item['sellPrice']);
  270. $shopGoodsNature->MARKET_PRICE = round($item['marketPrice']);
  271. $shopGoodsNature->TAX_RATE = $item['taxRate'];
  272. if (!$shopGoodsNature->save()) {
  273. $transaction->rollBack();
  274. throw new Exception(Form::formatErrorsForApi($shopGoodsNature->getErrors()));
  275. }
  276. }
  277. $transaction->commit();
  278. } catch (Exception $e) {
  279. $transaction->rollBack();
  280. $this->addError('edit', $e->getMessage());
  281. return null;
  282. }
  283. return $model;
  284. }
  285. /**
  286. * 上下架
  287. * @return null|static
  288. * @throws \yii\db\Exception
  289. */
  290. public function changeStatus() {
  291. if (!$this->validate()) {
  292. return null;
  293. }
  294. $db = \Yii::$app->db;
  295. $transaction = $db->beginTransaction();
  296. try {
  297. foreach ($this->selectedIds as $select) {
  298. $oneGoods = ShopGoods::findOne(['ID' => $select]);
  299. //判断状态
  300. if (($msg = ShopGoods::chkAuditStatus($oneGoods->STATUS, $this->status)) != '') {
  301. throw new Exception($msg);
  302. }
  303. $oneGoods->STATUS = $this->status;
  304. $oneGoods->UPDATED_AT = Date::nowTime();
  305. if (!$oneGoods->save()) {
  306. throw new Exception(Form::formatErrorsForApi($oneGoods->getErrors()));
  307. }
  308. }
  309. $transaction->commit();
  310. } catch (Exception $e) {
  311. $transaction->rollBack();
  312. $this->addError('changeStatus', $e->getMessage());
  313. return null;
  314. }
  315. return ['status' => $this->status];
  316. }
  317. /**
  318. * 异步更新商品属性表
  319. * @return bool
  320. */
  321. public function updateAsync($id){
  322. // 汇率配置
  323. $currencyConversions = CurrencyConversions::findOne(['ID' => $id]);
  324. // 商品属性
  325. $shopGoodsNature = ShopGoodsNature::findAll(['LOCAL_CURRENCY_ID' => $currencyConversions->TO_CURRENCY_ID]);
  326. $transaction = \Yii::$app->db->beginTransaction();
  327. try {
  328. foreach ($shopGoodsNature as $goodsNature) {
  329. // 商品
  330. $shopGoods = ShopGoods::findOne(['ID' => $goodsNature->GOODS_ID]);
  331. // 刷新销售价格、市场价格
  332. $goodsNature->SELL_PRICE = $shopGoods->SELL_PRICE_STANDARD * $currencyConversions->PRODUCT_RATE;
  333. $goodsNature->MARKET_PRICE = $shopGoods->SELL_PRICE_STANDARD * $currencyConversions->PRODUCT_RATE * 1.2;
  334. if (!$goodsNature->save()) {
  335. $transaction->rollBack();
  336. throw new Exception(Form::formatErrorsForApi($goodsNature->getErrors()));
  337. }
  338. }
  339. echo "商品更新成功!" . PHP_EOL;
  340. $transaction->commit();
  341. } catch (Exception $e) {
  342. $transaction->rollBack();
  343. $this->addError('update', $e->getMessage());
  344. return false;
  345. }
  346. return true;
  347. }
  348. }