This commit is contained in:
DDD1542
2026-04-23 14:32:52 +09:00
parent b3de4fc6d8
commit 2efe30e282
7 changed files with 531 additions and 132 deletions

View File

@@ -96,13 +96,13 @@ export async function getList(req: AuthenticatedRequest, res: Response) {
const countRes = await pool.query(countSql, params);
const totalCount = countRes.rows[0]?.cnt ?? 0;
// 2) 현재 페이지 WI id 목록
// 2) 현재 페이지 WI id 목록 (최신 생성순, 동일 created_date일 때 번호 내림차순)
const offset = (pageNum! - 1) * sizeNum!;
const pageSql = `
SELECT wi.id
FROM work_instruction wi
${whereClause}
ORDER BY wi.created_date DESC, wi.id DESC
ORDER BY wi.created_date DESC NULLS LAST, wi.work_instruction_no DESC
LIMIT ${sizeNum} OFFSET ${offset}
`;
const pageRes = await pool.query(pageSql, params);
@@ -128,6 +128,8 @@ export async function getList(req: AuthenticatedRequest, res: Response) {
wi.worker,
wi.remark AS wi_remark,
wi.created_date,
wi.batch_no,
wi.cutting_plan_id,
d.id AS detail_id,
d.item_number,
d.qty AS detail_qty,
@@ -148,7 +150,7 @@ export async function getList(req: AuthenticatedRequest, res: Response) {
COALESCE(e.equipment_code, '') AS equipment_code,
wi.routing AS routing_version_id,
COALESCE(rv.version_name, '') AS routing_name,
ROW_NUMBER() OVER (PARTITION BY wi.work_instruction_no ORDER BY d.created_date) AS detail_seq,
ROW_NUMBER() OVER (PARTITION BY wi.work_instruction_no ORDER BY d.created_date, d.id) AS detail_seq,
COUNT(*) OVER (PARTITION BY wi.work_instruction_no) AS detail_count
FROM work_instruction wi
INNER JOIN work_instruction_detail d
@@ -160,7 +162,7 @@ export async function getList(req: AuthenticatedRequest, res: Response) {
LEFT JOIN item_routing_version rv
ON wi.routing = rv.id AND rv.company_code = wi.company_code
WHERE wi.id = ANY($1::varchar[])
ORDER BY wi.created_date DESC, wi.id DESC, d.created_date ASC
ORDER BY wi.created_date DESC NULLS LAST, wi.work_instruction_no DESC, d.created_date ASC, d.id ASC
`;
const dataRes = await pool.query(dataSql, [wiIds]);
@@ -189,6 +191,8 @@ export async function getList(req: AuthenticatedRequest, res: Response) {
wi.worker,
wi.remark AS wi_remark,
wi.created_date,
wi.batch_no,
wi.cutting_plan_id,
d.id AS detail_id,
d.item_number,
d.qty AS detail_qty,
@@ -209,7 +213,7 @@ export async function getList(req: AuthenticatedRequest, res: Response) {
COALESCE(e.equipment_code, '') AS equipment_code,
wi.routing AS routing_version_id,
COALESCE(rv.version_name, '') AS routing_name,
ROW_NUMBER() OVER (PARTITION BY wi.work_instruction_no ORDER BY d.created_date) AS detail_seq,
ROW_NUMBER() OVER (PARTITION BY wi.work_instruction_no ORDER BY d.created_date, d.id) AS detail_seq,
COUNT(*) OVER (PARTITION BY wi.work_instruction_no) AS detail_count
FROM work_instruction wi
INNER JOIN work_instruction_detail d
@@ -219,7 +223,7 @@ export async function getList(req: AuthenticatedRequest, res: Response) {
LEFT JOIN equipment_mng e ON wi.equipment_id = e.id AND wi.company_code = e.company_code
LEFT JOIN item_routing_version rv ON wi.routing = rv.id AND rv.company_code = wi.company_code
${whereClause}
ORDER BY wi.created_date DESC, d.created_date ASC
ORDER BY wi.created_date DESC NULLS LAST, wi.work_instruction_no DESC, d.created_date ASC, d.id ASC
`;
const result = await pool.query(query, params);
@@ -262,7 +266,7 @@ export async function save(req: AuthenticatedRequest, res: Response) {
await ensureDetailRoutingColumn();
const companyCode = req.user!.companyCode;
const userId = req.user!.userId;
const { id: editId, status: wiStatus, progressStatus, reason, startDate, endDate, equipmentId, workTeam, worker, remark, items, routing: routingVersionId } = req.body;
const { id: editId, status: wiStatus, progressStatus, reason, startDate, endDate, equipmentId, workTeam, worker, remark, items, routing: routingVersionId, batchNo, cuttingPlanId } = req.body;
if (!items || items.length === 0) {
return res.status(400).json({ success: false, message: "품목을 선택해주세요" });
@@ -281,8 +285,8 @@ export async function save(req: AuthenticatedRequest, res: Response) {
wiId = editId;
wiNo = check.rows[0].work_instruction_no;
await client.query(
`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]
`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, batch_no=COALESCE($11, batch_no), cutting_plan_id=COALESCE($12, cutting_plan_id), updated_date=NOW(), writer=$13 WHERE id=$14 AND company_code=$15`,
[wiStatus||"일반", progressStatus||"", reason||"", startDate||"", endDate||"", equipmentId||"", workTeam||"", worker||"", remark||"", routingVersionId||null, batchNo||null, cuttingPlanId||null, userId, editId, companyCode]
);
await client.query(`DELETE FROM work_instruction_detail WHERE work_instruction_id=$1`, [wiId]);
} else {
@@ -296,8 +300,8 @@ export async function save(req: AuthenticatedRequest, res: Response) {
wiNo = `WI-${today}-${String(seqRes.rows[0].seq).padStart(3, "0")}`;
}
const insertRes = await client.query(
`INSERT INTO work_instruction (id,company_code,work_instruction_no,status,progress_status,reason,start_date,end_date,equipment_id,work_team,worker,remark,routing,created_date,writer) VALUES (gen_random_uuid()::text,$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,NOW(),$13) RETURNING id`,
[companyCode, wiNo, wiStatus||"일반", progressStatus||"", reason||"", startDate||"", endDate||"", equipmentId||"", workTeam||"", worker||"", remark||"", routingVersionId||null, userId]
`INSERT INTO work_instruction (id,company_code,work_instruction_no,status,progress_status,reason,start_date,end_date,equipment_id,work_team,worker,remark,routing,batch_no,cutting_plan_id,created_date,writer) VALUES (gen_random_uuid()::text,$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,NOW(),$15) RETURNING id`,
[companyCode, wiNo, wiStatus||"일반", progressStatus||"", reason||"", startDate||"", endDate||"", equipmentId||"", workTeam||"", worker||"", remark||"", routingVersionId||null, batchNo||null, cuttingPlanId||null, userId]
);
wiId = insertRes.rows[0].id;
}