添加:酒道馆进货订单完成后 自动同步商品信息

This commit is contained in:
wuhui_zzw 2024-03-09 16:53:09 +08:00
parent 8d2e9e8814
commit 1f95413a62
5 changed files with 220 additions and 14 deletions

View File

@ -35,7 +35,7 @@ class ProductAttrValue extends BaseModel
public function getDetailAttr($value)
{
return json_decode($value);
return json_decode($value,true);
}
public function product()

View File

@ -478,7 +478,7 @@ class ProductRepository extends BaseRepository
'guarantee_template_id' => $data['guarantee_template_id'] ?? 0,
'is_gift_bag' => $data['is_gift_bag'] ?? 0,
'integral_rate' => $integral_rate ?? 0,
'delivery_way' => implode(',',$data['delivery_way']),
'delivery_way' => is_array($data['delivery_way']) ? implode(',',$data['delivery_way']) : $data['delivery_way'],
'delivery_free' => $data['delivery_free'] ?? 0,
'once_min_count' => $data['once_min_count'] ?? 0,
'once_max_count' => $data['once_max_count'] ?? 0,
@ -555,7 +555,7 @@ class ProductRepository extends BaseRepository
'type' => 0,
'product_id' => $productId,
"attr_name" => $value['value'] ?? $value['attr_name'],
'attr_values' => implode('-!-', $value['detail']),
'attr_values' => implode('-!-', $value['detail'] ?? $value['attr_values']),
];
}
} catch (\Exception $exception) {
@ -632,6 +632,11 @@ class ProductRepository extends BaseRepository
'svip_price' => $_svip_price,
];
// 如果已经存在规格唯一键
if(isset($value['unique']) && $value['unique'] !== '') $array['copy_unique'] = $value['unique'];
if(isset($value['value_id']) && $value['value_id'] !== '') $array['copy_value_id'] = $value['value_id'];
if (isset($data['type']) && $data['type'] == 2) {
$array['cdkey'] = $cdkey;
}

View File

@ -0,0 +1,188 @@
<?php
namespace app\common\repositories\store\product;
use app\common\dao\store\product\ProductDao;
use app\common\model\store\product\Product;
use app\common\model\store\product\ProductAttr;
use app\common\model\store\product\ProductAttrValue;
use app\common\repositories\BaseRepository;
use app\common\repositories\store\order\StoreOrderProductRepository;
use app\common\repositories\store\parameter\ParameterValueRepository;
use app\model\product\sku\StoreProductAttrValue;
use think\facade\Db;
class WineProductRepository extends BaseRepository{
protected $dao;
public function __construct(ProductDao $dao){
$this->dao = $dao;
}
/**
* Common: 酒道馆进货订单收货 - 商品开始处理
* Author: wu-hui
* Time: 2024/03/09 16:50
* @param $orderId
* @param $withGoodsMerId
* @return bool
*/
public function stockWithGoodsInit($orderId,$withGoodsMerId){
// 获取全部商品
$orderProductList = app()->make(StoreOrderProductRepository::class)
->getSearch([])
->field(['order_product_id','order_id','product_id','product_sku','product_num'])
->where('order_id',$orderId)
->select()
->toArray();
// 循环处理
foreach($orderProductList as $item){
// 判断: 商品是否存在
$productInfo = app()->make(ProductRepository::class)
->getSearch([])
->where('copy_product_id',$item['product_id'])
->where('mer_id', $withGoodsMerId)
->findOrEmpty();
if(($productInfo->product_id ?? 0) > 0) {
// 商品已经存在
$this->stockWithGoodsAddStock($item,$productInfo);
}else{
// 商品不存在
$this->stockWithGoodsCopy($item,$withGoodsMerId);
}
}
return true;
}
/**
* Common: 酒道馆进货订单收货 - 商品不存在 - 复制商品
* Author: wu-hui
* Time: 2024/03/09 15:19
* @param $item
* @param $withGoodsMerId
* @return mixed
*/
public function stockWithGoodsCopy($item,$withGoodsMerId){
$oldGood = app()->make(ProductRepository::class)
->getSearch([])
->where('product_id',$item['product_id'])
->with(['attrValue','attr','merCateId','content'])
->append(['Params'])
->findOrEmpty()
->toArray();
$oldGood['Params'] = $oldGood['Params']->toArray();
$oldGood['mer_cate_id'] = array_column($oldGood['merCateId'], 'mer_cate_id');
// 生成新的商品信息
$product = app()->make(ProductRepository::class)->setProduct($oldGood);
$product['mer_id'] = $withGoodsMerId;
$product['copy_product_id'] = $oldGood['product_id'];
// 处理商品添加
return Db::transaction(function () use ($item, $product, $withGoodsMerId, $oldGood) {
// 增加商品
$result = (new Product())->create($product);
// 规格处理
$settleParams = app()->make(ProductRepository::class)->setAttrValue($oldGood, $result->product_id, $oldGood['product_type'], 0);
$settleParams['cate'] = app()->make(ProductRepository::class)->setMerCate($oldGood['mer_cate_id'], $result->product_id, $product['mer_id']);
$settleParams['attr'] = app()->make(ProductRepository::class)->setAttr($oldGood['attr'], $result->product_id);
if ($oldGood['Params']) app()->make(ParameterValueRepository::class)->create($result->product_id, $oldGood['Params'] ?? [],$product['mer_id']);
// 循环 修改商品库存
$settleParams['attrValue'] = array_map(function($attrValue) use ($item){
if($attrValue['copy_unique'] == $item['product_sku']) $attrValue['stock'] = $item['product_num'];
else $attrValue['stock'] = 0;
return $attrValue;
}, $settleParams['attrValue']);
$settleParams['data']['stock'] = $item['product_num'];
$content = $oldGood['content'] ?? [];
app()->make(ProductRepository::class)->save($result->product_id, $settleParams, $content,$product,$oldGood['product_type']);
if (in_array($oldGood['product_type'], [0, 1])) {
$product['price'] = $settleParams['data']['price'];
$product['ot_price'] = $settleParams['data']['ot_price'];
$product['mer_labels'] = app()->make(SpuRepository::class)
->getSearch([])
->where('product_id',$oldGood['product_id'])
->value('mer_labels');
app()->make(SpuRepository::class)->create($product, $result->product_id, 0, $oldGood['product_type']);
}
});
}
/**
* Common: 酒道馆进货订单收货 - 商品存在 - 规格不存在 - 同步规格
* Author: wu-hui
* Time: 2024/03/09 16:50
* @param $item
* @param $productInfo
* @return mixed
*/
public function stockWithGoodsCopySpec($item, $productInfo){
$oldGood = app()->make(ProductRepository::class)
->getSearch([])
->where('product_id',$item['product_id'])
->with(['attrValue','attr'])
->append(['Params'])
->findOrEmpty()
->toArray();
// 获取已经存在规格
$hasAttrValue = (new ProductAttrValue())->where('product_id',$productInfo->product_id)->column('stock','copy_unique');
// 处理商品添加
return Db::transaction(function () use ($item, $productInfo, $oldGood, $hasAttrValue) {
// 规格处理
$settleParams = app()->make(ProductRepository::class)->setAttrValue($oldGood, $productInfo->product_id, $oldGood['product_type'], 0);
$attr = app()->make(ProductRepository::class)->setAttr($oldGood['attr'], $productInfo->product_id);
// 修改规格
$productAttrRepository = app()->make(ProductAttrRepository::class);
$productAttrRepository->clearAttr($productInfo->product_id);
$productAttrRepository->insert($attr);
// 循环 修改商品库存
$attrValue = array_map(function($attrValue) use ($item, $hasAttrValue){
if($attrValue['copy_unique'] == $item['product_sku']) $attrValue['stock'] = $item['product_num'];
else $attrValue['stock'] = $hasAttrValue[$attrValue['copy_unique']] ?? 0;
return $attrValue;
}, $settleParams['attrValue']);
$productAttrValueRepository = app()->make(ProductAttrValueRepository::class);
$productAttrValueRepository->clearAttr($productInfo->product_id);
if (isset($attrValue)) {
$attrValueArr = array_chunk($attrValue, 30);
foreach ($attrValueArr as $attrValueArrItem){
$productAttrValueRepository->add($attrValueArrItem,$productInfo->type);
}
}
// 修改总库存
$productInfo->stock += $item['product_num'];
$productInfo->save();
});
}
/**
* Common: 酒道馆进货订单收货 - 商品存在 - 增加库存
* Author: wu-hui
* Time: 2024/03/09 16:50
* @param $item
* @param $productInfo
* @return mixed
*/
public function stockWithGoodsAddStock($item, $productInfo){
// 获取规格信息
$info = (new StoreProductAttrValue())
->where('copy_unique', $item['product_sku'])
->where('product_id',$productInfo->product_id)
->findOrEmpty();
if(($info->value_id ?? 0) > 0){
// 规格存在 增加库存
return Db::transaction(function () use ($info, $productInfo, $item) {
$info->stock += $item['product_num'];
$info->save();
$productInfo->stock += $item['product_num'];
$productInfo->save();
});
}else{
// 规格不存在
return $this->stockWithGoodsCopySpec($item, $productInfo);
}
}
}

View File

@ -10,6 +10,7 @@ use app\common\model\user\ExchangePickupPoint;
use app\common\repositories\marketing\AgentRepository;
use app\common\repositories\store\order\StoreOrderRepository;
use app\common\repositories\store\order\StoreRefundOrderRepository;
use app\common\repositories\store\product\WineProductRepository;
use app\common\repositories\system\notice\SystemNoticeConfigRepository;
use app\common\repositories\user\ExchangePickupPointRepository;
use app\common\repositories\user\ExchangeQuotaRepository;
@ -64,6 +65,12 @@ class Auth extends BaseController
// if ($status['notice_routine'] == 1) {
// app()->make(WechatTemplateMessageService::class)->subscribeSendTemplate($data);
// }
// 酒道馆进货商品订单 完成调试
// app()->make(WineProductRepository::class)->stockWithGoodsInit(289, 100);
}
/**

View File

@ -5,6 +5,7 @@ namespace app\listener\exchangeQuota;
use app\common\model\user\ExchangeQuota;
use app\common\model\user\ExchangeQuotaRecord;
use app\common\repositories\store\product\WineProductRepository;
use app\common\repositories\user\ExchangeQuotaRecordRepository;
use app\common\repositories\user\ExchangeQuotaRepository;
use think\facade\Log;
@ -16,20 +17,21 @@ class OrderTakeEvent{
public function handle($data){
try{
$order = $data['order'];
Log::info('订单进入待评价 - 酒卡额度相关处理 - 开始: '.var_export([
'order_id' => $order->order_id,
'uid' => $order->uid,
'activity_type' => $order->activity_type
],1));
if($order->activity_type == 35){
// 酒道馆进货商品确认收货 复制商品||增加库存
Log::info('订单进入待评价 - 酒道馆进货商品处理 - 开始: '.var_export([
'order_id' => $order->order_id,
'with_goods_mer_id' => $order->with_goods_mer_id,
'activity_type' => $order->activity_type
],1));
app()->make(WineProductRepository::class)->stockWithGoodsInit($order->order_id, $order->with_goods_mer_id);
}
else{
// Log::info('订单进入待评价 - 酒卡额度相关处理 - 开始: '.var_export([
// 'order_id' => $order->order_id,
// 'uid' => $order->uid,
// 'activity_type' => $order->activity_type
// ],1));
// 其他商品
# 获取变更记录 条件order_id=当前订单id、变更类型=增加
$sum = (float)app()->make(ExchangeQuotaRecordRepository::class)
@ -54,7 +56,11 @@ class OrderTakeEvent{
}
}
}catch(\Exception $e){
Log::info('订单进入待评价 - 酒卡额度相关处理 - 错误: '.$e->getMessage());
if($order->activity_type == 35){
Log::info('订单进入待评价 - 酒道馆进货商品处理 - 错误: '.$e->getMessage());
}else{
Log::info('订单进入待评价 - 酒卡额度相关处理 - 错误: '.$e->getMessage());
}
}
}