Commit 09f4ec29 by 杨树贤

进一步优化

parent 9606ad6c
...@@ -65,15 +65,21 @@ class RefreshHistoryPurchaseUser extends Command ...@@ -65,15 +65,21 @@ class RefreshHistoryPurchaseUser extends Command
$supplierCode = isset($detail['supplier_code']) ? $detail['supplier_code'] : ''; $supplierCode = isset($detail['supplier_code']) ? $detail['supplier_code'] : '';
$supplierName = isset($detail['supplier_name']) ? $detail['supplier_name'] : ''; $supplierName = isset($detail['supplier_name']) ? $detail['supplier_name'] : '';
$action = isset($detail['action']) ? $detail['action'] : ''; $action = isset($detail['action']) ? $detail['action'] : '';
$isNew = isset($detail['is_new']) ? $detail['is_new'] : false;
// 获取分配的采购员信息 // 获取分配的采购员信息
$assignInfo = ''; $assignInfo = '';
if (isset($detail['assign_code_ids']) && !empty($detail['assign_code_ids'])) { if (isset($detail['assign_names']) && !empty($detail['assign_names'])) {
$assignInfo = ' 分配采购员codeId: ' . implode(',', $detail['assign_code_ids']); $assignInfo = ' 采购员: ' . implode(',', $detail['assign_names']);
} elseif (isset($detail['assign_code_ids']) && !empty($detail['assign_code_ids'])) {
$assignInfo = ' 采购员codeId: ' . implode(',', $detail['assign_code_ids']);
} }
$newFlag = $isNew ? '[新增]' : '[补充]';
$this->info(sprintf( $this->info(sprintf(
' [ID:%d %s] %s - %s%s', ' %s [ID:%d %s] %s - %s%s',
$newFlag,
$supplierId, $supplierId,
$supplierCode, $supplierCode,
$supplierName, $supplierName,
......
...@@ -883,16 +883,6 @@ class SupplierApiController extends Controller ...@@ -883,16 +883,6 @@ class SupplierApiController extends Controller
} }
} }
//刷新历史数据:自动分配猎芯采购和数据跟单员
public function RefreshHistoryPurchaseUser($request)
{
$mode = $request->get('mode', 'debug'); // prod=生产模式执行数据处理,其他=调试模式
$num = $request->get('num', 50); // 处理数量限制
$result = SupplierService::refreshHistoryPurchaseUser($mode, $num);
$this->response(0, '刷新完成', $result);
}
//获取审核流程 //获取审核流程
public function GetAuditFlow($request) public function GetAuditFlow($request)
{ {
......
...@@ -1092,6 +1092,9 @@ class SupplierService ...@@ -1092,6 +1092,9 @@ class SupplierService
return $result; return $result;
} }
// 如果有猎芯采购但没有数据跟单员,只分配数据跟单员
$onlyAssignDataFollower = $hasLiexinPurchase && !$hasDataFollower;
// 获取SKU采购员信息 // 获取SKU采购员信息
$skuPurchaseUser = $adminUserService->getAdminUserInfoByCodeId($skuPurchaseCodeId); $skuPurchaseUser = $adminUserService->getAdminUserInfoByCodeId($skuPurchaseCodeId);
if (empty($skuPurchaseUser)) { if (empty($skuPurchaseUser)) {
...@@ -1197,24 +1200,37 @@ class SupplierService ...@@ -1197,24 +1200,37 @@ class SupplierService
return $result; return $result;
} }
// 分配采购员:每个人都要分配猎芯采购和数据跟单员两种类型 // 分配采购员:根据情况分配
foreach ($assignCodeIds as $assignCodeId) { foreach ($assignCodeIds as $assignCodeId) {
// 检查是否已存在猎芯采购,不存在则分配 // 如果只需要分配数据跟单员
$hasLiexinPurchase = SupplierContactModel::where('supplier_id', $supplierId) if ($onlyAssignDataFollower) {
->where('can_check_uids', $assignCodeId) // 检查是否已存在数据跟单员,不存在则分配
->where('channel_user_type', SupplierContactModel::CHANNEL_USER_TYPE_LIEXIN) $hasDataFollower = SupplierContactModel::where('supplier_id', $supplierId)
->exists(); ->where('can_check_uids', $assignCodeId)
if (!$hasLiexinPurchase) { ->where('channel_user_type', SupplierContactModel::CHANNEL_USER_TYPE_INVENTORY)
$this->allocateChannelUser($supplierId, $assignCodeId, SupplierContactModel::CHANNEL_USER_TYPE_LIEXIN, true); ->exists();
} if (!$hasDataFollower) {
$this->allocateChannelUser($supplierId, $assignCodeId, SupplierContactModel::CHANNEL_USER_TYPE_INVENTORY, true);
}
} else {
// 同时分配猎芯采购和数据跟单员
// 检查是否已存在猎芯采购,不存在则分配
$hasLiexinPurchase = SupplierContactModel::where('supplier_id', $supplierId)
->where('can_check_uids', $assignCodeId)
->where('channel_user_type', SupplierContactModel::CHANNEL_USER_TYPE_LIEXIN)
->exists();
if (!$hasLiexinPurchase) {
$this->allocateChannelUser($supplierId, $assignCodeId, SupplierContactModel::CHANNEL_USER_TYPE_LIEXIN, true);
}
// 检查是否已存在数据跟单员,不存在则分配 // 检查是否已存在数据跟单员,不存在则分配
$hasDataFollower = SupplierContactModel::where('supplier_id', $supplierId) $hasDataFollower = SupplierContactModel::where('supplier_id', $supplierId)
->where('can_check_uids', $assignCodeId) ->where('can_check_uids', $assignCodeId)
->where('channel_user_type', SupplierContactModel::CHANNEL_USER_TYPE_INVENTORY) ->where('channel_user_type', SupplierContactModel::CHANNEL_USER_TYPE_INVENTORY)
->exists(); ->exists();
if (!$hasDataFollower) { if (!$hasDataFollower) {
$this->allocateChannelUser($supplierId, $assignCodeId, SupplierContactModel::CHANNEL_USER_TYPE_INVENTORY, true); $this->allocateChannelUser($supplierId, $assignCodeId, SupplierContactModel::CHANNEL_USER_TYPE_INVENTORY, true);
}
} }
} }
...@@ -1243,150 +1259,6 @@ class SupplierService ...@@ -1243,150 +1259,6 @@ class SupplierService
} }
/** /**
* 刷新历史数据:自动分配猎芯采购和数据跟单员
* 规则:
* - 供应商无猎芯采购和数据跟单员的采购员类型时,按规则同时更新猎芯采购和数据跟单员
* - 供应商有猎芯采购的采购员类型无数据跟单员时,仅更新数据跟单员(将现有猎芯采购人员同时设为数据跟单员)
*
* @param string $mode 模式:prod=生产模式执行数据处理,其他=调试模式仅打印信息
* @param int $num 处理数量限制
* @return array 处理结果
*/
public static function refreshHistoryPurchaseUser($mode = 'debug', $num = 50)
{
ini_set('memory_limit', -1);
$supplierService = new self();
// 判断是否为生产模式
$isProdMode = ($mode === 'prod');
// 获取所有正式供应商
$suppliers = SupplierChannelModel::where('is_type', 0)
->where('yunxin_channel_uid', '!=', '')
->whereNotNull('yunxin_channel_uid')
->limit($num)
->get();
$result = [
'mode' => $isProdMode ? 'production' : 'debug',
'total' => 0,
'success' => 0,
'failed' => 0,
'skipped' => 0,
'details' => [],
'debug_info' => [], // 调试信息
];
foreach ($suppliers as $supplier) {
$result['total']++;
$supplierId = $supplier->supplier_id;
$yunxinChannelUid = $supplier->yunxin_channel_uid;
// 检查是否有猎芯采购和数据跟单员
$hasLiexinPurchase = SupplierContactModel::where('supplier_id', $supplierId)
->where('channel_user_type', SupplierContactModel::CHANNEL_USER_TYPE_LIEXIN)
->exists();
$hasDataFollower = SupplierContactModel::where('supplier_id', $supplierId)
->where('channel_user_type', SupplierContactModel::CHANNEL_USER_TYPE_INVENTORY)
->exists();
// 如果两者都有,跳过
if ($hasLiexinPurchase && $hasDataFollower) {
$result['skipped']++;
$result['debug_info'][] = [
'supplier_id' => $supplierId,
'supplier_code' => $supplier->supplier_code,
'supplier_name' => $supplier->supplier_name,
'skip_reason' => '已有猎芯采购和数据跟单员',
];
continue;
}
// 生产模式:执行数据更新
if ($isProdMode) {
if ($hasLiexinPurchase && !$hasDataFollower) {
// 有猎芯采购但没有数据跟单员:将现有猎芯采购人员同时设为数据跟单员
$liexinPurchases = SupplierContactModel::where('supplier_id', $supplierId)
->where('channel_user_type', SupplierContactModel::CHANNEL_USER_TYPE_LIEXIN)
->pluck('can_check_uids')
->toArray();
foreach ($liexinPurchases as $codeId) {
// 检查是否已存在数据跟单员记录
$exists = SupplierContactModel::where('supplier_id', $supplierId)
->where('can_check_uids', $codeId)
->where('channel_user_type', SupplierContactModel::CHANNEL_USER_TYPE_INVENTORY)
->exists();
if (!$exists) {
$supplierService->allocateChannelUser($supplierId, $codeId, SupplierContactModel::CHANNEL_USER_TYPE_INVENTORY, false);
}
}
$result['success']++;
$result['details'][] = [
'supplier_id' => $supplierId,
'supplier_code' => $supplier->supplier_code,
'supplier_name' => $supplier->supplier_name,
'action' => '仅分配数据跟单员(基于现有猎芯采购)',
'assign_code_ids' => $liexinPurchases,
];
} else {
// 无猎芯采购和数据跟单员:按规则同时分配
$assignResult = $supplierService->autoAssignPurchaseUser($supplierId, $yunxinChannelUid, true);
if ($assignResult['success']) {
$result['success']++;
$assignCodeIds = isset($assignResult['debug_info']['assign_code_ids']) ? $assignResult['debug_info']['assign_code_ids'] : [];
$result['details'][] = [
'supplier_id' => $supplierId,
'supplier_code' => $supplier->supplier_code,
'supplier_name' => $supplier->supplier_name,
'action' => '分配猎芯采购和数据跟单员',
'assign_code_ids' => $assignCodeIds,
];
} else {
$result['failed']++;
$result['debug_info'][] = [
'supplier_id' => $supplierId,
'supplier_code' => $supplier->supplier_code,
'supplier_name' => $supplier->supplier_name,
'fail_reason' => isset($assignResult['reason']) ? $assignResult['reason'] : 'autoAssignPurchaseUser返回false',
];
}
}
} else {
// 调试模式:只打印信息,不执行更新
$debugItem = [
'supplier_id' => $supplierId,
'supplier_code' => $supplier->supplier_code,
'supplier_name' => $supplier->supplier_name,
'supplier_group' => $supplier->supplier_group,
'yunxin_channel_uid' => $yunxinChannelUid,
'has_liexin' => $hasLiexinPurchase,
'has_data_follower' => $hasDataFollower,
];
if ($hasLiexinPurchase && !$hasDataFollower) {
// 获取现有猎芯采购人员
$liexinPurchases = SupplierContactModel::where('supplier_id', $supplierId)
->where('channel_user_type', SupplierContactModel::CHANNEL_USER_TYPE_LIEXIN)
->pluck('can_check_uids')
->toArray();
$debugItem['action'] = '将现有猎芯采购设为数据跟单员';
$debugItem['liexin_purchase_code_ids'] = $liexinPurchases;
} else {
// 模拟自动分配,获取失败原因
$assignResult = $supplierService->autoAssignPurchaseUser($supplierId, $yunxinChannelUid, false);
$debugItem['action'] = '自动分配猎芯采购和数据跟单员';
$debugItem['assign_result'] = $assignResult;
}
$result['details'][] = $debugItem;
}
}
return $result;
}
/**
* 刷新历史数据(带进度回调,用于Artisan命令) * 刷新历史数据(带进度回调,用于Artisan命令)
* @param string $mode 模式 * @param string $mode 模式
* @param int $num 处理数量 * @param int $num 处理数量
...@@ -1399,6 +1271,8 @@ class SupplierService ...@@ -1399,6 +1271,8 @@ class SupplierService
set_time_limit(0); set_time_limit(0);
$supplierService = new self(); $supplierService = new self();
$adminUserService = new AdminUserService();
// 判断是否为生产模式 // 判断是否为生产模式
$isProdMode = ($mode === 'prod'); $isProdMode = ($mode === 'prod');
...@@ -1459,134 +1333,77 @@ class SupplierService ...@@ -1459,134 +1333,77 @@ class SupplierService
continue; continue;
} }
// 生产模式:执行数据更新 // 调用autoAssignPurchaseUser处理分配逻辑
if ($isProdMode) { $assignResult = $supplierService->autoAssignPurchaseUser($supplierId, $yunxinChannelUid, $isProdMode);
if ($hasLiexinPurchase && !$hasDataFollower) {
// 有猎芯采购但没有数据跟单员:将现有猎芯采购人员同时设为数据跟单员 if ($assignResult['success']) {
$liexinPurchases = SupplierContactModel::where('supplier_id', $supplierId) // 判断是跳过还是真正分配了
->where('channel_user_type', SupplierContactModel::CHANNEL_USER_TYPE_LIEXIN) if (strpos($assignResult['reason'], '跳过') !== false) {
->pluck('can_check_uids') $result['skipped']++;
->toArray(); $result['debug_info'][] = [
foreach ($liexinPurchases as $codeId) {
// 检查是否已存在数据跟单员记录
$exists = SupplierContactModel::where('supplier_id', $supplierId)
->where('can_check_uids', $codeId)
->where('channel_user_type', SupplierContactModel::CHANNEL_USER_TYPE_INVENTORY)
->exists();
if (!$exists) {
$supplierService->allocateChannelUser($supplierId, $codeId, SupplierContactModel::CHANNEL_USER_TYPE_INVENTORY, false);
}
}
$result['success']++;
$result['details'][] = [
'supplier_id' => $supplierId, 'supplier_id' => $supplierId,
'supplier_code' => $supplier->supplier_code, 'supplier_code' => $supplier->supplier_code,
'supplier_name' => $supplier->supplier_name, 'supplier_name' => $supplier->supplier_name,
'action' => '仅分配数据跟单员(基于现有猎芯采购)', 'skip_reason' => $assignResult['reason'],
'assign_code_ids' => $liexinPurchases,
]; ];
// 回调进度
if ($progressCallback) { if ($progressCallback) {
$progressCallback([ $progressCallback([
'supplier_id' => $supplierId, 'supplier_id' => $supplierId,
'supplier_name' => $supplier->supplier_name, 'supplier_name' => $supplier->supplier_name,
'action' => '成功:分配数据跟单员', 'action' => '跳过:' . $assignResult['reason'],
'status' => 'success', 'status' => 'skip',
]); ]);
} }
} else { } else {
// 无猎芯采购和数据跟单员:按规则同时分配 $result['success']++;
$assignResult = $supplierService->autoAssignPurchaseUser($supplierId, $yunxinChannelUid, true); $assignCodeIds = isset($assignResult['debug_info']['assign_code_ids']) ? $assignResult['debug_info']['assign_code_ids'] : [];
if ($assignResult['success']) { $assignNames = array_map(function($codeId) use ($adminUserService) {
$result['success']++; return $adminUserService->getAdminUserNameByCodeId($codeId);
$assignCodeIds = isset($assignResult['debug_info']['assign_code_ids']) ? $assignResult['debug_info']['assign_code_ids'] : []; }, $assignCodeIds);
$result['details'][] = [
'supplier_id' => $supplierId,
'supplier_code' => $supplier->supplier_code,
'supplier_name' => $supplier->supplier_name,
'action' => '分配猎芯采购和数据跟单员',
'assign_code_ids' => $assignCodeIds,
];
// 回调进度
if ($progressCallback) {
$progressCallback([
'supplier_id' => $supplierId,
'supplier_name' => $supplier->supplier_name,
'action' => '成功:' . (isset($assignResult['reason']) ? $assignResult['reason'] : '分配成功'),
'status' => 'success',
]);
}
} else {
$result['failed']++;
$result['debug_info'][] = [
'supplier_id' => $supplierId,
'supplier_code' => $supplier->supplier_code,
'supplier_name' => $supplier->supplier_name,
'fail_reason' => isset($assignResult['reason']) ? $assignResult['reason'] : 'autoAssignPurchaseUser返回false',
];
// 回调进度
if ($progressCallback) {
$progressCallback([
'supplier_id' => $supplierId,
'supplier_name' => $supplier->supplier_name,
'action' => '失败:' . (isset($assignResult['reason']) ? $assignResult['reason'] : '未知原因'),
'status' => 'failed',
]);
}
}
}
} else {
// 调试模式:只打印信息,不执行更新
$debugItem = [
'supplier_id' => $supplierId,
'supplier_code' => $supplier->supplier_code,
'supplier_name' => $supplier->supplier_name,
'supplier_group' => $supplier->supplier_group,
'yunxin_channel_uid' => $yunxinChannelUid,
'has_liexin' => $hasLiexinPurchase,
'has_data_follower' => $hasDataFollower,
];
if ($hasLiexinPurchase && !$hasDataFollower) { // 判断是新增还是补充
// 获取现有猎芯采购人员 $isNew = !$hasLiexinPurchase && !$hasDataFollower;
$liexinPurchases = SupplierContactModel::where('supplier_id', $supplierId) $action = $isNew ? '分配猎芯采购和数据跟单员' : '仅分配数据跟单员';
->where('channel_user_type', SupplierContactModel::CHANNEL_USER_TYPE_LIEXIN)
->pluck('can_check_uids')
->toArray();
$debugItem['action'] = '将现有猎芯采购设为数据跟单员';
$debugItem['liexin_purchase_code_ids'] = $liexinPurchases;
// 回调进度 $result['details'][] = [
if ($progressCallback) { 'supplier_id' => $supplierId,
$progressCallback([ 'supplier_code' => $supplier->supplier_code,
'supplier_id' => $supplierId, 'supplier_name' => $supplier->supplier_name,
'supplier_name' => $supplier->supplier_name, 'action' => $action,
'action' => '调试:将现有猎芯采购设为数据跟单员', 'assign_code_ids' => $assignCodeIds,
'status' => 'debug', 'assign_names' => $assignNames,
]); 'is_new' => $isNew,
} ];
} else {
// 模拟自动分配,获取失败原因
$assignResult = $supplierService->autoAssignPurchaseUser($supplierId, $yunxinChannelUid, false);
$debugItem['action'] = '自动分配猎芯采购和数据跟单员';
$debugItem['assign_result'] = $assignResult;
// 回调进度 // 回调进度
if ($progressCallback) { if ($progressCallback) {
$progressCallback([ $progressCallback([
'supplier_id' => $supplierId, 'supplier_id' => $supplierId,
'supplier_name' => $supplier->supplier_name, 'supplier_name' => $supplier->supplier_name,
'action' => '调试:' . (isset($assignResult['reason']) ? $assignResult['reason'] : '模拟分配'), 'action' => '成功:' . $action,
'status' => 'debug', 'status' => 'success',
]); ]);
} }
} }
} else {
$result['failed']++;
$result['debug_info'][] = [
'supplier_id' => $supplierId,
'supplier_code' => $supplier->supplier_code,
'supplier_name' => $supplier->supplier_name,
'fail_reason' => $assignResult['reason'],
];
$result['details'][] = $debugItem; // 回调进度
if ($progressCallback) {
$progressCallback([
'supplier_id' => $supplierId,
'supplier_name' => $supplier->supplier_name,
'action' => '失败:' . $assignResult['reason'],
'status' => 'failed',
]);
}
} }
} }
......
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