<?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.BizLabDetectionMapper">

    <resultMap type="com.ruoyi.web.modules.epidemic.domain.BizLabDetection" id="BizLabDetectionResult">
        <id     property="id"                    column="id"/>
        <result property="testDate"              column="test_date"/>
        <result property="quarantineStationId"   column="quarantine_station_id"/>
        <result property="quarantineStationName" column="quarantine_station_name"/>
        <result property="sampleSource"          column="sample_source"/>
        <result property="testQuantity"          column="test_quantity"/>
        <result property="positiveCount"         column="positive_count"/>
        <result property="detectionRate"         column="detection_rate"/>
        <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 d.id, d.test_date, d.quarantine_station_id, s.station_name as quarantine_station_name,
               d.sample_source, d.test_quantity, d.positive_count, d.detection_rate,
               d.create_by, d.create_time, d.update_by, d.update_time, d.remark
        from biz_lab_detection d
        left join biz_quarantine_station s on s.id = d.quarantine_station_id and s.del_flag = '0'
    </sql>

    <select id="selectBizLabDetectionById" parameterType="long" resultMap="BizLabDetectionResult">
        <include refid="selectVo"/>
        where d.id = #{id}
    </select>

    <select id="selectBizLabDetectionList" parameterType="com.ruoyi.web.modules.epidemic.domain.BizLabDetection" resultMap="BizLabDetectionResult">
        <include refid="selectVo"/>
        <where>
            <if test="sampleSource != null">
                and d.sample_source = #{sampleSource}
            </if>
            <if test="quarantineStationId != null">
                and d.quarantine_station_id = #{quarantineStationId}
            </if>
        </where>
        order by d.test_date desc, d.create_time desc, d.id desc
    </select>

    <insert id="insertBizLabDetection" parameterType="com.ruoyi.web.modules.epidemic.domain.BizLabDetection" useGeneratedKeys="true" keyProperty="id">
        insert into biz_lab_detection (
            test_date, quarantine_station_id, sample_source, test_quantity, positive_count, detection_rate,
            create_by, create_time, remark
        ) values (
            #{testDate}, #{quarantineStationId}, #{sampleSource}, #{testQuantity}, #{positiveCount}, #{detectionRate},
            #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <update id="updateBizLabDetection" parameterType="com.ruoyi.web.modules.epidemic.domain.BizLabDetection">
        update biz_lab_detection set
            test_date = #{testDate},
            quarantine_station_id = #{quarantineStationId},
            sample_source = #{sampleSource},
            test_quantity = #{testQuantity},
            positive_count = #{positiveCount},
            detection_rate = #{detectionRate},
            update_by = #{updateBy},
            update_time = sysdate(),
            remark = #{remark}
        where id = #{id}
    </update>

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

    <select id="countByQuarantineStationId" parameterType="long" resultType="int">
        select count(1) from biz_lab_detection where quarantine_station_id = #{quarantineStationId}
    </select>

    <select id="selectByTestYear" resultMap="BizLabDetectionResult">
        <include refid="selectVo"/>
        where d.test_date &gt;= concat(#{statYear}, '-01-01')
          and d.test_date &lt;= concat(#{statYear}, '-12-31')
        order by d.test_date asc, d.id asc
    </select>

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