<?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.sttlmng.SttlStateMapper">

    <select id="selectSttlStateGridList" resultType="com.oqpo.api.entity.settmng.StlMgntBaseInfoEntity">
        /* SttlStateMapper.selectSttlStateGridList */
        SELECT A.sttl_mgnt_unq_no,
            DATE_FORMAT(A.sttl_req_dt, '%Y.%m.%d') AS sttl_req_dt,
            DATE_FORMAT(A.sttl_dt, '%Y.%m.%d') AS sttl_dt,
            A.brand_id, FN_BRAND_NM(A.brand_id) AS brand_nm,
            A.store_id, FN_STORE_NM(A.brand_id, A.store_id) AS store_nm,
            A.spply_id, FN_SPPLY_NM(A.spply_id) AS spply_nm,
            A.sttl_st_cd, FN_CODE_NM('STTL_ST_CD', A.sttl_st_cd) AS sttl_st_nm,
            A.sttl_req_amt,         /* 정산요청금액 */
            A.sttl_amt,             /* 정산확정금액 */
            A.rcpt_amt,             /* 수납금액 */
            (A.sttl_amt - IFNULL(A.rcpt_amt, 0)) AS unpaid_amt,     /* 미납금액 */
            A.sttl_reg_mgr_nm, A.sttl_reg_mgr_id,
            B.loan_dvsn, FN_CODE_NM('LOAN_DVSN', B.loan_dvsn) AS loan_dvsn_nm
        FROM stl_mgnt_base_info A
        INNER JOIN store_base_info B ON A.brand_id = B.brand_id AND A.store_id = B.store_id
        WHERE A.brand_id = #{sBrandId}
        AND A.sttl_req_dt BETWEEN #{fromDt} AND #{toDt}
        AND A.sttl_st_cd IN ('ST10','ST20','ST30')
        <if test="sGubun == 1">
            AND A.store_id IS NOT NULL
            AND A.sttl_target_cd = 'S0'
            <if test="sStoreId != null and sStoreId != ''">
                AND A.store_id = #{sStoreId}
            </if>
        </if>
        <if test="sGubun == 2">
            AND A.spply_id IS NOT NULL
            AND A.sttl_target_cd = 'S1'
            <if test="sSpplyId != null and sSpplyId != ''">
                AND A.spply_id = #{sSpplyId}
            </if>
        </if>
        <if test="sSttlStCd != null and sSttlStCd != ''">
            AND A.sttl_st_cd = #{sSttlStCd}
        </if>
        <choose>
            <when test="gridRequest.sidx != null and gridRequest.sidx != ''">
                <if test="gridRequest.sidx == 'VIEW_NUM'.toString()">
                    <if test="gridRequest.sord == 'asc'.toString()">
                        ORDER BY A.sttl_mgnt_unq_no ASC
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        ORDER BY A.sttl_mgnt_unq_no DESC
                    </if>
                </if>
            </when>
            <otherwise>
                ORDER BY A.sttl_mgnt_unq_no ASC
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectSttlStateGridCnt" resultType="int">
        /* SttlStateMapper.selectSttlStateGridCnt */
        SELECT COUNT(*)
        FROM stl_mgnt_base_info A
        INNER JOIN store_base_info B ON A.brand_id = B.brand_id AND A.store_id = B.store_id
        WHERE A.brand_id = #{sBrandId}
        AND A.sttl_req_dt BETWEEN #{fromDt} AND #{toDt}
        AND A.sttl_st_cd IN ('ST10','ST20','ST30')
        <if test="sGubun == 1">
            AND A.store_id IS NOT NULL
            AND A.sttl_target_cd = 'S0'
            <if test="sStoreId != null and sStoreId != ''">
                AND A.store_id = #{sStoreId}
            </if>
        </if>
        <if test="sGubun == 2">
            AND A.spply_id IS NOT NULL
            AND A.sttl_target_cd = 'S1'
            <if test="sSpplyId != null and sSpplyId != ''">
                AND A.spply_id = #{sSpplyId}
            </if>
        </if>
        <if test="sSttlStCd != null and sSttlStCd != ''">
            AND A.sttl_st_cd = #{sSttlStCd}
        </if>
    </select>

</mapper>
