<?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.DiagnosisMapper">
    
    <resultMap type="Diagnosis" id="DiagnosisResult">
        <result property="id"    column="id"    />
        <result property="treatId"    column="treat_id"    />
        <result property="sheepId"    column="sheep_id"    />
        <result property="sheepNo" column="sheep_no"/>
        <result property="datetime"    column="datetime"    />
        <result property="sheepType"    column="sheep_type"    />
        <result property="gender"    column="gender"    />
        <result property="monthAge" column="month_age"/>
        <result property="parity"    column="parity"    />
        <result property="diseasePName" column="disease_pname"/>
        <result property="diseaseName" column="disease_name"/>
        <result property="diseasePid"    column="disease_pid"    />
        <result property="diseaseId"    column="disease_id"    />
        <result property="result"    column="result"    />
        <result property="begindate"    column="begindate"    />
        <result property="enddate"    column="enddate"    />
        <result property="treatDay"    column="treat_day"    />
        <result property="sheepfoldId"    column="sheepfold_id"    />
        <result property="sheepfold" column="sheepfold_name"/>
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectDiagnosisVo">
        select sd.id, treat_id, sheep_id, sd.datetime, sheep_type, sd.gender,sd.month_age,sd.parity, disease_pid, disease_id,
        result, begindate, enddate, treat_day, sd.sheepfold_id, sd.create_by, sd.create_time,
        sf.bs_manage_tags sheep_no,sf.sheepfold_name ,
        s2.name disease_pname,s1.name disease_name
        from sw_diagnosis sd
        left join sheep_file sf on sf.id = sd.sheep_id
        left join sw_disease s1 on sd.disease_id = s1.id
        left join sw_disease s2 on sd.disease_pid = s2.id

    </sql>

    <select id="selectDiagnosisList" parameterType="Diagnosis" resultMap="DiagnosisResult">
        <include refid="selectDiagnosisVo"/>
        <where>  
            <if test="sheepId != null "> and sd.sheep_id = #{sheepId}</if>
            <if test="sheepNo != null"> and sf.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="diseasePid != null "> and disease_pid = #{diseasePid}</if>
            <if test="diseaseId != null "> and disease_id = #{diseaseId}</if>
            <if test="result != null "> and result = #{result}</if>
            <if test="treatDay != null "> and treat_day = #{treatDay}</if>
            <if test="sheepfoldId != null "> and sd.sheepfold_id = #{sheepfoldId}</if>
            <if test="isDelete != null and isDelete != '' ">and sf.is_delete = #{isDelete}</if>
            ${params.dataScope}
        </where>
        ORDER BY datetime  DESC
        <choose>
            <when test="orderByColumn == 'begindate'">, begindate ${isAsc}</when>
            <when test="orderByColumn == 'enddate'">, enddate ${isAsc}</when>
            <when test="orderByColumn == 'parity'">, parity ${isAsc}</when>
        </choose>
    </select>
    
    <select id="selectDiagnosisById" parameterType="Long" resultMap="DiagnosisResult">
        <include refid="selectDiagnosisVo"/>
        where sd.id = #{id}
    </select>

    <insert id="insertDiagnosis" parameterType="Diagnosis" useGeneratedKeys="true" keyProperty="id">
        insert into sw_diagnosis
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="treatId != null">treat_id,</if>
            <if test="sheepId != null">sheep_id,</if>
            <if test="datetime != null">datetime,</if>
            <if test="sheepType != null">sheep_type,</if>
            <if test="gender != null">gender,</if>
            <if test="parity != null">parity,</if>
            <if test="diseasePid != null">disease_pid,</if>
            <if test="diseaseId != null">disease_id,</if>
            <if test="result != null">result,</if>
            <if test="begindate != null">begindate,</if>
            <if test="enddate != null">enddate,</if>
            <if test="treatDay != null">treat_day,</if>
            <if test="sheepfoldId != null">sheepfold_id,</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="treatId != null">#{treatId},</if>
            <if test="sheepId != null">#{sheepId},</if>
            <if test="datetime != null">#{datetime},</if>
            <if test="sheepType != null">#{sheepType},</if>
            <if test="gender != null">#{gender},</if>
            <if test="parity != null">#{parity},</if>
            <if test="diseasePid != null">#{diseasePid},</if>
            <if test="diseaseId != null">#{diseaseId},</if>
            <if test="result != null">#{result},</if>
            <if test="begindate != null">#{begindate},</if>
            <if test="enddate != null">#{enddate},</if>
            <if test="treatDay != null">#{treatDay},</if>
            <if test="sheepfoldId != null">#{sheepfoldId},</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>

    <update id="updateDiagnosis" parameterType="Diagnosis">
        update sw_diagnosis
        <trim prefix="SET" suffixOverrides=",">
            <if test="treatId != null">treat_id = #{treatId},</if>
            <if test="sheepId != null">sheep_id = #{sheepId},</if>
            <if test="datetime != null">datetime = #{datetime},</if>
            <if test="sheepType != null">sheep_type = #{sheepType},</if>
            <if test="gender != null">gender = #{gender},</if>
            <if test="parity != null">parity = #{parity},</if>
            <if test="diseasePid != null">disease_pid = #{diseasePid},</if>
            <if test="diseaseId != null">disease_id = #{diseaseId},</if>
            <if test="result != null">result = #{result},</if>
            <if test="begindate != null">begindate = #{begindate},</if>
            <if test="enddate != null">enddate = #{enddate},</if>
            <if test="treatDay != null">treat_day = #{treatDay},</if>
            <if test="sheepfoldId != null">sheepfold_id = #{sheepfoldId},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDiagnosisById" parameterType="Long">
        delete from sw_diagnosis where id = #{id}
    </delete>

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