#feature: update SQL generator
This commit is contained in:
@@ -25,7 +25,7 @@ class SqlGeneratorController extends Controller
|
||||
if (empty($caseCodes)) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => '请提供有效的 case_id 列表'
|
||||
'message' => '请提供有效的 case_id 列表',
|
||||
], 400);
|
||||
}
|
||||
|
||||
@@ -59,8 +59,212 @@ class SqlGeneratorController extends Controller
|
||||
} catch (\Exception $e) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => '查询 case_extras 失败: ' . $e->getMessage(),
|
||||
'message' => '查询 case_extras 失败: '.$e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询 CRM 加工单关联地址国家,用于区分 PP-CN / PP-US。
|
||||
*/
|
||||
public function checkProductionCountries(Request $request): JsonResponse
|
||||
{
|
||||
try {
|
||||
$request->validate([
|
||||
'production_codes' => 'required|array|min:1',
|
||||
'production_codes.*' => 'required|string|max:255',
|
||||
]);
|
||||
|
||||
$productionCodes = array_values(array_unique(array_filter(array_map('trim', $request->input('production_codes')))));
|
||||
|
||||
if (empty($productionCodes)) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => '请提供有效的加工单列表',
|
||||
], 400);
|
||||
}
|
||||
|
||||
$productionCountries = $this->getProductionCountries($productionCodes);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'production_countries' => $productionCountries,
|
||||
],
|
||||
]);
|
||||
} catch (ValidationException $e) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => '请求参数验证失败',
|
||||
'errors' => $e->errors(),
|
||||
], 422);
|
||||
} catch (\Exception $e) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => '查询 CRM 加工单国家失败: '.$e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
private function getProductionCountries(array $productionCodes): array
|
||||
{
|
||||
$productionCountries = [];
|
||||
|
||||
foreach (array_chunk($productionCodes, 1000) as $chunk) {
|
||||
$productions = DB::connection('crmslave')
|
||||
->table('ea_production as ep')
|
||||
->join('ea_production_cstm as epc', 'ep.id', '=', 'epc.id_c')
|
||||
->where('ep.deleted', 0)
|
||||
->whereIn('ep.name', $chunk)
|
||||
->select([
|
||||
'ep.name as production_code',
|
||||
'epc.ea_case_id_c',
|
||||
'epc.ea_businessorder_id_c',
|
||||
'epc.ea_salesorder_id_c',
|
||||
])
|
||||
->get();
|
||||
|
||||
$caseIds = $productions->pluck('ea_case_id_c')->filter()->unique()->values()->all();
|
||||
$businessOrderIds = $productions->pluck('ea_businessorder_id_c')->filter()->unique()->values()->all();
|
||||
$salesOrderIds = $productions->pluck('ea_salesorder_id_c')->filter()->unique()->values()->all();
|
||||
|
||||
$caseCountries = $this->getCountriesByCaseIds($caseIds);
|
||||
$businessOrderCountries = $this->getCountriesByBusinessOrderIds($businessOrderIds);
|
||||
$salesOrderCountries = $this->getCountriesBySalesOrderIds($salesOrderIds);
|
||||
|
||||
foreach ($productions as $production) {
|
||||
$countries = array_merge(
|
||||
$caseCountries[(string) $production->ea_case_id_c] ?? [],
|
||||
$businessOrderCountries[(string) $production->ea_businessorder_id_c] ?? [],
|
||||
$salesOrderCountries[(string) $production->ea_salesorder_id_c] ?? []
|
||||
);
|
||||
|
||||
$productionCountries[(string) $production->production_code] = array_values(array_unique(array_filter($countries)));
|
||||
}
|
||||
}
|
||||
|
||||
return $productionCountries;
|
||||
}
|
||||
|
||||
private function getCountriesByCaseIds(array $caseIds): array
|
||||
{
|
||||
if (empty($caseIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$results = DB::connection('crmslave')
|
||||
->table('ea_case as ec')
|
||||
->join('accounts_ea_case_1_c as aec1c', function ($join) {
|
||||
$join->on('aec1c.accounts_ea_case_1ea_case_idb', '=', 'ec.id')
|
||||
->where('aec1c.deleted', '=', 0);
|
||||
})
|
||||
->join('accounts as a', 'a.id', '=', 'aec1c.accounts_ea_case_1accounts_ida')
|
||||
->join('accounts_cstm as ac', 'ac.id_c', '=', 'a.id')
|
||||
->where('ec.deleted', 0)
|
||||
->where('a.deleted', 0)
|
||||
->whereIn('ec.id', $caseIds)
|
||||
->whereNotNull('ac.country_c')
|
||||
->select([
|
||||
'ec.id as entity_id',
|
||||
'ac.country_c as country',
|
||||
'ac.province_c as province',
|
||||
])
|
||||
->get();
|
||||
|
||||
return $this->groupCountriesByEntityId($results);
|
||||
}
|
||||
|
||||
private function getCountriesByBusinessOrderIds(array $businessOrderIds): array
|
||||
{
|
||||
if (empty($businessOrderIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$results = DB::connection('crmslave')
|
||||
->table('ea_businessorder as eb')
|
||||
->join('accounts_ea_businessorder_1_c as aeb1c', function ($join) {
|
||||
$join->on('aeb1c.accounts_ea_businessorder_1ea_businessorder_idb', '=', 'eb.id')
|
||||
->where('aeb1c.deleted', '=', 0);
|
||||
})
|
||||
->join('accounts as a', 'a.id', '=', 'aeb1c.accounts_ea_businessorder_1accounts_ida')
|
||||
->join('accounts_cstm as ac', 'ac.id_c', '=', 'a.id')
|
||||
->where('eb.deleted', 0)
|
||||
->where('a.deleted', 0)
|
||||
->whereIn('eb.id', $businessOrderIds)
|
||||
->whereNotNull('ac.country_c')
|
||||
->select([
|
||||
'eb.id as entity_id',
|
||||
'ac.country_c as country',
|
||||
'ac.province_c as province',
|
||||
])
|
||||
->get();
|
||||
|
||||
return $this->groupCountriesByEntityId($results);
|
||||
}
|
||||
|
||||
private function getCountriesBySalesOrderIds(array $salesOrderIds): array
|
||||
{
|
||||
if (empty($salesOrderIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$results = DB::connection('crmslave')
|
||||
->table('ea_salesorder as es')
|
||||
->join('accounts_ea_salesorder_1_c as aes1c', function ($join) {
|
||||
$join->on('aes1c.accounts_ea_salesorder_1ea_salesorder_idb', '=', 'es.id')
|
||||
->where('aes1c.deleted', '=', 0);
|
||||
})
|
||||
->join('accounts as a_base', 'a_base.id', '=', 'aes1c.accounts_ea_salesorder_1accounts_ida')
|
||||
->join('accounts_cstm as ac', 'ac.id_c', '=', 'aes1c.accounts_ea_salesorder_1accounts_ida')
|
||||
->where('es.deleted', 0)
|
||||
->where('a_base.deleted', 0)
|
||||
->whereIn('es.id', $salesOrderIds)
|
||||
->whereNotNull('ac.country_c')
|
||||
->select([
|
||||
'es.id as entity_id',
|
||||
'ac.country_c as country',
|
||||
'ac.province_c as province',
|
||||
])
|
||||
->get();
|
||||
|
||||
return $this->groupCountriesByEntityId($results);
|
||||
}
|
||||
|
||||
private function groupCountriesByEntityId($results): array
|
||||
{
|
||||
$countriesByEntityId = [];
|
||||
|
||||
foreach ($results as $result) {
|
||||
$countryCode = $this->getCountryCode($result->country, $result->province);
|
||||
if (! $countryCode) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$entityId = (string) $result->entity_id;
|
||||
$countriesByEntityId[$entityId] ??= [];
|
||||
$countriesByEntityId[$entityId][] = $countryCode;
|
||||
}
|
||||
|
||||
return array_map(fn ($countries) => array_values(array_unique($countries)), $countriesByEntityId);
|
||||
}
|
||||
|
||||
private function getCountryCode(?string $country, ?string $province): ?string
|
||||
{
|
||||
if (! $country) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (in_array($country, ['1', '156'], true) && ! in_array((string) $province, ['710000', '810000', '820000'], true)) {
|
||||
return 'CN';
|
||||
}
|
||||
|
||||
return [
|
||||
'840' => 'US',
|
||||
'US' => 'US',
|
||||
'316' => 'GU',
|
||||
'GU' => 'GU',
|
||||
'630' => 'PR',
|
||||
'PR' => 'PR',
|
||||
][strtoupper($country)] ?? strtoupper($country);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user