123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\common\service\goods\source;
- use app\common\model\ActivityDiscount;
- use app\common\model\Goods as GoodsModel;
- use app\common\model\GoodsSku as GoodsSkuModel;
- use app\common\model\ShopGoods as ShopGoodsModel;
- use app\common\model\ShopGoodsSku as ShopGoodsSkuModel;
- use app\common\model\ActivityDiscountGoods as ActivityDiscountGoodsModel;
- use app\common\model\fullsend\FullSendGoods as FullSendGoodsModel;
- use app\common\model\mj\MjSendGoods as MjSendGoodsModel;
- use app\common\model\qc\QcMjSendGoods as QcMjSendGoodsModel;
- use app\common\model\kjactivity\KjActivityGood as KjActivityGoodModel;
- use app\common\model\groupbuy\GroupBuyGoods as GroupBuyGoodsModel;
- use app\common\model\groupbuylb\GroupBuyLbGoods as GroupBuyGoodsLbModel;
- use app\common\enum\goods\DeductStockType as DeductStockTypeEnum;
- /**
- * 商品来源-普通商品扩展类
- * Class Master
- * @package app\common\service\stock
- */
- class Master extends Basics
- {
- /**
- * 更新商品库存 (针对下单减库存的商品)
- * @param $order
- * @return bool
- */
- public function updateGoodsStock($order)
- {
- $goodsList = $order['goodsList'];
- $deliveryType = $order['delivery']; // 配送方式 10-普通快递 20-门店自提
- $shopInfo = $order['shopInfo']??[];
- $goodsData = [];
- $goodsSkuData = [];
- $shopGoodsData = [];
- $shopGoodsSkuData = [];
- if ($deliveryType == 10) {
- foreach ($goodsList as $goods) {
- // 是否减库存 (下单减库存)
- $isUpdateStockNum = $goods['deduct_stock_type'] == DeductStockTypeEnum::CREATE;
- // 商品的数据
- $isUpdateStockNum && $goodsData[] = [
- 'where' => ['goods_id' => $goods['goods_id']],
- 'data' => [
- // 递减商品总库存
- 'stock_total' => ['dec', $goods['total_num']]
- ]
- ];
- // 商品SKU数据
- $isUpdateStockNum && $goodsSkuData[] = [
- 'data' => ['stock_num' => ['dec', $goods['total_num']]],
- 'where' => [
- 'goods_id' => $goods['goods_id'],
- 'goods_sku_id' => $goods['goods_sku_id'],
- ],
- ];
- //如果当前商品有满件送赠品
- if(isset($goods['mj_send_goods_list'])){
- foreach ($goods['mj_send_goods_list'] as $mj_send_goods){
- // 商品的数据
- $isUpdateStockNum && $goodsData[] = [
- 'where' => ['goods_id' => $mj_send_goods['goods_id']],
- 'data' => [
- // 递减商品总库存
- 'stock_total' => ['dec', $mj_send_goods['total_num']]
- ]
- ];
- // 商品SKU数据
- $isUpdateStockNum && $goodsSkuData[] = [
- 'data' => ['stock_num' => ['dec', $mj_send_goods['total_num']]],
- 'where' => [
- 'goods_id' => $mj_send_goods['goods_id'],
- 'goods_sku_id' => $mj_send_goods['goods_sku_id'],
- ],
- ];
- }
- }
- }
- // 更新商品总库存
- !empty($goodsData) && $this->updateGoods($goodsData);
- // 更新商品sku库存
- !empty($goodsSkuData) && $this->updateGoodsSku($goodsSkuData);
- } else {
- foreach ($goodsList as $goods) {
- // 如果是赠品不减库存 则跳出循环
- // if ($goods['goods_type']==20) {
- // continue;
- // }
- // 是否减库存 (下单减库存)
- $isUpdateStockNum = $goods['deduct_stock_type'] == DeductStockTypeEnum::CREATE;
- // 商品的数据
- if ($isUpdateStockNum) {
- $goodsData[] = [
- 'where' => ['goods_id' => $goods['goods_id']],
- 'data' => [
- // 递减商品总库存
- 'shops_stock_total' => ['dec', $goods['total_num']]
- ]
- ];
- $shopGoodsData[] = [
- 'where' => ['goods_id' => $goods['goods_id'], 'shop_id' => $shopInfo['shop_id']],
- 'data' => [
- // 递减商品总库存
- 'stock_total' => ['dec', $goods['total_num']]
- ]
- ];
- }
-
- // 商品SKU数据
- $isUpdateStockNum && $shopGoodsSkuData[] = [
- 'data' => ['stock_num' => ['dec', $goods['total_num']]],
- 'where' => [
- 'goods_id' => $goods['goods_id'],
- 'goods_sku_id' => $goods['goods_sku_id'],
- 'shop_id' => $shopInfo['shop_id']
- ],
- ];
- //如果当前商品有满件送赠品
- if(isset($goods['mj_send_goods_list'])){
- foreach ($goods['mj_send_goods_list'] as $mj_send_goods){
- // 商品的数据
- if ($isUpdateStockNum) {
- $goodsData[] = [
- 'where' => ['goods_id' => $mj_send_goods['goods_id']],
- 'data' => [
- // 递减商品总库存
- 'shops_stock_total' => ['dec', $mj_send_goods['total_num']]
- ]
- ];
- $shopGoodsData[] = [
- 'where' => ['goods_id' => $mj_send_goods['goods_id'], 'shop_id' => $shopInfo['shop_id']],
- 'data' => [
- // 递减商品总库存
- 'stock_total' => ['dec', $mj_send_goods['total_num']]
- ]
- ];
- $shopGoodsSkuData[] = [
- 'data' => ['stock_num' => ['dec', $mj_send_goods['total_num']]],
- 'where' => [
- 'goods_id' => $mj_send_goods['goods_id'],
- 'goods_sku_id' => $mj_send_goods['goods_sku_id'],
- 'shop_id' => $shopInfo['shop_id']
- ],
- ];
- }
- }
- }
- }
- // 更新商品的门店总库存
- !empty($goodsData) && $this->updateGoods($goodsData);
- // 更新门店的商品总库存
- !empty($shopGoodsData) && $this->updateShopGoods($shopGoodsData);
- // 更新门店的商品sku库存
- !empty($shopGoodsSkuData) && $this->updateShopGoodsSku($shopGoodsSkuData);
- }
- return true;
- }
- /**
- * 更新商品库存销量(订单付款后)
- * @param $order
- * @return bool
- */
- public function updateStockSales($order)
- {
- $goodsList = $order['goods'];
- $deliveryType = $order['delivery_type'];
- $goodsData = [];
- $goodsSkuData = [];
- $shopGoodsData = [];
- $shopGoodsSkuData = [];
- if ($deliveryType == 10) {
- foreach ($goodsList as $goods) {
- // 是否减库存 (付款减库存)
- $isUpdateStockNum = $goods['deduct_stock_type'] == DeductStockTypeEnum::PAYMENT;
- // 商品的数据
- $goodsDataItm = [
- 'where' => ['goods_id' => $goods['goods_id']],
- 'data' => [
- // 累计商品实际销量
- 'sales_actual' => ['inc', $goods['total_num']]
- ]
- ];
- // 递减商品总库存
- $isUpdateStockNum && $goodsDataItm['data']['stock_total'] = ['dec', $goods['total_num']];
- $goodsData[] = $goodsDataItm;
- // 付款减库存
- $isUpdateStockNum && $goodsSkuData[] = [
- 'where' => [
- 'goods_id' => $goods['goods_id'],
- 'goods_sku_id' => $goods['goods_sku_id'],
- ],
- 'data' => ['stock_num' => ['dec', $goods['total_num']]],
- ];
- }
- // 更新商品信息
- !empty($goodsData) && $this->updateGoods($goodsData);
- // 更新商品sku信息
- !empty($goodsSkuData) && $this->updateGoodsSku($goodsSkuData);
- } else { // 门店自提
- foreach ($goodsList as $goods) {
- // 如果是赠品不加库存 则跳出循环
- // if ($goods['goods_type']==20) {
- // continue;
- // }
- // 是否减库存 (付款减库存)
- $isUpdateStockNum = $goods['deduct_stock_type'] == DeductStockTypeEnum::PAYMENT;
- // 商品的数据
- $goodsDataItm = [
- 'where' => ['goods_id' => $goods['goods_id']],
- 'data' => [
- // 累计商品门店销量
- 'sales_shops' => ['inc', $goods['total_num']]
- ]
- ];
- $shopGoodsDataItm = [
- 'where' => ['goods_id' => $goods['goods_id'], 'shop_id' => $order['shop_id']],
- 'data' => [
- // 累计商品门店销量
- 'sale_num' => ['inc', $goods['total_num']]
- ]
- ];
- // 递减商品总库存
- if ($isUpdateStockNum) {
- $shopGoodsDataItm['data']['shops_stock_total'] = ['dec', $goods['total_num']];
- }
- $goodsData[] = $goodsDataItm;
- $shopGoodsData[] = $shopGoodsDataItm;
- // 付款减库存
- $isUpdateStockNum && $shopGoodsSkuData[] = [
- 'where' => [
- 'goods_id' => $goods['goods_id'],
- 'goods_sku_id' => $goods['goods_sku_id'],
- 'shop_id' => $order['shop_id']
- ],
- 'data' => ['stock_num' => ['dec', $goods['total_num']]],
- ];
- }
- // 更新商品信息
- !empty($goodsData) && $this->updateGoods($goodsData);
- // 更新门店商品信息
- !empty($shopGoodsData) && $this->updateShopGoods($shopGoodsData);
- // 更新门店商品sku信息
- !empty($shopGoodsSkuData) && $this->updateShopGoodsSku($shopGoodsSkuData);
- }
- return true;
- }
- /**
- * 回退商品库存事件 (用于取消订单时调用)
- * @param mixed $order
- * @param bool $isPayOrder 是否为已支付订单
- * @return bool|mixed
- */
- public function backGoodsStock($order, bool $isPayOrder = false)
- {
- $goodsList = $order['goods'];
- $deliveryType = $order['delivery_type'];
- $goodsData = [];
- $goodsSkuData = [];
- $shopGoodsData = [];
- $shopGoodsSkuData = [];
- if ($deliveryType == 10) {
- foreach ($goodsList as $goods) {
- // 验证商品规格是否存在 不存在 则跳出循环
- if (!$this->validateGoodsSkuExist($goods)) {
- continue;
- }
- // 更新条件: 订单已经支付或者订单商品为 "下单减库存"
- if ($isPayOrder == true || $goods['deduct_stock_type'] == DeductStockTypeEnum::CREATE) {
- $goodsData[] = [
- 'where' => ['goods_id' => $goods['goods_id']],
- 'data' => ['stock_total' => ['inc', $goods['total_num']]]
- ];
- $goodsSkuData[] = [
- 'where' => [
- 'goods_id' => $goods['goods_id'],
- 'goods_sku_id' => $goods['goods_sku_id'],
- ],
- 'data' => ['stock_num' => ['inc', $goods['total_num']]],
- ];
- }
- }
- // 更新商品总库存
- !empty($goodsData) && $this->updateGoods($goodsData);
- // 更新商品sku库存
- !empty($goodsSkuData) && $this->updateGoodsSku($goodsSkuData);
- } else { // 门店自提
- foreach ($goodsList as $goods) {
- // 验证商品规格是否存在 不存在 则跳出循环
- if (!$this->validateGoodsSkuExist($goods)) {//如果是赠品不加库存
- continue;
- }
- // if (!$this->validateGoodsSkuExist($goods) || $goods['goods_type']==20) {//如果是赠品不加库存
- // continue;
- // }
- // 更新条件: 订单已经支付或者订单商品为 "下单减库存"
- if ($isPayOrder == true || $goods['deduct_stock_type'] == DeductStockTypeEnum::CREATE) {
- $goodsData[] = [
- 'where' => ['goods_id' => $goods['goods_id']],
- 'data' => ['shops_stock_total' => ['inc', $goods['total_num']]]
- ];
- $shopGoodsData[] = [
- 'where' => ['goods_id' => $goods['goods_id'], 'shop_id' => $order['shop_id']],
- 'data' => ['stock_total' => ['inc', $goods['total_num']]]
- ];
- $shopGoodsSkuData[] = [
- 'where' => [
- 'goods_id' => $goods['goods_id'],
- 'goods_sku_id' => $goods['goods_sku_id'],
- 'shop_id' => $order['shop_id']
- ],
- 'data' => ['stock_num' => ['inc', $goods['total_num']]],
- ];
- }
- }
- // 更新商品总库存
- !empty($goodsData) && $this->updateGoods($goodsData);
- // 更新门店商品总库存
- !empty($shopGoodsData) && $this->updateShopGoods($shopGoodsData);
- // 更新门店商品sku库存
- !empty($shopGoodsSkuData) && $this->updateShopGoodsSku($shopGoodsSkuData);
- }
- return true;
- }
- // 验证商品规格是否存在(后台删除规格 则不回退库存)
- public function validateGoodsSkuExist($goods)
- {
- return GoodsSkuModel::detail($goods['goods_id'], $goods['goods_sku_id']);
- }
- // 验证商品规格是否存在(后台删除规格 则不回退库存)
- public function validateGoodsSkuExistOne($goods_id,$goods_sku_id)
- {
- return GoodsSkuModel::where('goods_id',$goods_id)->where('goods_sku_id',$goods_sku_id)->find();
- }
- /**
- * 回退商品库存事件 (用于售后订单时调用,针对单个商品)
- * @param mixed $order
- * @param bool $isPayOrder 是否为已支付订单
- * @return bool|mixed
- */
- public function backGoodsSkuStock($order,$goods_id=0,$goods_sku_id =0,$total_num =0){
- if(empty($order)||empty($goods_id)||empty($goods_sku_id)||empty($total_num)){
- return false;
- }
-
- if(!$this->validateGoodsSkuExistOne($goods_id,$goods_sku_id)){
- return false;
- }
- $goodsList = $order['goods'];
- $deliveryType = $order['delivery_type'];
- $goodsData = [];
- $goodsSkuData = [];
- $shopGoodsData = [];
- $shopGoodsSkuData = [];
- if ($deliveryType == 10) {
- // 更新条件: 订单已经支付或者订单商品为 "下单减库存"
- // if ($isPayOrder == true || $goods['deduct_stock_type'] == DeductStockTypeEnum::CREATE) {
- $goodsData[] = [
- 'where' => ['goods_id' => $goods_id],
- 'data' => ['stock_total' => ['inc', $total_num]]
- ];
- $goodsSkuData[] = [
- 'where' => [
- 'goods_id' => $goods_id,
- 'goods_sku_id' => $goods_sku_id,
- ],
- 'data' => ['stock_num' => ['inc', $total_num]],
- ];
- // }
- // 更新商品总库存
- !empty($goodsData) && $this->updateGoods($goodsData);
- // 更新商品sku库存
- !empty($goodsSkuData) && $this->updateGoodsSku($goodsSkuData);
- } else { // 门店自提
- // 更新条件: 订单已经支付或者订单商品为 "下单减库存"
- // if ($isPayOrder == true || $goods['deduct_stock_type'] == DeductStockTypeEnum::CREATE) {
- $goodsData[] = [
- 'where' => ['goods_id' => $goods_id],
- 'data' => ['shops_stock_total' => ['inc', $total_num]]
- ];
- $shopGoodsData[] = [
- 'where' => ['goods_id' => $goods_id, 'shop_id' => $order['shop_id']],
- 'data' => ['stock_total' => ['inc', $total_num]]
- ];
- $shopGoodsSkuData[] = [
- 'where' => [
- 'goods_id' => $goods_id,
- 'goods_sku_id' => $goods_sku_id,
- 'shop_id' => $order['shop_id']
- ],
- 'data' => ['stock_num' => ['inc', $total_num]],
- ];
- // }
- // 更新商品总库存
- !empty($goodsData) && $this->updateGoods($goodsData);
- // 更新门店商品总库存
- !empty($shopGoodsData) && $this->updateShopGoods($shopGoodsData);
- // 更新门店商品sku库存
- !empty($shopGoodsSkuData) && $this->updateShopGoodsSku($shopGoodsSkuData);
- }
- return true;
- }
- /**
- * 更新商品销量
- * @param $order
- * @return bool
- */
- public function updateStockSalesExpress($goods_id,$total_num,$is_inc=true){
- $goodsData = [];
- // 商品的数据
- //是+还是-
- if($is_inc){
- $value = 'inc';
- }else{
- $value = 'dec';
- }
- $goodsDataItm = [
- 'where' => ['goods_id' => $goods_id],
- 'data' => [
- // 累计商品实际销量
- 'sales_actual' => [$value,$total_num]
- ]
- ];
- $goodsData[] = $goodsDataItm;
- // 更新商品信息
- !empty($goodsData) && $this->updateGoods($goodsData);
- return true;
- }
- /**
- * 回退商品库存事件 (用于取消订单时调用,针对单个商品)
- * @param mixed $order
- * @param bool $isPayOrder 是否为已支付订单
- * @return bool|mixed
- */
- public function backGoodsSkuStockExpress($goods_id=0,$goods_sku_id =0,$total_num =0){
- if(empty($goods_id)||empty($total_num)){
- var_dump(1);
- return false;
- }
- if(!$this->validateGoodsSkuExistOne($goods_id,$goods_sku_id)){
- var_dump(2);
- return false;
- }
- $goodsData = [];
- $goodsSkuData = [];
- $goodsData[] = [
- 'where' => ['goods_id' => $goods_id],
- 'data' => ['stock_total' => ['inc', $total_num]]
- ];
- $goodsSkuData[] = [
- 'where' => [
- 'goods_id' => $goods_id,
- 'goods_sku_id' => $goods_sku_id,
- ],
- 'data' => ['stock_num' => ['inc', $total_num]],
- ];
- // 更新商品总库存
- !empty($goodsData) && $this->updateGoods($goodsData);
- // 更新商品sku库存
- !empty($goodsSkuData) && $this->updateGoodsSku($goodsSkuData);
-
- return true;
- }
- /**
- * 更新商品信息
- * @param array $data
- * @return array|false
- */
- private function updateGoods(array $data)
- {
- return (new GoodsModel)->updateAll($data);
- }
- /**
- * 更新商品sku信息
- * @param array $data
- * @return array|false
- */
- private function updateGoodsSku(array $data)
- {
- return (new GoodsSkuModel)->updateAll($data);
- }
- /**
- * 更新门店商品信息
- * @param array $data
- * @return array|false
- */
- private function updateShopGoods(array $data)
- {
- return (new ShopGoodsModel)->updateAll($data);
- }
- /**
- * 更新门店商品sku信息
- * @param array $data
- * @return array|false
- */
- private function updateShopGoodsSku(array $data)
- {
- return (new ShopGoodsSkuModel)->updateAll($data);
- }
- /**
- * 确认下单更改多个活动的活动库存
- * @param array $data
- * @return array|false
- */
- public function updateActivityStock($order){
- $goodsList = $order['goodsList'];
- // $deliveryType = $order['delivery']; // 配送方式 10-普通快递 20-门店自提
- // $shopInfo = $order['shopInfo']??[];
- foreach ($goodsList as $goods) {
- //N件X折
- $goods['is_activity_discount'] && $activityDiscount[] = [
- 'where' => [
- 'activity_id' => $goods['activity_discount_id'],
- 'goods_id' => $goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['inc', $goods['activity_discount_count']]
- ]
- ];
- //满就送
- $goods['is_full_send'] && $activityFullSend[] = [
- 'where' => [
- 'full_send_activity_id' => $goods['full_send_activity_id'],
- 'goods_id' => $goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['inc', $goods['full_send_count']]
- ]
- ];
- //全场满件送
- $goods['is_qc_send_send'] && $activityQcMjSend[] = [
- 'where' => [
- 'qc_mj_send_activity_id' => $goods['qc_send_activity_id'],
- 'goods_id' => $goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['inc', $goods['qc_send_use_count']]
- ]
- ];
- //满件送
- $goods['is_mj_send'] && $activityMjSend[] = [
- 'where' => [
- 'mj_send_activity_id' => $goods['mj_send_activity_id'],
- 'goods_id' => $goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['inc', $goods['mj_send_use_count']]
- ]
- ];
- //砍价
- $goods['bargain_activity_id'] && $kjActivityGood[] = [
- 'where' => [
- 'activity_id' => $goods['bargain_activity_id'],
- 'goods_id' => $goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['inc', 1]
- ]
- ];
- //拼团
- $goods['group_buy_id'] && $groupBuyGood[] = [
- 'where' => [
- 'group_buy_activity_id' => $goods['group_buy_id'],
- 'goods_id' => $goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['inc', $goods['total_num']]//1
- ]
- ];
- //拼团裂变
- $goods['group_buy_lb_id'] && $groupBuyLbGood[] = [
- 'where' => [
- 'group_buy_lb_activity_id' => $goods['group_buy_lb_id'],
- 'goods_id' => $goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['inc', $goods['total_num']]//1
- ]
- ];
- }
- // 更新N件X折活动库存
- !empty($activityDiscount) && $this->updateDiscount($activityDiscount);
- // 更新满就送活动库存
- !empty($activityFullSend) && $this->updateFullSend($activityFullSend);
- // 更新全称满件送活动库存
- !empty($activityQcMjSend) && $this->updateQcMjSend($activityQcMjSend);
- // 更新满件送活动库存
- !empty($activityMjSend) && $this->updateMjSend($activityMjSend);
- // 更新砍价活动库存
- !empty($kjActivityGood) && $this->updateKj($kjActivityGood);
- // 更新拼团活动库存
- !empty($groupBuyGood) && $this->updateGroupBuy($groupBuyGood);
- //更新拼团裂变活动库存
- !empty($groupBuyLbGood) && $this->updateGroupBuyLb($groupBuyLbGood);
- return true;
- }
- /**
- * 取消订单回退多个活动的活动库存
- * @param array $data
- * @return array|false
- */
- public function backActivityStock($order, bool $isPayOrder = false){
- $goodsList = $order['goods'];
- foreach ($goodsList as $goods) {
- // 更新条件: 订单已经支付或者订单商品为 "下单减库存"
- if ($isPayOrder == true || $goods['deduct_stock_type'] == DeductStockTypeEnum::CREATE) {
- //N件X折
- $goods['activity_discount_count'] && $activityDiscount[] = [
- 'where' => [
- 'activity_id' => $goods['activity_discount_id'],
- 'goods_id' => $goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['dec', $goods['activity_discount_count']]
- ]
- ];
- //满就送
- $goods['full_send_count'] && $activityFullSend[] = [
- 'where' => [
- 'full_send_activity_id' => $goods['full_send_activity_id'],
- 'goods_id' => $goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['dec', $goods['full_send_count']]
- ]
- ];
- //全场满件送
- $goods['qc_send_use_count'] && $activityQcMjSend[] = [
- 'where' => [
- 'qc_mj_send_activity_id' => $goods['qc_send_activity_id'],
- 'goods_id' => $goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['dec', $goods['qc_send_use_count']]
- ]
- ];
- //满件送
- $goods['mj_send_use_count'] && $activityMjSend[] = [
- 'where' => [
- 'mj_send_activity_id' => $goods['mj_send_activity_id'],
- 'goods_id' => $goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['dec', $goods['mj_send_use_count']]
- ]
- ];
- //砍价
- $goods['bargain_id'] && $kjActivityGood[] = [
- 'where' => [
- 'activity_id' => $goods['bargain_id'],
- 'goods_id' => $goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['dec', 1]
- ]
- ];
- //拼团
- $goods['group_buy_id'] && $groupBuyGood[] = [
- 'where' => [
- 'group_buy_activity_id' => $goods['group_buy_id'],
- 'goods_id' => $goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['dec', 1]
- ]
- ];
- }
- }
- // 更新N件X折活动库存
- !empty($activityDiscount) && $this->updateDiscount($activityDiscount);
- // 更新满就送活动库存
- !empty($activityFullSend) && $this->updateFullSend($activityFullSend);
- // 更新全称满件送活动库存
- !empty($activityQcMjSend) && $this->updateQcMjSend($activityQcMjSend);
- // 更新满件送活动库存
- !empty($activityMjSend) && $this->updateMjSend($activityMjSend);
- // 更新砍价活动库存
- !empty($kjActivityGood) && $this->updateKj($kjActivityGood);
- // 更新拼团活动库存
- !empty($groupBuyGood) && $this->updateGroupBuy($groupBuyGood);
- return true;
- }
- /**
- * 申请退货退多个活动单商品的活动库存
- * @param array $data
- * @return array|false
- */
- public function backActivityGoodsStock($order_goods, $total_num=0){
- if(empty($order_goods)&& empty($total_num)){
- return false;
- }
- //N件X折
- $discount = $order_goods['total_num'] -$order_goods['activity_discount_count'];
- if($discount<$total_num){
- $count = $total_num-$discount;
- $activityDiscount[] = [
- 'where' => [
- 'activity_id' => $order_goods['activity_discount_id'],
- 'goods_id' => $order_goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['dec', $count]
- ]
- ];
- }
- //满就送
- $discount = $order_goods['total_num'] -$order_goods['full_send_count'];
- if($discount<$total_num){
- $count = $total_num-$discount;
- $activityFullSend[] = [
- 'where' => [
- 'full_send_activity_id' => $order_goods['full_send_activity_id'],
- 'goods_id' => $order_goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['dec', $count]
- ]
- ];
- }
- //全场满件送
- $discount = $order_goods['total_num'] -$order_goods['qc_send_use_count'];
- if($discount<$total_num){
- $count = $total_num-$discount;
- $activityQcMjSend[] = [
- 'where' => [
- 'qc_mj_send_activity_id' => $order_goods['qc_send_activity_id'],
- 'goods_id' => $order_goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['dec', $count]
- ]
- ];
- }
- //满件送
- $discount = $order_goods['total_num'] -$order_goods['mj_send_use_count'];
- if($discount<$total_num){
- $count = $total_num-$discount;
- $activityMjSend[] = [
- 'where' => [
- 'mj_send_activity_id' => $order_goods['mj_send_activity_id'],
- 'goods_id' => $order_goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['dec', $count]
- ]
- ];
- }
- //砍价
- $order_goods['bargain_id'] && $kjActivityGood[] = [
- 'where' => [
- 'activity_id' => $order_goods['bargain_id'],
- 'goods_id' => $order_goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['dec', 1]
- ]
- ];
- //拼团
- $order_goods['group_buy_id'] && $groupBuyGood[] = [
- 'where' => [
- 'group_buy_activity_id' => $order_goods['group_buy_id'],
- 'goods_id' => $order_goods['goods_id']
- ],
- 'data' => [
- // 递减商品总库存
- 'purchase_stock' => ['dec', 1]
- ]
- ];
- // 更新N件X折活动库存
- !empty($activityDiscount) && $this->updateDiscount($activityDiscount);
- // 更新满就送活动库存
- !empty($activityFullSend) && $this->updateFullSend($activityFullSend);
- // 更新全称满件送活动库存
- !empty($activityQcMjSend) && $this->updateQcMjSend($activityQcMjSend);
- // 更新满件送活动库存
- !empty($activityMjSend) && $this->updateMjSend($activityMjSend);
- // 更新砍价活动库存
- !empty($kjActivityGood) && $this->updateKj($kjActivityGood);
- // 更新拼团活动库存
- !empty($groupBuyGood) && $this->updateGroupBuy($groupBuyGood);
- return true;
- }
- /**
- * 更新N件X折活动库存
- * @param array $data
- * @return array|false
- */
- private function updateDiscount(array $data)
- {
- return (new ActivityDiscountGoodsModel)->updateAll($data);
- }
- /**
- * 更新满就送活动库存
- * @param array $data
- * @return array|false
- */
- private function updateFullSend(array $data)
- {
- return (new FullSendGoodsModel)->updateAll($data);
- }
- /**
- * 更新全称满件送活动库存
- * @param array $data
- * @return array|false
- */
- private function updateQcMjSend(array $data)
- {
- return (new QcMjSendGoodsModel)->updateAll($data);
- }
- /**
- * 更新满件送活动库存
- * @param array $data
- * @return array|false
- */
- private function updateMjSend(array $data)
- {
- return (new MjSendGoodsModel)->updateAll($data);
- }
- /**
- * 更新砍价活动库存
- * @param array $data
- * @return array|false
- */
- private function updateKj(array $data)
- {
- return (new KjActivityGoodModel)->updateAll($data);
- }
- /**
- * 更新拼团活动库存
- * @param array $data
- * @return array|false
- */
- private function updateGroupBuy(array $data)
- {
- return (new GroupBuyGoodsModel)->updateAll($data);
- }
- /**
- * 更新拼团活动库存
- * @param array $data
- * @return array|false
- */
- private function updateGroupBuyLb(array $data)
- {
- return (new GroupBuyGoodsLbModel)->updateAll($data);
- }
- }
|