<?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.ruoyi.web.modules.epidemic.mapper.BizEpidemicReportMapper">

    <resultMap type="com.ruoyi.web.modules.epidemic.domain.BizEpidemicReport" id="BizEpidemicReportResult">
        <id     property="id"                      column="id"/>
        <result property="reportDate"              column="report_date"/>
        <result property="transmissionRoute"     column="transmission_route"/>
        <result property="affectedLocationTrack"   column="affected_location_track"/>
        <result property="epidemicType"            column="epidemic_type"/>
        <result property="epidemicName"            column="epidemic_name"/>
        <result property="riskLevel"               column="risk_level"/>
        <result property="releaseDate"             column="release_date"/>
        <result property="relieveStatus"           column="relieve_status"/>
        <result property="createBy"                column="create_by"/>
        <result property="createTime"              column="create_time"/>
        <result property="updateBy"                column="update_by"/>
        <result property="updateTime"              column="update_time"/>
        <result property="remark"                  column="remark"/>
    </resultMap>

    <sql id="selectVo">
        select id, report_date, transmission_route, affected_location_track, epidemic_type, epidemic_name,
               risk_level, release_date,
               case when release_date is null then 0 else 1 end as relieve_status,
               create_by, create_time, update_by, update_time, remark
        from biz_epidemic_report
    </sql>

    <select id="selectBizEpidemicReportById" parameterType="long" resultMap="BizEpidemicReportResult">
        <include refid="selectVo"/>
        where id = #{id}
    </select>

    <select id="selectBizEpidemicReportList" parameterType="com.ruoyi.web.modules.epidemic.domain.BizEpidemicReport" resultMap="BizEpidemicReportResult">
        <include refid="selectVo"/>
        <where>
            <if test="transmissionRoute != null">
                and transmission_route = #{transmissionRoute}
            </if>
            <if test="riskLevel != null">
                and risk_level = #{riskLevel}
            </if>
            <if test="relieveStatus != null and relieveStatus == 0">
                and release_date is null
            </if>
            <if test="relieveStatus != null and relieveStatus == 1">
                and release_date is not null
            </if>
        </where>
        order by report_date desc, create_time desc, id desc
    </select>

    <insert id="insertBizEpidemicReport" parameterType="com.ruoyi.web.modules.epidemic.domain.BizEpidemicReport" useGeneratedKeys="true" keyProperty="id">
        insert into biz_epidemic_report (
            report_date, transmission_route, affected_location_track, epidemic_type, epidemic_name,
            risk_level, release_date, create_by, create_time, remark
        ) values (
            #{reportDate}, #{transmissionRoute}, #{affectedLocationTrack}, #{epidemicType}, #{epidemicName},
            #{riskLevel}, #{releaseDate}, #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="updateBizEpidemicReport" parameterType="com.ruoyi.web.modules.epidemic.domain.BizEpidemicReport">
        update biz_epidemic_report set
            report_date = #{reportDate},
            transmission_route = #{transmissionRoute},
            affected_location_track = #{affectedLocationTrack},
            epidemic_type = #{epidemicType},
            epidemic_name = #{epidemicName},
            risk_level = #{riskLevel},
            update_by = #{updateBy},
            update_time = sysdate(),
            remark = #{remark}
        where id = #{id}
    </update>

    <update id="updateReleaseDate" parameterType="com.ruoyi.web.modules.epidemic.domain.BizEpidemicReport">
        update biz_epidemic_report set
            release_date = #{releaseDate},
            update_by = #{updateBy},
            update_time = sysdate()
        where id = #{id}
    </update>

    <delete id="deleteBizEpidemicReportByIds" parameterType="long">
        delete from biz_epidemic_report where id in
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectByReportYear" resultMap="BizEpidemicReportResult">
        <include refid="selectVo"/>
        where report_date &gt;= concat(#{statYear}, '-01-01')
          and report_date &lt;= concat(#{statYear}, '-12-31')
        order by report_date desc, create_time desc, id desc
    </select>

    <select id="selectRelievedByReleaseYear" resultMap="BizEpidemicReportResult">
        <include refid="selectVo"/>
        where release_date is not null
          and release_date &gt;= concat(#{statYear}, '-01-01')
          and release_date &lt;= concat(#{statYear}, '-12-31')
        order by release_date desc, id desc
    </select>

    <select id="selectActiveControlByReportYear" resultMap="BizEpidemicReportResult">
        <include refid="selectVo"/>
        where report_date &gt;= concat(#{statYear}, '-01-01')
          and report_date &lt;= concat(#{statYear}, '-12-31')
          and release_date is null
        order by report_date desc, id desc
    </select>

    <select id="selectDistinctReportYears" resultType="string">
        select distinct cast(year(report_date) as char) as y
        from biz_epidemic_report
        where report_date is not null
        order by y desc
    </select>
</mapper>
