From 644f2b0c187fff956de7d43d183a1cf32492ddb9 Mon Sep 17 00:00:00 2001 From: wuhui_zzw <1760308791@qq.com> Date: Tue, 11 Jun 2024 16:29:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=EF=BC=9A=E8=BF=9B=E8=B4=A7?= =?UTF-8?q?=E5=95=86=E5=93=81=E5=94=AE=E5=87=BA=E5=90=8E=20=E8=BF=94?= =?UTF-8?q?=E8=BF=98=E5=AF=B9=E5=BA=94=E7=9A=84=E8=BF=9B=E8=B4=A7=E9=A2=9D?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dao/store/product/WithGoodsLogDao.php | 13 ++ .../model/store/product/WithGoodsLog.php | 18 +++ .../store/product/WineProductRepository.php | 64 +++++++-- .../store/product/WithGoodsLogRepository.php | 18 +++ app/listener/exchangeQuota/OrderTakeEvent.php | 122 +++++++++++++++++- 5 files changed, 223 insertions(+), 12 deletions(-) create mode 100644 app/common/dao/store/product/WithGoodsLogDao.php create mode 100644 app/common/model/store/product/WithGoodsLog.php create mode 100644 app/common/repositories/store/product/WithGoodsLogRepository.php diff --git a/app/common/dao/store/product/WithGoodsLogDao.php b/app/common/dao/store/product/WithGoodsLogDao.php new file mode 100644 index 0000000..f9f51cb --- /dev/null +++ b/app/common/dao/store/product/WithGoodsLogDao.php @@ -0,0 +1,13 @@ +make(StoreOrderProductRepository::class) - ->getSearch([]) - ->field(['order_product_id','order_id','product_id','product_sku','product_num','mer_quota_title','mer_quota_other']) - ->where('order_id',$orderId) + $orderProductList = (new StoreOrderProduct()) + ->alias('a') + ->leftJoin('StoreProduct sp', 'a.product_id = sp.product_id') + ->field([ + 'a.order_product_id', + 'a.order_id', + 'a.product_id', + 'a.product_sku', + 'a.product_num', + 'a.mer_quota_title', + 'a.mer_quota_other', + 'a.total_price', + 'a.product_num', + 'sp.mer_id', + ]) + ->where('a.order_id',$orderId) ->select() ->toArray(); // 循环处理 @@ -46,7 +60,12 @@ class WineProductRepository extends BaseRepository{ if(($productInfo->product_id ?? 0) > 0) { // 商品已经存在 $this->stockWithGoodsAddStock($item,$productInfo); + // 增加已使用进货额度 $this->addMerQuota((int)$productInfo->product_id, (float)$item['mer_quota_title'], (float)$item['mer_quota_other']); + // 进货记录 + $this->addLog((int)$productInfo->product_id, $withGoodsMerId, $item); + + }else{ // 商品不存在 $this->stockWithGoodsCopy($item,$withGoodsMerId); @@ -106,9 +125,10 @@ class WineProductRepository extends BaseRepository{ ->value('mer_labels'); app()->make(SpuRepository::class)->create($product, $result->product_id, 0, $oldGood['product_type']); } - - + // 增加已使用进货额度 $this->addMerQuota((int)$result->product_id, (float)$item['mer_quota_title'], (float)$item['mer_quota_other']); + // 进货记录 + $this->addLog((int)$result->product_id, $withGoodsMerId, $item); }); } /** @@ -194,6 +214,7 @@ class WineProductRepository extends BaseRepository{ * @param int $productId * @param float $merQuotaTitle * @param float $merQuotaOther + * @return bool */ public function addMerQuota(int $productId,float $merQuotaTitle,float $merQuotaOther){ $productInfo = app()->make(ProductRepository::class)->getSearch([])->where('product_id',$productId)->findOrEmpty(); @@ -204,10 +225,35 @@ class WineProductRepository extends BaseRepository{ if($merQuotaOther > 0) $productInfo->mer_quota_other += $merQuotaOther; $productInfo->save(); } + + return true; + } + /** + * Common: 进货完成 - 记录进货记录 + * Author: wu-hui + * Time: 2024/06/11 11:16 + * @param $productId + * @param $withGoodsMerId + * @param $item + */ + public function addLog($productId, $withGoodsMerId, $item){ + $unique = (new StoreProductAttrValue()) + ->where('copy_unique', $item['product_sku']) + ->where('product_id',$productId) + ->value('unique'); + WithGoodsLog::insert([ + 'supplier_mer_id' => $item['mer_id'], + 'mer_id' => $withGoodsMerId, + 'with_product_id' => $item['product_id'], + 'product_id' => $productId, + 'with_product_price' => (float)sprintf("%.2f",$item['total_price'] / $item['product_num']), + 'with_product_num' => $item['product_num'], + 'with_product_sku' => $item['product_sku'], + 'product_sku' => $unique, + 'mer_quota_title' => $item['mer_quota_title'], + 'mer_quota_other' => $item['mer_quota_other'], + ]); } - - - } diff --git a/app/common/repositories/store/product/WithGoodsLogRepository.php b/app/common/repositories/store/product/WithGoodsLogRepository.php new file mode 100644 index 0000000..eae7d0f --- /dev/null +++ b/app/common/repositories/store/product/WithGoodsLogRepository.php @@ -0,0 +1,18 @@ +dao = $dao; + } + + + + + +} diff --git a/app/listener/exchangeQuota/OrderTakeEvent.php b/app/listener/exchangeQuota/OrderTakeEvent.php index 05f5f7d..6f847ff 100644 --- a/app/listener/exchangeQuota/OrderTakeEvent.php +++ b/app/listener/exchangeQuota/OrderTakeEvent.php @@ -3,7 +3,11 @@ namespace app\listener\exchangeQuota; +use app\common\model\store\order\StoreOrderProduct; +use app\common\model\store\product\WithGoodsLog; use app\common\repositories\store\product\WineProductRepository; +use app\common\repositories\system\merchant\MerchantQuotaRecordRepository; +use app\common\repositories\system\merchant\MerchantQuotaRepository; use app\common\repositories\system\merchant\MerchantRepository; use app\common\repositories\user\ExchangeQuotaRecordRepository; use app\common\repositories\user\ExchangeQuotaRepository; @@ -42,6 +46,8 @@ class OrderTakeEvent{ $this->wineQuotaThawing($order); // 商户佣金结算 $this->merMoneyHandle($order->order_id); + // 品牌额度返还 + $this->returnBrandQuota($order->order_id,$order->mer_id); } }catch(\Exception $e){ @@ -149,9 +155,119 @@ class OrderTakeEvent{ ]); } } - - - + // 品牌额度返还 + private function returnBrandQuota($order_id, $merId){ + // 获取订单商品列表 + $orderProductList = (new StoreOrderProduct()) + ->field(['order_product_id','product_id','product_num','product_sku','order_id']) + ->where(['order_id' => $order_id]) + ->select() + ->toArray(); + // 循环处理每个商品 + $totalTitleReturnQuota = 0;// 本订单 总返还冠名品牌额度 + $totalOtherReturnQuota = 0;// 本订单 总返还其他品牌额度 + $withProductUpdateData = [];// 进货额度返还信息 + foreach($orderProductList as $orderProductInfo){ + $productNum = $orderProductInfo['product_num'] ?? 0;// 本订单商品购买数量 + // 获取当前商户、当前商品、当前规格进货记录信息;根据是否存在补货额度信息 进行操作 + $withGoodsLogList = (new WithGoodsLog()) + ->field([ + 'id', + 'with_product_price', + 'mer_quota_title', + 'mer_quota_other', + 'return_mer_quota_title', + 'return_mer_quota_other', + 'product_sku' + ]) + ->where('product_sku', $orderProductInfo['product_sku']) + ->whereRaw('mer_quota_title > return_mer_quota_title or mer_quota_other > return_mer_quota_other') + ->order('id ASC') + ->select() + ->toArray(); + // 判断:无有效补货额度信息(存在未返还的补货额度) + if(count($withGoodsLogList) <= 0 || $productNum <= 0) continue; + // 循环处理 返还补货额度 + $returnQuota = 0; + foreach($withGoodsLogList as $withGoodsInfo){ + // 计算剩余未返还补货额度 同时只会存在一种补货额度,因此直接同时计算两种额度 + $surplusTitleQuota = (float)sprintf("%.2f",$withGoodsInfo['mer_quota_title'] - $withGoodsInfo['return_mer_quota_title']);// 剩余冠名品牌额度 + $surplusOtherQuota = (float)sprintf("%.2f",$withGoodsInfo['mer_quota_other'] - $withGoodsInfo['return_mer_quota_other']);// 剩余其他品牌额度 + // 计算:根据剩余购买数量 * 进货单价 = 应返还补货额度 + if($returnQuota <= 0) $returnQuota = (float)sprintf("%.2f", $productNum * $withGoodsInfo['with_product_price']); + // 判断:应返还额度类型 + if($surplusTitleQuota > 0){ + // 冠名品牌额度 返还 + $realReturnQuota = $surplusTitleQuota < $returnQuota ? $surplusTitleQuota : $returnQuota;// 实际返还额度 + $surplusReturnQuota = (float)sprintf("%.2f", $returnQuota - $realReturnQuota);// 剩余未返还额度 + $totalTitleReturnQuota += $realReturnQuota; + // 进货记录修改信息 + $withProductUpdateData[] = [ + 'id' => $withGoodsInfo['id'], + 'return_mer_quota_title' => (float)sprintf("%.2f",$withGoodsInfo['return_mer_quota_title'] + $realReturnQuota), + 'return_mer_quota_other' => (float)$withGoodsInfo['return_mer_quota_other'], + ]; + } + else if($surplusOtherQuota > 0){ + // 其他品牌额度 返还 + $realReturnQuota = $surplusOtherQuota < $returnQuota ? $surplusOtherQuota : $returnQuota;// 实际返还额度 + $surplusReturnQuota = (float)sprintf("%.2f", $returnQuota - $realReturnQuota);// 剩余未返还额度 + $totalOtherReturnQuota += $realReturnQuota; + $withProductUpdateData[] = [ + 'id' => $withGoodsInfo['id'], + 'return_mer_quota_title' => (float)$withGoodsInfo['return_mer_quota_title'], + 'return_mer_quota_other' => (float)sprintf("%.2f",$withGoodsInfo['return_mer_quota_other'] + $realReturnQuota), + ]; + } + else{ + // 额度类型不明确 错误 跳出 + continue; + } + // 判断:是否存在剩余未返还额度 存在则计算剩余数量 + if($surplusReturnQuota > 0) $returnQuota = (float)$surplusReturnQuota; + else break; + } + } + // 修改进货记录信息 + WithGoodsLog::batchUpdate(array_values($withProductUpdateData)); + // 额度返还处理 + $quotaRecordInsertData = []; + $quotaHoldInfo = app()->make(MerchantQuotaRepository::class)->getQuotaInfo($merId); + if((float)$totalTitleReturnQuota > 0){ + // 变更持有信息 + $changeFront = (float)sprintf("%.2f", $quotaHoldInfo->title_brand_limit - $quotaHoldInfo->title_brand_used); + $quotaHoldInfo->title_brand_used -= (float)$totalTitleReturnQuota; + //变更记录 + $quotaRecordInsertData[] = [ + 'mer_id' => $merId, + 'change_type' => (int)1, + 'change_front' => $changeFront, + 'change_quantity' => (float)$totalTitleReturnQuota, + 'change_after' => (float)sprintf("%.2f", $quotaHoldInfo->title_brand_limit - $quotaHoldInfo->title_brand_used), + 'source' => 2, + 'order_id' => $order_id, + 'quota_type' => 2 + ]; + } + if((float)$totalOtherReturnQuota > 0){ + // 变更持有信息 + $changeFront = (float)sprintf("%.2f", $quotaHoldInfo->other_brand_limit - $quotaHoldInfo->other_brand_used); + $quotaHoldInfo->other_brand_used -= (float)$totalOtherReturnQuota; + //变更记录 + $quotaRecordInsertData[] = [ + 'mer_id' => $merId, + 'change_type' => (int)1, + 'change_front' => $changeFront, + 'change_quantity' => (float)$totalOtherReturnQuota, + 'change_after' => (float)sprintf("%.2f", $quotaHoldInfo->other_brand_limit - $quotaHoldInfo->other_brand_used), + 'source' => 2, + 'order_id' => $order_id, + 'quota_type' => 3 + ]; + } + $quotaHoldInfo->save(); + if(count($quotaRecordInsertData) > 0) app()->make(MerchantQuotaRecordRepository::class)->insertAll($quotaRecordInsertData); + }