- product_types 참조 테이블 + projects.product_type_id FK (tkuser 마이그레이션) - schedule_entries에 work_type_id, risk_assessment_id, source 컬럼 추가 - schedule_phases에 product_type_id 추가 (phase 오염 방지) - generateFromTemplate: tksafety 템플릿 기반 공정 자동 생성 (트랜잭션) - phase 매칭 3단계 우선순위 (전용→범용→신규) - 간트 데이터 NULL 날짜 guard 추가 - system1 startup 마이그레이션 러너 추가 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
98 lines
3.5 KiB
JavaScript
98 lines
3.5 KiB
JavaScript
/**
|
|
* Project Model
|
|
*
|
|
* projects 테이블 CRUD (MariaDB)
|
|
* System 1과 같은 DB를 공유
|
|
*/
|
|
|
|
const { getPool } = require('./userModel');
|
|
|
|
async function getAll() {
|
|
const db = getPool();
|
|
const [rows] = await db.query(
|
|
`SELECT p.*, pt.code AS product_type_code, pt.name AS product_type_name
|
|
FROM projects p
|
|
LEFT JOIN product_types pt ON p.product_type_id = pt.id
|
|
ORDER BY p.project_id DESC`
|
|
);
|
|
return rows;
|
|
}
|
|
|
|
async function getActive() {
|
|
const db = getPool();
|
|
const [rows] = await db.query(
|
|
`SELECT p.*, pt.code AS product_type_code, pt.name AS product_type_name
|
|
FROM projects p
|
|
LEFT JOIN product_types pt ON p.product_type_id = pt.id
|
|
WHERE p.is_active = TRUE ORDER BY p.project_name ASC`
|
|
);
|
|
return rows;
|
|
}
|
|
|
|
async function getById(id) {
|
|
const db = getPool();
|
|
const [rows] = await db.query(
|
|
`SELECT p.*, pt.code AS product_type_code, pt.name AS product_type_name
|
|
FROM projects p
|
|
LEFT JOIN product_types pt ON p.product_type_id = pt.id
|
|
WHERE p.project_id = ?`,
|
|
[id]
|
|
);
|
|
return rows[0] || null;
|
|
}
|
|
|
|
async function getProductTypes() {
|
|
const db = getPool();
|
|
const [rows] = await db.query(
|
|
'SELECT * FROM product_types WHERE is_active = TRUE ORDER BY display_order'
|
|
);
|
|
return rows;
|
|
}
|
|
|
|
async function create({ job_no, project_name, contract_date, due_date, delivery_method, site, pm, product_type_id }) {
|
|
const db = getPool();
|
|
const [result] = await db.query(
|
|
`INSERT INTO projects (job_no, project_name, contract_date, due_date, delivery_method, site, pm, product_type_id)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
[job_no, project_name, contract_date || null, due_date || null, delivery_method || null, site || null, pm || null, product_type_id || null]
|
|
);
|
|
return getById(result.insertId);
|
|
}
|
|
|
|
async function update(id, data) {
|
|
const db = getPool();
|
|
const fields = [];
|
|
const values = [];
|
|
|
|
if (data.job_no !== undefined) { fields.push('job_no = ?'); values.push(data.job_no); }
|
|
if (data.project_name !== undefined) { fields.push('project_name = ?'); values.push(data.project_name); }
|
|
if (data.contract_date !== undefined) { fields.push('contract_date = ?'); values.push(data.contract_date || null); }
|
|
if (data.due_date !== undefined) { fields.push('due_date = ?'); values.push(data.due_date || null); }
|
|
if (data.delivery_method !== undefined) { fields.push('delivery_method = ?'); values.push(data.delivery_method); }
|
|
if (data.site !== undefined) { fields.push('site = ?'); values.push(data.site); }
|
|
if (data.pm !== undefined) { fields.push('pm = ?'); values.push(data.pm); }
|
|
if (data.is_active !== undefined) { fields.push('is_active = ?'); values.push(data.is_active); }
|
|
if (data.project_status !== undefined) { fields.push('project_status = ?'); values.push(data.project_status); }
|
|
if (data.completed_date !== undefined) { fields.push('completed_date = ?'); values.push(data.completed_date || null); }
|
|
if (data.product_type_id !== undefined) { fields.push('product_type_id = ?'); values.push(data.product_type_id || null); }
|
|
|
|
if (fields.length === 0) return getById(id);
|
|
|
|
values.push(id);
|
|
await db.query(
|
|
`UPDATE projects SET ${fields.join(', ')} WHERE project_id = ?`,
|
|
values
|
|
);
|
|
return getById(id);
|
|
}
|
|
|
|
async function deactivate(id) {
|
|
const db = getPool();
|
|
await db.query(
|
|
'UPDATE projects SET is_active = FALSE, project_status = ? WHERE project_id = ?',
|
|
['completed', id]
|
|
);
|
|
}
|
|
|
|
module.exports = { getAll, getActive, getById, create, update, deactivate, getProductTypes };
|