<?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.stinfo.SupplyMapper">

    <select id="selectPopSpplyGridList" resultType="com.oqpo.api.entity.stinfo.SpplyEntity">
        select  /* selectPopSpplyGridList */
                brand_id, fn_brand_nm(brand_id) as brand_nm, spply_id, spply_nm, st_cd, fn_code_nm('ST_CD',st_cd) as st_nm
        from   spply_base_info
        where  brand_id = #{sBrandId}
        and   st_cd = 'S000'
        <if test="sSpplyNm != null and sSpplyNm != ''">
        and  spply_nm like concat('%',#{sSpplyNm},'%')
        </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 spply_nm asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by spply_nm desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by spply_nm asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectPopSpplyGridCnt" resultType="int">
        select /*selectPopSpplyGridCnt*/
                count(*)
        from   spply_base_info
        where  brand_id = #{sBrandId}
        and   st_cd = 'S000'
        <if test="sSpplyNm != null and sSpplyNm != ''">
            and  spply_nm like concat('%',#{sSpplyNm},'%')
        </if>
    </select>

    <select id="selectSpplyGridList" resultType="com.oqpo.api.entity.stinfo.SpplyEntity">
        select /* selectSpplyGridList */
                brand_id, fn_brand_nm(brand_id) as brand_nm, spply_id, spply_nm, st_cd, fn_code_nm('ST_CD',st_cd) as st_nm
                , bsns_reg_no, owner_nm, date_format(sys_reg_dttm,'%Y.%m.%d %H:%i:%s') as sys_reg_dttm
        from   spply_base_info
        where  brand_id = #{sBrandId}
        <if test="sStCd != null and sStCd != ''">
        and   st_cd = #{sStCd}
        </if>
        <if test="sBsnsRegNo != null and sBsnsRegNo != ''">
        and    bsns_reg_no like concat('%',#{sBsnsRegNo},'%')
        </if>
        <if test="sSpplyNm != null and sSpplyNm != ''">
        and  spply_nm like concat('%',#{sSpplyNm},'%')
        </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 spply_nm asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by spply_nm desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by spply_nm asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>


    <select id="selectSpplyGridCnt" resultType="int">
        select  /* selectSpplyGridCnt */
             count(*) as cnt
        from   spply_base_info
        where  brand_id = #{sBrandId}
        <if test="sStCd != null and sStCd != ''">
            and   st_cd = #{sStCd}
        </if>
        <if test="sBsnsRegNo != null and sBsnsRegNo != ''">
            and    bsns_reg_no like concat('%',#{sBsnsRegNo},'%')
        </if>
        <if test="sSpplyNm != null and sSpplyNm != ''">
            and  spply_nm like concat('%',#{sSpplyNm},'%')
        </if>
    </select>

    <insert id="insertSpplyInfo" >
        insert into spply_base_info
        (
            spply_id, spply_nm, st_cd, corp_type, bsns_reg_no,
            corp_reg_no, owner_nm, ctgr_bsns, bsns_cndt, corp_dvsn,
            mbl_no, mgr_nm, area_cd,
            dept_nm, mgr_emal, mgr_tel_no, mgr_mbl_no, del_yn,
            lst_login_dttm,  zip_no, addr1, addr2, brand_id,
            bnk_cd, acct_no, owac_nm, bbook_copy_file_no, bsns_reg_file_no,
            sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id,
            lat_x, long_y
        )
        values
        (
            #{entity.spplyId}, #{entity.spplyNm}, #{entity.stCd}, #{entity.corpType}, #{entity.bsnsRegNo},
            #{entity.corpRegNo}, #{entity.ownerNm}, #{entity.ctgrBsns}, #{entity.bsnsCndt}, #{entity.corpDvsn},
            #{entity.mblNo}, #{entity.mgrNm}, #{entity.areaCd},
            #{entity.deptNm}, #{entity.mgrEmal}, #{entity.mgrTelNo}, #{entity.mgrMblNo}, 'N',
            null,  #{entity.zipNo}, #{entity.addr1}, #{entity.addr2}, #{entity.brandId},
            #{entity.bnkCd}, #{entity.acctNo}, #{entity.owacNm},  #{entity.bbookCopyFileNo}, #{entity.bsnsRegFileNo},
            now(), #{userId}, now(), #{userId},
            #{entity.latX}, #{entity.longY}
        )
    </insert>

    <update id="updateSpplyInfo">
        update spply_base_info  /* updateSpplyInfo */
        set	sys_chg_id		    = #{userId}
            ,sys_chg_dttm		= now()
            ,spply_nm		    = #{entity.spplyNm}
            ,st_cd			    = #{entity.stCd}
            ,corp_type		    = #{entity.corpType}
            ,bsns_reg_no		= #{entity.bsnsRegNo}
            ,corp_reg_no		= #{entity.corpRegNo}
            ,owner_nm		    = #{entity.ownerNm}
            ,ctgr_bsns		    = #{entity.ctgrBsns}
            ,bsns_cndt		    = #{entity.bsnsCndt}
            ,corp_dvsn		    = #{entity.corpDvsn}
            ,mbl_no			    = #{entity.mblNo}
            ,mgr_nm			    = #{entity.mgrNm}
            ,mgr_emal		    = #{entity.mgrEmal}
            ,mgr_tel_no		    = #{entity.mgrTelNo}
            ,mgr_mbl_no		    = #{entity.mgrMblNo}
            ,del_yn			    = #{entity.delYn}
            ,area_cd            = #{entity.areaCd}
            ,zip_no			    = #{entity.zipNo}
            ,addr1			    = #{entity.addr1}
            ,addr2			    = #{entity.addr2}
            ,lat_x              = #{entity.latX}
            ,long_y             = #{entity.longY}
            ,bnk_cd			    = #{entity.bnkCd}
            ,acct_no		    = #{entity.acctNo}
            ,owac_nm		    = #{entity.owacNm}
            ,bbook_copy_file_no	= #{entity.bbookCopyFileNo}
            ,bsns_reg_file_no	= #{entity.bsnsRegFileNo}
        where spply_id  = #{entity.spplyId}
    </update>

    <insert id="insertSpplyMgrInfo" >
        insert into spply_mgr_mgnt /* insertSpplyMgrInfo */
        (
            spply_id, spply_mgr_id, spply_mgr_nm,  rprst_tel_no,
            rprst_emal, position, assgn_task, sys_reg_dttm,
            sys_reg_id, sys_chg_dttm, sys_chg_id
        )
        values
        <foreach collection="gridInsertData" item="item" separator=",">
        (
            #{item.spplyId}, #{item.spplyMgrId}, #{item.spplyMgrNm}, #{item.rprstTelNo},
            #{item.rprstEmal}, #{item.position}, #{item.assgnTask}, now(),
            #{userId}, now(), #{userId}
        )
        </foreach>
    </insert>

    <update id="deleteSpplyMgrInfo">
        delete from  spply_mgr_mgnt
        where  spply_id = #{spplyId}
        and    spply_mgr_id   in
        <foreach collection="gridDeleteData" item="item" separator="," open="(" close=")">
            #{item.spplyMgrId}
        </foreach>
    </update>

    <select id="selectSpplyInfo" resultType="com.oqpo.api.entity.stinfo.SpplyEntity">
        select  /* selectSpplyInfo */
				spply_id, spply_nm, st_cd, fn_code_nm('ST_CD',st_cd) as st_nm,  corp_type, fn_code_nm('CORP_TYPE',corp_type) AS corp_type_nm, fn_get_regno(1,bsns_reg_no) as bsns_reg_no,
                    fn_get_regno(2,corp_reg_no) as corp_reg_no, owner_nm, ctgr_bsns, bsns_cndt, corp_dvsn, fn_code_nm('CORP_DVSN',corp_dvsn) as corp_dvsn_nm,
                    mbl_no, mgr_nm, area_cd, fn_code_nm('AREA_CD',area_cd) as area_nm,
                    dept_nm, mgr_emal, fn_get_telno(mgr_tel_no) as mgr_tel_no, fn_get_telno(mgr_mbl_no) as mgr_mbl_no, del_yn,
                    lst_login_dttm,  zip_no, addr1, addr2, brand_id, fn_brand_nm(brand_id) as brand_nm,
                    bnk_cd, fn_code_nm('BNK_CD',bnk_cd) as bnk_nm, acct_no, owac_nm, bbook_copy_file_no, bsns_reg_file_no,
                    date_format(sys_reg_dttm,'%Y.%m.%d %H:%i:%s') as sys_reg_dttm, sys_reg_id, date_format(sys_chg_dttm,'%Y.%m.%d %H:%i:%s') as sys_chg_dttm, sys_chg_id,
                    lat_x, long_y
        from   spply_base_info
        where  brand_id = #{brandId}
        and    spply_id = #{spplyId}
    </select>

    <select id="selectSpplyMgrList" resultType="com.oqpo.api.entity.stinfo.SpplyMgrEntity">
        select   /*  selectSpplyMgrList */
                spply_id, spply_mgr_id, spply_mgr_nm, fn_get_telno(rprst_tel_no) as rprst_tel_no, rprst_emal, position, assgn_task, fn_code_nm('ASSGN_TASK',assgn_task) as assgn_task_nm
               ,spply_mgr_nm
        from   spply_mgr_mgnt
        where  spply_id = #{spplyId}
        order by spply_mgr_id asc
    </select>

    <select id="selectSpplyInitPw" resultType="String">
        select bsns_reg_no
         from spply_base_info
         where  brand_id = #{brandId}
        and    spply_id = #{spplyId}
    </select>

    <select id="selectMaxSpplyMgrId" resultType="int">
        select   /*  selectMaxSpplyMgrId */
                nvl(max(spply_mgr_id),0) + 1 as spply_mgr_id
        from   spply_mgr_mgnt
        where  spply_id = #{spplyId}
    </select>

    <update id="updateCloseSpply" >
        update  spply_base_info
        set  sys_chg_dttm	= now()
            ,sys_chg_id	= #{userId}
            ,st_cd = 'S001'
        where 1=1
        <if test="brandId != null and brandId != ''">
            and   brand_id = #{brandId}
        </if>
        <if test="afflShopId != null and afflShopId != ''">
            and   brand_id in (select brand_id from affl_shop_base_info where affl_shop_id = #{afflShopId})
        </if>
    </update>

</mapper>
