<?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.TreatmentMapper">
    
    <resultMap type="Treatment" id="TreatmentResult">
        <result property="id"    column="id"    />
        <result property="diagId"    column="diag_id"    />
        <result property="sheepId"    column="sheep_id"    />
        <result property="sheepNo"  column="manage_tags"/>
        <result property="variety"    column="variety"    />
        <result property="sheepType"    column="sheep_type"    />
        <result property="monthAge"    column="month_age"    />
        <result property="gender"    column="gender"    />
        <result property="parity"    column="parity"    />
        <result property="breed"    column="breed"    />
        <result property="lactDay"    column="lact_day"    />
        <result property="gestDay"    column="gest_day"    />
        <result property="datetime"    column="datetime"    />
        <result property="diseaseId"    column="disease_id"    />
        <result property="diseaseName" column="disease_name"/>
        <result property="diseasePid"    column="disease_pid"    />
        <result property="diseasePName" column="disease_pname"/>
        <result property="veterinary"    column="veterinary"    />
        <result property="usageId"    column="usage_id"    />
        <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="selectTreatmentVo">
        select t.id, diag_id, sheep_id, variety, sheep_type, month_age, t.gender, t.parity, breed, lact_day, gest_day, datetime, disease_id, disease_pid, veterinary, usage_id,status, t.comment, t.update_by, t.update_time, t.create_by, t.create_time,
        bs.manage_tags,
        sd.name disease_name,
        sd2.name disease_pname
        from sw_treatment t
        left join bas_sheep bs on t.sheep_id = bs.id
        left join sw_disease sd on t.disease_id = sd.id
        left join sw_disease sd2 on t.disease_pid = sd2.id
    </sql>

    <select id="selectTreatmentList" parameterType="Treatment" resultMap="TreatmentResult">
        select t.id, diag_id, sheep_id, variety, sheep_type, month_age, t.gender, t.parity, breed, lact_day, gest_day, datetime, disease_id, disease_pid, veterinary, usage_id,status, t.comment, t.update_by, t.update_time, t.create_by, t.create_time,
        bs.manage_tags,
        sd.name disease_name,
        sd2.name disease_pname
        from sw_treatment t
        left join bas_sheep bs on t.sheep_id = bs.id
        left join sw_disease sd on t.disease_id = sd.id
        left join sw_disease sd2 on t.disease_pid = sd2.id
        <where>
            <!-- 根据ids数组查询 -->
            <if test="ids != null and ids.length > 0">
                AND t.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="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="params.beginDatetime != null and params.beginDatetime != '' and params.endDatetime != null and params.endDatetime != ''"> and datetime between #{params.beginDatetime} and #{params.endDatetime}</if>
            <if test="diseaseId != null "> and disease_id = #{diseaseId}</if>
            <if test="status != null and status !=''"> and status = #{status}</if>
            <if test="veterinary != null  and veterinary != ''"> and veterinary = #{veterinary}</if>
            <if test="isDelete != null and isDelete != '' ">and bs.is_delete = #{isDelete}</if>
            ${params.dataScope}
        </where>
        ORDER BY datetime  DESC
        <choose>
            <when test="orderByColumn == 'monthAge'">, month_age ${isAsc}</when>
            <when test="orderByColumn == 'parity'">, parity ${isAsc}</when>
            <when test="orderByColumn == 'lactDay'">, lact_day ${isAsc}</when>
            <when test="orderByColumn == 'gestDay'">, gest_day ${isAsc}</when>

        </choose>
    </select>
    
    <select id="selectTreatmentById" parameterType="Long" resultMap="TreatmentResult">
        <include refid="selectTreatmentVo"/>
        where t.id = #{id}
    </select>
    <select id="selectTreatmentStatus" resultMap="TreatmentResult">
        select * from sw_treatment where sheep_id = #{sheepId} and  status in (0, 1)
    </select>

    <insert id="insertTreatment" parameterType="Treatment" useGeneratedKeys="true" keyProperty="id">
        insert into sw_treatment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="diagId != null">diag_id,</if>
            <if test="sheepId != null">sheep_id,</if>
            <if test="variety != null">variety,</if>
            <if test="sheepType != null">sheep_type,</if>
            <if test="monthAge != null">month_age,</if>
            <if test="gender != null">gender,</if>
            <if test="parity != null">parity,</if>
            <if test="breed != null">breed,</if>
            <if test="lactDay != null">lact_day,</if>
            <if test="gestDay != null">gest_day,</if>
            <if test="datetime != null">datetime,</if>
            <if test="diseaseId != null">disease_id,</if>
            <if test="diseasePid != null">disease_pid,</if>
            <if test="veterinary != null">veterinary,</if>
            <if test="usageId != null">usage_id,</if>
            <if test="status != null">status,</if>
            <if test="comment != null">comment,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="userId != null">#{user_id},</if>
            <if test="deptId != null">#{dept_id},</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="diagId != null">#{diagId},</if>
            <if test="sheepId != null">#{sheepId},</if>
            <if test="variety != null">#{variety},</if>
            <if test="sheepType != null">#{sheepType},</if>
            <if test="monthAge != null">#{monthAge},</if>
            <if test="gender != null">#{gender},</if>
            <if test="parity != null">#{parity},</if>
            <if test="breed != null">#{breed},</if>
            <if test="lactDay != null">#{lactDay},</if>
            <if test="gestDay != null">#{gestDay},</if>
            <if test="datetime != null">#{datetime},</if>
            <if test="diseaseId != null">#{diseaseId},</if>
            <if test="diseasePid != null">#{diseasePid},</if>
            <if test="veterinary != null">#{veterinary},</if>
            <if test="usageId != null">#{usageId},</if>
            <if test="status != null">#{status},</if>
            <if test="comment != null">#{comment},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="userId != null">#{userId},</if>
            <if test="deptId != null">#{deptId},</if>
         </trim>
    </insert>

    <insert id="insertTreatmentList" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        insert into sw_treatment
        (diag_id, sheep_id, variety, sheep_type, month_age, gender,
        parity, breed, lact_day, gest_day, datetime,
        disease_id, disease_pid, veterinary, usage_id,status ,
        comment, update_by, update_time, create_by, create_time, user_id, dept_id)
        values
        <foreach collection="list" item="t" separator=",">
            (#{t.diagId},     #{t.sheepId},   #{t.variety},   #{t.sheepType},
            #{t.monthAge},   #{t.gender},    #{t.parity},    #{t.breed},
            #{t.lactDay},    #{t.gestDay},   #{t.datetime},  #{t.diseaseId},
            #{t.diseasePid}, #{t.veterinary},#{t.usageId}, #{t.status},  #{t.comment},
            #{t.updateBy},   #{t.updateTime},#{t.createBy},  #{t.createTime},#{t.userId},#{t.deptId})
        </foreach>
    </insert>

    <update id="updateTreatment" parameterType="Treatment">
        update sw_treatment
        <trim prefix="SET" suffixOverrides=",">
            <if test="diagId != null">diag_id = #{diagId},</if>
            <if test="sheepId != null">sheep_id = #{sheepId},</if>
            <if test="variety != null">variety = #{variety},</if>
            <if test="sheepType != null">sheep_type = #{sheepType},</if>
            <if test="monthAge != null">month_age = #{monthAge},</if>
            <if test="gender != null">gender = #{gender},</if>
            <if test="parity != null">parity = #{parity},</if>
            <if test="breed != null">breed = #{breed},</if>
            <if test="lactDay != null">lact_day = #{lactDay},</if>
            <if test="gestDay != null">gest_day = #{gestDay},</if>
            <if test="datetime != null">datetime = #{datetime},</if>
            <if test="diseaseId != null">disease_id = #{diseaseId},</if>
            <if test="diseasePid != null">disease_pid = #{diseasePid},</if>
            <if test="veterinary != null">veterinary = #{veterinary},</if>
            <if test="usageId != null">usage_id = #{usageId},</if>
            <if test="status != null">status = #{status},</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="deleteTreatmentById" parameterType="Long">
        delete from sw_treatment where id = #{id}
    </delete>

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