diff --git a/backend-node/src/controllers/popProductionController.ts b/backend-node/src/controllers/popProductionController.ts index fca4baf2..c75a7476 100644 --- a/backend-node/src/controllers/popProductionController.ts +++ b/backend-node/src/controllers/popProductionController.ts @@ -393,8 +393,7 @@ export const syncWorkInstructions = async ( -- detail에 routing이 있는 경우 (다중 품목 지원) EXISTS ( SELECT 1 FROM work_instruction_detail wid - WHERE wid.work_instruction_no = wi.work_instruction_no - AND wid.company_code = $1 + WHERE wid.work_instruction_id = wi.id AND wid.routing_version_id IS NOT NULL AND CAST(COALESCE(NULLIF(wid.qty, ''), '0') AS numeric) > 0 AND NOT EXISTS ( @@ -433,11 +432,11 @@ export const syncWorkInstructions = async ( const detailResult = await pool.query( `SELECT wid.item_number, wid.routing_version_id, wid.qty FROM work_instruction_detail wid - WHERE wid.work_instruction_no = $1 AND wid.company_code = $2 + WHERE wid.work_instruction_id = $1 AND wid.routing_version_id IS NOT NULL AND CAST(COALESCE(NULLIF(wid.qty, ''), '0') AS numeric) > 0 ORDER BY wid.created_date ASC`, - [wi.work_instruction_no, companyCode], + [wi.id], ); const detailRows = detailResult.rows; @@ -448,9 +447,9 @@ export const syncWorkInstructions = async ( const firstDetail = await pool.query( `SELECT routing_version_id, qty, item_number FROM work_instruction_detail - WHERE work_instruction_no = $1 AND company_code = $2 + WHERE work_instruction_id = $1 LIMIT 1`, - [wi.work_instruction_no, companyCode], + [wi.id], ); const wid = firstDetail.rows[0]; if (wid) { diff --git a/backend-node/src/controllers/workInstructionController.ts b/backend-node/src/controllers/workInstructionController.ts index 1de120a7..f37584d3 100644 --- a/backend-node/src/controllers/workInstructionController.ts +++ b/backend-node/src/controllers/workInstructionController.ts @@ -96,7 +96,7 @@ export async function getList(req: AuthenticatedRequest, res: Response) { COUNT(*) OVER (PARTITION BY wi.work_instruction_no) AS detail_count FROM work_instruction wi INNER JOIN work_instruction_detail d - ON d.work_instruction_no = wi.work_instruction_no AND d.company_code = wi.company_code + ON d.work_instruction_id = wi.id LEFT JOIN LATERAL ( SELECT item_name, size, type FROM item_info WHERE item_number = d.item_number AND company_code = wi.company_code LIMIT 1 @@ -170,7 +170,7 @@ export async function save(req: AuthenticatedRequest, res: Response) { `UPDATE work_instruction SET status=$1, progress_status=$2, reason=$3, start_date=$4, end_date=$5, equipment_id=$6, work_team=$7, worker=$8, remark=$9, routing=$10, updated_date=NOW(), writer=$11 WHERE id=$12 AND company_code=$13`, [wiStatus||"일반", progressStatus||"", reason||"", startDate||"", endDate||"", equipmentId||"", workTeam||"", worker||"", remark||"", routingVersionId||null, userId, editId, companyCode] ); - await client.query(`DELETE FROM work_instruction_detail WHERE work_instruction_no=$1 AND company_code=$2`, [wiNo, companyCode]); + await client.query(`DELETE FROM work_instruction_detail WHERE work_instruction_id=$1`, [wiId]); } else { try { const rule = await numberingRuleService.getNumberingRuleByColumn(companyCode, "work_instruction", "work_instruction_no"); @@ -190,8 +190,8 @@ export async function save(req: AuthenticatedRequest, res: Response) { for (const item of items) { await client.query( - `INSERT INTO work_instruction_detail (id,company_code,work_instruction_no,item_number,qty,remark,source_table,source_id,part_code,routing_version_id,created_date,writer) VALUES (gen_random_uuid()::text,$1,$2,$3,$4,$5,$6,$7,$8,$9,NOW(),$10)`, - [companyCode, wiNo, item.itemNumber||item.itemCode||"", item.qty||"0", item.remark||"", item.sourceTable||"", item.sourceId||"", item.partCode||item.itemNumber||item.itemCode||"", item.routing||null, userId] + `INSERT INTO work_instruction_detail (id,company_code,work_instruction_no,work_instruction_id,item_number,qty,remark,source_table,source_id,part_code,routing_version_id,created_date,writer) VALUES (gen_random_uuid()::text,$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,NOW(),$11)`, + [companyCode, wiNo, wiId, item.itemNumber||item.itemCode||"", item.qty||"0", item.remark||"", item.sourceTable||"", item.sourceId||"", item.partCode||item.itemNumber||item.itemCode||"", item.routing||null, userId] ); } @@ -216,10 +216,8 @@ export async function remove(req: AuthenticatedRequest, res: Response) { const client = await pool.connect(); try { await client.query("BEGIN"); - const wiNos = await client.query(`SELECT work_instruction_no FROM work_instruction WHERE id=ANY($1) AND company_code=$2`, [ids, companyCode]); - for (const row of wiNos.rows) { - await client.query(`DELETE FROM work_instruction_detail WHERE work_instruction_no=$1 AND company_code=$2`, [row.work_instruction_no, companyCode]); - } + // 디테일 삭제 (id 기반) + await client.query(`DELETE FROM work_instruction_detail WHERE work_instruction_id=ANY($1)`, [ids]); const result = await client.query(`DELETE FROM work_instruction WHERE id=ANY($1) AND company_code=$2`, [ids, companyCode]); await client.query("COMMIT"); return res.json({ success: true, deletedCount: result.rowCount });