<?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.ScChangeEarMapper">

    <resultMap type="com.zhyc.module.produce.manage_sheep.domain.ScChangeEar" id="ScChangeEarResult">
        <result property="id" column="sce_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="earType" column="ear_type"/>
        <result property="newTag" column="newTag"/>
        <result property="oldTag" column="oldTag"/>
        <result property="comment" column="comment"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="eventDate" column="event_date"/>
        <result property="technician" column="technician"/>
    </resultMap>

    <sql id="selectScChangeEarVo">
        select sce.id            as sce_id,
               sce.sheep_id,
               bs.manage_tags    as manage_tags,
               sf.sheepfold_name as sheepfold_name,
               bv.variety        as varietyName,
               case
                   when sce.ear_type = 0 then '改电子耳号'
                   when sce.ear_type = 1 then '改管理耳号'
                   else ''
                   end           as event_type,
               sce.ear_type,
               sce.newTag,
               sce.oldTag        as oldTag,
               sce.comment,
               sce.create_by,
               sce.create_time,
               sce.event_date,
               sce.technician
        from sc_change_ear sce
                 LEFT JOIN bas_sheep bs ON sce.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="selectScChangeEarList" resultMap="ScChangeEarResult">
        <include refid="selectScChangeEarVo"/>
        <where>
            <if test="sc.sheepId != null">and sce.sheep_id = #{sc.sheepId}</if>
            <if test="manageTagsList != null and manageTagsList.size() > 0">
                <foreach collection="manageTagsList" item="tag" separator="or" open="and (" close=")">
                    (bs.manage_tags like concat('%', #{tag}, '%')
                    or bs.electronic_tags like concat('%', #{tag}, '%'))
                </foreach>
            </if>
            <if test="sc.technician != null and sc.technician != ''">and sce.technician like concat('%', #{sc.technician}, '%')</if>
            <if test="sc.earType != null">and sce.ear_type = #{sc.earType}</if>
            <if test="sc.newTag != null and sc.newTag != ''">and sce.newTag like concat('%', #{sc.newTag}, '%')</if>
            <if test="sc.oldTag != null and sc.oldTag != ''">and sce.oldTag like concat('%', #{sc.oldTag}, '%')</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 sce.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 sce.event_date between #{sc.params.beginEventDate} and #{sc.params.endEventDate}
            </if>
            <if test="sc.inGroup != null">
                <choose>
                    <when test="sc.inGroup == 1">and bs.is_delete = 0</when>
                    <when test="sc.inGroup == 2">and bs.is_delete = 1</when>
                </choose>
            </if>
            ${sc.params.dataScope}
        </where>
        ORDER BY sce.create_time DESC
    </select>

    <select id="selectScChangeEarById" parameterType="Integer" resultMap="ScChangeEarResult">
        <include refid="selectScChangeEarVo"/>
        where sce.id = #{id}
    </select>

    <insert id="insertScChangeEar" parameterType="ScChangeEar" useGeneratedKeys="true" keyProperty="id">
        insert into sc_change_ear
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sheepId != null">sheep_id,</if>
            <if test="earType != null">ear_type,</if>
            <if test="newTag != null and newTag != ''">newTag,</if>
            <if test="oldTag != null and oldTag != ''">oldTag,</if>
            <if test="comment != null">comment,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="eventDate != null">event_date,</if>
            <if test="technician != null and technician != ''">technician,</if>
            <if test="userId != null">user_id,</if>
            <if test="deptId != null">dept_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sheepId != null">#{sheepId},</if>
            <if test="earType != null">#{earType},</if>
            <if test="newTag != null and newTag != ''">#{newTag},</if>
            <if test="oldTag != null and oldTag != ''">#{oldTag},</if>
            <if test="comment != null">#{comment},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="eventDate != null">#{eventDate},</if>
            <if test="technician != null and technician != ''">#{technician},</if>
            <if test="userId != null">#{userId},</if>
            <if test="deptId != null">#{deptId},</if>
        </trim>
    </insert>

    <update id="updateScChangeEar" parameterType="ScChangeEar">
        update sc_change_ear
        <trim prefix="SET" suffixOverrides=",">
            <if test="sheepId != null">sheep_id = #{sheepId},</if>
            <if test="earType != null">ear_type = #{earType},</if>
            <if test="newTag != null and newTag != ''">newTag = #{newTag},</if>
            <if test="oldTag != null and oldTag != ''">oldTag = #{oldTag},</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="deleteScChangeEarById" parameterType="Integer">
        delete
        from sc_change_ear
        where id = #{id}
    </delete>

    <delete id="deleteScChangeEarByIds" parameterType="String">
        delete from sc_change_ear 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
    </select>
</mapper>