-- liquibase formatted sql
-- changeset enson:alter_pick_execution_issue_category_20251023

CREATE TABLE productprocess (
    id INT AUTO_INCREMENT PRIMARY KEY,
    productprocesscode VARCHAR(50) UNIQUE NOT NULL COMMENT '生产工序编号',
    jobOrderId INT COMMENT '工单ID',
    itemsId INT COMMENT '产品ID',
    
    -- 状态
    status VARCHAR(20) DEFAULT 'pending' ,
    
    -- 时间
    startTime DATETIME ,
    endTime DATETIME ,
    date DATE ,
    
    -- 审计字段
    created DATETIME,
    createdBy VARCHAR(30),
    version INT DEFAULT 0,
    modified DATETIME,
    modifiedBy VARCHAR(30),
    deleted TINYINT(1) DEFAULT 0,
    
    FOREIGN KEY (jobOrderId) REFERENCES job_order(id),
    FOREIGN KEY (itemsId) REFERENCES items(id),
    
    INDEX idx_status (status),
    INDEX idx_date (date),
    INDEX idx_job_order (jobOrderId)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


CREATE TABLE productprocessline (
    id INT AUTO_INCREMENT PRIMARY KEY,
    productprocessid INT NOT NULL ,
    
    -- 操作员和设备
    operatorId INT  ,
    equipmentId INT,
    
    -- 步骤信息
    name VARCHAR(100) ,
    description varchar(255) ,
    equipment VARCHAR(100) ,
    
    -- 副产品信息
    byproductId INT ,
    byproductName VARCHAR(100) ,
    byproductQty DECIMAL(16,2) ,
    byproductUom VARCHAR(20) ,
    
    -- 废品/次品/产出
    scrapQty DECIMAL(16,2) ,
    scrapUom VARCHAR(20) ,
    defectQty DECIMAL(16,2) ,
    defectUom VARCHAR(20) ,
    outputFromProcessQty DECIMAL(16,2) ,
    outputFromProcessUom VARCHAR(20) ,
    
    -- 时间
    startTime DATETIME ,
    endTime DATETIME ,
    
    -- 审计字段
    created DATETIME,
    createdBy VARCHAR(30),
    version INT DEFAULT 0,
    modified DATETIME,
    modifiedBy VARCHAR(30),
    deleted TINYINT(1) DEFAULT 0,
    
    FOREIGN KEY (productprocessid) REFERENCES productprocess(id),
    FOREIGN KEY (operatorId) REFERENCES user(id),
    FOREIGN KEY (equipmentId) REFERENCES equipment(id),
    FOREIGN KEY (byproductId) REFERENCES items(id),
    
    INDEX idx_productprocess (productprocessid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE productionprocessissue (
    id INT AUTO_INCREMENT PRIMARY KEY,
    productprocessid INT NOT NULL ,
    operatorId INT ,
    operatorName VARCHAR(100) ,
    
    -- 暂停信息
    reason varchar(255) NOT NULL ,
    stopTime DATETIME NOT NULL ,
    resumeTime DATETIME ,
    totalTime INT ,
    
    -- 审计字段
    created DATETIME,
    createdBy VARCHAR(30),
    version INT DEFAULT 0,
    modified DATETIME,
    modifiedBy VARCHAR(30),
    deleted TINYINT(1) DEFAULT 0,
    
    FOREIGN KEY (productprocessid) REFERENCES productprocess(id),
    FOREIGN KEY (operatorId) REFERENCES user(id),
    
    INDEX idx_productprocess (productprocessid),
    INDEX idx_stop_time (stopTime)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;