<?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.QuarantineReportMapper">
    
    <resultMap type="QuarantineReport" id="QuarantineReportResult">
        <result property="id"    column="id"    />
        <result property="sheepId"    column="sheep_id"    />
        <result property="sheepNo" column="sheep_no"/>
        <result property="variety" column="variety"/>
        <result property="sheepType" column="sheep_type"/>
        <result property="gender" column="gender"/>
        <result property="monthAge" column="month_age"/>
        <result property="parity" column="parity"/>
        <result property="breed" column="breed"/>
        <result property="datetime"    column="datetime"    />
        <result property="quarItem"    column="quar_item"    />
        <result property="itemName" column="item_name"/>
        <result property="sample"    column="sample"    />
        <result property="sampleType"    column="sample_type"    />
        <result property="sampler"    column="sampler"    />
        <result property="quarOfficer"    column="quar_officer"    />
        <result property="result"    column="result"    />
        <result property="status"    column="status"    />
        <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="selectQuarantineReportVo">
        select sqr.id, sheep_type,sqr.variety,sqr.gender,sqr.parity,sqr.breed,sqr.month_age,sheep_id, datetime, quar_item, sample_type, sampler, quar_officer, result, status,
               sqr.update_by, sqr.update_time, sqr.create_by, sqr.create_time,
        sqi.name as item_name,
        sqs.name as sample,
        sf.bs_manage_tags sheep_no
        from sw_quarantine_report sqr
        left join sw_quarantine_items sqi on sqr.quar_item = sqi.id
        left join sw_quarantine_sample sqs on sqr.sample_type = sqs.id
        left join sheep_file sf on sqr.sheep_id = sf.id
    </sql>

    <select id="selectQuarantineReportList" parameterType="QuarantineReport" resultMap="QuarantineReportResult">
        <include refid="selectQuarantineReportVo"/>
        <where>
            <!-- 根据ids数组查询 -->
            <if test="ids != null and ids.length > 0">
                AND sqr.id IN
                <foreach collection="ids" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="sheepId != null "> and sheep_id = #{sheepId}</if>
            <if test="sheepNo != null and sheepNo != ''">and bs.manage_tags like concat('%',#{sheepNo},'%')</if>
            <if test="params.beginDatetime != null and params.beginDatetime != '' and params.endDatetime != null and params.endDatetime != ''"> and datetime between #{params.beginDatetime} and #{params.endDatetime}</if>
            <if test="sheepNos != null and sheepNos.length > 0">
                AND (
                <foreach collection="sheepNos" item="item" separator=" OR " open="" close="">
                    bs.manage_tags LIKE CONCAT('%', #{item}, '%')
                </foreach>
                )
            </if>
            <!-- 全部羊多耳号查询（母羊或公羊匹配即可） -->
            <if test="allEarNumbers != null and allEarNumbers.size() > 0">
                AND (
                bs.manage_tags IN
                <foreach collection="allEarNumbers" item="earNumber" open="(" separator="," close=")">
                    #{earNumber}
                </foreach>

                )
            </if>
            <if test="quarItem != null "> and quar_item = #{quarItem}</if>
            <if test="sampleType != null "> and sample_type = #{sampleType}</if>
            <if test="sampler != null  and sampler != ''"> and sampler like concat('%',#{sampler},'%') </if>
            <if test="quarOfficer != null  and quarOfficer != ''"> and quar_officer like concat('%',#{quarOfficer},'%')</if>
            <if test="result != null "> and result = #{result}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="sheepType != null and sheepType!= ''"> and sqr.sheep_type=#{sheepType}</if>
            <if test="isDelete != null and isDelete != '' ">and sf.is_delete = #{isDelete}</if>
            ${params.dataScope}
        </where>
        ORDER BY datetime  DESC
        <choose>
            <when test="orderByColumn == 'monthAge'">, month_age ${isAsc}</when>
        </choose>
    </select>
    
    <select id="selectQuarantineReportById" parameterType="Long" resultMap="QuarantineReportResult">
        select sqr.id, sheep_type,sheep_id, datetime, quar_item, sample_type, sampler, quar_officer, result, status,
               sqr.comment
               ,sqr.update_by, sqr.update_time, sqr.create_by, sqr.create_time,
               sqi.name as item_name,
               sqs.name as sample,
               sf.bs_manage_tags sheep_no
        from sw_quarantine_report sqr
                 left join sw_quarantine_items sqi on sqr.quar_item = sqi.id
                 left join sw_quarantine_sample sqs on sqr.sample_type = sqs.id
                 left join sheep_file sf on sqr.sheep_id = sf.id
        where  sqr.id = #{id}
        ORDER BY datetime  DESC
    </select>

    <insert id="insertQuarantineReport" parameterType="java.util.List">
        INSERT INTO sw_quarantine_report
        (
        sheep_id,
         variety,
        sheep_type,
        month_age,
        parity,
        breed,
        gender,
        datetime,
        quar_item,
        sample_type,
        sampler,
        quar_officer,
        result,
        status,
        update_by,
        update_time,
        create_by,
        create_time,
        user_id,
        dept_id
        )
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.sheepId},
             #{item.variety},
            #{item.sheepType},
            #{item.monthAge},
            #{item.parity},
            #{item.breed},
            #{item.gender},
            #{item.datetime},
            #{item.quarItem},
            #{item.sampleType},
            #{item.sampler},
            #{item.quarOfficer},
            #{item.result},
            #{item.status},
            #{item.updateBy},
            #{item.updateTime},
            #{item.createBy},
            #{item.createTime},
             #{item.userId},
             #{item.deptId}
            )
        </foreach>
    </insert>

    <update id="updateQuarantineReport" parameterType="QuarantineReport">
        update sw_quarantine_report
        <trim prefix="SET" suffixOverrides=",">
            <if test="sheepId != null">sheep_id = #{sheepId},</if>
            <if test="datetime != null">datetime = #{datetime},</if>
            <if test="quarItem != null">quar_item = #{quarItem},</if>
            <if test="sampleType != null">sample_type = #{sampleType},</if>
            <if test="sampler != null">sampler = #{sampler},</if>
            <if test="quarOfficer != null">quar_officer = #{quarOfficer},</if>
            <if test="result != null">result = #{result},</if>
            <if test="status != null">status = #{status},</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="deleteQuarantineReportById" parameterType="Long">
        delete from sw_quarantine_report where id = #{id}
    </delete>

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