<?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.manage_sheep.mapper.ScChangeVarietyMapper">

    <resultMap type="com.zhyc.module.produce.manage_sheep.domain.ScChangeVariety" id="ScChangeVarietyResult">
        <result property="id" column="id"/>
        <result property="sheepId" column="sheep_id"/>
        <result property="manageTags" column="manage_tags"/>
        <result property="sheepfoldName" column="sheepfold_name"/>
        <result property="eventType" column="event_type"/>
        <result property="varietyOld" column="variety_old"/>
        <result property="varietyNew" column="variety_new"/>
        <result property="comment" column="comment"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="technician" column="technician"/>
        <result property="eventDate" column="event_date"/>
    </resultMap>

    <sql id="selectScChangeVarietyVo">
        select scv.id,
               scv.sheep_id,
               bs.manage_tags    as manage_tags,
               sf.sheepfold_name as sheepfold_name,
               '改品种'          as event_type,
               scv.variety_old,
               scv.variety_new,
               scv.comment,
               scv.create_by,
               scv.create_time,
               scv.technician,
               scv.event_date
        from sc_change_variety scv
                 left join bas_sheep bs on scv.sheep_id = bs.id
                 left join da_sheepfold sf on bs.sheepfold_id = sf.id
    </sql>

    <select id="selectScChangeVarietyList" resultMap="ScChangeVarietyResult">
        <include refid="selectScChangeVarietyVo"/>
        <where>
            <!-- 多耳号 + 每只模糊 -->
            <if test="manageTagsList != null and manageTagsList.size() > 0">
                <foreach collection="manageTagsList" item="tag" separator="or" open="and (" close=")">
                    bs.manage_tags like concat('%', #{tag}, '%')
                </foreach>
            </if>

            <!-- 其余统一加 sc. 前缀 -->
            <if test="sc.sheepId != null">and scv.sheep_id = #{sc.sheepId}</if>
            <if test="sc.sheepfoldId != null">and bs.sheepfold_id = #{sc.sheepfoldId}</if>
            <if test="sc.varietyOld != null and sc.varietyOld != ''">and scv.variety_old like concat('%', #{sc.varietyOld}, '%')</if>
            <if test="sc.varietyNew != null and sc.varietyNew != ''">and scv.variety_new like concat('%', #{sc.varietyNew}, '%')</if>
            <if test="sc.params != null and sc.params.beginCreateTime != null and sc.params.beginCreateTime != '' and sc.params.endCreateTime != null and sc.params.endCreateTime != ''">
                and scv.create_time between #{sc.params.beginCreateTime} and #{sc.params.endCreateTime}
            </if>
            <if test="sc.params != null and sc.params.beginEventDate != null and sc.params.beginEventDate != '' and sc.params.endEventDate != null and sc.params.endEventDate != ''">
                and scv.event_date between #{sc.params.beginEventDate} and #{sc.params.endEventDate}
            </if>
        </where>
        ORDER BY scv.create_time DESC
    </select>

    <select id="selectScChangeVarietyById" parameterType="Integer" resultMap="ScChangeVarietyResult">
        <include refid="selectScChangeVarietyVo"/>
        where scv.id = #{id}
    </select>

    <insert id="insertScChangeVariety" parameterType="ScChangeVariety" useGeneratedKeys="true" keyProperty="id">
        insert into sc_change_variety
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sheepId != null">sheep_id,</if>
            <if test="varietyOld != null and varietyOld != ''">variety_old,</if>
            <if test="varietyNew != null and varietyNew != ''">variety_new,</if>
            <if test="comment != null">comment,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="technician != null and technician != ''">technician,</if> <!-- 新增 -->
            <if test="eventDate != null">event_date,</if>                         <!-- 新增 -->
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sheepId != null">#{sheepId},</if>
            <if test="varietyOld != null and varietyOld != ''">#{varietyOld},</if>
            <if test="varietyNew != null and varietyNew != ''">#{varietyNew},</if>
            <if test="comment != null">#{comment},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="technician != null and technician != ''">#{technician},</if> <!-- 新增 -->
            <if test="eventDate != null">#{eventDate},</if>                         <!-- 新增 -->
        </trim>
    </insert>

    <update id="updateScChangeVariety" parameterType="ScChangeVariety">
        update sc_change_variety
        <trim prefix="SET" suffixOverrides=",">
            <if test="sheepId != null">sheep_id = #{sheepId},</if>
            <if test="varietyOld != null and varietyOld != ''">variety_old = #{varietyOld},</if>
            <if test="varietyNew != null and varietyNew != ''">variety_new = #{varietyNew},</if>
            <if test="comment != null">comment = #{comment},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteScChangeVarietyById" parameterType="Integer">
        delete
        from sc_change_variety
        where id = #{id}
    </delete>

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

    <select id="searchEarNumbers" resultType="java.lang.String">
        SELECT DISTINCT bs.manage_tags
        FROM bas_sheep bs
        WHERE bs.manage_tags LIKE CONCAT('%', #{query}, '%')
          AND bs.is_delete = 0
        ORDER BY bs.manage_tags
            LIMIT 50
    </select>
</mapper>