<?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="cn.com.countrygarden.bi.main.dao.advise.AfAdviseNoticeMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap"
               type="cn.com.countrygarden.bi.main.model.advise.AfAdviseNotice">
        <id column="id" property="id"/>
        <result column="advise_feedback_id" property="adviseFeedbackId"/>
        <result column="state" property="state"/>
        <result column="advise_type_id" property="adviseTypeId"/>
        <result column="advise_type_name" property="adviseTypeName"/>
        <result column="title" property="title"/>
        <result column="content" property="content"/>
        <result column="report_time" property="reportTime"/>
        <result column="organ_name" property="organName"/>
        <result column="organ_code" property="organCode"/>
        <result column="report_user_code" property="reportUserCode"/>
        <result column="publish_time" property="publishTime"/>
        <result column="publish_user_name" property="publishUserName"/>
        <result column="visible_type" property="visibleType"/>
        <result column="publish_user_code" property="publishUserCode"/>
        <result column="report_user_name" property="reportUserName"/>
        <result column="branch_type_code" property="branchTypeCode"/>
    </resultMap>

    <select id="selectAdviseNoticeList" resultMap="BaseResultMap">

        SELECT
        aan.id id,
        aan.advise_feedback_id,
        aan.advise_type_name,
        aan.title,
        aan.report_user_name,
        aan.report_time,
        aan.organ_name,
        aan.publish_user_name,
        aan.publish_time,
        aan.visible_type visibleType,
        aan.branch_type_code branchTypeCode
        FROM
        af_advise_notice aan
        where aan.state=1
        <if test="adviseFeedbackParam!=null">
            <if test="adviseFeedbackParam.branchTypeCode!=null">
                and aan.branch_type_code=#{adviseFeedbackParam.branchTypeCode}

            </if>
            <if test="adviseFeedbackParam.adviseTypeId!=null">
                and aan.advise_type_id=#{adviseFeedbackParam.adviseTypeId}
            </if>
            <if
                    test="adviseFeedbackParam.reportStartTime!=null and adviseFeedbackParam.reportEndTime!=null">
                and aan.report_time between #{adviseFeedbackParam.reportStartTime}
                and
                date_add(#{adviseFeedbackParam.reportEndTime},interval 1 day)

            </if>
            <if
                    test="adviseFeedbackParam.publishStartTime!=null and adviseFeedbackParam.publishEndTime!=null">
                and aan.publish_time between #{adviseFeedbackParam.publishStartTime}
                and
                date_add(#{adviseFeedbackParam.publishEndTime},interval 1 day)

            </if>
            <!--无论有没有角色，visible_type=0时为全部人都可见，自己发起的建言与自己发布的公告都是可见的-->
            and ( visible_type=0 or report_user_code=#{adviseFeedbackParam.userCode} or
            publish_user_code=#{adviseFeedbackParam.userCode}


            <!--是否是管理員角色-->
            <if test="adviseFeedbackParam.role!=null or adviseFeedbackParam.role==true">
            <choose>
                <!-- 当只是区域角色（如有更高的角色外层做了处理，不会进入条件） -->
                <when test="null!=adviseFeedbackParam.regionAdminRole and  adviseFeedbackParam.regionAdminRole==true ">
                    or (
                    <!-- 全部区域可见-->
                    aan.visible_type =2 or
                    <!-- 本区域可见-->
                    ( aan.visible_type =1 and
                    aan.organ_code in
                    <foreach collection="adviseFeedbackParam.regionList" item="organ" index="index" open="("
                             separator="," close=")">
                        #{organ.organCode}
                    </foreach>
                    )
                    )
                </when>
                <!-- 进入otherwise的只会是区域负责人以上角色 -->
                <otherwise>
                   or  1=1
                </otherwise>
            </choose>
            </if>
            )
        </if>
    </select>

</mapper>