<?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.hzs.system.config.mapper.BdNoticeConfigMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.system.config.BdNoticeConfig">
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_ID" property="pkId"/>
        <result column="TOP_CATEGORY" property="topCategory"/>
        <result column="SECOND_CATEGORY" property="secondCategory"/>
        <result column="CONTENT" property="content"/>
        <result column="FUNCTION_TYPE" property="functionType"/>
        <result column="WEBSITE_TYPE" property="websiteType"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        CREATION_TIME,
        MODIFIED_TIME,
        DEL_FLAG,
        PK_COUNTRY,
        PK_CREATOR,
        PK_MODIFIED,
        PK_ID, TOP_CATEGORY, SECOND_CATEGORY, CONTENT, FUNCTION_TYPE, WEBSITE_TYPE
    </sql>

    <select id="getBdNoticeConfigList" resultMap="BaseResultMap">
        select *
        from bd_notice_config c
        where c.del_flag = 0
        and c.pk_country = #{pkCountry}
        <if test="functionType != null">
            and c.function_type = #{functionType}
        </if>
        <if test="websiteType != null">
            and c.website_type = #{websiteType}
        </if>
    </select>

    <select id="getBdNoticeConfigListByCategroy" resultType="String">
        select listagg(content,',') within GROUP (order by content) as content
        from BD_NOTICE_CONFIG c
        where c.del_flag = 0
        and c.pk_country = #{pkCountry}
        <if test="topCategory != null">
            and c.top_category = #{topCategory}
        </if>
        <if test="secondCategoryList != null and secondCategoryList.size >0">
            and c.second_category in
            <foreach collection="secondCategoryList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        and c.website_type= #{websiteType}
        and c.function_type = #{functionType}
    </select>

    <select id="getNoticeRoleAndUser" resultType="map">
        select c.second_category roles, listagg(content,',') within GROUP (order by content) users
        from BD_NOTICE_CONFIG c
        where c.del_flag = 0
        and c.pk_country = #{pkCountry}
        <if test="secondCategoryList != null and secondCategoryList.size >0">
            and c.second_category in
            <foreach collection="secondCategoryList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        and c.website_type= 2
        and c.function_type = #{functionType}
        group by c.second_category
    </select>



    <update id="updateNoticeConfig">
        update bd_notice_config c set c.del_flag = 1,PK_MODIFIED = #{pkMember},MODIFIED_TIME=sysdate
        where c.pk_country = #{pkCountry}
        <if test="functionType != null">
            and c.function_type = #{functionType}
        </if>
        <if test="websiteType != null">
            and c.website_type = #{websiteType}
        </if>
    </update>


    <update id="updateNoticeConfigByContent">
        update bd_notice_config c set c.del_flag = 1,PK_MODIFIED = #{pkMember},MODIFIED_TIME=sysdate
        where c.del_flag = 0
        <if test="pkCountry != null">
            and c.pk_country = #{pkCountry}
        </if>
        <if test="roleIds != null and roleIds.size >0">
            and c.SECOND_CATEGORY not in
            <foreach collection="roleIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        and c.CONTENT = #{userId}
    </update>
</mapper>
