<?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.zhyc.module.biosafety.mapper.DisinfectMapper">
    
    <resultMap type="Disinfect" id="DisinfectResult">
        <result property="id"    column="id"    />
        <result property="sheepfoldId"    column="sheepfold_id"    />
        <result property="sheepfoldName" column="sheepfold_name"/>
        <result property="datetime"    column="datetime"    />
        <result property="technician"    column="technician"    />
        <result property="way"    column="way"    />
        <result property="usageId"    column="usage_id"    />
        <result property="ratio"    column="ratio"    />
        <result property="comment"    column="comment"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectDisinfectVo">
        select sd.id, sheepfold_id, datetime, technician, way, usage_id, ratio, sd.comment, update_by, update_time, create_by, create_time,
           ds.sheepfold_name
        from sw_disinfect sd
        left join da_sheepfold ds on sd.sheepfold_id = ds.id
    </sql>

    <select id="selectDisinfectList" parameterType="Disinfect" resultMap="DisinfectResult">
        SELECT sd.id,
        sd.sheepfold_id,
        sd.datetime,
        sd.technician,
        sd.way,
        sd.usage_id,
        sd.ratio,
        sd.comment,
        sd.update_by,
        sd.update_time,
        sd.create_by,
        sd.create_time,
        ds.sheepfold_name
        FROM   sw_disinfect sd
        LEFT JOIN da_sheepfold ds ON ds.id = sd.sheepfold_id
        WHERE 1 = 1
        <if test="sheepfoldId != null">            AND sd.sheepfold_id = #{sheepfoldId}</if>
        <if test="datetime != null">               AND sd.datetime     = #{datetime}</if>
        <if test="technician != null and technician != ''"> AND sd.technician = #{technician}</if>
        <if test="way != null and way != ''">      AND sd.way          = #{way}</if>
        <if test="usageId != null">                AND sd.usage_id     = #{usageId}</if>
        <if test="ratio != null and ratio != ''">  AND sd.ratio        = #{ratio}</if>
        <if test="comment != null and comment != ''"> AND sd.comment   = #{comment}</if>
        <!-- 子表过滤条件：仅保留满足药品名称的记录 -->
        <if test="mediName != null and mediName != ''">
            AND EXISTS (
            SELECT 1
            FROM sw_medicine_usage_details mud
            JOIN sw_medicine sm ON sm.id = mud.medi_id
            WHERE mud.medi_usage = sd.usage_id
            AND sm.name   like  concat('%',#{mediName},'%')
            )
        </if>
        ORDER BY datetime  DESC

    </select>
    
    <select id="selectDisinfectById" parameterType="Long" resultMap="DisinfectResult">
        <include refid="selectDisinfectVo"/>
        where sd.id = #{id}
    </select>

    <insert id="insertDisinfect" parameterType="Disinfect" useGeneratedKeys="true" keyProperty="id">
        insert into sw_disinfect
        (sheepfold_id, datetime, technician, way, usage_id, ratio, comment, update_by, update_time, create_by, create_time)
        values
        <foreach collection="list" item="d" separator=",">
            (#{d.sheepfoldId}, #{d.datetime}, #{d.technician}, #{d.way},
            #{d.usageId}, #{d.ratio}, #{d.comment},
            #{d.updateBy}, #{d.updateTime}, #{d.createBy}, #{d.createTime})
        </foreach>
    </insert>

    <update id="updateDisinfect" parameterType="Disinfect">
        update sw_disinfect
        <trim prefix="SET" suffixOverrides=",">
            <if test="sheepfoldId != null">sheepfold_id = #{sheepfoldId},</if>
            <if test="datetime != null">datetime = #{datetime},</if>
            <if test="technician != null">technician = #{technician},</if>
            <if test="way != null">way = #{way},</if>
            <if test="usageId != null">usage_id = #{usageId},</if>
            <if test="ratio != null">ratio = #{ratio},</if>
            <if test="comment != null">comment = #{comment},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDisinfectById" parameterType="Long">
        delete from sw_disinfect where id = #{id}
    </delete>

    <delete id="deleteDisinfectByIds" parameterType="String">
        delete from sw_disinfect where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>