Commit 52a4a5bd by 杨树贤

统计

parent 9310690b
......@@ -71,30 +71,7 @@ class DataService
const TYPE_MYS = 2;
//处理供应商类型的数据
public function dealSupplierTypeData($isUpdate = false)
{
//1.1 当前供应商系统中,没有品质保证协议的供应商,类型统一修正为临时供应商,注意:之前的代理商跟原厂性质的供应商类别保持不变。
$suppliers = SupplierChannelModel::where('is_type', 0)->whereNotIn('supplier_group', [1, 4])
->get()->toArray();
$skipSupplierNames = config('field.SkipChangeSupplierTypeNames');
foreach ($suppliers as $supplier) {
if (in_array($supplier['supplier_name'], $skipSupplierNames)) {
echo "代购供应商 ${supplier['supplier_name']},不能修改为临时,跳过";
}
//判断是否有品质保证协议,没有的话修改为临时类型
$hasQualityAssuranceAgreement = SupplierAttachmentsModel::where('supplier_id', $supplier['supplier_id'])
->where('field_name', 'quality_assurance_agreement')->exists();
if (!$hasQualityAssuranceAgreement) {
echo "供应商 ${supplier['supplier_code']} 没有品质保证协议,转换类型为临时;" . PHP_EOL;
if ($isUpdate) {
SupplierChannelModel::where('supplier_id', $supplier['supplier_id'])->update([
'supplier_type' => 2
]);
}
}
}
}
//导出供应商
public function exportSupplierForUnionData()
......@@ -180,182 +157,6 @@ class DataService
})->store('csv');
}
public function exportSupplierJsonForUnited()
{
$suppliers = SupplierChannelModel::where('is_type', 0)
->pluck('supplier_group', 'supplier_id')->where('create_time', '>', 1655778780)
->where('update_time', '>', 1655778780)->where('group_code', '!=', '')->toArray();
$exportJson = [];
$items = array_map(function ($value) {
$value = array_get(config('fixed.SupplierGroup'), $value);
return $value;
}, $suppliers);
$jsonItems = [];
foreach ($items as $key => $item) {
$jsonItems[] = [
'i' => $key,
'n' => $item
];
}
$exportJson = [
'sysId' => 1,
'cType' => 2,
'list' => $jsonItems,
];
echo json_encode($exportJson);
}
//禁用(没有集团编码)供应商
public function disableNoUnitedSupplier()
{
SupplierChannelModel::where('is_type', 0)->where('group_code', '')
->update([
'is_type' => 2,
'update_time' => time(),
]);
}
public function checkCompanyName()
{
$suppliers = SupplierChannelModel::where('group_code', '')->where('is_type', 0)
->where('create_time', '>', 1655778780)->where('update_time', '>', 1655778780)->get();
foreach ($suppliers as $supplier) {
$company = (new CompanyService())->getCompanyInfo($supplier['supplier_name'], '', 1);
if ($company) {
echo $supplier['supplier_name'] . PHP_EOL;
sleep(0.1);
}
}
}
//导入公司性质
public function disableSupplier($isUpdate = false)
{
ini_set('memory_limit', -1);
$filePath = public_path('data') . DIRECTORY_SEPARATOR . 'supplier_disable.xlsx';
try {
Excel::selectSheetsByIndex(1)->load($filePath, function ($reader) use ($isUpdate) {
$reader->sheet('禁用', function () use ($reader, $isUpdate) {
$num = 0;
foreach ($reader->all()->toArray() as $key => $item) {
$supplierId = trim($item[0]);
$supplierName = trim($item[1]);
$supplier = SupplierChannelModel::where('supplier_id', $supplierId)->first();
if (empty($supplier)) {
echo '供应商不存在 : ' . $supplierId . '__' . $supplierName . PHP_EOL;
continue;
}
$supplier = $supplier->toArray();
if ($supplier['group_code']) {
echo '已经有集团编码,跳过 : ' . $supplier['supplier_name'] . PHP_EOL;
continue;
}
if ($supplierName != $supplier['supplier_name']) {
echo '供应商名称遭到更改 : ' . $supplierName . '___' . $supplier['supplier_name'] . PHP_EOL;
continue;
}
$num++;
if ($isUpdate) {
SupplierChannelModel::where('supplier_id', $supplierId)->update([
'is_type' => 0,
'status' => SupplierChannelModel::STATUS_DISABLE,
]);
}
}
echo "一共处理 $num 家供应商" . PHP_EOL;
});
});
} catch (\Exception $exception) {
dd($exception);
}
}
//导出供应商基础信息
public function exportSuppliers()
{
$suppliers = SupplierChannelModel::where('is_type', 0)->select([
'supplier_name',
'supplier_group',
'status',
])->get()->toArray();
$excelData = [];
$header = [
'供应商名称',
'供应商性质',
'供应商状态',
];
foreach ($suppliers as $supplier) {
$itemData = [
$supplier['supplier_name'],
array_get(config('fixed.SupplierGroup'), $supplier['supplier_group']),
array_get(config('fixed.SupplierStatus'), $supplier['status']),
];
$excelData[] = $itemData;
}
array_unshift($excelData, $header);
Excel::create('供应商导出', function ($excel) use ($excelData) {
$excel->sheet('sheet1', function ($sheet) use ($excelData) {
$sheet->fromArray($excelData);
});
})->export('xlsx');
}
public function repairSupplierName()
{
$map = [
8581 => 'Holdwell Electronics(HK) Limited',
10245 => 'Micropower Communication Co.,Limited',
12021 => '深圳市卓美信电子科技有限公司',
12430 => '華创进出口有限公司',
12562 => 'Martec Unipersonale S.r.l.',
12577 => 'INELTEK Industrieelektronik und Technologien Ges.m.b.H.',
];
foreach ($map as $key => $value) {
SupplierChannelModel::where('supplier_id', $key)->update(['supplier_name' => $value]);
(new SyncSupplierService())->syncSupplierToUnited($key);
}
}
//导出重复的供应商
public function findDuplicateSupplier()
{
$suppliers = \DB::connection('web')->select('SELECT a.* FROM lie_supplier_channel a INNER JOIN (SELECT supplier_name FROM lie_supplier_channel GROUP BY supplier_name HAVING COUNT(supplier_id) > 1) b ON a.supplier_name = b.supplier_name ORDER BY supplier_name DESC');
$header = [
'供应商编码',
'供应商名称',
'采购员列表',
];
$excelData = [];
foreach ($suppliers as $supplier) {
$allChannelUserName = '';
$channelUids = $supplier['channel_uid'] ? explode(',', $supplier['channel_uid']) : [];
if ($channelUids) {
$channelUsers = (new AdminUserService())->getAdminUserListByCodeIds($channelUids);
if (!empty($channelUsers)) {
$allChannelUserName = implode(',', array_column($channelUsers, 'name'));
}
}
$excelData[] = [
$supplier['supplier_code'],
$supplier['supplier_name'],
$allChannelUserName,
];
}
array_unshift($excelData, $header);
Excel::create('重复供应商导出', function ($excel) use ($excelData) {
$excel->sheet('sheet1', function ($sheet) use ($excelData) {
$sheet->fromArray($excelData);
});
})->export('xlsx');
}
public function exportHalfYearSupplier()
{
......@@ -402,849 +203,6 @@ class DataService
})->export('csv');
}
//修复等级E的问题
public function repairLevelESupplierData($isUpdate = false)
{
$suppliers = SupplierChannelModel::where('level', 'E')->select([
'supplier_id',
'supplier_name',
'supplier_type',
])->get()->toArray();
foreach ($suppliers as $supplier) {
//去除供应商E等级,之前为E等级并且无品质保证协议的正式供应商,统一改为临时供应商。有品质保证协议的正式供应商继续保持为正式。
$hasQualityAssuranceAgreement = SupplierAttachmentsModel::where('supplier_id', $supplier['supplier_id'])
->where('field_name', 'quality_assurance_agreement')->exists();
if (!$hasQualityAssuranceAgreement && !in_array($supplier['supplier_name'],
config('field.SkipChangeSupplierTypeNames'))) {
dump("没有品质保证协议,修改为临时供应商 : " . $supplier['supplier_name']);
if ($isUpdate) {
SupplierChannelModel::where('supplier_id', $supplier['supplier_id'])->update([
'level' => '',
'supplier_type' => SupplierChannelModel::SUPPLIER_TYPE_TEMPORARY
]);
}
} else {
if ($isUpdate) {
SupplierChannelModel::where('supplier_id', $supplier['supplier_id'])->update([
'level' => '',
]);
}
}
}
}
//同步基石的sku上传内部编码到供应商的线上采购员
public function syncFootstoneSkuUploadEncoded($isUpdate = false)
{
$suppliers = SupplierChannelModel::select(['supplier_code', 'yunxin_channel_uid'])->where('is_type',
0)->get()->toArray();
foreach ($suppliers as $supplier) {
//去基石的上传表找
$lastEncoded = SkuUploadLogModel::where('supplier_code', $supplier['supplier_code'])
->orderBy('create_time', 'desc')->value('encoded');
if (!$lastEncoded) {
continue;
}
//判断是否已经有线上采购员了,有的话跳过
$hasSkuChannelUid = SupplierChannelModel::where('supplier_code', $supplier['supplier_code'])
->where('yunxin_channel_uid', '!=', 0)->exists();
if ($hasSkuChannelUid) {
echo "已经有线上采购员,供应商编码为 : ${supplier['supplier_code']} , 跳过" . PHP_EOL;
continue;
}
echo "修改线上采购员 : ${supplier['supplier_code']} => ${lastEncoded}" . PHP_EOL;
if ($isUpdate) {
SupplierChannelModel::where('supplier_code', $supplier['supplier_code'])
->update(['yunxin_channel_uid' => $lastEncoded]);
}
}
}
//导入公司性质
public function importSupplierLevel($isUpdate = false)
{
ini_set('memory_limit', -1);
$filePath = public_path('data') . DIRECTORY_SEPARATOR . 'supplier_level_20220816.csv';
try {
Excel::selectSheetsByIndex(0)->load($filePath, function ($reader) use ($isUpdate) {
$reader->sheet('supplier_level_20220816', function () use ($reader, $isUpdate) {
$suppliers = $reader->all()->toArray();
foreach ($suppliers as $supplier) {
$supplierCode = trim($supplier[1]);
$level = trim($supplier[16]);
echo "修改供应商 $supplierCode 等级为 $level" . PHP_EOL;
if ($isUpdate) {
SupplierChannelModel::where('supplier_code', $supplierCode)->update([
'level' => $level,
]);
}
}
});
});
} catch (\Exception $exception) {
dd($exception);
}
}
public static function exportSupplierByDepartment($topDepartmentId)
{
if ($topDepartmentId == 47) {
$append = '联营一部';
}
if ($topDepartmentId == 51) {
$append = '自营采购部';
}
if ($topDepartmentId == 72) {
$append = '联营二部';
}
Excel::create('采购供应商数据导出_' . $append, function ($excel) use ($topDepartmentId) {
$departmentIds = DepartmentModel::where('parent_id', $topDepartmentId)->pluck('department_id')->toArray();
$userIds = UserInfoModel::whereIn('department_id', $departmentIds)->pluck('userId')->toArray();
$suppliers2022 = $suppliersZhangQi2022 = $suppliersAll = $suppliersZhangQi = [];
//找出相关用户创建的所有供应商
$suppliersAll = SupplierChannelModel::whereIn('create_uid', $userIds)->where('is_type',
0)->get()->toArray();
foreach ($suppliersAll as $supplier) {
$createTime = date('Y-m-d H:i:s', $supplier['create_time']);
$creatName = UserInfoModel::where('userId', $supplier['create_uid'])->value('name');
$supplierGroup = array_get(config('fixed.SupplierGroup'), $supplier['supplier_group']);
$data = [
$creatName,
$supplier['supplier_name'],
$supplierGroup,
$createTime,
];
if ($supplier['create_time'] >= 1640966400) {
$suppliers2022[] = $data;
if ($supplier['pay_type'] == 1) {
$suppliersZhangQi2022[] = $data;
}
}
if ($supplier['pay_type'] == 1) {
$suppliersZhangQi[] = $data;
}
$suppliersAllList[] = $data;
}
$header = ['创建人', '供应商名称', '供应商性质', '创建时间'];
// dd($suppliersZhangQi,$header);
// $excel->sheet('2022年创建的供应商', function ($sheet) use ($header, $suppliers2022) {
// array_unshift($suppliers2022, $header);
// $sheet->fromArray($suppliers2022);
// });
// $excel->sheet('2022年创建的账期供应商', function ($sheet) use ($header, $suppliersZhangQi2022) {
// array_unshift($suppliersZhangQi2022, $header);
// $sheet->fromArray($suppliersZhangQi2022);
// });
// $excel->sheet('全部账期供应商', function ($sheet) use ($header, $suppliersZhangQi) {
// array_unshift($suppliersZhangQi, $header);
// $sheet->fromArray($suppliersZhangQi);
// });
$excel->sheet('全部供应商', function ($sheet) use ($header, $suppliersAllList) {
array_unshift($suppliersAllList, $header);
$sheet->fromArray($suppliersAllList);
});
})->store('xlsx');
}
//导出供应商
public function exportSupplier()
{
ini_set('memory_limit', -1);
$excelData = [];
$header = [
'供应商编码',
'供应商名称',
'供应商性质',
'采购员',
'线上采购员',
'最新修改人',
'创建人',
'创建时间',
'首次上传sku时间',
];
$users = (new IntracodeModel())->getSampleEncode(true);
$map = [
'source_type' => 'all',
'uploaded_sku' => 1,
'is_export' => 1,
'is_type' => 0,
];
$suppliers = SupplierChannelModel::where('uploaded_sku', 1)->where('is_type',
0)->whereRaw('supplier_name NOT LIKE "%-1"')->get()->toArray();
//获取最近修改信息
$logModel = new LogModel();
foreach ($suppliers as $supplier) {
$log = $logModel->where('supplier_id', $supplier['supplier_id'])
->where('type', LogModel::UPDATE_OPERATE)->where('ignore_audit_check', 0)
->orderBy('id', 'desc')->first();
$supplier['last_update_name'] = $log['admin_name'] ?: '无';
if (empty($supplier['create_name'])) {
$userInfo = (new AdminUserService())->getAdminUserInfo($supplier['create_uid']);
$supplier['create_name'] = array_get($userInfo, 'name');
}
$supplier['create_time'] = $supplier['create_time'] ? date('Y-m-d H:i:s', $supplier['create_time']) : '';
$firstUploadSkuTime = SkuService::getUploadTimeBySupplierCode($supplier['supplier_code'], 'first');
$supplier['first_upload_sku_time'] = $firstUploadSkuTime ? date('Y-m-d H:i:s', $firstUploadSkuTime) : '';
$supplier['supplier_group'] = array_get(config('fixed.SupplierGroup'), $supplier['supplier_group'], '未设置');
$itemData = [
$supplier['supplier_code'],
$supplier['supplier_name'],
$supplier['supplier_group'],
(new SupplierTransformer())->getChannelUserNames($supplier['channel_uid']),
array_get($users, $supplier['yunxin_channel_uid']),
$supplier['last_update_name'],
$supplier['create_name'],
$supplier['create_time'],
$supplier['first_upload_sku_time'],
];
$excelData[] = $itemData;
}
array_unshift($excelData, $header);
Excel::create('供应商导出_2023423', function ($excel) use ($excelData) {
$excel->sheet('sheet1', function ($sheet) use ($excelData) {
$sheet->fromArray($excelData);
});
})->store('csv');
}
public function exportNotUploadSkuSupplier()
{
ini_set('memory_limit', -1);
// $time = strtotime('2021-1-1');
// $suppliers = SupplierChannelModel::where('is_type', 0)->where('uploaded_sku', 1)
$time = strtotime('2021-1-1');
$suppliers = SupplierChannelModel::where('is_type', 0)->where('uploaded_sku', '!=', 1)
->whereRaw('supplier_name NOT LIKE "%-1"')
->where('create_time', '>', $time)->get();
$excelData = [];
$header = [
'供应商编码',
'供应商名称',
'采购员',
'部门',
];
$intraCodeModel = new IntracodeModel();
$users = $intraCodeModel->getSampleName(true);
foreach ($suppliers as $supplier) {
//去采购系统找采购单金额最多的用户
$purchaseAmountMap = [];
$channelUids = PurchaseOrderModel::where('supplier_id', $supplier['supplier_id'])->pluck('purchase_uid')->unique()->toArray();
if (empty($channelUids)) {
continue;
}
//去获取对应的uid的采购金额
foreach ($channelUids as $channelUid) {
//去采购系统取采购单金额
$purchaseOrders = PurchaseOrderModel::where('purchase_uid', $channelUid)->select([
'purchase_amount',
'exchange_rate',
'currency'
])->where('status', '!=', -3)->get()->toArray();
//计算出采购金额
$totalAmount = 0;
foreach ($purchaseOrders as $purchaseOrder) {
if ($purchaseOrder['currency'] == 1) {
$totalAmount += $purchaseOrder['purchase_amount'] * $purchaseOrder['exchange_rate'] * 1.13;
} else {
$totalAmount += $purchaseOrder['purchase_amount'] * $purchaseOrder['exchange_rate'];
}
}
$purchaseAmountMap[$channelUid] = $totalAmount;
}
arsort($purchaseAmountMap);
dump($purchaseAmountMap);
$maxAmountPurchaseAdminId = array_keys($purchaseAmountMap)[0];
dump($maxAmountPurchaseAdminId);
$skuUserNameRaw = UserInfoModel::where('userId', $maxAmountPurchaseAdminId)->value('name');
$skuUserDepartment = (new DepartmentService())->getDepartmentNameByUserName($skuUserNameRaw);
$itemData = [
$supplier['supplier_code'],
$supplier['supplier_name'],
$skuUserNameRaw,
$skuUserDepartment
];
$excelData[] = $itemData;
}
array_unshift($excelData, $header);
Excel::create('供应商无sku做单导出', function ($excel) use ($excelData) {
$excel->sheet('sheet1', function ($sheet) use ($excelData) {
$sheet->fromArray($excelData);
});
})->store('csv');
}
public function exportSupplierBySourceType()
{
// //找出所有大数据的供应商
$bigDataSuppliers = DataManageModel::where('is_type', 0)->get()->toArray();
// //如果platform_type不等于6,都是API采集
// //剩下的是普通采集的
// $apiSourceSupplierCodeList = $paSourceSupplierCodeList = [];
// foreach ($bigDataSuppliers as $supplier) {
// if ($supplier['platform_type'] != 6) {
// $apiSourceSupplierCodeList[] = $supplier['canal'];
// } else {
// $paSourceSupplierCodeList[] = $supplier['canal'];
// }
// }
$intraCodeModel = new IntracodeModel();
$users = $intraCodeModel->getSampleName(true);
$header = ['供应商编码', '供应商名称', '线上采购员', '采购组别'];
//
// //导出sku接入方式为专营API
// $excelData = [];
// //导出sku接入方式为专营API
// foreach ($apiSourceSupplierCodeList as $code) {
// $supplier = SupplierChannelModel::where('supplier_code', $code)->first();
// if (empty($supplier)) {
// continue;
// }
// $skuUserNameRaw = array_get($users, $supplier['yunxin_channel_uid']);
// $supplier = $supplier->toArray();
// $departmentName = (new DepartmentService())->getDepartmentNameByUserName($skuUserNameRaw);
// $excelData[] = [
// $supplier['supplier_code'],
// $supplier['supplier_name'],
// $skuUserNameRaw,
// $departmentName,
// ];
// }
// array_unshift($excelData, $header);
// Excel::create('sku接入方式为专营API', function ($excel) use ($excelData) {
// $excel->sheet('sheet1', function ($sheet) use ($excelData) {
// $sheet->fromArray($excelData);
// });
// })->store('csv');
//
// //专营采集的供应商
// $excelData = [];
// foreach ($paSourceSupplierCodeList as $code) {
// $supplier = SupplierChannelModel::where('supplier_code', $code)->first();
// if (empty($supplier)) {
// continue;
// }
// $skuUserNameRaw = array_get($users, $supplier['yunxin_channel_uid']);
// $supplier = $supplier->toArray();
// $departmentName = (new DepartmentService())->getDepartmentNameByUserName($skuUserNameRaw);
// $excelData[] = [
// $supplier['supplier_code'],
// $supplier['supplier_name'],
// $skuUserNameRaw,
// $departmentName,
// ];
// }
// array_unshift($excelData, $header);
// Excel::create('专营采集的供应商', function ($excel) use ($excelData) {
// $excel->sheet('sheet1', function ($sheet) use ($excelData) {
// $sheet->fromArray($excelData);
// });
// })->store('csv');
//供应商性质为“分销平台”的有上传过sku的供应商
$excelData = [];
$uploadedSkuSuppliers = SupplierChannelModel::where('supplier_group', 5)
->where('is_type', 0)->where('uploaded_sku', 1)->get()->toArray();
foreach ($uploadedSkuSuppliers as $supplier) {
$supplier = SupplierChannelModel::where('supplier_code', $supplier['supplier_code'])->first();
if (empty($supplier)) {
continue;
}
$skuUserNameRaw = array_get($users, $supplier['yunxin_channel_uid']);
$supplier = $supplier->toArray();
$departmentName = (new DepartmentService())->getDepartmentNameByUserName($skuUserNameRaw);
$excelData[] = [
$supplier['supplier_code'],
$supplier['supplier_name'],
$skuUserNameRaw,
$departmentName,
];
}
array_unshift($excelData, $header);
Excel::create('供应商性质为“分销平台”的有上传过sku的供应商', function ($excel) use ($excelData) {
$excel->sheet('sheet1', function ($sheet) use ($excelData) {
$sheet->fromArray($excelData);
});
})->store('csv');
}
//获取非原厂,非api对接的,有sku的正式供应商列表
public function getOffShelfSupplierList()
{
$suppliers = SupplierChannelModel::where('sku_num', '>', 0)->where('supplier_group', '!=', 4)
->where('is_type', 0)->get()->toArray();
$supplierList = [];
foreach ($suppliers as $supplier) {
$isApiSupplier = DataManageModel::where('canal', $supplier['supplier_code'])->where('is_type', 0)->exists();
if ($isApiSupplier) {
continue;
}
$supplierList[] = $supplier['supplier_code'];
}
echo json_encode($supplierList);
}
//获取上传了平台平台合作协议的供应商
public function exportHasCooperationAgreementSupplierList()
{
$header = [
'供应商编码',
'供应商名称',
'供应商性质',
'线上采购员',
'组别',
];
$intraCodeModel = new IntracodeModel();
$users = $intraCodeModel->getSampleName(true);
$excelData = [];
$attachments = SupplierAttachmentsModel::where('field_name', 'cooperation_agreement')->groupBy('supplier_id')->get()->toArray();
foreach ($attachments as $attachment) {
$supplier = SupplierChannelModel::where('supplier_id', $attachment['supplier_id'])->first()->toArray();
if ($supplier['is_type'] == 1) {
continue;
}
$skuUserNameRaw = array_get($users, $supplier['yunxin_channel_uid']);
$departmentName = (new DepartmentService())->getDepartmentNameByUserName($skuUserNameRaw);
$excelData[] = [
$supplier['supplier_code'],
$supplier['supplier_name'],
array_get(config('fixed.SupplierGroup'), $supplier['supplier_group'], '未设置'),
$skuUserNameRaw,
$departmentName
];
}
array_unshift($excelData, $header);
Excel::create('上传了“平台平台合作协议”的供应商', function ($excel) use ($excelData) {
$excel->sheet('sheet1', function ($sheet) use ($excelData) {
$sheet->fromArray($excelData);
});
})->export('csv');
}
//初始化主营品牌到上传限制
public function initialMainBrandsToSkuRuler()
{
$suppliers = SupplierChannelModel::where('main_brands', '!=', '')->get()->toArray();
$redis = new RedisModel();
foreach ($suppliers as $supplier) {
// if (DataManageModel::where('canal', $supplier['supplier_code'])->where('is_type', 0)->exists()) {
// dump("大数据供应商");
// continue;
// }
if (trim($supplier['main_brands'], ',')) {
$mainBrands = explode(',', trim($supplier['main_brands'], ','));
$ruler = $redis->hget('supplier_sku_upload_ruler_v2', $supplier['supplier_id']);
if ($ruler) {
$ruler = json_decode($ruler, true);
$ruler['upload_allow_brands'] = $mainBrands;
} else {
$ruler['upload_allow_brands'] = $mainBrands;
}
dump($supplier['supplier_id'], $ruler);
$redis->hset('supplier_sku_upload_ruler_v2', $supplier['supplier_id'], json_encode($ruler));
}
}
}
//初始化品牌数量限制(还要跳过API对接的供应商)
public function initialMainBrandsLimit()
{
$suppliers = SupplierChannelModel::where('is_type', 0)->where('main_brands_limit', 0)->get()->toArray();
foreach ($suppliers as $supplier) {
//判断是否是API对接的供应商
if (DataManageModel::where('canal', $supplier['supplier_code'])->where('is_type', 0)->exists()) {
dump("大数据供应商");
continue;
}
//获取具体限制数量
$mainBrandsLimit = array_get(config('field.SupplierGroupMainBrandsLimitMap'), $supplier['supplier_group'], -1);
SupplierChannelModel::where('supplier_id', $supplier['supplier_id'])->update([
'main_brands_limit' => $mainBrandsLimit,
]);
}
}
public function statisticsSkuNumber()
{
$excludeSupplierCodeList = [
'L0004938',
'L0007913',
'L0011546',
'L0001175',
'L0007243',
'L0015040',
];
$dataManagerSupplierCodeList = DataManageModel::where('is_type', 0)->where('canal', '!=', '')->pluck('canal')->toArray();
$excludeSupplierCodeList = array_merge($excludeSupplierCodeList, $dataManagerSupplierCodeList);
//sku上架总数含api接口
$skuNum1 = SupplierChannelModel::where('is_type', 0)->where('supplier_group', '!=', 4)->whereNotIn('supplier_name', config('field.SkipChangeSupplierTypeNames'))->whereNotIn('supplier_code', $excludeSupplierCodeList)->sum('sku_num');
//sku上架总数,不含api接口
$skuNum2 = SupplierChannelModel::where('is_type', 0)->where('supplier_group', '!=', 4)->whereNotIn('supplier_name', config('field.SkipChangeSupplierTypeNames'))->whereNotIn('supplier_code', $excludeSupplierCodeList)->sum('sku_num');
//有上架sku的供应商
$supplierCount = SupplierChannelModel::where('is_type', 0)->where('supplier_group', '!=', 4)->whereNotIn('supplier_name', config('field.SkipChangeSupplierTypeNames'))->whereNotIn('supplier_code', $excludeSupplierCodeList)->where('sku_num', '>', 0)->count();
//芯链供应商编码
$yunxinSupplierCodes = SupplierChannelModel::where('is_type', 0)->where('stockup_type', 'like', '%5%')->where('sku_num', '>', 0)->pluck('supplier_code')->toArray();
//找出哪些是芯链上传的
$yunxinUploadSupplierCodes = SkuUploadLogModel::whereIn('supplier_code', $yunxinSupplierCodes)->where('source', 2)->pluck('supplier_code')->unique()->toArray();
$skuNumYunxin = SupplierChannelModel::whereIn('supplier_code', $yunxinUploadSupplierCodes)->sum('sku_num');
echo("<p>上架供应商数(全部):" . $supplierCount . '</p>');
echo("<p>上架供应商数(仅芯链):" . count($yunxinUploadSupplierCodes) . '</p>');
echo("<p>SKU上架总条数(全部):" . $skuNum1 . '</p>');
echo("<p>SKU上架总条数(仅芯链):" . $skuNumYunxin . '</p>');
// echo("<p>SKU 上架条数(不含API接口):" . $skuNum2 . '</p>');
}
public function statisticsSkuNumberFromSKu()
{
$excludeSupplierCodeList = [
'L0004938',
'L0007913',
'L0011546',
'L0001175',
'L0007243',
'L0015040',
];
$dataManagerSupplierCodeList = DataManageModel::where('is_type', 0)->where('canal', '!=', '')->pluck('canal')->toArray();
$excludeSupplierCodeList = array_merge($excludeSupplierCodeList, $dataManagerSupplierCodeList);
//sku上架总数含api接口
$skuNum1 = SupplierChannelModel::where('is_type', 0)->where('supplier_group', '!=', 4)->whereNotIn('supplier_name', config('field.SkipChangeSupplierTypeNames'))->whereNotIn('supplier_code', $excludeSupplierCodeList)->sum('sku_num');
//sku上架总数,不含api接口
$skuNum2 = SupplierChannelModel::where('is_type', 0)->where('supplier_group', '!=', 4)->whereNotIn('supplier_name', config('field.SkipChangeSupplierTypeNames'))->whereNotIn('supplier_code', $excludeSupplierCodeList)->sum('sku_num');
//有上架sku的供应商
$supplierCount = SupplierChannelModel::where('is_type', 0)->where('supplier_group', '!=', 4)->whereNotIn('supplier_name', config('field.SkipChangeSupplierTypeNames'))->whereNotIn('supplier_code', $excludeSupplierCodeList)->where('sku_num', '>', 0)->count();
//芯链供应商编码
$yunxinSupplierCodes = SupplierChannelModel::where('is_type', 0)->where('stockup_type', 'like', '%5%')->where('sku_num', '>', 0)->pluck('supplier_code')->toArray();
//找出哪些是芯链上传的
// $yunxinUploadSupplierCodes = SkuUploadLogModel::whereIn('supplier_code', $yunxinSupplierCodes)->where('source', 2)->pluck('supplier_code')->unique()->toArray();
$yunxinUploadSupplierCount = 0;
$esUrl = env('ES_URL', '');
$map = [
"source" => 7,
"supplier_id" => 17,
"goods_status" => 1,
];
$return = curl($esUrl . '/es/countZhuanmaiSource', $map, 1);
$return = json_decode($return, true);
$yunxinUploadSupplierCount = 0;
if (isset($return['code']) & $return['code'] == 0) {
$yunxinUploadSupplierCount = $return['data']['total'];
}
// $skuNumYunxin = SupplierChannelModel::whereIn('supplier_code', $yunxinUploadSupplierCodes)->sum('sku_num');
//去搜索直接获取
$url = env('ES_SKU_URL', '');
$map = [
"source/eq" => 7,
"supplier_id" => 17,
"p" => "1",
"offset" => "20",
"show_status" => 1,
"goods_status/condition" => 1,
];
$return = curl($url, $map, 1);
$return = json_decode($return, true);
$skuNumYunxin = 0;
if (isset($return['error_code']) & $return['error_code'] == 0) {
$skuNumYunxin = $return['data']['total'];
}
echo("<p>上架供应商数(全部):" . $supplierCount . '</p>');
echo("<p>上架供应商数(仅芯链):" . $yunxinUploadSupplierCount . '</p>');
echo("<p>SKU上架总条数(全部):" . $skuNum1 . '</p>');
echo("<p>SKU上架总条数(仅芯链):" . $skuNumYunxin . '</p>');
// echo("<p>SKU 上架条数(不含API接口):" . $skuNum2 . '</p>');
}
//统计有上传sku的供应商
public function exportHasSkuSupplierList()
{
$header = [
'采购部门',
'线上采购员',
'供应商性质',
'供应商编码',
'供应商名称',
'是否开通芯链',
'接入方式',
'sku上架数量',
'末次更新时间'
];
$intraCodeModel = new IntracodeModel();
$users = $intraCodeModel->getSampleName(true);
$excelData = [];
$suppliers = SupplierChannelModel::where('sku_num', '>', 0)->where('is_type', 0)->get()->toArray();
foreach ($suppliers as $supplier) {
$skuUserNameRaw = array_get($users, $supplier['yunxin_channel_uid']);
$departmentName = (new DepartmentService())->getDepartmentNameByUserName($skuUserNameRaw);
//判断是否是api对接那边的供应商
$isApiSupplier = DataManageModel::where('canal', $supplier['supplier_code'])->where('is_type', 0)->exists();
//末次人工更新时间
$lastUpload = SkuUploadLogModel::where('supplier_code', $supplier['supplier_code'])->orderBy('create_time', 'desc')
->first();
$lastUploadTime = '';
if ($lastUpload) {
$lastUpload = $lastUpload->toArray();
$lastUploadTime = date('Y-md-m H:i:s', $lastUpload['create_time']);
}
$excelData[] = [
$departmentName,
$skuUserNameRaw,
array_get(config('fixed.SupplierGroup'), $supplier['supplier_group'], '未设置'),
$supplier['supplier_code'],
$supplier['supplier_name'],
strpos($supplier['stockup_type'], "5") !== false ? '是' : '否',
$isApiSupplier ? 'API和采集对接' : '人工上传',
$supplier['sku_num'],
$lastUploadTime
];
}
array_unshift($excelData, $header);
Excel::create('供应商统计报表', function ($excel) use ($excelData) {
$excel->sheet('sheet1', function ($sheet) use ($excelData) {
$sheet->fromArray($excelData);
});
})->export('csv');
}
//导入主营品牌
public function importMainBrands()
{
$isUpdate = request()->input('is_update', 0);
ini_set('memory_limit', -1);
$filePath = public_path('data') . DIRECTORY_SEPARATOR . 'supplier_brands.xlsx';
try {
Excel::selectSheetsByIndex(0)->load($filePath, function ($reader) use ($isUpdate) {
$reader->sheet('Sheet1', function () use ($reader, $isUpdate) {
$allBrands = $reader->all()->toArray();
$allBrandsGroupBySupplierCode = [];
foreach ($allBrands as $brand) {
$supplierCode = trim($brand[0]);
$brandName = trim($brand[2]);
$supplierCodeList = array_keys($allBrandsGroupBySupplierCode);
if (in_array($supplierCode, $supplierCodeList)) {
$allBrandsGroupBySupplierCode[$supplierCode][] = trim($brandName);
} else {
$allBrandsGroupBySupplierCode[$supplierCode][] = trim($brandName);
}
}
foreach ($allBrandsGroupBySupplierCode as $supplierCode => $brands) {
$supplier = SupplierChannelModel::select(['main_brands', 'supplier_id', 'supplier_code'])->where('supplier_code', $supplierCode)->first();
if (empty($supplier)) {
continue;
}
$supplier = $supplier->toArray();
$supplier['main_brands'] = trim($supplier['main_brands'], ',');
dump('供应商编码 : ' . $supplierCode);
//找出所有标品的id
$standardBrandIds = StandardBrandModel::whereIn('brand_name', $brands)->pluck('standard_brand_id')->toArray();
dump('excel得出的标品ids : ' . implode(',', $standardBrandIds));
if ($supplier['main_brands']) {
dump('原来有标品id :' . $supplier['main_brands']);
$supplier['main_brands'] = explode(',', $supplier['main_brands']);
$mainBrands = array_merge($supplier['main_brands'], $standardBrandIds);
} else {
$mainBrands = $standardBrandIds;
}
$mainBrands = implode(',', $mainBrands);
if ($isUpdate) {
SupplierChannelModel::where('supplier_code', $supplierCode)->update([
'main_brands' => $mainBrands,
]);
}
dump($mainBrands);
}
});
});
} catch (\Exception $exception) {
dd($exception);
}
}
public function exportSupplierByPurchaseAmount()
{
ini_set('memory_limit', -1);
$isUpdate = false;
$filePath = public_path('data') . DIRECTORY_SEPARATOR . '没有线上采购员供应商清单.xlsx';
$supplierCodeList = [];
Excel::selectSheetsByIndex(0)->load($filePath, function ($reader) use ($isUpdate, &$supplierCodeList) {
$reader->sheet('Sheet1', function () use ($reader, $isUpdate, &$supplierCodeList) {
$allSuppliers = $reader->all()->toArray();
foreach ($allSuppliers as $supplier) {
$supplierCodeList[] = $supplier[0];
}
});
});
$suppliers = SupplierChannelModel::select([
'supplier_name',
'supplier_code',
'supplier_id',
'channel_uid',
'yunxin_channel_uid',
'create_uid',
'create_name',
])->where('is_type', 0)->whereIn('supplier_code', $supplierCodeList)
->whereRaw('supplier_name NOT LIKE "%-1"')->get()->toArray();
$excelData = [];
$header = [
'供应商名称',
'最大金额采购员',
'线上采购员',
'采购员',
'创建人',
];
$intraCodeModel = new IntracodeModel();
$users = $intraCodeModel->getSampleName(true);
foreach ($suppliers as $supplier) {
$purchaseNames = $skuUserName = $skuUserNameRaw = $purchasePurchaseName = '';
if ($supplier['yunxin_channel_uid']) {
$skuUserNameRaw = array_get($users, $supplier['yunxin_channel_uid']);
$skuUserName = $skuUserNameRaw;
}
if ($supplier['create_name']) {
$createName = $supplier['create_name'];
} else if ($supplier['create_uid']) {
$createUser = (new AdminUserService())->getAdminUserInfo($supplier['create_uid']);
$createName = array_get($createUser, 'name', '');
}
if ($supplier['channel_uid']) {
$purchaseCodeIdList = explode(',', $supplier['channel_uid']);
$usersT = (new AdminUserService())->getAdminUserListByCodeIds($purchaseCodeIdList);
$purchaseNames = array_column($usersT, 'name');
$purchaseNames = implode(',', $purchaseNames);
$channelUids = IntracodeModel::whereIn('code_id', $purchaseCodeIdList)->pluck('admin_id')->toArray();
//去采购系统找最多金额的用户id
$purchaseAmountMap = [];
foreach ($channelUids as $channelUid) {
//去采购系统取采购单金额
$purchaseOrders = PurchaseOrderModel::where('purchase_uid', $channelUid)
->where('supplier_sn', $supplier['supplier_code'])->select([
'purchase_amount',
'exchange_rate',
'currency'
])->where('status', '!=', -3)->get()->toArray();
//计算出采购金额
$totalAmount = 0;
foreach ($purchaseOrders as $purchaseOrder) {
if ($purchaseOrder['currency'] == 1) {
$totalAmount += $purchaseOrder['purchase_amount'] * $purchaseOrder['exchange_rate'] * 1.13;
} else {
$totalAmount += $purchaseOrder['purchase_amount'] * $purchaseOrder['exchange_rate'];
}
}
$purchaseAmountMap[$channelUid] = $totalAmount;
}
if (!empty($purchaseAmountMap)) {
arsort($purchaseAmountMap);
if (array_values($purchaseAmountMap)[0] == 0) {
$purchasePurchaseName = '';
} else {
$maxAmountPurchaseAdminId = array_keys($purchaseAmountMap)[0];
$codeId = IntracodeModel::where('admin_id', $maxAmountPurchaseAdminId)->value('code_id');
if ($codeId) {
$purchasePurchaseName = array_get($users, $codeId);
} else {
$purchasePurchaseName = '';
}
}
}
} else {
$purchasePurchaseName = '';
}
$itemData = [
$supplier['supplier_name'],
$purchasePurchaseName,
$skuUserName,
$purchaseNames,
$createName,
];
$excelData[] = $itemData;
}
array_unshift($excelData, $header);
Excel::create('供应商导出', function ($excel) use ($excelData) {
$excel->sheet('sheet1', function ($sheet) use ($excelData) {
$sheet->fromArray($excelData);
});
})->export('csv');
}
//初始化sku规则
public function initSkuRulers()
{
//全部供应商修改默认值
// SupplierChannelModel::where('supplier_id', 12270)->update([
SupplierChannelModel::where('supplier_id', '>', 0)->update([
'sku_audit_ruler' => json_encode([
'skip' => 1,
'audit_ruler' => ["1" => 1],
])
]);
$redis = Redis::connection();
$supplierIds = SupplierChannelModel::where('supplier_id', '>', 0)->pluck('supplier_id')->toArray();
foreach ($supplierIds as $supplierId) {
$redis->hset('supplier_sku_audit_ruler', $supplierId, json_encode([
'skip' => 1,
'audit_ruler' => ["1" => 1],
]));
}
}
//初始化期货有效期
public function initFuturesCpTime()
{
$redis = new RedisModel();
$cpTimeList = $redis->hgetall('supplier_sku_upload_ruler_v2');
foreach ($cpTimeList as $key => $cpTime) {
$cpTime = json_decode($cpTime, true);
if (empty($cpTime['upload_futures_goods_validity_period'])) {
$cpTime['upload_futures_goods_validity_period'] = 365;
}
$cpTime = json_encode($cpTime);
$redis->hset('supplier_sku_upload_ruler_v2', $key, $cpTime);
}
}
//初始化sku的规则
public function initFuturesSkuUploadRuler()
{
$suppliers = SupplierChannelModel::where('sku_upload_ruler', '!=', '')->select([
'sku_upload_ruler',
'supplier_id'
])->get()->toArray();
$redis = new RedisModel();
foreach ($suppliers as $supplier) {
$uploadRuler = $supplier['sku_upload_ruler'];
$uploadRuler = json_decode($uploadRuler, true);
$uploadRuler['futures_allow_stock_lte_0'] = 1;
$uploadRuler['futures_allow_moq_lte_0'] = 0;
$uploadRuler['futures_allow_price_null'] = 1;
$uploadRuler = json_encode($uploadRuler);
SupplierChannelModel::where('supplier_id', $supplier['supplier_id'])->update([
'sku_upload_ruler' => $uploadRuler,
]);
$redis->hset('supplier_sku_upload_ruler', $supplier['supplier_id'], $uploadRuler);
}
}
//初始化品牌数据
public function initBrandData()
{
......@@ -1285,7 +243,6 @@ class DataService
$date = Carbon::create(2023, 5, 1, 0, 0, 0);
$halfYearTime = $date->timestamp;
$endTime = Carbon::create(2024, 1, 1, 0, 0, 0)->timestamp;
//$suppliers = SupplierChannelModel::where('is_type', 0)->where('uploaded_sku', 1)->get()->toArray();
$suppliers = SupplierChannelModel::select([
'supplier_id',
'supplier_code',
......@@ -1331,21 +288,6 @@ class DataService
if ($skuUserNameRaw) {
$skuUserDepartment = (new DepartmentService())->getDepartmentNameByUserName($skuUserNameRaw);
}
////获取采购系统的成单总额(含税)
//$purchaseOrders = PurchaseOrderModel::where('supplier_id', $supplierId)
// ->where('create_time', '>=', $halfYearTime)
// ->where('status', 4)->select([
// 'currency',
// 'purchase_amount',
// 'exchange_rate'
// ])->get()->toArray();
//$purchasePrice = 0;
//foreach ($purchaseOrders as $order) {
// if ($order['currency'] != 1) {
// $purchasePrice += $order['purchase_amount'] * $order['exchange_rate'] * 1.13;
// }
// $purchasePrice += $order['purchase_amount'];
//}
//获取sku数量最多的前五个品牌
//要去上传记录表
......
......@@ -95,7 +95,7 @@ class DepartmentService
return $departmentModel->where('department_id', $departmentId)->first()->toArray();
} else {
$department = $departmentModel->where('department_id', $departmentId)->first();
//如果当前的部门id已经是联营采购部门Id(二级,目前有联营一部和联营二部)
//如果当前的部门id已经是联营采购部门Id(二级,目前有联营一部和联营二P部)
if (in_array($departmentId, config('field.LiangYingDepartmentIds'))) {
return $department->toArray();
} else {
......
<?php
namespace App\Http\Services;
//后台用户相关信息服务
use App\Model\RedisModel;
use App\Model\SkuUploadItem;
use App\Model\SkuUploadLogModel;
use App\Model\SupplierBlacklistModel;
use App\Model\SupplierChannelModel;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
class StatisticsSkuUploadService
{
//统计近半年上传过sku的品牌sku数量情况
public function statisticBrandSkuNum()
{
$redis = new RedisModel();
$startTime = Carbon::create(2023, 5, 1, 0, 0, 0)->timestamp;
$endTime = Carbon::create(2024, 1, 1, 0, 0, 0)->timestamp;
//先找出主表半年内所有供应商并且按照供应商编码来分组
$logIdsGroupBySupplierCode = SkuUploadLogModel::whereBetween('create_time', [$startTime, $endTime])
->where('status', 3)
->select([
'id',
'supplier_code'
])->get()->groupBy('supplier_code')->toArray();
foreach ($logIdsGroupBySupplierCode as $supplierCode => $items) {
$logIds = array_column($items, 'id');
$exists = $redis->hget('half_year_brand_sku_upload_statistics', $supplierCode);
if (!empty($exists)) {
continue;
}
$brandCountMap = [];
//开始根据logIds去找到所有上传详情去统计
foreach ($logIds as $logId) {
$countByBrandName = SkuUploadItem::selectRaw('brand_name,count(sku_id) as sku_count')
->where('log_id', $logId)
->where('status', 3)
->groupBy('brand_name')->get();
if (empty($countByBrandName)) {
continue;
}
$countByBrandName = collect($countByBrandName)->pluck('sku_count', 'brand_name')->toArray();
foreach ($countByBrandName as $brandName => $skuCount) {
if (isset($brandCountMap[$brandName])) {
$brandCountMap[$brandName] += $skuCount;
} else {
$brandCountMap[$brandName] = $skuCount;
}
}
}
if (empty($brandCountMap)) {
continue;
}
$redis->hset('half_year_brand_sku_upload_statistics', $supplierCode, json_encode($brandCountMap));
}
}
public function getUploadItems($firstId, $lastId)
{
$items = SkuUploadItem::select([
'brand_name',
'sku_id',
'spu_id',
])->where('id', '>=', $firstId)->where('id', '<', $lastId)->get()->toArray();
return $items;
}
}
......@@ -82,5 +82,5 @@ Route::group(['middleware' => ['external'], 'namespace' => 'Sync'], function ()
});
Route::match(['get', 'post'], '/test', function () {
(new \App\Http\Services\DataService())->exportUpdatedSkuSupplier();
(new \App\Http\Services\StatisticsSkuUploadService())->statisticBrandSkuNum();
});
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