// ============================================================
// 개발관리 PART (M1·M2·상세) 공용 SELECT fragment.
// wace_plm/src/com/pms/mapper/partMng.xml#partMngBaseSimple 1:1 이식 +
// vexplor_rps part_mng 의 15 추가컬럼(acctfg/odrfg/unit_dc/unitmang_dc/
// lot_fg/use_yn/qc_fg/setitem_fg/req_fg/unit_length/unit_qty/
// heat_treatment_hardness/heat_treatment_method/surface_treatment/unitchng_nb)
// 의 *_NM(comm_code 라벨) / Y/N CASE 변환 추가.
//
// 검색/페이징은 호출 측에서 WHERE 절·LIMIT/OFFSET 만 덧붙여 사용.
// ============================================================

/**
 * partMngBaseSimple — wace 운영판 1:1.
 * SELECT 컬럼만 정의. 호출 측에서 `${PART_BASE_SIMPLE} WHERE … ORDER BY …` 형태로 조합.
 */
export const PART_BASE_SIMPLE = `
  SELECT
    -- 기본 컬럼 (wace partMngBaseSimple 1:1)
    P.OBJID,
    P.PART_NO,
    P.PART_NAME,
    P.PRODUCT_MGMT_OBJID,
    P.UPG_NO,
    P.UNIT,
    CC_UNIT.code_name                                                   AS UNIT_TITLE,
    COALESCE(
      (SELECT QTY FROM BOM_PART_QTY Q
        WHERE Q.LAST_PART_OBJID = P.OBJID::varchar
          AND Q.STATUS = 'deploy'
        ORDER BY Q.DEPLOY_DATE DESC LIMIT 1),
      P.QTY
    )                                                                    AS QTY,
    P.SPEC,
    P.POST_PROCESSING,
    P.MATERIAL,
    P.WEIGHT,
    P.PART_TYPE,
    CC_PART.code_name                                                   AS PART_TYPE_TITLE,
    P.REMARK,
    P.ES_SPEC,
    P.MS_SPEC,
    P.CHANGE_TYPE,
    P.DESIGN_APPLY_POINT,
    P.CHANGE_OPTION,
    -- CHANGE_OPTION 다중 라벨 ARRAY_AGG (wace 1:1)
    (SELECT ARRAY_TO_STRING(ARRAY_AGG(CC.code_name), ',')
       FROM COMM_CODE CC
      WHERE CC.code_id IN (
        SELECT UNNEST(STRING_TO_ARRAY(P.CHANGE_OPTION, ','))
      ))                                                                AS CHANGE_OPTION_NAME,
    P.MANAGEMENT_FLAG,
    P.REVISION,
    P.STATUS,
    P.REG_DATE,
    TO_CHAR(P.REG_DATE, 'YYYY-MM-DD')                                   AS PART_REGDATE_TITLE,
    P.EDIT_DATE,
    P.WRITER,
    P.IS_LAST,
    P.IS_LONGD,
    P.EO_DATE,
    P.EO_NO,
    P.EO_TEMP,
    P.MAKER,
    P.CONTRACT_OBJID,
    P.THICKNESS,
    P.WIDTH,
    P.HEIGHT,
    P.OUT_DIAMETER,
    P.IN_DIAMETER,
    P.LENGTH,
    P.SOURCING_CODE,
    P.SUPPLY_CODE,
    SUP.SUPPLY_NAME                                                     AS SUPPLY_NAME,
    P.SUB_MATERIAL,
    P.PARENT_PART_NO,
    P.DESIGN_DATE,
    P.DEPLOY_DATE,
    P.EXCEL_UPLOAD_SEQ,

    -- 첨부 파일 카운트
    (SELECT COUNT(1) FROM ATTACH_FILE_INFO F
      WHERE F.TARGET_OBJID = P.OBJID::varchar AND F.DOC_TYPE = '3D_CAD')           AS CU01_CNT,
    (SELECT COUNT(1) FROM ATTACH_FILE_INFO F
      WHERE F.TARGET_OBJID = P.OBJID::varchar AND F.DOC_TYPE = '2D_DRAWING_CAD')   AS CU02_CNT,
    (SELECT COUNT(1) FROM ATTACH_FILE_INFO F
      WHERE F.TARGET_OBJID = P.OBJID::varchar AND F.DOC_TYPE = '2D_PDF_CAD')       AS CU03_CNT,
    (SELECT COUNT(1) FROM ATTACH_FILE_INFO F
      WHERE F.TARGET_OBJID = P.OBJID::varchar
        AND F.DOC_TYPE IN ('2D_PDF_CAD','2D_DRAWING_CAD'))                         AS CU_TOTAL_CNT,

    -- 추가 15컬럼(301_alter_part_mng.sql) + 라벨
    P.HEAT_TREATMENT_HARDNESS,
    P.HEAT_TREATMENT_METHOD,
    P.SURFACE_TREATMENT,
    P.ACCTFG,
    CC_ACCT.code_name                                                   AS ACCTFG_NM,
    P.ODRFG,
    CC_ODR.code_name                                                    AS ODRFG_NM,
    P.UNIT_DC,
    CC_UDC.code_name                                                    AS UNIT_DC_NM,
    P.UNITMANG_DC,
    CC_UMDC.code_name                                                   AS UNITMANG_DC_NM,
    P.UNITCHNG_NB,
    P.LOT_FG,
    CASE WHEN P.LOT_FG     = '1' THEN '예' WHEN P.LOT_FG     = '0' THEN '아니오' ELSE '' END AS LOT_FG_NM,
    P.USE_YN,
    CASE WHEN P.USE_YN     = '1' THEN '예' WHEN P.USE_YN     = '0' THEN '아니오' ELSE '' END AS USE_YN_NM,
    P.QC_FG,
    CASE WHEN P.QC_FG      = '1' THEN '예' WHEN P.QC_FG      = '0' THEN '아니오' ELSE '' END AS QC_FG_NM,
    P.SETITEM_FG,
    CASE WHEN P.SETITEM_FG = '1' THEN '예' WHEN P.SETITEM_FG = '0' THEN '아니오' ELSE '' END AS SETITEM_FG_NM,
    P.REQ_FG,
    CASE WHEN P.REQ_FG     = '1' THEN '예' WHEN P.REQ_FG     = '0' THEN '아니오' ELSE '' END AS REQ_FG_NM,
    P.UNIT_LENGTH,
    P.UNIT_QTY

  FROM PART_MNG P
  LEFT JOIN COMM_CODE CC_UNIT ON CC_UNIT.code_id = P.UNIT        AND CC_UNIT.status = 'active'
  LEFT JOIN COMM_CODE CC_PART ON CC_PART.code_id = P.PART_TYPE   AND CC_PART.status = 'active'
  LEFT JOIN COMM_CODE CC_ACCT ON CC_ACCT.code_id = P.ACCTFG      AND CC_ACCT.status = 'active'
  LEFT JOIN COMM_CODE CC_ODR  ON CC_ODR.code_id  = P.ODRFG       AND CC_ODR.status  = 'active'
  LEFT JOIN COMM_CODE CC_UDC  ON CC_UDC.code_id  = P.UNIT_DC     AND CC_UDC.status  = 'active'
  LEFT JOIN COMM_CODE CC_UMDC ON CC_UMDC.code_id = P.UNITMANG_DC AND CC_UMDC.status = 'active'
  LEFT JOIN admin_supply_mng SUP ON SUP.OBJID::varchar = P.SUPPLY_CODE
`;
