<?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.bodyManage.mapper.ScBodyScoreMapper">

    <resultMap type="com.zhyc.module.produce.bodyManage.domain.ScBodyScore" id="ScBodyScoreResult">
        <result property="id" column="id"/>
        <result property="sheepId" column="sheep_id"/>
        <result property="manageTags" column="manage_tags"/>
        <result property="varietyId" column="varietyId"/>
        <result property="varietyName" column="varietyName"/>
        <result property="eventType" column="event_type"/>
        <result property="datetime" column="datetime"/>
        <result property="score" column="score"/>
        <result property="sheepfold" column="sheepfold"/>
        <result property="sheepfoldName" column="sheepfoldName"/>
        <result property="comment" column="comment"/>
        <result property="technician" column="technician"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="selectScBodyScoreVo">
        select sbs.id,
               sbs.sheep_id,
               sbs.user_id,
               sbs.dept_id,
               bs.manage_tags,
               bsv.id            as varietyId,
               bsv.variety       as varietyName,
               '体况评分'        as event_type,
               sbs.datetime,
               sbs.score,
               ds.sheepfold_name as sheepfoldName,
               sbs.comment,
               sbs.technician,
               sbs.create_by,
               sbs.create_time
        from sc_body_score sbs
                 left join bas_sheep bs on sbs.sheep_id = bs.id
                 left join da_sheepfold ds on bs.sheepfold_id = ds.id
                 left join bas_sheep_variety bsv on bs.variety_id = bsv.id
    </sql>

    <select id="selectScBodyScoreList" resultMap="ScBodyScoreResult">
        <include refid="selectScBodyScoreVo"/>
        <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.sheepId != null  and sc.sheepId != ''">and sbs.sheep_id = #{sc.sheepId}</if>
            <if test="sc.varietyId != null">and bsv.id = #{sc.varietyId}</if>
            <if test="sc.sheepfold != null">and bs.sheepfold_id = #{sc.sheepfold}</if>
            <if test="sc.score != null ">and sbs.score = #{sc.score}</if>
            <if test="sc.params != null and sc.params.beginDatetime != null and sc.params.beginDatetime != '' and sc.params.endDatetime != null and sc.params.endDatetime != ''">
                and sbs.datetime between #{sc.params.beginDatetime} and #{sc.params.endDatetime}
            </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 sbs.create_time between #{sc.params.beginCreateTime} and #{sc.params.endCreateTime}
            </if>
            <if test="sc.isDelete != null">
                and bs.is_delete = #{sc.isDelete}
            </if>
            <if test="sc.technician != null and sc.technician != ''">
                and sbs.technician like concat('%', #{sc.technician}, '%')
            </if>
            ${sc.params.dataScope}
        </where>
        ORDER BY sbs.create_time DESC
    </select>

    <select id="selectScBodyScoreById" parameterType="Long" resultMap="ScBodyScoreResult">
        <include refid="selectScBodyScoreVo"/>
        where sbs.id = #{id}
    </select>

    <insert id="insertScBodyScore" parameterType="ScBodyScore" useGeneratedKeys="true" keyProperty="id">
        insert into sc_body_score
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sheepId != null">sheep_id,</if>
            <if test="datetime != null">datetime,</if>
            <if test="score != null">score,</if>
            <if test="sheepfold != null">sheepfold,</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="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="datetime != null">#{datetime},</if>
            <if test="score != null">#{score},</if>
            <if test="sheepfold != null">#{sheepfold},</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="userId != null">#{userId},</if>
            <if test="deptId != null">#{deptId},</if>
        </trim>
    </insert>

    <update id="updateScBodyScore" parameterType="ScBodyScore">
        update sc_body_score
        <trim prefix="SET" suffixOverrides=",">
            <if test="sheepId != null">sheep_id = #{sheepId},</if>
            <if test="datetime != null">datetime = #{datetime},</if>
            <if test="score != null">score = #{score},</if>
            <if test="sheepfold != null">sheepfold = #{sheepfold},</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="deleteScBodyScoreById" parameterType="Long">
        delete
        from sc_body_score
        where id = #{id}
    </delete>

    <delete id="deleteScBodyScoreByIds" parameterType="String">
        delete from sc_body_score 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>