Commit 33a9b215 by 杨树贤

统计脚本

parent 1fcea9ba
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\Http\Services\DataService; use App\Http\Services\DataService;
use App\Http\Services\StatisticsSkuUploadService;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Foundation\Inspiring; use Illuminate\Foundation\Inspiring;
...@@ -31,14 +32,8 @@ class SetSupplierFollowUp extends Command ...@@ -31,14 +32,8 @@ class SetSupplierFollowUp extends Command
public function handle() public function handle()
{ {
$service = new DataService(); $service = new DataService();
// $service->transferAttachmentToNewTable();
// $service->importSupplierLevel(); $uploadService = new StatisticsSkuUploadService();
// $service->changeSupplierTypeByIsType(); $uploadService->statisticBrandSkuNum();
// $service->transferPayType();
// $service->importSupplierGroup(true);
// $service->changeSupplierType();
// $service->generateYunxinAccount(true);
$service->exportNotUploadSkuSupplier();
// $service->makeSupplierSystemTagAbnormal();
} }
} }
...@@ -71,8 +71,6 @@ class DataService ...@@ -71,8 +71,6 @@ class DataService
const TYPE_MYS = 2; const TYPE_MYS = 2;
//导出供应商 //导出供应商
public function exportSupplierForUnionData() public function exportSupplierForUnionData()
{ {
...@@ -240,8 +238,7 @@ class DataService ...@@ -240,8 +238,7 @@ class DataService
{ {
ini_set('memory_limit', -1); ini_set('memory_limit', -1);
$redis = new RedisModel(); $redis = new RedisModel();
$date = Carbon::create(2023, 5, 1, 0, 0, 0); $startTime = Carbon::create(2023, 5, 1, 0, 0, 0)->timestamp;
$halfYearTime = $date->timestamp;
$endTime = Carbon::create(2024, 1, 1, 0, 0, 0)->timestamp; $endTime = Carbon::create(2024, 1, 1, 0, 0, 0)->timestamp;
$suppliers = SupplierChannelModel::select([ $suppliers = SupplierChannelModel::select([
'supplier_id', 'supplier_id',
...@@ -289,53 +286,8 @@ class DataService ...@@ -289,53 +286,8 @@ class DataService
$skuUserDepartment = (new DepartmentService())->getDepartmentNameByUserName($skuUserNameRaw); $skuUserDepartment = (new DepartmentService())->getDepartmentNameByUserName($skuUserNameRaw);
} }
//获取sku数量最多的前五个品牌 $skuCountByStandardBrandName = StatisticsSkuUploadService::getSortedBrandNameSkuCount($supplier['supplier_code']);
//要去上传记录表 $skuCountByStandardBrandName = array_keys($skuCountByStandardBrandName);
$logIds = SkuUploadLogModel::where('supplier_code', $supplier['supplier_code'])
->where('create_time', '>=', $halfYearTime)
->where('create_time', '<=', $endTime)
->where('status', 3)->pluck('id')->toArray();
$skuCountByStandardBrandName = [];
if (!empty($logIds)) {
$uploadLogItemGroupByBrandName = SkuUploadItem::selectRaw('brand_name,COUNT(DISTINCT sku_id) as sku_count')
->where('brand_name', '!=', '')
->whereIn('log_id', $logIds)
->groupBy('brand_name')
->get()->toArray();
foreach ($uploadLogItemGroupByBrandName as &$value) {
//然后获取每个品牌的标准品牌
//$redis->hset('brand_name_all', md5(strtolower($find['brand_name'])), $brandId);
$brandName = $value['brand_name'];
$brandNameAllKey = md5(strtolower($brandName));
$brandId = $redis->hget('brand_name_all', $brandNameAllKey);
if (empty($brandId)) {
$value['standard_brand_id'] = 0;
continue;
}
//然后找到标准品牌id
$standardBrandId = $redis->hget('standard_brand_mapping', $brandId);
$value['standard_brand_id'] = $standardBrandId;
$standardBrand = $redis->hget('standard_brand', $standardBrandId);
$standardBrand = json_decode($standardBrand, true);
$value['standard_brand_name'] = $standardBrand['brand_name'];
}
//统计标品的sku数量
$uploadLogItemGroupByStandardBrandName = collect($uploadLogItemGroupByBrandName)->groupBy('standard_brand_name')->toArray();
$skuCountByStandardBrandName = [];
foreach ($uploadLogItemGroupByStandardBrandName as $brandName => $standardBrandItem) {
if (empty($brandName)) {
continue;
}
$skuCount = 0;
foreach ($standardBrandItem as $value) {
$skuCount = $value['sku_count'];
}
$skuCountByStandardBrandName[$brandName] = $skuCount;
}
arsort($skuCountByStandardBrandName);
$skuCountByStandardBrandName = array_values(array_flip($skuCountByStandardBrandName));
}
$itemData = [ $itemData = [
$supplier['supplier_name'], $supplier['supplier_name'],
array_get(config('fixed.SupplierGroup'), $supplier['supplier_group'], '未设置'), array_get(config('fixed.SupplierGroup'), $supplier['supplier_group'], '未设置'),
...@@ -352,22 +304,24 @@ class DataService ...@@ -352,22 +304,24 @@ class DataService
array_get($skuCountByStandardBrandName, 3, ''), array_get($skuCountByStandardBrandName, 3, ''),
array_get($skuCountByStandardBrandName, 4, ''), array_get($skuCountByStandardBrandName, 4, ''),
]; ];
$excelData[] = $itemData; $excelData[] = $itemData;
} }
//近半年内,查出更新过的品牌对应SKU数量最多排名(前100名);需要字段“品牌名称,SKU数量,型号数量” //近半年内,查出更新过的品牌对应SKU数量最多排名(前100名);需要字段“品牌名称,SKU数量,型号数量”
$excelData2 = []; $excelData2 = [];
$countByBrandName = SkuUploadItem::where('status', 3)->where('create_time', '>=', $halfYearTime) $countByBrandName = [];
->where('create_time', '<=', $endTime) $allBrandCountSort = StatisticsSkuUploadService::getAllBrandSkuCountSort();
->selectRaw('brand_name,count(distinct(sku_id)) as sku_count,count(distinct(spu_id)) as spu_id_count') foreach ($allBrandCountSort as $brandName => $skuCount) {
->groupBy('brand_name')->orderBy('sku_count', 'desc')->get()->toArray();
foreach ($countByBrandName as &$value) {
//然后获取每个品牌的标准品牌 //然后获取每个品牌的标准品牌
$brandName = $value['brand_name'];
$brandNameAllKey = md5(strtolower($brandName)); $brandNameAllKey = md5(strtolower($brandName));
$brandId = $redis->hget('brand_name_all', $brandNameAllKey); $brandId = $redis->hget('brand_name_all', $brandNameAllKey);
if (empty($brandId)) { if (empty($brandId)) {
$value['standard_brand_id'] = 0; $value['standard_brand_id'] = 0;
$countByBrandName[] = [
'standard_brand_id' => 0,
];
continue; continue;
} }
//然后找到标准品牌id //然后找到标准品牌id
...@@ -376,8 +330,12 @@ class DataService ...@@ -376,8 +330,12 @@ class DataService
$standardBrand = $redis->hget('standard_brand', $standardBrandId); $standardBrand = $redis->hget('standard_brand', $standardBrandId);
$standardBrand = json_decode($standardBrand, true); $standardBrand = json_decode($standardBrand, true);
$value['standard_brand_name'] = $standardBrand['brand_name']; $value['standard_brand_name'] = $standardBrand['brand_name'];
$countByBrandName[] = [
'standard_brand_id' => $standardBrandId,
'standard_brand_name' => $standardBrand['brand_name'],
'sku_count' => $skuCount,
];
} }
//统计标品的sku数量 //统计标品的sku数量
$dataByStandardBrandName = collect($countByBrandName)->groupBy('standard_brand_name')->toArray(); $dataByStandardBrandName = collect($countByBrandName)->groupBy('standard_brand_name')->toArray();
$countByStandardBrandName = []; $countByStandardBrandName = [];
...@@ -388,11 +346,9 @@ class DataService ...@@ -388,11 +346,9 @@ class DataService
$skuCount = 0; $skuCount = 0;
foreach ($standardBrandItem as $value) { foreach ($standardBrandItem as $value) {
$skuCount = $value['sku_count']; $skuCount = $value['sku_count'];
$goodsNameCount = $value['spu_id_count'];
} }
$countByStandardBrandName[$brandName] = [ $countByStandardBrandName[$brandName] = [
'sku_count' => $skuCount, 'sku_count' => $skuCount,
'spu_id_count' => $goodsNameCount,
]; ];
} }
...@@ -403,18 +359,20 @@ class DataService ...@@ -403,18 +359,20 @@ class DataService
$excel2Header = [ $excel2Header = [
'品牌名称', '品牌名称',
'SKU数量', 'SKU数量',
'型号数量', //'型号数量',
]; ];
foreach ($countByStandardBrandName as $brandName => $value) { foreach ($countByStandardBrandName as $brandName => $value) {
//去搜索服务获取型号数量
$excelData2[] = [ $excelData2[] = [
$brandName, $brandName,
$value['sku_count'], $value['sku_count'],
$value['spu_id_count'],
]; ];
} }
array_unshift($excelData, $header); array_unshift($excelData, $header);
array_unshift($excelData2, $excel2Header); array_unshift($excelData2, $excel2Header);
Excel::create('供应商导出', function ($excel) use ($excelData, $excelData2) { Excel::create('供应商导出评级', function ($excel) use ($excelData, $excelData2) {
$excelData = array_map('array_values', $excelData); $excelData = array_map('array_values', $excelData);
$excelData2 = array_map('array_values', $excelData2); $excelData2 = array_map('array_values', $excelData2);
$excel->sheet('sheet1', function ($sheet) use ($excelData) { $excel->sheet('sheet1', function ($sheet) use ($excelData) {
...@@ -423,7 +381,7 @@ class DataService ...@@ -423,7 +381,7 @@ class DataService
$excel->sheet('sheet2', function ($sheet) use ($excelData2) { $excel->sheet('sheet2', function ($sheet) use ($excelData2) {
$sheet->fromArray($excelData2); $sheet->fromArray($excelData2);
}); });
})->export('xlsx'); })->save('xlsx');
} }
//导出2023年有更新过sku得供应商 //导出2023年有更新过sku得供应商
......
...@@ -41,7 +41,6 @@ class StatisticsSkuUploadService ...@@ -41,7 +41,6 @@ class StatisticsSkuUploadService
->where('log_id', $logId) ->where('log_id', $logId)
->where('status', 3) ->where('status', 3)
->groupBy('brand_name')->get(); ->groupBy('brand_name')->get();
dd($countByBrandName);
if (empty($countByBrandName)) { if (empty($countByBrandName)) {
continue; continue;
} }
...@@ -70,4 +69,39 @@ class StatisticsSkuUploadService ...@@ -70,4 +69,39 @@ class StatisticsSkuUploadService
])->where('id', '>=', $firstId)->where('id', '<', $lastId)->get()->toArray(); ])->where('id', '>=', $firstId)->where('id', '<', $lastId)->get()->toArray();
return $items; return $items;
} }
public static function getSortedBrandNameSkuCount($supplierCode)
{
$redis = new RedisModel();
$data = $redis->hget('half_year_brand_sku_upload_statistics', $supplierCode);
if (empty($data)) {
return [];
}
$data = json_decode($data, true);
arsort($data);
return $data;
}
//获取所有品牌的sku数量排序
public static function getAllBrandSkuCountSort()
{
$redis = new RedisModel();
$data = $redis->hgetall('half_year_brand_sku_upload_statistics');
$brandSkuCountMap = [];
foreach ($data as $supplierCode => $item) {
$item = json_decode($item, true);
foreach ($item as $brandName => $skuCount) {
if (isset($brandSkuCountMap[$brandName])) {
$brandSkuCountMap[$brandName] += $skuCount;
} else {
$brandSkuCountMap[$brandName] = $skuCount;
}
}
}
arsort($brandSkuCountMap);
return array_slice($brandSkuCountMap, 0, 100);
}
} }
...@@ -82,5 +82,6 @@ Route::group(['middleware' => ['external'], 'namespace' => 'Sync'], function () ...@@ -82,5 +82,6 @@ Route::group(['middleware' => ['external'], 'namespace' => 'Sync'], function ()
}); });
Route::match(['get', 'post'], '/test', function () { Route::match(['get', 'post'], '/test', function () {
(new \App\Http\Services\StatisticsSkuUploadService())->statisticBrandSkuNum(); //(new \App\Http\Services\StatisticsSkuUploadService())->statisticBrandSkuNum();
(new \App\Http\Services\DataService())->exportSupplierDataForLevel();
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment