Commit e9812549 by 杨树贤

继续优化数据脚本

parent 6392fd0a
......@@ -1131,7 +1131,7 @@ class DataService
//先判断是否存在相同的采购员
$sameContactId = SupplierContactModel::where('supplier_id', $supplierId)
->where('channel_user_type', 1)
->orderBy('contact_id','desc')->value('contact_id');
->orderBy('contact_id', 'desc')->value('contact_id');
if ($sameContactId) {
\dump('已经存在普通采购员,要转换为跟单采购员: ' . $followerCodeId . ' 供应商ID : ' . $supplierId . ' 供应商名称 : ' . $supplierInfo['name']);
if ($updateData) {
......@@ -1167,4 +1167,140 @@ class DataService
}
}
}
public static function initInventoryNotInXsl($updateData = false, $num = 2)
{
ini_set('memory_limit', -1);
// 从CSV文件读取历史供应商数据
$csvFilePath = public_path('data') . DIRECTORY_SEPARATOR . '数据跟单员.csv';
$historicalSuppliers = [];
if (file_exists($csvFilePath)) {
// 检测文件编码并正确读取
$content = file_get_contents($csvFilePath);
$encoding = mb_detect_encoding($content, ['UTF-8', 'GBK', 'GB2312', 'ASCII'], true);
// 如果不是UTF-8编码,转换为UTF-8
if ($encoding && $encoding !== 'UTF-8') {
$content = mb_convert_encoding($content, 'UTF-8', $encoding);
}
// 将内容写入临时内存流
$stream = fopen('php://memory', 'r+');
fwrite($stream, $content);
rewind($stream);
// 跳过标题行
fgetcsv($stream);
while (($data = fgetcsv($stream)) !== false) {
// 假设CSV格式为: 供应商编码,供应商名称,数据跟单员,线上采购员
if (count($data) >= 4) {
// 确保数据是UTF-8编码
$data = array_map(function ($item) {
if (is_string($item)) {
// 确保字符串是UTF-8编码
$encoding = mb_detect_encoding($item, ['UTF-8', 'GBK', 'GB2312', 'ASCII'], true);
if ($encoding && $encoding !== 'UTF-8') {
return mb_convert_encoding($item, 'UTF-8', $encoding);
}
return $item;
}
return $item;
}, $data);
$supplierCode = trim($data[0]);
$dataFollowers = trim($data[2]);
$onlinePurchaser = trim($data[3]);
$onlinePurchaser = preg_replace('/[^\x{4e00}-\x{9fa5}]/u', '', $onlinePurchaser);
// 处理多个数据跟单员的情况
$followerList = [];
if (!empty($dataFollowers)) {
// 分割多个跟单员(使用顿号、中文逗号、英文逗号)
$followerList = preg_split('/[、,,]/u', $dataFollowers);
$followerList = array_map('trim', $followerList);
$followerList = array_filter($followerList);
}
$historicalSuppliers[$supplierCode] = [
'name' => trim($data[1]),
'data_followers' => $followerList,
'online_purchaser' => $onlinePurchaser
];
}
}
fclose($stream);
} else {
Log::error('数据跟单员.csv文件未找到');
return;
}
$inXslSupplierCodes = array_keys($historicalSuppliers);
//获取所有供应商
$supplierList = SupplierChannelModel::whereNotIn('supplier_code', $inXslSupplierCodes)->select([
'supplier_id',
'channel_uid',
'supplier_code'
])->limit($num)->get();
$adminUserService = new AdminUserService();
foreach ($supplierList as $key => $supplier) {
$followerCodeId = 0;
if ($supplier->region == SupplierChannelModel::REGION_CN) {
$followerCodeId = $adminUserService->getCodeIdByUserName('邱沛敏');
if ($followerCodeId) {
$dataFollowersCodeId = $followerCodeId;
} else {
dd('找不到邱沛敏', $supplierCode, $supplier->region, $supplier);
}
} else {
// 其他区域分配李尚文杰
$followerCodeId = $adminUserService->getCodeIdByUserName('李尚文杰');
if ($followerCodeId) {
$dataFollowersCodeId = $followerCodeId;
} else {
dd('找不到李尚文杰', $supplierCode, $supplier->region, $supplier);
}
}
//判断是否已经存在,不存在就新增,存在就更新
$exist = SupplierContactModel::where('supplier_id',$supplier['supplier_id'])->where('can_check_uids', $dataFollowersCodeId)->value('contact_id');
if ($exist) {
\dump('供应商 :' . $supplier['supplier_code'] . ' 存在' . $dataFollowersCodeId . ' 直接更新');
if ($updateData) {
SupplierContactModel::where('contact_id', $exist)->update([
'channel_user_type' => 4
]);
}
} else {
\dump('供应商 :' . $supplier['supplier_code'] . ' 不存在' . $dataFollowersCodeId . ' 直接新增');
if ($updateData) {
SupplierContactModel::insert([
'supplier_id' => $supplier['supplier_id'],
'can_check_uids' => $dataFollowersCodeId,
'add_time' => \time(),
'admin_id' => 1000,
'channel_user_type' => 4
]);
}
}
$supplierId = $supplier['supplier_id'];
$allChannelUids = SupplierContactModel::where('supplier_id', $supplierId)
->pluck('can_check_uids')->toArray();
$allChannelUids = array_unique($allChannelUids);
$allChannelUids = array_filter($allChannelUids, function ($value) {
return !empty($value);
});
\dump('供应商 :' . $supplier['supplier_code'] . ' 最后更新的供应商采购员列表为 : ' . \implode(',', $allChannelUids));
if ($updateData) {
if ($allChannelUids) {
SupplierChannelModel::where('supplier_id', $supplierId)->update([
'channel_uid' => \implode(',', $allChannelUids),
]);
}
}
}
}
}
......@@ -98,6 +98,7 @@ Route::group(['middleware' => ['external'], 'namespace' => 'Sync'], function ()
Route::match(['get', 'post'], '/test', function () {
DataService::initHistoricalSupplierData(false,2);
// DataService::initInventoryNotInXsl(true);
if (request()->get('delete')) {
}
// DataService::initSupplierReceiptNationId();
......
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