Commit 33a9b215 by 杨树贤

统计脚本

parent 1fcea9ba
......@@ -3,6 +3,7 @@
namespace App\Console\Commands;
use App\Http\Services\DataService;
use App\Http\Services\StatisticsSkuUploadService;
use Illuminate\Console\Command;
use Illuminate\Foundation\Inspiring;
......@@ -31,14 +32,8 @@ class SetSupplierFollowUp extends Command
public function handle()
{
$service = new DataService();
// $service->transferAttachmentToNewTable();
// $service->importSupplierLevel();
// $service->changeSupplierTypeByIsType();
// $service->transferPayType();
// $service->importSupplierGroup(true);
// $service->changeSupplierType();
// $service->generateYunxinAccount(true);
$service->exportNotUploadSkuSupplier();
// $service->makeSupplierSystemTagAbnormal();
$uploadService = new StatisticsSkuUploadService();
$uploadService->statisticBrandSkuNum();
}
}
......@@ -71,8 +71,6 @@ class DataService
const TYPE_MYS = 2;
//导出供应商
public function exportSupplierForUnionData()
{
......@@ -240,8 +238,7 @@ class DataService
{
ini_set('memory_limit', -1);
$redis = new RedisModel();
$date = Carbon::create(2023, 5, 1, 0, 0, 0);
$halfYearTime = $date->timestamp;
$startTime = Carbon::create(2023, 5, 1, 0, 0, 0)->timestamp;
$endTime = Carbon::create(2024, 1, 1, 0, 0, 0)->timestamp;
$suppliers = SupplierChannelModel::select([
'supplier_id',
......@@ -289,53 +286,8 @@ class DataService
$skuUserDepartment = (new DepartmentService())->getDepartmentNameByUserName($skuUserNameRaw);
}
//获取sku数量最多的前五个品牌
//要去上传记录表
$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));
}
$skuCountByStandardBrandName = StatisticsSkuUploadService::getSortedBrandNameSkuCount($supplier['supplier_code']);
$skuCountByStandardBrandName = array_keys($skuCountByStandardBrandName);
$itemData = [
$supplier['supplier_name'],
array_get(config('fixed.SupplierGroup'), $supplier['supplier_group'], '未设置'),
......@@ -352,22 +304,24 @@ class DataService
array_get($skuCountByStandardBrandName, 3, ''),
array_get($skuCountByStandardBrandName, 4, ''),
];
$excelData[] = $itemData;
}
//近半年内,查出更新过的品牌对应SKU数量最多排名(前100名);需要字段“品牌名称,SKU数量,型号数量”
$excelData2 = [];
$countByBrandName = SkuUploadItem::where('status', 3)->where('create_time', '>=', $halfYearTime)
->where('create_time', '<=', $endTime)
->selectRaw('brand_name,count(distinct(sku_id)) as sku_count,count(distinct(spu_id)) as spu_id_count')
->groupBy('brand_name')->orderBy('sku_count', 'desc')->get()->toArray();
foreach ($countByBrandName as &$value) {
$countByBrandName = [];
$allBrandCountSort = StatisticsSkuUploadService::getAllBrandSkuCountSort();
foreach ($allBrandCountSort as $brandName => $skuCount) {
//然后获取每个品牌的标准品牌
$brandName = $value['brand_name'];
$brandNameAllKey = md5(strtolower($brandName));
$brandId = $redis->hget('brand_name_all', $brandNameAllKey);
if (empty($brandId)) {
$value['standard_brand_id'] = 0;
$countByBrandName[] = [
'standard_brand_id' => 0,
];
continue;
}
//然后找到标准品牌id
......@@ -376,8 +330,12 @@ class DataService
$standardBrand = $redis->hget('standard_brand', $standardBrandId);
$standardBrand = json_decode($standardBrand, true);
$value['standard_brand_name'] = $standardBrand['brand_name'];
$countByBrandName[] = [
'standard_brand_id' => $standardBrandId,
'standard_brand_name' => $standardBrand['brand_name'],
'sku_count' => $skuCount,
];
}
//统计标品的sku数量
$dataByStandardBrandName = collect($countByBrandName)->groupBy('standard_brand_name')->toArray();
$countByStandardBrandName = [];
......@@ -388,11 +346,9 @@ class DataService
$skuCount = 0;
foreach ($standardBrandItem as $value) {
$skuCount = $value['sku_count'];
$goodsNameCount = $value['spu_id_count'];
}
$countByStandardBrandName[$brandName] = [
'sku_count' => $skuCount,
'spu_id_count' => $goodsNameCount,
];
}
......@@ -403,18 +359,20 @@ class DataService
$excel2Header = [
'品牌名称',
'SKU数量',
'型号数量',
//'型号数量',
];
foreach ($countByStandardBrandName as $brandName => $value) {
//去搜索服务获取型号数量
$excelData2[] = [
$brandName,
$value['sku_count'],
$value['spu_id_count'],
];
}
array_unshift($excelData, $header);
array_unshift($excelData2, $excel2Header);
Excel::create('供应商导出', function ($excel) use ($excelData, $excelData2) {
Excel::create('供应商导出评级', function ($excel) use ($excelData, $excelData2) {
$excelData = array_map('array_values', $excelData);
$excelData2 = array_map('array_values', $excelData2);
$excel->sheet('sheet1', function ($sheet) use ($excelData) {
......@@ -423,7 +381,7 @@ class DataService
$excel->sheet('sheet2', function ($sheet) use ($excelData2) {
$sheet->fromArray($excelData2);
});
})->export('xlsx');
})->save('xlsx');
}
//导出2023年有更新过sku得供应商
......
......@@ -41,7 +41,6 @@ class StatisticsSkuUploadService
->where('log_id', $logId)
->where('status', 3)
->groupBy('brand_name')->get();
dd($countByBrandName);
if (empty($countByBrandName)) {
continue;
}
......@@ -70,4 +69,39 @@ class StatisticsSkuUploadService
])->where('id', '>=', $firstId)->where('id', '<', $lastId)->get()->toArray();
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 ()
});
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