<?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.DewormMapper">
    
    <resultMap type="Deworm" id="DewormResult">
        <result property="id"    column="id"    />
        <result property="sheepId"    column="sheep_id"    />
        <result property="sheepNo" column="sheep_no"/>
        <result property="usageId"    column="usage_id"    />
        <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="technical"    column="technical"    />
        <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="selectDewormVo">
        select s.id, sheep_id, usage_id, variety, sheep_type, s.gender, month_age, s.parity,breed, datetime, technical, s.comment, s.update_by, s.update_time, s.create_by, s.create_time,
        bs.manage_tags sheep_no
        from sw_deworm s
        left join bas_sheep bs on s.sheep_id = bs.id
    </sql>

    <select id="selectDewormList" parameterType="Deworm" resultMap="DewormResult">
        <include refid="selectDewormVo"/>
        <where>
            <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="technical != null and technical != ''">and technical = #{technical}</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>
        </choose>
    </select>
    
    <select id="selectDewormById" parameterType="Long" resultMap="DewormResult">
        <include refid="selectDewormVo"/>
        where s.id = #{id}
    </select>

    <insert id="insertDeworm" parameterType="java.util.List">
        insert into sw_deworm
        (sheep_id, usage_id, variety, sheep_type, gender, month_age,
        parity, breed,datetime, technical, comment,
        update_by, update_time, create_by, create_time,user_id, dept_id)
        values
        <foreach collection="list" item="d" separator=",">
            (#{d.sheepId}, #{d.usageId}, #{d.variety}, #{d.sheepType},
            #{d.gender}, #{d.monthAge}, #{d.parity},#{d.breed}, #{d.datetime},
            #{d.technical}, #{d.comment},
            #{d.updateBy}, #{d.updateTime}, #{d.createBy}, #{d.createTime},#{d.userId},#{d.deptId})
        </foreach>
    </insert>

    <update id="updateDeworm" parameterType="Deworm">
        update sw_deworm
        <trim prefix="SET" suffixOverrides=",">
            <if test="sheepId != null">sheep_id = #{sheepId},</if>
            <if test="usageId != null">usage_id = #{usageId},</if>
            <if test="variety != null">variety = #{variety},</if>
            <if test="sheepType != null">sheep_type = #{sheepType},</if>
            <if test="gender != null">gender = #{gender},</if>
            <if test="monthAge != null">month_age = #{monthAge},</if>
            <if test="parity != null">parity = #{parity},</if>
            <if test="datetime != null">datetime = #{datetime},</if>
            <if test="technical != null">technical = #{technical},</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="deleteDewormById" parameterType="Long">
        delete from sw_deworm where id = #{id}
    </delete>

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