<?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.UserMngMapper">
    <select id="selectUserGridList" resultType="com.oqpo.api.entity.oper.UserMngEntity">
        select /* selectUserGridList */
                affl_shop_id, brand_id, fn_affl_nm(affl_shop_id) as affl_shop_nm, fn_brand_nm(brand_id) as brand_nm,
                user_id, user_nm, fn_get_telno(tel_no) as tel_no, email, user_stat_cd, fn_code_nm('USER_STAT_CD',user_stat_cd) as user_stat_nm,
                date_format(user_stat_dt, '%Y.%m.%d') as user_stat_dt, date_format(add_dt, '%Y.%m.%d') as add_dt,
                date_format(last_login_dt, '%Y.%m.%d %H:%i:%s') as last_login_dt, auth_tp_cd, fn_code_nm('AUTH_TP_CD',auth_tp_cd) as auth_tp_nm,
                store_id, fn_store_nm(brand_id, store_id) as store_nm, spply_id, fn_spply_nm(spply_id) as spply_nm
        from  np_user_info
        where 1 = 1
        <if test="fromDt != null and fromDt != '' and toDt != null and toDt != '' ">
            and   add_dt between DATE_FORMAT(CONCAT(FN_DATE_TO(#{fromDt}), '000000'), '%Y%m%d%H%i%S') and DATE_FORMAT(CONCAT(FN_DATE_TO(#{toDt}), '235959'), '%Y%m%d%H%i%S')
        </if>
        <if test="sUserNm != null and sUserNm != ''">
            and    (user_nm like concat('%',#{sUserNm},'%') or user_id like concat('%',#{sUserNm},'%'))
        </if>
        <if test="sUserStatCd != null and sUserStatCd != ''">
            and    user_stat_cd = #{sUserStatCd}
        </if>
        <if test="sAuthTpCd != null and sAuthTpCd != ''">
            and    auth_tp_cd = #{sAuthTpCd}
        </if>
        <if test="afflShopId != null and afflShopId != ''">
            and    affl_shop_id = #{afflShopId}
        </if>
        <if test="brandId != null and brandId != ''">
            and    brand_id = #{brandId}
        </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 user_id asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by user_id desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by user_id asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>


    <select id="selectUserGridListCnt" resultType="int">
        select count(1) as cnt /* selectUserGridListCnt */
        from np_user_info
        where 1 = 1
        <if test="fromDt != null and fromDt != '' and toDt != null and toDt != '' ">
            and   add_dt between DATE_FORMAT(CONCAT(FN_DATE_TO(#{fromDt}), '000000'), '%Y%m%d%H%i%S') and DATE_FORMAT(CONCAT(FN_DATE_TO(#{toDt}), '235959'), '%Y%m%d%H%i%S')
        </if>
        <if test="sUserNm != null and sUserNm != ''">
            and    (user_nm like concat('%',#{sUserNm},'%') or user_id like concat('%',#{sUserNm},'%'))
        </if>
        <if test="sUserStatCd != null and sUserStatCd != ''">
            and    user_stat_cd = #{sUserStatCd}
        </if>
        <if test="sAuthTpCd != null and sAuthTpCd != ''">
            and    auth_tp_cd = #{sAuthTpCd}
        </if>
        <if test="afflShopId != null and afflShopId != ''">
            and    affl_shop_id = #{afflShopId}
        </if>
        <if test="brandId != null and brandId != ''">
            and    brand_id = #{brandId}
        </if>
    </select>

    <select id="selectUserIdDupYn" resultType="String">
        select case when count(*) <![CDATA[ > ]]> 0 then 'Y' else 'N' end as cnt /* selectUserIdDupYn */
        from np_user_info
        where user_id = #{userId}
        or left(upper(#{userId}),3) in ('AFS', 'BRD','STR' )
    </select>

    <insert id="insertUserInfo" >
        insert into np_user_info  /* insertUserInfo */
        (user_id, user_nm, tel_no, email, user_pw,  user_stat_cd, user_stat_dt, auth_tp_cd, affl_shop_id, brand_id, store_id, spply_id, add_dt, add_id, chg_dt, chg_id)
        values
        (upper(#{entity.userId}), #{entity.userNm}, #{entity.telNo}, #{entity.email}, #{entity.userPw}
         ,#{entity.userStatCd}, now(), nvl(#{entity.authTpCd},'60'),  #{entity.afflShopId}, #{entity.brandId}, #{entity.storeId}, #{entity.spplyId}, now(), #{userId}, now(), #{userId})
    </insert>

    <update id="updateUserInfo">
        update np_user_info
        set    chg_dt           = now()
              ,chg_id           = #{userId}
              ,tel_no           = #{entity.telNo}
              ,email            = #{entity.email}
              ,user_stat_cd     = #{entity.userStatCd}
              ,user_nm          = #{entity.userNm}
              ,user_stat_dt     = case when user_stat_cd = #{entity.userStatCd} then user_stat_dt else now() end
        where user_id = #{entity.userId}
    </update>

    <insert id="insertUserAuth" >
        insert into np_user_auth /** insertUserAuth */
        (user_id, auth_no, add_dt, add_id)
        values
        <foreach collection="gridInsertData" item="item" separator=",">
            (upper(#{item.userId}),#{item.authNo}, now(), #{userId})
        </foreach>
    </insert>

    <delete id="deleteUserAuth">
        delete from np_user_auth /** deleteUserAuth */
        where  user_id  =  #{userId}
        and    auth_no   in
        <foreach collection="gridDeleteData" item="item" separator="," open="(" close=")">
            #{item.authNo}
        </foreach>
    </delete>

    <update id="updateInitUserPw">
        update np_user_info /* updateInitUserPw */
        set    user_pw = #{userPw}
        where user_id = #{userId}
    </update>

    <select id="selectUserInfo" resultType="com.oqpo.api.entity.oper.UserMngEntity">
            select user_id, user_nm, fn_get_telno(tel_no) as tel_no, email, user_pw, user_stat_cd, fn_code_nm('USER_STAT_CD', user_stat_cd) as user_stat_nm ,
                   date_format(user_stat_dt, '%Y.%m.%d') as user_stat_dt, date_format(last_login_dt, '%Y.%m.%d %H:%i:%s') as last_login_dt,
                   date_format(add_dt, '%Y.%m.%d') as add_dt, brand_id, affl_shop_id, fn_affl_nm(affl_shop_id) as affl_shop_nm, fn_brand_nm(brand_id) as brand_nm,
                   auth_tp_cd, fn_code_nm('AUTH_TP_CD',auth_tp_cd) as auth_tp_nm,  spply_id, fn_spply_nm(spply_id) as spply_nm, store_id, fn_store_nm(brand_id, store_id) as store_nm
            from  np_user_info
            where user_id = #{userId}
    </select>

    <select id="selectUserAuthGridList" resultType="com.oqpo.api.entity.oper.UserAuthInfoEntity">
            select uauth.auth_no
                  , auth.auth_nm as  'authInfo.auth_nm', auth.use_yn as  'authInfo.use_yn'
                  , fn_code_nm('USE_YN', auth.use_yn) as 'authInfo.use_yn_nm', uauth.add_dt
            from  np_user_auth uauth
            left join np_auth_info auth
            on uauth.auth_no = auth.auth_no
            where  uauth.user_id = #{userId}
        <choose>
            <when test="gridRequest.sidx != null and gridRequest.sidx != ''">
                <if test="gridRequest.sidx == 'VIEW_NUM'.toString()">
                    <if test="gridRequest.sord == 'asc'.toString()">
                        order by uauth.auth_no asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by uauth.auth_no desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by uauth.auth_no asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectUserAuthGridListCnt" resultType="int">
        select count(*)
        from  np_user_auth uauth
        where  uauth.user_id = #{userId}
    </select>

    <select id="selectPopUserGridList" resultType="com.oqpo.api.entity.oper.UserMngEntity">
        select /* selectPopUserGridList */
        user_id, user_nm,  fn_code_nm('USER_STAT_CD',user_stat_cd) as user_stat_nm
        from  np_user_info
        where 1 = 1
        <if test="sAfflShopId != null and sAfflShopId != ''">
            and    affl_shop_id = #{sAfflShopId}
        </if>
        <if test="sBrandId != null and sBrandId != ''">
            and    brand_id = #{sBrandId}
        </if>
        <if test="sUserId != null and sUserId != ''">
            and    user_id like concat('%',#{sUserId},'%')
        </if>
        <if test="sUserNm != null and sUserNm != ''">
            and    user_nm like concat('%',#{sUserNm},'%')
        </if>
        <if test="sUserStatCd != null and sUserStatCd != ''">
            and    user_stat_cd = #{sUserStatCd}
        </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 user_nm asc
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        order by user_nm desc
                    </if>
                </if>
            </when>
            <otherwise>
                order by user_nm asc
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectPopUserGridCnt" resultType="Integer">
        select /* selectPopUserGridCnt */
            count(*)
        from  np_user_info
        where 1 = 1
        <if test="sAfflShopId != null and sAfflShopId != ''">
            and    affl_shop_id = #{sAfflShopId}
        </if>
        <if test="sBrandId != null and sBrandId != ''">
            and    brand_id = #{sBrandId}
        </if>
        <if test="sUserId != null and sUserId != ''">
            and    user_id like concat('%',#{sUserId},'%')
        </if>
        <if test="sUserNm != null and sUserNm != ''">
            and    user_nm like concat('%',#{sUserNm},'%')
        </if>
        <if test="sUserStatCd != null and sUserStatCd != ''">
            and    user_stat_cd = #{sUserStatCd}
        </if>
    </select>


    <insert id="insertFrcompUserAuth" >
        insert into np_user_auth /** insertFrcompUserAuth */
        (user_id, auth_no, add_dt, add_id)
        select #{userId}, auth_no, now(), #{addId}
        from np_auth_info
        where auth_tp_cd = #{authTpCd}
        and use_yn = 'Y'
    </insert>

    <delete id="deleteFrcompUserAuth" >
        delete from np_user_auth /** deleteFrcompUserAuth */
        where user_id = #{userId}
    </delete>


    <update id="updateLoginDt">
        update np_user_info /* updateLoginDt */
        set    first_login_dt = case when first_login_dt is null then DATE_FORMAT(now(),'%Y%m%d%H%s%i') else first_login_dt end
              ,last_login_dt = DATE_FORMAT(now(),'%Y%m%d%H%s%i')
        where user_id = #{userId}
    </update>


    <update id="updatePrivateUserInfo">
        update np_user_info
        set    chg_dt           = now()
              ,chg_id           = #{userId}
              ,tel_no           = #{entity.telNo}
              ,email            = #{entity.email}
        where user_id = #{entity.userId}
    </update>

    <insert id="insertUserTypeAuth" >
        insert into np_user_auth
        (user_id, auth_no, add_dt, add_id)
        select #{newId}, auth_no, now(), #{userId}
        from np_auth_info
        where auth_tp_cd = #{authTpCd}
        and   use_yn = 'Y'
    </insert>

    <insert id="insertBrandUserTypeAuth" >
        insert into np_user_auth
        (user_id, auth_no, add_dt, add_id)
        select #{newId}, auth_no, now(), #{userId}
        from np_auth_info
        where auth_tp_cd = #{authTpCd}
        <if test="shmtColProcType == '10'">
        and   auth_no not like concat('%','_SL')
        </if>
        <if test="shmtColProcType == '20'">
        and   auth_no not in ('BRAND_MPOMNG' ,  'BRAND_MRTNMNG' ,  'BRAND_POMNG' ,  'BRAND_RTNMNG')
        </if>
        and   use_yn = 'Y'
    </insert>


    <update id="updateCloseUser" >
        update  np_user_info
        set  chg_dt	= now()
            ,chg_id	= #{userId}
            ,user_stat_cd = '90'
        where 1=1
        <if test="brandId != null and brandId != ''">
            and   brand_id = #{brandId}
        </if>
        <if test="afflShopId != null and afflShopId != ''">
            and   affl_shop_id = #{afflShopId}
        </if>
    </update>

    <select id="selectGroupUserInfo" resultType="com.oqpo.api.entity.oper.UserMngEntity">
        select uinfo.user_id, uinfo.user_nm, uinfo.brand_id, uinfo.affl_shop_id, fn_brand_nm(uinfo.brand_id) as brand_nm, fn_affl_nm(uinfo.affl_shop_id) as affl_nm
                , uinfo.auth_tp_cd, fn_get_telno(uinfo.tel_no)  as tel_no
                , uinfo.store_id, fn_store_nm(uinfo.brand_id, uinfo.store_id) as store_nm
                , uinfo.spply_id, fn_spply_nm(uinfo.spply_id) as spply_nm
        from np_user_info uinfo
        where user_id = #{userId}
    </select>

    <select id="selectInitPwData" resultType="String">
        select /* selectInitPwData */ bsns_reg_no
        <if test="authTpCd == '30'">
            from affl_shop_base_info
            where affl_shop_id = #{serData}
        </if>
        <if test="authTpCd == '40'">
            from brand_base_info
            where brand_id = #{serData}
        </if>
        <if test="authTpCd == '50'">
            from store_base_info
            where store_id = #{serData}
        </if>
        <if test="authTpCd == '60'">
            from  spply_base_info
            where spply_id = #{serData}
        </if>
    </select>

    <update id="updateAppLoginDt">
        update np_user_info /* updateLoginDt */
        set    first_login_dt = case when first_login_dt is null then DATE_FORMAT(now(),'%Y%m%d%H%s%i') else first_login_dt end
              ,last_login_dt = DATE_FORMAT(now(),'%Y%m%d%H%s%i')
              ,devi_id = #{deviId}
              ,devi_tp_cd = #{deviTpCd}
              ,devi_os_ver = #{deviOsVer}
              ,app_reg = #{appReg}
              ,app_ver = #{appVer}
        where user_id = #{userId}
    </update>

    <insert id="insertUserDevi">
        insert into np_user_devi /** Merge updateAuthDtl */
        (user_id, devi_id, devi_tp_cd, devi_os_ver, app_reg, app_ver, chg_dt,chg_id, add_dt, add_id)
        values
            ( #{userId},#{deviId},#{deviTpCd},#{deviOsVer},#{appReg}, #{appVer}, now(), #{userId}, now(), #{userId})
        on duplicate key
        update
        chg_dt       = now()
        , chg_id       = #{userId}
        , devi_id       = VALUES(devi_id)
        , devi_tp_cd        = VALUES(devi_tp_cd)
        , devi_os_ver        = VALUES(devi_os_ver)
        , app_reg        = VALUES(app_reg)
        , app_ver        = VALUES(app_ver)        ;
    </insert>

    <select id="selectWishListCnt" resultType="int">
        select count(*)
        from np_user_auth
        where user_id in (
                                select user_id
                                from   np_user_info
                                where  brand_id = #{brandId}
                                and    auth_tp_cd = '50'
                                )
        and   auth_no = 'SHOP_WISH'
    </select>

    <insert id="insertUserAuthWishList" >
        insert into np_user_auth
        (user_id, auth_no, add_dt, add_id)
        select user_id ,'SHOP_WISH',  now(), #{userId}
        from   np_user_info
        where  brand_id = #{brandId}
        and    auth_tp_cd = '50'
    </insert>

    <insert id="deleteUserAuthWishList" >
        delete from np_user_auth
        where user_id in (
                                select user_id
                                from   np_user_info
                                where  brand_id = #{brandId}
                                and    auth_tp_cd = '50'
                                )
        and   auth_no = 'SHOP_WISH'
    </insert>

    <select id="selectPushTargetList" resultType="com.oqpo.api.entity.oper.MemberEntity">
        select  user_nm, app_reg
        from np_user_info
        where brand_id = #{brandId}
        and   auth_tp_cd = '40'
        and   app_reg is not null
        and   app_reg <![CDATA[ <> ]]> ''
    </select>

</mapper>
