|
|
@@ -7,6 +7,7 @@ use App\Authority;
|
|
|
use App\Batch;
|
|
|
use App\City;
|
|
|
use App\Commodity;
|
|
|
+use App\CommodityBarcode;
|
|
|
use App\Events\CancelOrder;
|
|
|
use App\Log;
|
|
|
use App\Logistic;
|
|
|
@@ -411,9 +412,107 @@ class TestController extends Controller
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public function test2(){
|
|
|
- $commodity = app('commodityService')->ownerBarcodeSeekCommodityFirst(['id'=>8],"6971393121341");
|
|
|
- dd($commodity);
|
|
|
+ private function cleanBarcode(){
|
|
|
+ $logCommodityBarcodes = CommodityBarcode::query()->where('code',"")->get();
|
|
|
+ if (count($logCommodityBarcodes) > 0)LogService::log(__METHOD__,"纠正商品-删除空条码",json_encode($logCommodityBarcodes,JSON_UNESCAPED_UNICODE));
|
|
|
+
|
|
|
+ CommodityBarcode::query()->where('code',"")->delete();
|
|
|
+ $barcodes = DB::select(DB::raw('select * from commodity_barcodes c
|
|
|
+where (c.code,c.commodity_id) in (select code,commodity_id from commodity_barcodes group by code,commodity_id having count(*) > 1) order by commodity_id'));
|
|
|
+ $barcodeMap = [];
|
|
|
+ $barcodeDelete = [];
|
|
|
+ foreach ($barcodes as $barcode){
|
|
|
+ if (isset($barcodeMap[$barcode->code.'_'.$barcode->commodity_id]))$barcodeDelete[] = $barcode->id;
|
|
|
+ else $barcodeMap[$barcode->code.'_'.$barcode->commodity_id] = $barcode->id;
|
|
|
+ }
|
|
|
+
|
|
|
+ $logCommodityBarcodes = CommodityBarcode::query()->whereIn('id',$barcodeDelete)->get();
|
|
|
+ if (count($logCommodityBarcodes) > 0)LogService::log(__METHOD__,"纠正商品-删除重复条码",json_encode($logCommodityBarcodes,JSON_UNESCAPED_UNICODE));
|
|
|
+
|
|
|
+ CommodityBarcode::destroy($barcodeDelete);
|
|
|
+ }
|
|
|
+ public function correctCommodity(){
|
|
|
+ //清理冗余条码
|
|
|
+ $this->cleanBarcode();
|
|
|
+
|
|
|
+ //获取重复条码
|
|
|
+ $commodities = DB::select(DB::raw('select commodities.name,commodities.sku,commodities.owner_id,commodity_barcodes.id as barcode_id,commodity_barcodes.code as barcode_code,commodity_barcodes.commodity_id from commodities LEFT JOIN commodity_barcodes on commodities.id = commodity_barcodes.commodity_id
|
|
|
+where (commodities.owner_id,commodity_barcodes.code) in (select commodities.owner_id,commodity_barcodes.code from commodities LEFT JOIN commodity_barcodes on commodities.id = commodity_barcodes.commodity_id group by commodities.owner_id,commodity_barcodes.code having count(*) > 1) order by commodities.owner_id,commodity_barcodes.code'));
|
|
|
+
|
|
|
+ //对比map池
|
|
|
+ $commodityMap = [];
|
|
|
+ //需要删除项
|
|
|
+ $commodityDelete = [];
|
|
|
+ foreach ($commodities as $index => $commodity){
|
|
|
+ //货主+条码 为唯一key值 设想正常数据下同货主不应该有同条码
|
|
|
+ $key = $commodity->owner_id.'_'.$commodity->barcode_code;
|
|
|
+ //使用map池对比为重复数据
|
|
|
+ if(isset($commodityMap[$key])){
|
|
|
+ //获取下标指针指向的源数据
|
|
|
+ $c = $commodities[$commodityMap[$key]];
|
|
|
+ //源数据不符合规范,当前数据替换掉它
|
|
|
+ if (($c->sku == null || $c->sku == "") && ($commodity->sku != null && $commodity->sku != "")){
|
|
|
+ $commodityDelete[] = $commodityMap[$key];
|
|
|
+ $commodityMap[$key] = $index;
|
|
|
+ }else{
|
|
|
+ //当前数据比源数据更符合预期,替换掉
|
|
|
+ if (($c->sku == $commodity->sku) && (strlen($c->name) < strlen($commodity->name))){
|
|
|
+ $commodityDelete[] = $commodityMap[$key];
|
|
|
+ $commodityMap[$key] = $index;
|
|
|
+ }else{
|
|
|
+ //扔进处理池
|
|
|
+ $commodityDelete[] = $index;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //第一次出现的新数据,val为下标,当作指针指向源数据
|
|
|
+ }else $commodityMap[$key] = $index;
|
|
|
+ }
|
|
|
+ $createBarcodes = []; //批量生成条码
|
|
|
+ $deleteCommodities = []; //批量删除商品及商品条码
|
|
|
+
|
|
|
+ $updateCommodities = [];
|
|
|
+ foreach ($commodityDelete as $index){
|
|
|
+ //获取到被处理数据 与 目标数据
|
|
|
+ $del = $commodities[$index];
|
|
|
+ $target = $commodities[$commodityMap[$del->owner_id.'_'.$del->barcode_code]];
|
|
|
+
|
|
|
+ //记录ID删除商品与条码
|
|
|
+ $deleteCommodities[] = $del->commodity_id;
|
|
|
+
|
|
|
+ //有效条码合并
|
|
|
+ $barcodes = DB::select(DB::raw("select * from
|
|
|
+ (select * from commodity_barcodes where commodity_id = ".$del->commodity_id.")a
|
|
|
+ where (select count(1) as num from commodity_barcodes b
|
|
|
+ where commodity_id = ".$target->commodity_id." and b.code = a.code) = 0"));
|
|
|
+ if (count($barcodes) > 0){
|
|
|
+ foreach ($barcodes as $barcode){
|
|
|
+ $createBarcodes[] = ['code'=>$barcode->code, 'commodity_id'=>$target->commodity_id];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $updateCommodities[$del->commodity_id] = $target->commodity_id;
|
|
|
+ }
|
|
|
+ if (count($updateCommodities) > 0){
|
|
|
+ app('inventoryAccountMissionService')->batchUpdateItself('commodity_id', $updateCommodities);//批量更新库存盘点任务
|
|
|
+ app('inventoryCompareService')->batchUpdateItself('commodity_id', $updateCommodities);//批量更新库存对比
|
|
|
+ app('inventoryDailyLogService')->batchUpdateItself('commodity_id', $updateCommodities);//批量更新库存每日记录
|
|
|
+ app('processesContentService')->batchUpdateItself('commodity_id', $updateCommodities);//批量更新二次加工内容单
|
|
|
+ app('storeCheckingReceiveItemService')->batchUpdateItself('commodity_id', $updateCommodities);//批量更新入库盘收一体
|
|
|
+ app('orderPackageCommoditiesService')->batchUpdateItself('commodity_id', $updateCommodities);//批量更新订单商品
|
|
|
+ }
|
|
|
+
|
|
|
+ if (count($createBarcodes) > 0)LogService::log(__METHOD__,"纠正商品-录入合并条码",json_encode($createBarcodes,JSON_UNESCAPED_UNICODE));
|
|
|
+
|
|
|
+ CommodityBarcode::query()->insert($createBarcodes);
|
|
|
+
|
|
|
+ $logCommodityBarcodes = CommodityBarcode::query()->whereIn('commodity_id',$deleteCommodities)->get();
|
|
|
+ if (count($logCommodityBarcodes) > 0)LogService::log(__METHOD__,"纠正商品-删除无用商品条码",json_encode($logCommodityBarcodes,JSON_UNESCAPED_UNICODE));
|
|
|
+
|
|
|
+ CommodityBarcode::query()->whereIn('commodity_id',$deleteCommodities)->delete();
|
|
|
+
|
|
|
+ $logCommodities = Commodity::query()->whereIn('id',$deleteCommodities)->get();
|
|
|
+ if (count($logCommodities) > 0)LogService::log(__METHOD__,"纠正商品-删除无用商品",json_encode($logCommodities,JSON_UNESCAPED_UNICODE));
|
|
|
+
|
|
|
+ Commodity::destroy($deleteCommodities);
|
|
|
}
|
|
|
|
|
|
/*1*/
|