<?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="com.oqpo.api.mapper.oper.MainChartMapper">

    <select id="selectBrandMainCount" resultType="com.oqpo.api.entity.oper.MainChartEntity">
        /* MainChartMapper.selectBrandMainCount */
        SELECT *
        FROM (
            /* 구매요청 건수 */
            SELECT COUNT(*) AS PR20_VAL
            FROM pch_req_base_info A
            WHERE 1 = 1
            AND A.PCH_REQ_ST_CD = 'PR20'
            AND A.PCH_REQ_DT = DATE_FORMAT(NOW(), '%Y%m%d')
            AND A.BRAND_ID = #{brandId}
        ) T1, (
            /* 입금액 */
            SELECT IFNULL(SUM(A.DPST_AMT), 0) AS DP02_VAL
            FROM loan_mgnt_dspt_proc A
            WHERE 1 = 1
            AND A.DPST_ST_CD = 'DP02'
            AND A.DPST_DT = DATE_FORMAT(NOW(), '%Y%m%d')
            AND A.BRAND_ID = #{brandId}
        ) T2, (
            /* 미납금 */
            SELECT IFNULL(SUM(IFNULL(A.STTL_AMT, A.STTL_REQ_AMT) - IFNULL(A.RCPT_AMT, 0)), 0) AS UNPAID_AMT
            FROM stl_mgnt_base_info A               /* 정산관리기본정보 */
            INNER JOIN store_base_info B ON A.STORE_ID = B.STORE_ID     /* 매장 미납금액 */
            WHERE 1 = 1
            AND B.LOAN_DVSN = 'LD02'
            AND A.STTL_REQ_DT BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 7 DAY), '%Y%m%d') AND DATE_FORMAT(NOW(), '%Y%m%d')
            AND A.STTL_ST_CD IN ('ST10','ST20')         /* 정산요청,정산확정 */
            AND IFNULL(A.RCPT_YN,'N') = 'N'             /* 수납완료여부 */
            AND A.STTL_TARGET_CD = 'S0'                 /* STTL_TARGET_CD(정산대상코드) : S0(매장정산), S1(공급사정산) */
            AND A.BRAND_ID = #{brandId}
        ) T3
    </select>

    <select id="selectStoreMainCount" resultType="com.oqpo.api.entity.oper.MainChartEntity">
        /* MainChartMapper.selectStoreMainCount */
        SELECT *
        FROM (
            SELECT COUNT(*) AS PR20_VAL
            FROM pch_req_base_info A                /* 구매요청기본정보 */
            WHERE 1 = 1
            AND A.PCH_REQ_ST_CD IN ('PR20', 'PR30')
            AND A.PCH_REQ_DT = DATE_FORMAT(NOW(), '%Y%m%d')
            AND A.STORE_ID = #{storeId}
        ) T1, (
            SELECT COUNT(*) AS PO30_VAL
            FROM pch_odr_base_info A                /* 구매발주기본정보 */
            WHERE 1 = 1
            AND A.PCH_ODR_ST_CD = 'PO30'            /* 발주접수 */
            AND A.PODR_DT = DATE_FORMAT(NOW(), '%Y%m%d')
            AND A.STORE_ID = #{storeId}
        ) T2, (
            SELECT COUNT(*) AS DLV_VAL
            FROM deli_info_mgnt A               /* 납품정보관리 */
            WHERE A.DLV_ST_CD = 'DLV1'          /* 완료 */
            AND A.DLV_CMPLT_DT = DATE_FORMAT(NOW(), '%Y%m%d')
            AND A.STORE_ID = #{storeId}
        ) T3, (
            SELECT
                CASE
                WHEN B.LOAN_DVSN = 'LD01' THEN B.ACCT_BAL + IFNULL(B.TMP_RCV_ACCT_TOTAL,0) - IFNULL(B.USE_AMT_TOTAL,0) /* 선불 */
                WHEN B.LOAN_DVSN = 'LD02' THEN B.CREDT_LIMIT_AMT + B.ACCT_BAL + IFNULL(B.TMP_RCV_ACCT_TOTAL,0) - IFNULL(B.USE_AMT_TOTAL,0) /* 후불 */
                END PO_PSBL_AMT
            FROM store_base_info A
            INNER JOIN loan_mgnt_base_info B ON A.LOAN_MGNT_UNQ_NO = B.LOAN_MGNT_UNQ_NO
            WHERE A.STORE_ID = #{storeId}
        ) T4
    </select>

    <select id="selectSpplyMainCount" resultType="com.oqpo.api.entity.oper.MainChartEntity">
        /* MainChartMapper.selectSpplyMainCount */
        SELECT *
        FROM (
            SELECT COUNT(*) AS ORDERS_COUNT     /* 수주건수 : 납품서생성 */
                , IFNULL(SUM(A.DLV_AMT),0) AS ORDERS_AMT  /* 수주금액 */
            FROM deli_info_mgnt A               /* 납품정보관리 */
            WHERE A.DLV_ST_CD IN ('DLV0', 'DLV1') /* 생성, 완료 */
            AND A.DLV_REG_DT = DATE_FORMAT(NOW(), '%Y%m%d')     /* 납품생성일자 */
            AND A.SPPLY_ID = #{spplyId}
        ) T1, (
            SELECT IFNULL(SUM(A.STTL_REQ_AMT), 0) AS STTL_REQ_AMT  /* 정산요청금액 */
            FROM stl_mgnt_base_info A                   /* 정산관리기본정보 */
            WHERE A.STTL_ST_CD  IN ('ST10', 'ST20') /* 정산요청, 정산확정 */
            AND A.SPPLY_ID = #{spplyId}
            AND A.STTL_REQ_DT =  DATE_FORMAT(NOW(), '%Y%m%d') 
        ) T2
    </select>

    <select id="selectPchReqChart" resultType="com.oqpo.api.entity.oper.MainChartEntity">
        /* MainChartMapper.selectPchReqChart */
        SELECT
            COUNT(CASE WHEN A.pch_req_st_cd = 'PR20' THEN A.pch_req_unq_no END) AS pr20_val,
            COUNT(CASE WHEN A.pch_req_st_cd = 'PR30' THEN A.pch_req_unq_no END) AS pr30_val,
            COUNT(CASE WHEN A.pch_req_st_cd = 'PR40' THEN A.pch_req_unq_no END) AS pr40_val
        FROM pch_req_base_info A
        WHERE 1 = 1
        <if test="brandId != null and brandId != '' ">
        AND A.brand_id = #{brandId}
        </if>
        <if test="storeId != null and storeId != '' ">
        AND A.store_id = #{storeId}
        </if>
        <if test='"A".equals(prType)'>
        AND A.pch_req_dt = DATE_FORMAT(NOW(), '%Y%m%d')
        </if>
        <if test='"B".equals(prType)'>
        AND A.pch_req_dt BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 7 DAY), '%Y%m%d') AND DATE_FORMAT(NOW(), '%Y%m%d')
        </if>
        <if test='"C".equals(prType)'>
        AND A.pch_req_dt BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 15 DAY), '%Y%m%d') AND DATE_FORMAT(NOW(), '%Y%m%d')
        </if>
        <if test='"D".equals(prType)'>
        AND A.pch_req_dt BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH), '%Y%m%d') AND DATE_FORMAT(NOW(), '%Y%m%d')
        </if>
    </select>

    <select id="selectPchOdrChart" resultType="com.oqpo.api.entity.oper.MainChartEntity">
        /* MainChartMapper.selectPchOdrChart */
        SELECT
        COUNT(CASE WHEN A.pch_odr_st_cd = 'PO20' THEN A.pch_podr_unq_no END) AS po20_val,
        COUNT(CASE WHEN A.pch_odr_st_cd = 'PO30' THEN A.pch_podr_unq_no END) AS po30_val,
        COUNT(CASE WHEN A.pch_odr_st_cd = 'PO99' THEN A.pch_podr_unq_no END) AS po99_val
        FROM pch_odr_base_info A
        WHERE 1 = 1
        <if test="brandId != null and brandId != '' ">
            AND A.brand_id = #{brandId}
        </if>
        <if test="storeId != null and storeId != '' ">
            AND A.store_id = #{storeId}
        </if>
        <if test="spplyId != null and spplyId != '' ">
            AND A.spply_id = #{spplyId}
        </if>
        <if test='"A".equals(poType)'>
            AND A.podr_dt = DATE_FORMAT(NOW(), '%Y%m%d')
        </if>
        <if test='"B".equals(poType)'>
            AND A.podr_dt BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 7 DAY), '%Y%m%d') AND DATE_FORMAT(NOW(), '%Y%m%d')
        </if>
        <if test='"C".equals(poType)'>
            AND A.podr_dt BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 15 DAY), '%Y%m%d') AND DATE_FORMAT(NOW(), '%Y%m%d')
        </if>
        <if test='"D".equals(poType)'>
            AND A.podr_dt BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH), '%Y%m%d') AND DATE_FORMAT(NOW(), '%Y%m%d')
        </if>
    </select>

    <select id="selectStlChart" resultType="com.oqpo.api.entity.oper.MainChartEntity">
        /* MainChartMapper.selectStlChart */
        SELECT
            COUNT(CASE WHEN A.sttl_st_cd = 'ST10' THEN A.sttl_mgnt_unq_no END) AS st10_val,
            COUNT(CASE WHEN A.sttl_st_cd = 'ST20' THEN A.sttl_mgnt_unq_no END) AS st20_val,
            COUNT(CASE WHEN A.sttl_st_cd = 'ST30' THEN A.sttl_mgnt_unq_no END) AS st30_val
        FROM stl_mgnt_base_info A
        WHERE 1 = 1
        AND A.sttl_req_dt BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 7 DAY), '%Y%m%d') AND DATE_FORMAT(NOW(), '%Y%m%d')       /* 최근 7일 */
        <if test="brandId != null and brandId != '' ">
            AND A.brand_id = #{brandId}
            AND A.sttl_target_cd = 'S0'     /* 매장정산 */
        </if>
        <if test="storeId != null and storeId != '' ">
            AND A.store_id = #{storeId}
            AND A.sttl_target_cd = 'S0'     /* 매장정산 */
        </if>
        <if test="spplyId != null and spplyId != '' ">
            AND A.spply_id = #{spplyId}
            AND A.sttl_target_cd = 'S1'     /* 공급사정산 */
        </if>
    </select>

    <select id="selectPrRankChart" resultType="com.oqpo.api.entity.oper.MainChartEntity">
        /* MainChartMapper.selectPrRankChart */
        SELECT prdp.item_id, prdp.item_nm,
            SUM(prdp.PCH_REQ_QTY) AS item_cnt
          FROM pch_req_base_info prbi
             , pch_req_dtl_ptcl prdp
             , brand_base_info brand
         WHERE 1 = 1
           AND prbi.pch_req_unq_no = prdp.pch_req_unq_no
           AND prbi.brand_id = brand.brand_id
            <if test='"A".equals(prType)'>
                AND prbi.pch_req_dt = DATE_FORMAT(NOW(), '%Y%m%d')
            </if>
            <if test='"B".equals(prType)'>
                AND prbi.pch_req_dt BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 7 DAY), '%Y%m%d') AND DATE_FORMAT(NOW(), '%Y%m%d')
            </if>
            <if test='"C".equals(prType)'>
                AND prbi.pch_req_dt BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 15 DAY), '%Y%m%d') AND DATE_FORMAT(NOW(), '%Y%m%d')
            </if>
            <if test='"D".equals(prType)'>
                AND prbi.pch_req_dt BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH), '%Y%m%d') AND DATE_FORMAT(NOW(), '%Y%m%d')
            </if>
           AND prbi.pch_req_st_cd IN ('PR20', 'PR30', 'PR40')
           AND prbi.pch_req_dvsn = 'PD01'
            <if test="brandId != null and brandId != '' ">
                AND prbi.brand_id = #{brandId}
            </if>
            <if test="storeId != null and storeId != '' ">
                AND prbi.store_id = #{storeId}
            </if>
        GROUP BY prdp.item_id, prdp.item_nm
        ORDER BY SUM(prdp.PCH_REQ_QTY) DESC, prdp.item_id
        LIMIT 0, 5
    </select>

    <select id="selectPoRankChart4Spply" resultType="com.oqpo.api.entity.oper.MainChartEntity">
        /* MainChartMapper.selectPoRankChart4Spply */
        SELECT podp.ITEM_ID, podp.ITEM_NM,
            SUM(podp.PODR_QTY) AS item_cnt
        FROM pch_odr_base_info pobi, pch_odr_dtl_ptcl podp
        WHERE 1 = 1
        AND pobi.PCH_PODR_UNQ_NO = podp.PCH_PODR_UNQ_NO
        <if test='"A".equals(poType)'>
            AND pobi.PODR_DT = DATE_FORMAT(NOW(), '%Y%m%d')
        </if>
        <if test='"B".equals(poType)'>
            AND pobi.PODR_DT BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 7 DAY), '%Y%m%d') AND DATE_FORMAT(NOW(), '%Y%m%d')
        </if>
        <if test='"C".equals(poType)'>
            AND pobi.PODR_DT BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 15 DAY), '%Y%m%d') AND DATE_FORMAT(NOW(), '%Y%m%d')
        </if>
        <if test='"D".equals(poType)'>
            AND pobi.PODR_DT BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH), '%Y%m%d') AND DATE_FORMAT(NOW(), '%Y%m%d')
        </if>
        AND pobi.SPPLY_ID IS NOT NULL
        AND LENGTH(pobi.SPPLY_ID) > 0
        AND pobi.PCH_ODR_ST_CD IN ('PO20', 'PO30')
        AND pobi.SPPLY_ID = #{spplyId}
        GROUP BY podp.ITEM_ID, podp.ITEM_NM
        ORDER BY COUNT(*) DESC, podp.ITEM_ID
        LIMIT 0, 5
    </select>

    <select id="selectSftStckChart" resultType="com.oqpo.api.entity.oper.MainChartEntity">
        /* MainChartMapper.selectSftStckChart */
        SELECT A.item_id, B.item_nm, A.stck_qty, A.prp_stck_qty, A.sft_stck_rt,
            CEIL( A.prp_stck_qty * A.sft_stck_rt / 100 ) AS sft_stck_qty,
            A.stck_qty / CEIL( A.prp_stck_qty * A.sft_stck_rt / 100 ) AS stck_eval
        FROM stck_mgnt_base_info A
        INNER JOIN item_base_info B ON A.item_id = B.item_id AND A.BRAND_ID = B.BRAND_ID
        INNER JOIN whs_mgnt_base_info C
       ON A.BRAND_ID = C.BRAND_ID
       AND A.WHS_ID = C.WHS_ID
       AND C.WHS_DVSN = 'W01'
        WHERE A.del_yn = 'N'
        <if test="brandId != null and brandId != '' ">
            AND A.brand_id = #{brandId}
        </if>
        <if test="storeId != null and storeId != '' ">
            AND A.store_id = #{storeId}
        </if>
        AND A.prp_stck_qty > 0
        AND A.sft_stck_rt IS NOT NULL
        ORDER BY A.stck_qty / CEIL( A.prp_stck_qty * A.sft_stck_rt / 100 ) ASC
        LIMIT 10
    </select>

    <select id="selectDeliChart" resultType="com.oqpo.api.entity.oper.MainChartEntity">
        /* MainChartMapper.selectDeliChart */
        SELECT COUNT(*) AS dlv_val
        FROM deli_info_mgnt A
        WHERE A.dlv_st_cd = #{dlvStCd}
        <if test="brandId != null and brandId != '' ">
            AND A.brand_id = #{brandId}
        </if>
        <if test="storeId != null and storeId != '' ">
            AND A.store_id = #{storeId}
        </if>
        <if test="spplyId != null and spplyId != '' ">
            AND A.spply_id = #{spplyId}
        </if>
        <if test='"A".equals(deliType)'>
            AND A.${dateColumn} = DATE_FORMAT(NOW(), '%Y%m%d')
        </if>
        <if test='"B".equals(deliType)'>
            AND A.${dateColumn} BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 7 DAY), '%Y%m%d') AND DATE_FORMAT(NOW(), '%Y%m%d')
        </if>
        <if test='"C".equals(deliType)'>
            AND A.${dateColumn} BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 15 DAY), '%Y%m%d') AND DATE_FORMAT(NOW(), '%Y%m%d')
        </if>
        <if test='"D".equals(deliType)'>
            AND A.${dateColumn} BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH), '%Y%m%d') AND DATE_FORMAT(NOW(), '%Y%m%d')
        </if>
    </select>

</mapper>
