<?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.AfAdviseFeedbackMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap"
               type="cn.com.countrygarden.bi.main.model.advise.AfAdviseFeedback">
        <id column="id" property="id"/>
        <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="is_private" property="isPrivate"/>
        <result column="state" property="state"/>
        <result column="report_time" property="reportTime"/>
        <result column="is_timeout" property="isTimeout"/>
        <result column="schedule" property="schedule"/>
        <result column="distribution_user_code"
                property="distributionUserCode"/>
        <result column="distribution_user_name"
                property="distributionUserName"/>
        <result column="distribution_time" property="distributionTime"/>
        <result column="distribution_advise_type_id" property="distributionAdviseTypeId"/>
        <result column="comm_id" property="commId"/>
        <result column="comm_name" property="commName"/>
        <result column="organ_name" property="organName"/>
        <result column="organ_code" property="organCode"/>
        <result column="range_type" property="rangeType"/>
        <result column="enable" property="enable"/>
        <result column="create_time" property="createTime"/>
        <result column="create_by" property="createBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="version" property="version"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="branch_type_code" property="branchTypeCode"/>
    </resultMap>


    <select id="selectAdviseFeedbackList"
            resultType="cn.com.countrygarden.bi.main.dto.advise.AfAdviseFeedbackListVO">
        SELECT DISTINCT
        aaf.id id,
        aaf.is_private isPrivate,
        aaf.state state,
        aaf.advise_type_id adviseTypeId,
        aat. NAME adviseTypeName,
        aaf.title title,
        aaf.branch_type_code branchTypeCode,
        aaf.comm_name commName,
        aaf.organ_name organName,
        aaf.create_by feedBackCreateBy,
        aaf.report_time,
        aaf.create_time,
        aan.state noticeState
        FROM
        af_advise_feedback aaf
        LEFT JOIN af_advise_type aat ON aat.id = aaf.advise_type_id
        LEFT JOIN af_advise_process aap ON aap.advise_feedback_id = aaf.id
        LEFT JOIN af_advise_notice aan on aan.advise_feedback_id=aaf.id
        WHERE
        aaf. ENABLE = 1 and aap.enable=1
        <!--如果建言分支类型为空会不进入此sql，branchTypeCode是必须的条件，所以此处不再非空判断了-->
        and aaf.branch_type_code=#{adviseFeedbackParam.branchTypeCode}

        <if test="adviseFeedbackParam.adviseTypeId!=null">
            and
            aaf.advise_type_id=#{adviseFeedbackParam.adviseTypeId}
        </if>
        <if test="adviseFeedbackParam.state!=null">
            and aaf.state=#{adviseFeedbackParam.state}
        </if>
        <if
                test="adviseFeedbackParam.reportStartTime!=null and adviseFeedbackParam.reportEndTime!=null">
            and aaf.report_time between #{adviseFeedbackParam.reportStartTime} and
            DATE_ADD(#{adviseFeedbackParam.reportEndTime},INTERVAL 1 DAY)

        </if>
        <if test="adviseFeedbackParam.isTimeout!=null">
            and aaf.is_timeout=#{adviseFeedbackParam.isTimeout}
        </if>
        <if
                test="adviseFeedbackParam.isPrivate!=null">
            and aaf.is_private =#{adviseFeedbackParam.isPrivate}
        </if>
        <if test="adviseFeedbackParam.organCode!=null and adviseFeedbackParam.organCode!=''">
            and
            aaf.organ_code=#{adviseFeedbackParam.organCode}
        </if>

        <choose>

            <when test="adviseFeedbackParam.role!=null and adviseFeedbackParam.role==true ">
                and (aap.state=1
                <choose>
                    <!--超级管理员，可以查看全部建言数据-->
                    <when test=" null!=adviseFeedbackParam.superAdminRole or adviseFeedbackParam.superAdminRole==true">
                    </when>
                    <otherwise>
                        and aaf.is_private=0
                        <trim prefix="and" prefixOverrides="or">
                            <if
                                    test=" null!=adviseFeedbackParam.regionAdminRole and adviseFeedbackParam.regionAdminRole==true and null!=adviseFeedbackParam.regionList">
                                or ( aaf.organ_code in
                                <foreach collection="adviseFeedbackParam.regionList" item="organ" index="index" open="("
                                         separator="," close=")">
                                    #{organ.organCode}
                                </foreach>
                                and ( aap.process_type  <![CDATA[ =]]>1 or aap.process_type  <![CDATA[ =]]>0 )
                                )
                            </if>
                            <if
                                    test=" null!=adviseFeedbackParam.headquartersAdminRole and adviseFeedbackParam.headquartersAdminRole==true">

                                or (aap.process_type  <![CDATA[ =]]>2 or aap.process_type  <![CDATA[ =]]>1)
                            </if>

                            <if
                                    test=" null!=adviseFeedbackParam.typeAdminRole and adviseFeedbackParam.typeAdminRole==true">

                                or (0  <![CDATA[ <]]>(SELECT
                                count(aar.id)
                                FROM
                                af_advise_responsibler aar
                                WHERE aar.`enable`=1 and aar.advise_type_id = aap.distribution_advise_type_id and
                                aar.user_code=#{adviseFeedbackParam.userCode} )
                                and (aap.process_type  <![CDATA[ =]]>3 or aap.process_type  <![CDATA[ =]]>2))
                            </if>
                        </trim>
                        or aaf.create_by = #{adviseFeedbackParam.userCode}
                        OR (select count(1) from af_advise_process aap2 where aap2.enable=1 and aap2.advise_feedback_id
                        = aaf.id and aap2.user_code = #{adviseFeedbackParam.userCode})>0

                    </otherwise>
                </choose>
                )
            </when>
            <!--非建言管理员角色，只能查看自己的建言或参与过的建言-->
            <otherwise>
                and (aaf.create_by = #{adviseFeedbackParam.userCode}
                OR (select count(1) from af_advise_process aap2 where aap2.advise_feedback_id = aaf.id
                and aap2.user_code = #{adviseFeedbackParam.userCode})>0
                )
            </otherwise>
        </choose>
        ORDER BY aaf.create_time desc

    </select>

    <select id="selectAdviseFeedbackOne"
            resultType="cn.com.countrygarden.bi.main.dto.advise.AfAdviseFeedbackOneVO">

        SELECT
        aaf.id id,
        aaf.state state,
        aaf.advise_type_id adviseTypeId,
        aat.NAME adviseTypeName,
        aaf.title
        title,
        aaf.content content,
        aaf.organ_name organName,
        aaf.report_time reportTime,
        aaf.is_private isPrivate,
        aaf.create_by createBy,
        aaf.branch_type_code branchTypeCode
        FROM
        af_advise_feedback aaf
        LEFT
        JOIN
        af_advise_type aat ON aat.id
        = aaf.advise_type_id
        WHERE
        aaf.enable=1
        <if test="adviseFeedback!=null">
            <if test="adviseFeedback.id!=null">
                and aaf.id =#{adviseFeedback.id}
            </if>
            <if test="adviseFeedback.state!=null">
                and aaf.state =#{adviseFeedback.state}
            </if>
        </if>
    </select>
    <update id="batchUpdateAfAdviseFeedback">

        UPDATE af_advise_feedback SET is_timeout =1 WHERE id in

        <foreach collection="adviseFeedbackIds" index="index" item="id" open="(" separator="," close=")">

            #{id}

        </foreach>


    </update>
</mapper>