<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="WebTreatmentProcedureProduct">

    <select id="selectListTreatmentProcedureProduct" parameterType="hashmap" resultType="hashmap">
        SELECT
             MU_TREATMENT_PROCEDURE_PRODUCT_ID                                   AS "muTreatmentProcedureId"
            ,ORDER_NUMBER                                                        AS "orderNumber"
            ,MU_PRODUCT_ID                                                       AS "muProductId"
            ,IFNULL(PRODUCT_NAME, '')                                            AS "productName"
            ,price                                                               AS "price"
            ,IFNULL((SELECT MC.COMPANY_NAME
                     FROM MU_PRODUCT AS MP
                        INNER JOIN MU_COMPANY_PRODUCT MCP
                          ON MP.MU_PRODUCT_ID = MCP.MU_PRODUCT_ID
                        INNER JOIN MU_COMPANY MC
                          ON MC.MU_COMPANY_ID = MCP.MU_COMPANY_ID
                          AND MP.USE_YN        = 'Y'
                          AND MCP.USE_YN       = 'Y'
                    WHERE MP.MU_PRODUCT_ID = MTPP.MU_PRODUCT_ID
                        LIMIT 0, 1
                        ),'')                                                    AS "companyName"
            ,IFNULL(VOLUME, '')                                                  AS "volume"
            ,IFNULL(useVOLUME, '')                                               AS "useVolume"
            ,UNIT_CODE
            ,UNIT_NAME                                                           AS "unitName"
            ,useUnitName                                                         AS "useUnit"
        FROM (
            SELECT
                 MU_TREATMENT_PROCEDURE_PRODUCT_ID
                ,MTPP.ORDER_NUMBER
                ,MTPP.MU_PRODUCT_ID
                ,MTPP.PRODUCT_NAME
                ,MTPP.VOLUME                                        AS "useVolume"
                ,MTPP.UNIT_CODE                                     AS "useUnitCode"
                ,MTPP.UNIT_NAME                                      AS "useUnitName"
                ,MP.VOLUME
                ,MP.UNIT_CODE
                ,MP.UNIT_NAME
                ,FORMAT(IFNULL(MSS.price,0),0)               	    AS "price"
            FROM MU_TREATMENT_PROCEDURE_PRODUCT  MTPP
                INNER JOIN MU_COMPANY_PRODUCT AS MCP
              ON MTPP.MU_PRODUCT_ID = MCP.MU_PRODUCT_ID
                LEFT JOIN MU_STOCK_SUM MSS
              ON MTPP.MU_PRODUCT_ID = MSS.MU_PRODUCT_ID
              AND MSS.USE_YN='Y'
                LEFT JOIN MU_COMPANY AS MC
              ON MC.MU_COMPANY_ID = MCP.MU_COMPANY_ID
                LEFT JOIN MU_PRODUCT AS MP
              ON MTPP.MU_PRODUCT_ID = MP.MU_PRODUCT_ID
            WHERE MTPP.USE_YN  = 'Y'
            AND MTPP.MU_TREATMENT_PROCEDURE_ID = #{muTreatmentProcedureId}
            GROUP BY MTPP.MU_PRODUCT_ID
            ) AS MTPP
            WHERE 1 = 1
    </select>

    <select id="selectTreatmentProcedureProduct" parameterType="hashmap" resultType="hashmap">
        SELECT MP.MU_TREATMENT_ID        AS muTreatmentId
               ,CATEGORY_ITEM_NAME       AS treatmentName
               ,MP.MU_PRODUCT_ID         AS muProductId
               ,MP.PRODUCT_CODE          AS productCode
               ,MP.PRODUCT_NAME          AS productName
               ,MCP.MU_COMPANY_ID        AS muCompanyId
               ,COMPANY_NAME             AS companyName
               ,MTPP.VOLUME / MP.VOLUME  AS useQuantity
               ,MU_STOCK_SUM_ID          AS muStockSumId
               ,MU_STOCK_HISTORY_ID      AS stockStockHistoryId
               ,MSS.PRICE                AS price
               ,QUANTITY                 AS quantity
               ,CONCAT('고객명: ', USER_NAME, '/ 시술명: ', TREATMENT_PROCEDURE_NAME) AS etc
        FROM MU_TREATMENT_PROCEDURE_PRODUCT MTPP
                 JOIN MU_TREATMENT_PROCEDURE MTP
                      ON MTPP.MU_TREATMENT_PROCEDURE_ID = MTP.MU_TREATMENT_PROCEDURE_ID
                 JOIN MU_COMPANY_PRODUCT MCP
                      ON MTPP.MU_PRODUCT_ID = MCP.MU_PRODUCT_ID
                 JOIN MU_PRODUCT MP
                      ON MTPP.MU_PRODUCT_ID = MP.MU_PRODUCT_ID
                 JOIN MU_CATEGORY_ITEM MCI
                      ON MP.MU_TREATMENT_ID = MCI.MU_CATEGORY_ITEM_ID
                 JOIN (
            SELECT MU_PRODUCT_ID
                 , PRICE
                 , COMPANY_NAME
                 , REG_DATE
                 , MU_STOCK_HISTORY_ID
                 , MU_STOCK_SUM_ID
                 , QUANTITY
            FROM MU_STOCK_SUM MSS
            WHERE USE_YN = 'Y'
              AND REG_DATE = (
                SELECT MAX(REG_DATE)
                FROM MU_STOCK_SUM
                WHERE USE_YN = 'Y'
                  AND MU_PRODUCT_ID = MSS.MU_PRODUCT_ID
            )
        ) MSS ON MP.MU_PRODUCT_ID = MSS.MU_PRODUCT_ID
                 JOIN MU_USER MU
                      ON MU.MU_USER_ID = #{muUserId}
        WHERE MTPP.MU_TREATMENT_PROCEDURE_ID = #{muTreatmentProcedureId}
          AND MTPP.USE_YN = 'Y'
          AND MCP.USE_YN = 'Y'
          AND MP.USE_YN = 'Y'
          AND MU.USE_YN = 'Y'
          AND MTP.USE_YN = 'Y'
    </select>

    <insert id="insertTreatmentProcedureProduct" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muTreatmentProcedureProductId},LPAD((SELECT NEXTVAL(MU_TREATMENT_PROCEDURE_PRODUCT_SEQ)), 9, 0))
        </selectKey>
        INSERT INTO MU_TREATMENT_PROCEDURE_PRODUCT(
        MU_TREATMENT_PROCEDURE_PRODUCT_ID
        ,MU_TREATMENT_PROCEDURE_ID
        ,ORDER_NUMBER
        ,MU_PRODUCT_ID
        ,PRODUCT_NAME
        ,UNIT_CODE
        ,UNIT_NAME
        ,VOLUME
        ,WRITE_DATE
        ,WRITE_TIME
        ,CUD_FLAG
        ,USE_YN
        ,REG_ID
        ,REG_DATE
        ,MOD_ID
        ,MOD_DATE
        ,T_ID
        ,T_DATE
        )VALUES(
        #{id}
        ,#{muTreatmentProcedureId}
        ,#{orderNumber}
        ,#{muProductId}
        ,#{productName}
        ,#{unitCode}
        ,#{unitName}
        ,#{productUseVolume}
        ,CURDATE()
        ,CURTIME()
        ,'C'
        ,'Y'
        ,#{regId}
        ,NOW()
        ,#{modId}
        ,NOW()
        ,#{tId}
        ,#{tDate}
        )
    </insert>

    <update id="deleteTreatmentProcedureProduct" parameterType="hashmap">
        UPDATE MU_TREATMENT_PROCEDURE_PRODUCT
        SET MOD_ID                    = #{modId}
          ,MOD_DATE                  = NOW()
          ,CUD_FLAG                  = 'D'
          ,USE_YN                    = 'N'
        WHERE USE_YN                          = 'Y'
          AND MU_TREATMENT_PROCEDURE_ID       = #{muTreatmentProcedureId}
    </update>

</mapper>