<?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.produce.other.mapper.ScCastrateMapper">

    <resultMap type="com.zhyc.module.produce.other.domain.ScCastrate" id="ScCastrateResult">
        <result property="id" column="id"/>
        <result property="sheepId" column="sheep_id"/>
        <result property="manageTags" column="manageTags"/>
        <result property="eventType" column="event_type"/>
        <result property="sheepfold" column="sheepfold"/>
        <result property="sheepfoldName" column="sheepfoldName"/>
        <result property="varietyId" column="variety_id"/>
        <result property="varietyName" column="varietyName"/>
        <result property="comment" column="comment"/>
        <result property="technician" column="technician"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="eventDate" column="eventDate"/>
    </resultMap>

    <sql id="selectScCastrateVo">
        select sc.id,
               sc.sheep_id,
               bs.manage_tags    as manageTags,
               '去势' as event_type,
               bs.sheepfold_id   as sheepfold,
               sf.sheepfold_name as sheepfoldName,
               bs.variety_id     as varietyId,
               bv.variety        as varietyName,
               sc.comment,
               sc.technician,
               sc.create_by,
               sc.create_time,
               sc.event_date     as eventDate
        from sc_castrate sc
                 left join bas_sheep bs on sc.sheep_id = bs.id
                 left join da_sheepfold sf on bs.sheepfold_id = sf.id
                 left join bas_sheep_variety bv on bs.variety_id = bv.id
    </sql>

    <select id="selectScCastrateList" parameterType="ScCastrate" resultMap="ScCastrateResult">
        <include refid="selectScCastrateVo"/>
        <where>
            <if test="manageTags != null and manageTags != ''">
                and bs.manage_tags like concat('%', #{manageTags}, '%')
            </if>
            <if test="sheepfold != null ">and bs.sheepfold_id = #{sheepfold}</if>
            <if test="varietyId != null">and bs.variety_id = #{varietyId}</if>
            <if test="technician != null  and technician != ''">and sc.technician like concat('%', #{technician}, '%')
            </if>
            <if test="params.beginCreateTime != null and params.endCreateTime != null">
                and sc.create_time between #{params.beginCreateTime} and #{params.endCreateTime}
            </if>
            <if test="params.beginEventDate != null and params.endEventDate != null">
                and sc.event_date between #{params.beginEventDate} and #{params.endEventDate}
            </if>
        </where>
        ORDER BY sc.create_time DESC
    </select>

    <select id="selectScCastrateById" parameterType="Long" resultMap="ScCastrateResult">
        <include refid="selectScCastrateVo"/>
        where sc.id = #{id}
    </select>

    <insert id="insertScCastrate" parameterType="ScCastrate" useGeneratedKeys="true" keyProperty="id">
        insert into sc_castrate
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sheepId != null">sheep_id,</if>
            <if test="sheepfold != null">sheepfold,</if>
            <if test="varietyId != null">variety_id,</if>
            <if test="comment != null">comment,</if>
            <if test="technician != null">technician,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="eventDate != null">event_date,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sheepId != null">#{sheepId},</if>
            <if test="sheepfold != null">#{sheepfold},</if>
            <if test="varietyId != null">#{varietyId},</if>
            <if test="comment != null">#{comment},</if>
            <if test="technician != null">#{technician},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="eventDate != null">#{eventDate},</if>
        </trim>
    </insert>

    <update id="updateScCastrate" parameterType="ScCastrate">
        update sc_castrate
        <trim prefix="SET" suffixOverrides=",">
            <if test="sheepId != null">sheep_id = #{sheepId},</if>
            <if test="sheepfold != null">sheepfold = #{sheepfold},</if>
            <if test="varietyId != null">variety_id = #{varietyId},</if>
            <if test="comment != null">comment = #{comment},</if>
            <if test="technician != null">technician = #{technician},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteScCastrateById" parameterType="Long">
        delete
        from sc_castrate
        where id = #{id}
    </delete>

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