Master.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\service\goods\source;
  13. use app\common\model\ActivityDiscount;
  14. use app\common\model\Goods as GoodsModel;
  15. use app\common\model\GoodsSku as GoodsSkuModel;
  16. use app\common\model\ShopGoods as ShopGoodsModel;
  17. use app\common\model\ShopGoodsSku as ShopGoodsSkuModel;
  18. use app\common\model\ActivityDiscountGoods as ActivityDiscountGoodsModel;
  19. use app\common\model\fullsend\FullSendGoods as FullSendGoodsModel;
  20. use app\common\model\mj\MjSendGoods as MjSendGoodsModel;
  21. use app\common\model\qc\QcMjSendGoods as QcMjSendGoodsModel;
  22. use app\common\model\kjactivity\KjActivityGood as KjActivityGoodModel;
  23. use app\common\model\groupbuy\GroupBuyGoods as GroupBuyGoodsModel;
  24. use app\common\model\groupbuylb\GroupBuyLbGoods as GroupBuyGoodsLbModel;
  25. use app\common\enum\goods\DeductStockType as DeductStockTypeEnum;
  26. /**
  27. * 商品来源-普通商品扩展类
  28. * Class Master
  29. * @package app\common\service\stock
  30. */
  31. class Master extends Basics
  32. {
  33. /**
  34. * 更新商品库存 (针对下单减库存的商品)
  35. * @param $order
  36. * @return bool
  37. */
  38. public function updateGoodsStock($order)
  39. {
  40. $goodsList = $order['goodsList'];
  41. $deliveryType = $order['delivery']; // 配送方式 10-普通快递 20-门店自提
  42. $shopInfo = $order['shopInfo']??[];
  43. $goodsData = [];
  44. $goodsSkuData = [];
  45. $shopGoodsData = [];
  46. $shopGoodsSkuData = [];
  47. if ($deliveryType == 10) {
  48. foreach ($goodsList as $goods) {
  49. // 是否减库存 (下单减库存)
  50. $isUpdateStockNum = $goods['deduct_stock_type'] == DeductStockTypeEnum::CREATE;
  51. // 商品的数据
  52. $isUpdateStockNum && $goodsData[] = [
  53. 'where' => ['goods_id' => $goods['goods_id']],
  54. 'data' => [
  55. // 递减商品总库存
  56. 'stock_total' => ['dec', $goods['total_num']]
  57. ]
  58. ];
  59. // 商品SKU数据
  60. $isUpdateStockNum && $goodsSkuData[] = [
  61. 'data' => ['stock_num' => ['dec', $goods['total_num']]],
  62. 'where' => [
  63. 'goods_id' => $goods['goods_id'],
  64. 'goods_sku_id' => $goods['goods_sku_id'],
  65. ],
  66. ];
  67. //如果当前商品有满件送赠品
  68. if(isset($goods['mj_send_goods_list'])){
  69. foreach ($goods['mj_send_goods_list'] as $mj_send_goods){
  70. // 商品的数据
  71. $isUpdateStockNum && $goodsData[] = [
  72. 'where' => ['goods_id' => $mj_send_goods['goods_id']],
  73. 'data' => [
  74. // 递减商品总库存
  75. 'stock_total' => ['dec', $mj_send_goods['total_num']]
  76. ]
  77. ];
  78. // 商品SKU数据
  79. $isUpdateStockNum && $goodsSkuData[] = [
  80. 'data' => ['stock_num' => ['dec', $mj_send_goods['total_num']]],
  81. 'where' => [
  82. 'goods_id' => $mj_send_goods['goods_id'],
  83. 'goods_sku_id' => $mj_send_goods['goods_sku_id'],
  84. ],
  85. ];
  86. }
  87. }
  88. }
  89. // 更新商品总库存
  90. !empty($goodsData) && $this->updateGoods($goodsData);
  91. // 更新商品sku库存
  92. !empty($goodsSkuData) && $this->updateGoodsSku($goodsSkuData);
  93. } else {
  94. foreach ($goodsList as $goods) {
  95. // 如果是赠品不减库存 则跳出循环
  96. // if ($goods['goods_type']==20) {
  97. // continue;
  98. // }
  99. // 是否减库存 (下单减库存)
  100. $isUpdateStockNum = $goods['deduct_stock_type'] == DeductStockTypeEnum::CREATE;
  101. // 商品的数据
  102. if ($isUpdateStockNum) {
  103. $goodsData[] = [
  104. 'where' => ['goods_id' => $goods['goods_id']],
  105. 'data' => [
  106. // 递减商品总库存
  107. 'shops_stock_total' => ['dec', $goods['total_num']]
  108. ]
  109. ];
  110. $shopGoodsData[] = [
  111. 'where' => ['goods_id' => $goods['goods_id'], 'shop_id' => $shopInfo['shop_id']],
  112. 'data' => [
  113. // 递减商品总库存
  114. 'stock_total' => ['dec', $goods['total_num']]
  115. ]
  116. ];
  117. }
  118. // 商品SKU数据
  119. $isUpdateStockNum && $shopGoodsSkuData[] = [
  120. 'data' => ['stock_num' => ['dec', $goods['total_num']]],
  121. 'where' => [
  122. 'goods_id' => $goods['goods_id'],
  123. 'goods_sku_id' => $goods['goods_sku_id'],
  124. 'shop_id' => $shopInfo['shop_id']
  125. ],
  126. ];
  127. //如果当前商品有满件送赠品
  128. if(isset($goods['mj_send_goods_list'])){
  129. foreach ($goods['mj_send_goods_list'] as $mj_send_goods){
  130. // 商品的数据
  131. if ($isUpdateStockNum) {
  132. $goodsData[] = [
  133. 'where' => ['goods_id' => $mj_send_goods['goods_id']],
  134. 'data' => [
  135. // 递减商品总库存
  136. 'shops_stock_total' => ['dec', $mj_send_goods['total_num']]
  137. ]
  138. ];
  139. $shopGoodsData[] = [
  140. 'where' => ['goods_id' => $mj_send_goods['goods_id'], 'shop_id' => $shopInfo['shop_id']],
  141. 'data' => [
  142. // 递减商品总库存
  143. 'stock_total' => ['dec', $mj_send_goods['total_num']]
  144. ]
  145. ];
  146. $shopGoodsSkuData[] = [
  147. 'data' => ['stock_num' => ['dec', $mj_send_goods['total_num']]],
  148. 'where' => [
  149. 'goods_id' => $mj_send_goods['goods_id'],
  150. 'goods_sku_id' => $mj_send_goods['goods_sku_id'],
  151. 'shop_id' => $shopInfo['shop_id']
  152. ],
  153. ];
  154. }
  155. }
  156. }
  157. }
  158. // 更新商品的门店总库存
  159. !empty($goodsData) && $this->updateGoods($goodsData);
  160. // 更新门店的商品总库存
  161. !empty($shopGoodsData) && $this->updateShopGoods($shopGoodsData);
  162. // 更新门店的商品sku库存
  163. !empty($shopGoodsSkuData) && $this->updateShopGoodsSku($shopGoodsSkuData);
  164. }
  165. return true;
  166. }
  167. /**
  168. * 更新商品库存销量(订单付款后)
  169. * @param $order
  170. * @return bool
  171. */
  172. public function updateStockSales($order)
  173. {
  174. $goodsList = $order['goods'];
  175. $deliveryType = $order['delivery_type'];
  176. $goodsData = [];
  177. $goodsSkuData = [];
  178. $shopGoodsData = [];
  179. $shopGoodsSkuData = [];
  180. if ($deliveryType == 10) {
  181. foreach ($goodsList as $goods) {
  182. // 是否减库存 (付款减库存)
  183. $isUpdateStockNum = $goods['deduct_stock_type'] == DeductStockTypeEnum::PAYMENT;
  184. // 商品的数据
  185. $goodsDataItm = [
  186. 'where' => ['goods_id' => $goods['goods_id']],
  187. 'data' => [
  188. // 累计商品实际销量
  189. 'sales_actual' => ['inc', $goods['total_num']]
  190. ]
  191. ];
  192. // 递减商品总库存
  193. $isUpdateStockNum && $goodsDataItm['data']['stock_total'] = ['dec', $goods['total_num']];
  194. $goodsData[] = $goodsDataItm;
  195. // 付款减库存
  196. $isUpdateStockNum && $goodsSkuData[] = [
  197. 'where' => [
  198. 'goods_id' => $goods['goods_id'],
  199. 'goods_sku_id' => $goods['goods_sku_id'],
  200. ],
  201. 'data' => ['stock_num' => ['dec', $goods['total_num']]],
  202. ];
  203. }
  204. // 更新商品信息
  205. !empty($goodsData) && $this->updateGoods($goodsData);
  206. // 更新商品sku信息
  207. !empty($goodsSkuData) && $this->updateGoodsSku($goodsSkuData);
  208. } else { // 门店自提
  209. foreach ($goodsList as $goods) {
  210. // 如果是赠品不加库存 则跳出循环
  211. // if ($goods['goods_type']==20) {
  212. // continue;
  213. // }
  214. // 是否减库存 (付款减库存)
  215. $isUpdateStockNum = $goods['deduct_stock_type'] == DeductStockTypeEnum::PAYMENT;
  216. // 商品的数据
  217. $goodsDataItm = [
  218. 'where' => ['goods_id' => $goods['goods_id']],
  219. 'data' => [
  220. // 累计商品门店销量
  221. 'sales_shops' => ['inc', $goods['total_num']]
  222. ]
  223. ];
  224. $shopGoodsDataItm = [
  225. 'where' => ['goods_id' => $goods['goods_id'], 'shop_id' => $order['shop_id']],
  226. 'data' => [
  227. // 累计商品门店销量
  228. 'sale_num' => ['inc', $goods['total_num']]
  229. ]
  230. ];
  231. // 递减商品总库存
  232. if ($isUpdateStockNum) {
  233. $shopGoodsDataItm['data']['shops_stock_total'] = ['dec', $goods['total_num']];
  234. }
  235. $goodsData[] = $goodsDataItm;
  236. $shopGoodsData[] = $shopGoodsDataItm;
  237. // 付款减库存
  238. $isUpdateStockNum && $shopGoodsSkuData[] = [
  239. 'where' => [
  240. 'goods_id' => $goods['goods_id'],
  241. 'goods_sku_id' => $goods['goods_sku_id'],
  242. 'shop_id' => $order['shop_id']
  243. ],
  244. 'data' => ['stock_num' => ['dec', $goods['total_num']]],
  245. ];
  246. }
  247. // 更新商品信息
  248. !empty($goodsData) && $this->updateGoods($goodsData);
  249. // 更新门店商品信息
  250. !empty($shopGoodsData) && $this->updateShopGoods($shopGoodsData);
  251. // 更新门店商品sku信息
  252. !empty($shopGoodsSkuData) && $this->updateShopGoodsSku($shopGoodsSkuData);
  253. }
  254. return true;
  255. }
  256. /**
  257. * 回退商品库存事件 (用于取消订单时调用)
  258. * @param mixed $order
  259. * @param bool $isPayOrder 是否为已支付订单
  260. * @return bool|mixed
  261. */
  262. public function backGoodsStock($order, bool $isPayOrder = false)
  263. {
  264. $goodsList = $order['goods'];
  265. $deliveryType = $order['delivery_type'];
  266. $goodsData = [];
  267. $goodsSkuData = [];
  268. $shopGoodsData = [];
  269. $shopGoodsSkuData = [];
  270. if ($deliveryType == 10) {
  271. foreach ($goodsList as $goods) {
  272. // 验证商品规格是否存在 不存在 则跳出循环
  273. if (!$this->validateGoodsSkuExist($goods)) {
  274. continue;
  275. }
  276. // 更新条件: 订单已经支付或者订单商品为 "下单减库存"
  277. if ($isPayOrder == true || $goods['deduct_stock_type'] == DeductStockTypeEnum::CREATE) {
  278. $goodsData[] = [
  279. 'where' => ['goods_id' => $goods['goods_id']],
  280. 'data' => ['stock_total' => ['inc', $goods['total_num']]]
  281. ];
  282. $goodsSkuData[] = [
  283. 'where' => [
  284. 'goods_id' => $goods['goods_id'],
  285. 'goods_sku_id' => $goods['goods_sku_id'],
  286. ],
  287. 'data' => ['stock_num' => ['inc', $goods['total_num']]],
  288. ];
  289. }
  290. }
  291. // 更新商品总库存
  292. !empty($goodsData) && $this->updateGoods($goodsData);
  293. // 更新商品sku库存
  294. !empty($goodsSkuData) && $this->updateGoodsSku($goodsSkuData);
  295. } else { // 门店自提
  296. foreach ($goodsList as $goods) {
  297. // 验证商品规格是否存在 不存在 则跳出循环
  298. if (!$this->validateGoodsSkuExist($goods)) {//如果是赠品不加库存
  299. continue;
  300. }
  301. // if (!$this->validateGoodsSkuExist($goods) || $goods['goods_type']==20) {//如果是赠品不加库存
  302. // continue;
  303. // }
  304. // 更新条件: 订单已经支付或者订单商品为 "下单减库存"
  305. if ($isPayOrder == true || $goods['deduct_stock_type'] == DeductStockTypeEnum::CREATE) {
  306. $goodsData[] = [
  307. 'where' => ['goods_id' => $goods['goods_id']],
  308. 'data' => ['shops_stock_total' => ['inc', $goods['total_num']]]
  309. ];
  310. $shopGoodsData[] = [
  311. 'where' => ['goods_id' => $goods['goods_id'], 'shop_id' => $order['shop_id']],
  312. 'data' => ['stock_total' => ['inc', $goods['total_num']]]
  313. ];
  314. $shopGoodsSkuData[] = [
  315. 'where' => [
  316. 'goods_id' => $goods['goods_id'],
  317. 'goods_sku_id' => $goods['goods_sku_id'],
  318. 'shop_id' => $order['shop_id']
  319. ],
  320. 'data' => ['stock_num' => ['inc', $goods['total_num']]],
  321. ];
  322. }
  323. }
  324. // 更新商品总库存
  325. !empty($goodsData) && $this->updateGoods($goodsData);
  326. // 更新门店商品总库存
  327. !empty($shopGoodsData) && $this->updateShopGoods($shopGoodsData);
  328. // 更新门店商品sku库存
  329. !empty($shopGoodsSkuData) && $this->updateShopGoodsSku($shopGoodsSkuData);
  330. }
  331. return true;
  332. }
  333. // 验证商品规格是否存在(后台删除规格 则不回退库存)
  334. public function validateGoodsSkuExist($goods)
  335. {
  336. return GoodsSkuModel::detail($goods['goods_id'], $goods['goods_sku_id']);
  337. }
  338. // 验证商品规格是否存在(后台删除规格 则不回退库存)
  339. public function validateGoodsSkuExistOne($goods_id,$goods_sku_id)
  340. {
  341. return GoodsSkuModel::where('goods_id',$goods_id)->where('goods_sku_id',$goods_sku_id)->find();
  342. }
  343. /**
  344. * 回退商品库存事件 (用于售后订单时调用,针对单个商品)
  345. * @param mixed $order
  346. * @param bool $isPayOrder 是否为已支付订单
  347. * @return bool|mixed
  348. */
  349. public function backGoodsSkuStock($order,$goods_id=0,$goods_sku_id =0,$total_num =0){
  350. if(empty($order)||empty($goods_id)||empty($goods_sku_id)||empty($total_num)){
  351. return false;
  352. }
  353. if(!$this->validateGoodsSkuExistOne($goods_id,$goods_sku_id)){
  354. return false;
  355. }
  356. $goodsList = $order['goods'];
  357. $deliveryType = $order['delivery_type'];
  358. $goodsData = [];
  359. $goodsSkuData = [];
  360. $shopGoodsData = [];
  361. $shopGoodsSkuData = [];
  362. if ($deliveryType == 10) {
  363. // 更新条件: 订单已经支付或者订单商品为 "下单减库存"
  364. // if ($isPayOrder == true || $goods['deduct_stock_type'] == DeductStockTypeEnum::CREATE) {
  365. $goodsData[] = [
  366. 'where' => ['goods_id' => $goods_id],
  367. 'data' => ['stock_total' => ['inc', $total_num]]
  368. ];
  369. $goodsSkuData[] = [
  370. 'where' => [
  371. 'goods_id' => $goods_id,
  372. 'goods_sku_id' => $goods_sku_id,
  373. ],
  374. 'data' => ['stock_num' => ['inc', $total_num]],
  375. ];
  376. // }
  377. // 更新商品总库存
  378. !empty($goodsData) && $this->updateGoods($goodsData);
  379. // 更新商品sku库存
  380. !empty($goodsSkuData) && $this->updateGoodsSku($goodsSkuData);
  381. } else { // 门店自提
  382. // 更新条件: 订单已经支付或者订单商品为 "下单减库存"
  383. // if ($isPayOrder == true || $goods['deduct_stock_type'] == DeductStockTypeEnum::CREATE) {
  384. $goodsData[] = [
  385. 'where' => ['goods_id' => $goods_id],
  386. 'data' => ['shops_stock_total' => ['inc', $total_num]]
  387. ];
  388. $shopGoodsData[] = [
  389. 'where' => ['goods_id' => $goods_id, 'shop_id' => $order['shop_id']],
  390. 'data' => ['stock_total' => ['inc', $total_num]]
  391. ];
  392. $shopGoodsSkuData[] = [
  393. 'where' => [
  394. 'goods_id' => $goods_id,
  395. 'goods_sku_id' => $goods_sku_id,
  396. 'shop_id' => $order['shop_id']
  397. ],
  398. 'data' => ['stock_num' => ['inc', $total_num]],
  399. ];
  400. // }
  401. // 更新商品总库存
  402. !empty($goodsData) && $this->updateGoods($goodsData);
  403. // 更新门店商品总库存
  404. !empty($shopGoodsData) && $this->updateShopGoods($shopGoodsData);
  405. // 更新门店商品sku库存
  406. !empty($shopGoodsSkuData) && $this->updateShopGoodsSku($shopGoodsSkuData);
  407. }
  408. return true;
  409. }
  410. /**
  411. * 更新商品销量
  412. * @param $order
  413. * @return bool
  414. */
  415. public function updateStockSalesExpress($goods_id,$total_num,$is_inc=true){
  416. $goodsData = [];
  417. // 商品的数据
  418. //是+还是-
  419. if($is_inc){
  420. $value = 'inc';
  421. }else{
  422. $value = 'dec';
  423. }
  424. $goodsDataItm = [
  425. 'where' => ['goods_id' => $goods_id],
  426. 'data' => [
  427. // 累计商品实际销量
  428. 'sales_actual' => [$value,$total_num]
  429. ]
  430. ];
  431. $goodsData[] = $goodsDataItm;
  432. // 更新商品信息
  433. !empty($goodsData) && $this->updateGoods($goodsData);
  434. return true;
  435. }
  436. /**
  437. * 回退商品库存事件 (用于取消订单时调用,针对单个商品)
  438. * @param mixed $order
  439. * @param bool $isPayOrder 是否为已支付订单
  440. * @return bool|mixed
  441. */
  442. public function backGoodsSkuStockExpress($goods_id=0,$goods_sku_id =0,$total_num =0){
  443. if(empty($goods_id)||empty($total_num)){
  444. var_dump(1);
  445. return false;
  446. }
  447. if(!$this->validateGoodsSkuExistOne($goods_id,$goods_sku_id)){
  448. var_dump(2);
  449. return false;
  450. }
  451. $goodsData = [];
  452. $goodsSkuData = [];
  453. $goodsData[] = [
  454. 'where' => ['goods_id' => $goods_id],
  455. 'data' => ['stock_total' => ['inc', $total_num]]
  456. ];
  457. $goodsSkuData[] = [
  458. 'where' => [
  459. 'goods_id' => $goods_id,
  460. 'goods_sku_id' => $goods_sku_id,
  461. ],
  462. 'data' => ['stock_num' => ['inc', $total_num]],
  463. ];
  464. // 更新商品总库存
  465. !empty($goodsData) && $this->updateGoods($goodsData);
  466. // 更新商品sku库存
  467. !empty($goodsSkuData) && $this->updateGoodsSku($goodsSkuData);
  468. return true;
  469. }
  470. /**
  471. * 更新商品信息
  472. * @param array $data
  473. * @return array|false
  474. */
  475. private function updateGoods(array $data)
  476. {
  477. return (new GoodsModel)->updateAll($data);
  478. }
  479. /**
  480. * 更新商品sku信息
  481. * @param array $data
  482. * @return array|false
  483. */
  484. private function updateGoodsSku(array $data)
  485. {
  486. return (new GoodsSkuModel)->updateAll($data);
  487. }
  488. /**
  489. * 更新门店商品信息
  490. * @param array $data
  491. * @return array|false
  492. */
  493. private function updateShopGoods(array $data)
  494. {
  495. return (new ShopGoodsModel)->updateAll($data);
  496. }
  497. /**
  498. * 更新门店商品sku信息
  499. * @param array $data
  500. * @return array|false
  501. */
  502. private function updateShopGoodsSku(array $data)
  503. {
  504. return (new ShopGoodsSkuModel)->updateAll($data);
  505. }
  506. /**
  507. * 确认下单更改多个活动的活动库存
  508. * @param array $data
  509. * @return array|false
  510. */
  511. public function updateActivityStock($order){
  512. $goodsList = $order['goodsList'];
  513. // $deliveryType = $order['delivery']; // 配送方式 10-普通快递 20-门店自提
  514. // $shopInfo = $order['shopInfo']??[];
  515. foreach ($goodsList as $goods) {
  516. //N件X折
  517. $goods['is_activity_discount'] && $activityDiscount[] = [
  518. 'where' => [
  519. 'activity_id' => $goods['activity_discount_id'],
  520. 'goods_id' => $goods['goods_id']
  521. ],
  522. 'data' => [
  523. // 递减商品总库存
  524. 'purchase_stock' => ['inc', $goods['activity_discount_count']]
  525. ]
  526. ];
  527. //满就送
  528. $goods['is_full_send'] && $activityFullSend[] = [
  529. 'where' => [
  530. 'full_send_activity_id' => $goods['full_send_activity_id'],
  531. 'goods_id' => $goods['goods_id']
  532. ],
  533. 'data' => [
  534. // 递减商品总库存
  535. 'purchase_stock' => ['inc', $goods['full_send_count']]
  536. ]
  537. ];
  538. //全场满件送
  539. $goods['is_qc_send_send'] && $activityQcMjSend[] = [
  540. 'where' => [
  541. 'qc_mj_send_activity_id' => $goods['qc_send_activity_id'],
  542. 'goods_id' => $goods['goods_id']
  543. ],
  544. 'data' => [
  545. // 递减商品总库存
  546. 'purchase_stock' => ['inc', $goods['qc_send_use_count']]
  547. ]
  548. ];
  549. //满件送
  550. $goods['is_mj_send'] && $activityMjSend[] = [
  551. 'where' => [
  552. 'mj_send_activity_id' => $goods['mj_send_activity_id'],
  553. 'goods_id' => $goods['goods_id']
  554. ],
  555. 'data' => [
  556. // 递减商品总库存
  557. 'purchase_stock' => ['inc', $goods['mj_send_use_count']]
  558. ]
  559. ];
  560. //砍价
  561. $goods['bargain_activity_id'] && $kjActivityGood[] = [
  562. 'where' => [
  563. 'activity_id' => $goods['bargain_activity_id'],
  564. 'goods_id' => $goods['goods_id']
  565. ],
  566. 'data' => [
  567. // 递减商品总库存
  568. 'purchase_stock' => ['inc', 1]
  569. ]
  570. ];
  571. //拼团
  572. $goods['group_buy_id'] && $groupBuyGood[] = [
  573. 'where' => [
  574. 'group_buy_activity_id' => $goods['group_buy_id'],
  575. 'goods_id' => $goods['goods_id']
  576. ],
  577. 'data' => [
  578. // 递减商品总库存
  579. 'purchase_stock' => ['inc', $goods['total_num']]//1
  580. ]
  581. ];
  582. //拼团裂变
  583. $goods['group_buy_lb_id'] && $groupBuyLbGood[] = [
  584. 'where' => [
  585. 'group_buy_lb_activity_id' => $goods['group_buy_lb_id'],
  586. 'goods_id' => $goods['goods_id']
  587. ],
  588. 'data' => [
  589. // 递减商品总库存
  590. 'purchase_stock' => ['inc', $goods['total_num']]//1
  591. ]
  592. ];
  593. }
  594. // 更新N件X折活动库存
  595. !empty($activityDiscount) && $this->updateDiscount($activityDiscount);
  596. // 更新满就送活动库存
  597. !empty($activityFullSend) && $this->updateFullSend($activityFullSend);
  598. // 更新全称满件送活动库存
  599. !empty($activityQcMjSend) && $this->updateQcMjSend($activityQcMjSend);
  600. // 更新满件送活动库存
  601. !empty($activityMjSend) && $this->updateMjSend($activityMjSend);
  602. // 更新砍价活动库存
  603. !empty($kjActivityGood) && $this->updateKj($kjActivityGood);
  604. // 更新拼团活动库存
  605. !empty($groupBuyGood) && $this->updateGroupBuy($groupBuyGood);
  606. //更新拼团裂变活动库存
  607. !empty($groupBuyLbGood) && $this->updateGroupBuyLb($groupBuyLbGood);
  608. return true;
  609. }
  610. /**
  611. * 取消订单回退多个活动的活动库存
  612. * @param array $data
  613. * @return array|false
  614. */
  615. public function backActivityStock($order, bool $isPayOrder = false){
  616. $goodsList = $order['goods'];
  617. foreach ($goodsList as $goods) {
  618. // 更新条件: 订单已经支付或者订单商品为 "下单减库存"
  619. if ($isPayOrder == true || $goods['deduct_stock_type'] == DeductStockTypeEnum::CREATE) {
  620. //N件X折
  621. $goods['activity_discount_count'] && $activityDiscount[] = [
  622. 'where' => [
  623. 'activity_id' => $goods['activity_discount_id'],
  624. 'goods_id' => $goods['goods_id']
  625. ],
  626. 'data' => [
  627. // 递减商品总库存
  628. 'purchase_stock' => ['dec', $goods['activity_discount_count']]
  629. ]
  630. ];
  631. //满就送
  632. $goods['full_send_count'] && $activityFullSend[] = [
  633. 'where' => [
  634. 'full_send_activity_id' => $goods['full_send_activity_id'],
  635. 'goods_id' => $goods['goods_id']
  636. ],
  637. 'data' => [
  638. // 递减商品总库存
  639. 'purchase_stock' => ['dec', $goods['full_send_count']]
  640. ]
  641. ];
  642. //全场满件送
  643. $goods['qc_send_use_count'] && $activityQcMjSend[] = [
  644. 'where' => [
  645. 'qc_mj_send_activity_id' => $goods['qc_send_activity_id'],
  646. 'goods_id' => $goods['goods_id']
  647. ],
  648. 'data' => [
  649. // 递减商品总库存
  650. 'purchase_stock' => ['dec', $goods['qc_send_use_count']]
  651. ]
  652. ];
  653. //满件送
  654. $goods['mj_send_use_count'] && $activityMjSend[] = [
  655. 'where' => [
  656. 'mj_send_activity_id' => $goods['mj_send_activity_id'],
  657. 'goods_id' => $goods['goods_id']
  658. ],
  659. 'data' => [
  660. // 递减商品总库存
  661. 'purchase_stock' => ['dec', $goods['mj_send_use_count']]
  662. ]
  663. ];
  664. //砍价
  665. $goods['bargain_id'] && $kjActivityGood[] = [
  666. 'where' => [
  667. 'activity_id' => $goods['bargain_id'],
  668. 'goods_id' => $goods['goods_id']
  669. ],
  670. 'data' => [
  671. // 递减商品总库存
  672. 'purchase_stock' => ['dec', 1]
  673. ]
  674. ];
  675. //拼团
  676. $goods['group_buy_id'] && $groupBuyGood[] = [
  677. 'where' => [
  678. 'group_buy_activity_id' => $goods['group_buy_id'],
  679. 'goods_id' => $goods['goods_id']
  680. ],
  681. 'data' => [
  682. // 递减商品总库存
  683. 'purchase_stock' => ['dec', 1]
  684. ]
  685. ];
  686. }
  687. }
  688. // 更新N件X折活动库存
  689. !empty($activityDiscount) && $this->updateDiscount($activityDiscount);
  690. // 更新满就送活动库存
  691. !empty($activityFullSend) && $this->updateFullSend($activityFullSend);
  692. // 更新全称满件送活动库存
  693. !empty($activityQcMjSend) && $this->updateQcMjSend($activityQcMjSend);
  694. // 更新满件送活动库存
  695. !empty($activityMjSend) && $this->updateMjSend($activityMjSend);
  696. // 更新砍价活动库存
  697. !empty($kjActivityGood) && $this->updateKj($kjActivityGood);
  698. // 更新拼团活动库存
  699. !empty($groupBuyGood) && $this->updateGroupBuy($groupBuyGood);
  700. return true;
  701. }
  702. /**
  703. * 申请退货退多个活动单商品的活动库存
  704. * @param array $data
  705. * @return array|false
  706. */
  707. public function backActivityGoodsStock($order_goods, $total_num=0){
  708. if(empty($order_goods)&& empty($total_num)){
  709. return false;
  710. }
  711. //N件X折
  712. $discount = $order_goods['total_num'] -$order_goods['activity_discount_count'];
  713. if($discount<$total_num){
  714. $count = $total_num-$discount;
  715. $activityDiscount[] = [
  716. 'where' => [
  717. 'activity_id' => $order_goods['activity_discount_id'],
  718. 'goods_id' => $order_goods['goods_id']
  719. ],
  720. 'data' => [
  721. // 递减商品总库存
  722. 'purchase_stock' => ['dec', $count]
  723. ]
  724. ];
  725. }
  726. //满就送
  727. $discount = $order_goods['total_num'] -$order_goods['full_send_count'];
  728. if($discount<$total_num){
  729. $count = $total_num-$discount;
  730. $activityFullSend[] = [
  731. 'where' => [
  732. 'full_send_activity_id' => $order_goods['full_send_activity_id'],
  733. 'goods_id' => $order_goods['goods_id']
  734. ],
  735. 'data' => [
  736. // 递减商品总库存
  737. 'purchase_stock' => ['dec', $count]
  738. ]
  739. ];
  740. }
  741. //全场满件送
  742. $discount = $order_goods['total_num'] -$order_goods['qc_send_use_count'];
  743. if($discount<$total_num){
  744. $count = $total_num-$discount;
  745. $activityQcMjSend[] = [
  746. 'where' => [
  747. 'qc_mj_send_activity_id' => $order_goods['qc_send_activity_id'],
  748. 'goods_id' => $order_goods['goods_id']
  749. ],
  750. 'data' => [
  751. // 递减商品总库存
  752. 'purchase_stock' => ['dec', $count]
  753. ]
  754. ];
  755. }
  756. //满件送
  757. $discount = $order_goods['total_num'] -$order_goods['mj_send_use_count'];
  758. if($discount<$total_num){
  759. $count = $total_num-$discount;
  760. $activityMjSend[] = [
  761. 'where' => [
  762. 'mj_send_activity_id' => $order_goods['mj_send_activity_id'],
  763. 'goods_id' => $order_goods['goods_id']
  764. ],
  765. 'data' => [
  766. // 递减商品总库存
  767. 'purchase_stock' => ['dec', $count]
  768. ]
  769. ];
  770. }
  771. //砍价
  772. $order_goods['bargain_id'] && $kjActivityGood[] = [
  773. 'where' => [
  774. 'activity_id' => $order_goods['bargain_id'],
  775. 'goods_id' => $order_goods['goods_id']
  776. ],
  777. 'data' => [
  778. // 递减商品总库存
  779. 'purchase_stock' => ['dec', 1]
  780. ]
  781. ];
  782. //拼团
  783. $order_goods['group_buy_id'] && $groupBuyGood[] = [
  784. 'where' => [
  785. 'group_buy_activity_id' => $order_goods['group_buy_id'],
  786. 'goods_id' => $order_goods['goods_id']
  787. ],
  788. 'data' => [
  789. // 递减商品总库存
  790. 'purchase_stock' => ['dec', 1]
  791. ]
  792. ];
  793. // 更新N件X折活动库存
  794. !empty($activityDiscount) && $this->updateDiscount($activityDiscount);
  795. // 更新满就送活动库存
  796. !empty($activityFullSend) && $this->updateFullSend($activityFullSend);
  797. // 更新全称满件送活动库存
  798. !empty($activityQcMjSend) && $this->updateQcMjSend($activityQcMjSend);
  799. // 更新满件送活动库存
  800. !empty($activityMjSend) && $this->updateMjSend($activityMjSend);
  801. // 更新砍价活动库存
  802. !empty($kjActivityGood) && $this->updateKj($kjActivityGood);
  803. // 更新拼团活动库存
  804. !empty($groupBuyGood) && $this->updateGroupBuy($groupBuyGood);
  805. return true;
  806. }
  807. /**
  808. * 更新N件X折活动库存
  809. * @param array $data
  810. * @return array|false
  811. */
  812. private function updateDiscount(array $data)
  813. {
  814. return (new ActivityDiscountGoodsModel)->updateAll($data);
  815. }
  816. /**
  817. * 更新满就送活动库存
  818. * @param array $data
  819. * @return array|false
  820. */
  821. private function updateFullSend(array $data)
  822. {
  823. return (new FullSendGoodsModel)->updateAll($data);
  824. }
  825. /**
  826. * 更新全称满件送活动库存
  827. * @param array $data
  828. * @return array|false
  829. */
  830. private function updateQcMjSend(array $data)
  831. {
  832. return (new QcMjSendGoodsModel)->updateAll($data);
  833. }
  834. /**
  835. * 更新满件送活动库存
  836. * @param array $data
  837. * @return array|false
  838. */
  839. private function updateMjSend(array $data)
  840. {
  841. return (new MjSendGoodsModel)->updateAll($data);
  842. }
  843. /**
  844. * 更新砍价活动库存
  845. * @param array $data
  846. * @return array|false
  847. */
  848. private function updateKj(array $data)
  849. {
  850. return (new KjActivityGoodModel)->updateAll($data);
  851. }
  852. /**
  853. * 更新拼团活动库存
  854. * @param array $data
  855. * @return array|false
  856. */
  857. private function updateGroupBuy(array $data)
  858. {
  859. return (new GroupBuyGoodsModel)->updateAll($data);
  860. }
  861. /**
  862. * 更新拼团活动库存
  863. * @param array $data
  864. * @return array|false
  865. */
  866. private function updateGroupBuyLb(array $data)
  867. {
  868. return (new GroupBuyGoodsLbModel)->updateAll($data);
  869. }
  870. }