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

    <resultMap type="com.zhyc.module.produce.manage_sheep.domain.ScChangeComment" id="ScChangeCommentResult">
        <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="newComment" column="new_comment"/>
        <result property="oldComment" column="old_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="selectScChangeCommentVo">
        select scc.id,
               scc.sheep_id,
               scc.user_id,
               scc.dept_id,
               bs.manage_tags    as manage_tags,
               sf.sheepfold_name as sheepfold_name,
               '改备注'          as event_type,
               scc.new_comment,
               scc.old_comment,
               scc.create_by,
               scc.create_time,
               scc.event_date,
               scc.technician
        from sc_change_comment scc
                 left join bas_sheep bs on scc.sheep_id = bs.id
                 left join da_sheepfold sf on bs.sheepfold_id = sf.id
    </sql>

    <select id="selectScChangeCommentList" resultMap="ScChangeCommentResult">
        <include refid="selectScChangeCommentVo"/>
        <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>
            <!--            <if test="sc.sheepfoldId != null">-->
            <!--                and bs.sheepfold_id = #{sc.sheepfoldId}-->
            <!--            </if>-->
            <if test="sc.technician != null and sc.technician != ''">
                and scc.technician like concat('%', #{sc.technician}, '%')
            </if>
            <if test="sc.newComment != null and sc.newComment != ''">
                and scc.new_comment like concat('%', #{sc.newComment}, '%')
            </if>
            <if test="sc.oldComment != null and sc.oldComment != ''">
                and scc.old_comment like concat('%', #{sc.oldComment}, '%')
            </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 scc.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 scc.event_date between #{sc.params.beginEventDate} and #{sc.params.endEventDate}
            </if>
            <if test="sc.isDelete != null">
                and bs.is_delete = #{sc.isDelete}
            </if>
            ${sc.params.dataScope}
        </where>
        ORDER BY scc.create_time DESC
    </select>

    <select id="selectScChangeCommentById" parameterType="Long" resultMap="ScChangeCommentResult">
        <include refid="selectScChangeCommentVo"/>
        where scc.id = #{id}
    </select>

    <insert id="insertScChangeComment" parameterType="ScChangeComment" useGeneratedKeys="true" keyProperty="id">
        insert into sc_change_comment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sheepId != null and sheepId != ''">sheep_id,</if>
            <if test="newComment != null and newComment != ''">new_comment,</if>
            <if test="oldComment != null and oldComment != ''">old_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 and sheepId != ''">#{sheepId},</if>
            <if test="newComment != null and newComment != ''">#{newComment},</if>
            <if test="oldComment != null and oldComment != ''">#{oldComment},</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="updateScChangeComment" parameterType="ScChangeComment">
        update sc_change_comment
        <trim prefix="SET" suffixOverrides=",">
            <if test="sheepId != null and sheepId != ''">sheep_id = #{sheepId},</if>
            <if test="newComment != null and newComment != ''">new_comment = #{newComment},</if>
            <if test="oldComment != null and oldComment != ''">old_comment = #{oldComment},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteScChangeCommentById" parameterType="Long">
        delete
        from sc_change_comment
        where id = #{id}
    </delete>

    <delete id="deleteScChangeCommentByIds" parameterType="String">
        delete from sc_change_comment 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>