ShopGoodsForm.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. [['sellPriceStandard'], 'isPrice'],
  61. ];
  62. }
  63. public function attributeLabels()
  64. {
  65. return [
  66. 'selectedIds' => 'ID',
  67. 'id' => 'Product ID',
  68. 'goodsName' => 'Product Name', // 商品名称
  69. 'sellDiscount' => 'Discount Rate', // 会员折扣
  70. 'giftType' => 'Product Type', // 商品类型
  71. 'categoryType' => 'Product Category', // 商品分类
  72. 'sellType' => 'SellType', // 出售方式
  73. 'pvSplit' => 'PV Split', // pv分期
  74. 'goodsNo' => 'Product Code', // 产品编号
  75. 'unit' => 'Unit', // 单位
  76. 'cover' => 'Cover',// 封面
  77. 'sellPriceStandard' => 'US price',
  78. 'pricePv' => 'BV', // 销售PV
  79. 'storeNums' => 'Inventory', // 库存
  80. 'content' => 'Content',
  81. 'listOrder' => 'Order',// 排序
  82. ];
  83. }
  84. /**
  85. * 指定场景
  86. * @return array
  87. */
  88. public function scenarios()
  89. {
  90. $parentScenarios = parent::scenarios();
  91. $customScenarios = [
  92. 'add' => ['goodsName','sellDiscount','giftType', 'sellType','goodsNo','unit','pricePv','storeNums', 'content','sort','cover', 'categoryType', 'sellPriceStandard', 'taxRate', 'taxAmount','pvSplit', 'nature'],
  93. 'edit' => ['id','goodsName','sellDiscount','giftType', 'sellType','goodsNo','unit','pricePv', 'storeNums', 'content','sort','cover', 'categoryType', 'sellPriceStandard', 'nature','pvSplit'],
  94. 'changeStatus' => ['selectedIds', 'status'],
  95. ];
  96. return array_merge($parentScenarios, $customScenarios);
  97. }
  98. /**
  99. * 初始化model
  100. * @param $attributes
  101. */
  102. public function initModel($attributes) {
  103. $this->_model = ShopGoods::findOne(['ID' => $this->id]);
  104. if (!$this->_model) {
  105. $this->addError($attributes, \Yii::t('ctx', 'dataDoesNotExists')); // 数据不存在
  106. }
  107. }
  108. /**
  109. * 前置数据填充和校验.
  110. * @return bool
  111. */
  112. public function beforeValidate()
  113. {
  114. if($this->categoryType){
  115. // 处理sellType
  116. $categoryType = array_column(ShopGoods::CATEGORY_TYPE, NULL, 'id');
  117. $sellType = $categoryType[$this->categoryType]['sell_type'] ?? [];
  118. if (!$sellType) {
  119. $this->addError('add', \Yii::t('ctx', 'shopBuyActionErrorNotice'));
  120. } else {
  121. foreach ($sellType as $item) {
  122. if (!in_array($item['id'], array_keys(ShopGoods::getSaleType()))) {
  123. $this->addError('add', \Yii::t('ctx', 'shopBuyActionErrorNotice'));
  124. break;
  125. }
  126. }
  127. // 购买方式格式化为','分割的方式
  128. $this->sellType = implode(',', array_column($sellType, 'id'));
  129. // 如果是普通商品,有PV,旅游、名车、豪宅商品没有PV
  130. if ($this->categoryType != 1) {
  131. $this->pricePv = 0;
  132. }
  133. }
  134. }
  135. return parent::beforeValidate();
  136. }
  137. /**
  138. * 批量数据
  139. * @param $attributes
  140. */
  141. public function isSelected($attributes) {
  142. if (!$this->selectedIds) {
  143. $this->addError($attributes, \Yii::t('ctx', 'aPieceMustBeSelected')); // 必须选择一条数据
  144. }
  145. if (!is_array($this->selectedIds)) {
  146. $this->selectedIds = [$this->selectedIds];
  147. }
  148. }
  149. /**
  150. * 排序需大于等于1
  151. * @param $attributes
  152. */
  153. public function isSort($attributes) {
  154. if ($this->sort < 1) {
  155. $this->addError($attributes, \Yii::t('ctx', 'shopSortMoreThanOne'));
  156. }
  157. }
  158. // 折扣为0-1
  159. public function isDiscount($attributes) {
  160. if ($this->sellDiscount < 0 || $this->sellDiscount > 1 || !is_numeric($this->sellDiscount)) {
  161. $this->addError($attributes, \Yii::t('ctx', 'shopDiscountZeroBetweenOne'));
  162. }
  163. }
  164. public function isPrice($attributes)
  165. {
  166. if ($this->sellPriceStandard <= 0) {
  167. $this->addError($attributes, \Yii::t('ctx', 'sellPriceStandardMustGreaterThanZero'));
  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->SELL_DISCOUNT = $this->sellDiscount;
  185. $shopGoods->GIFT_TYPE = implode(',',$this->giftType);
  186. $shopGoods->SELL_TYPE = $this->sellType;
  187. $shopGoods->PV_SPLIT = $this->pvSplit;
  188. $shopGoods->GOODS_NO = $this->goodsNo;
  189. $shopGoods->UNIT = $this->unit ?: '个';
  190. $shopGoods->COVER = $this->cover ?: '';
  191. $shopGoods->SELL_PRICE_STANDARD = $this->sellPriceStandard;
  192. $shopGoods->PRICE_PV = $this->pricePv;
  193. $shopGoods->CONTENT = $this->content;
  194. $shopGoods->STORE_NUMS = $this->storeNums;
  195. $shopGoods->SORT = $this->sort;
  196. $shopGoods->CATE_ID = '1';
  197. $shopGoods->CREATED_AT = Date::nowTime();
  198. $shopGoods->CATEGORY_TYPE = $this->categoryType;
  199. if (!$shopGoods->save()) {
  200. throw new Exception(Form::formatErrorsForApi($shopGoods->getErrors()));
  201. }
  202. // 商品属性
  203. foreach ($this->nature as $item) {
  204. // 国家
  205. $country = Countries::getById($item['ID']);
  206. $shopGoodsNature = new ShopGoodsNature();
  207. $shopGoodsNature->GOODS_ID = $shopGoods->ID;
  208. $shopGoodsNature->COUNTRY_ID = $item['ID'];
  209. $shopGoodsNature->LOCAL_CURRENCY_ID = $country['LOCAL_CURRENCY_ID'];
  210. $shopGoodsNature->SELL_PRICE = round($item['sellPrice']);
  211. $shopGoodsNature->MARKET_PRICE = round($item['marketPrice']);
  212. $shopGoodsNature->TAX_RATE = $item['taxRate'];
  213. if (!$shopGoodsNature->save()) {
  214. $transaction->rollBack();
  215. throw new Exception(Form::formatErrorsForApi($shopGoodsNature->getErrors()));
  216. }
  217. }
  218. $transaction->commit();
  219. } catch (Exception $e) {
  220. $transaction->rollBack();
  221. $this->addError('add', $e->getMessage());
  222. return null;
  223. }
  224. return $shopGoods;
  225. }
  226. /**
  227. * 编辑商品
  228. * @return null
  229. * @throws \yii\db\Exception
  230. */
  231. public function edit() {
  232. if (!$this->validate()) {
  233. return null;
  234. }
  235. $transaction = \Yii::$app->db->beginTransaction();
  236. try {
  237. $model = $this->_model;
  238. $model->GOODS_NAME = $this->goodsName;
  239. $model->TYPE = 0;
  240. $model->SELL_DISCOUNT = $this->sellDiscount;
  241. $model->GIFT_TYPE = implode(',',$this->giftType);
  242. $model->SELL_TYPE = $this->sellType;
  243. $model->PV_SPLIT = $this->pvSplit;
  244. $model->GOODS_NO = $this->goodsNo;
  245. $model->UNIT = $this->unit ?: '个';
  246. $model->COVER = $this->cover ?: '';
  247. $model->SELL_PRICE_STANDARD = $this->sellPriceStandard;
  248. $model->PRICE_PV = $this->pricePv;
  249. $model->CONTENT = $this->content;
  250. $model->STORE_NUMS = $this->storeNums;
  251. $model->SORT = $this->sort;
  252. $model->UPDATED_AT = Date::nowTime();
  253. $model->CATEGORY_TYPE = $this->categoryType;
  254. if (!$model->save()) {
  255. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  256. }
  257. ShopGoodsNature::deleteAll(['GOODS_ID' => $model->ID]);
  258. // 商品属性
  259. foreach ($this->nature as $item) {
  260. $country = Countries::getById($item['ID']);
  261. $shopGoodsNature = new ShopGoodsNature();
  262. $shopGoodsNature->GOODS_ID = $model->ID;
  263. $shopGoodsNature->COUNTRY_ID = $item['ID'];
  264. $shopGoodsNature->LOCAL_CURRENCY_ID = $country['LOCAL_CURRENCY_ID'];
  265. $shopGoodsNature->SELL_PRICE = round($item['sellPrice']);
  266. $shopGoodsNature->MARKET_PRICE = round($item['marketPrice']);
  267. $shopGoodsNature->TAX_RATE = $item['taxRate'];
  268. if (!$shopGoodsNature->save()) {
  269. $transaction->rollBack();
  270. throw new Exception(Form::formatErrorsForApi($shopGoodsNature->getErrors()));
  271. }
  272. }
  273. $transaction->commit();
  274. } catch (Exception $e) {
  275. $transaction->rollBack();
  276. $this->addError('edit', $e->getMessage());
  277. return null;
  278. }
  279. return $model;
  280. }
  281. /**
  282. * 上下架
  283. * @return null|static
  284. * @throws \yii\db\Exception
  285. */
  286. public function changeStatus() {
  287. if (!$this->validate()) {
  288. return null;
  289. }
  290. $db = \Yii::$app->db;
  291. $transaction = $db->beginTransaction();
  292. try {
  293. foreach ($this->selectedIds as $select) {
  294. $oneGoods = ShopGoods::findOne(['ID' => $select]);
  295. //判断状态
  296. if (($msg = ShopGoods::chkAuditStatus($oneGoods->STATUS, $this->status)) != '') {
  297. throw new Exception($msg);
  298. }
  299. $oneGoods->STATUS = $this->status;
  300. $oneGoods->UPDATED_AT = Date::nowTime();
  301. if (!$oneGoods->save()) {
  302. throw new Exception(Form::formatErrorsForApi($oneGoods->getErrors()));
  303. }
  304. }
  305. $transaction->commit();
  306. } catch (Exception $e) {
  307. $transaction->rollBack();
  308. $this->addError('changeStatus', $e->getMessage());
  309. return null;
  310. }
  311. return ['status' => $this->status];
  312. }
  313. /**
  314. * 异步更新商品属性表
  315. * @return bool
  316. */
  317. public function updateAsync($id){
  318. // 汇率配置
  319. $currencyConversions = CurrencyConversions::findOne(['ID' => $id]);
  320. // 商品属性
  321. $shopGoodsNature = ShopGoodsNature::findAll(['LOCAL_CURRENCY_ID' => $currencyConversions->TO_CURRENCY_ID]);
  322. $transaction = \Yii::$app->db->beginTransaction();
  323. try {
  324. foreach ($shopGoodsNature as $goodsNature) {
  325. // 商品
  326. $shopGoods = ShopGoods::findOne(['ID' => $goodsNature->GOODS_ID]);
  327. // 刷新销售价格、市场价格
  328. $goodsNature->SELL_PRICE = $shopGoods->SELL_PRICE_STANDARD * $currencyConversions->PRODUCT_RATE;
  329. $goodsNature->MARKET_PRICE = $shopGoods->SELL_PRICE_STANDARD * $currencyConversions->PRODUCT_RATE * 1.2;
  330. if (!$goodsNature->save()) {
  331. $transaction->rollBack();
  332. throw new Exception(Form::formatErrorsForApi($goodsNature->getErrors()));
  333. }
  334. }
  335. echo "商品更新成功!" . PHP_EOL;
  336. $transaction->commit();
  337. } catch (Exception $e) {
  338. $transaction->rollBack();
  339. $this->addError('update', $e->getMessage());
  340. return false;
  341. }
  342. return true;
  343. }
  344. }