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

    <resultMap type="com.zhyc.module.produce.bodyManage.domain.ScBreastRating" id="ScBreastRatingResult">
        <result property="id" column="id"/>
        <result property="sheepId" column="sheep_id"/>
        <result property="manageTags" column="manageTags"/>
        <result property="varietyId" column="varietyId"/>
        <result property="varietyName" column="varietyName"/>
        <result property="eventType" column="event_type"/>
        <result property="eventDate" column="event_date"/>
        <result property="sheepfoldId" column="sheepfoldId"/>
        <result property="sheepfoldName" column="sheepfoldName"/>
        <result property="depth" column="depth"/>
        <result property="length" column="length"/>
        <result property="position" column="position"/>
        <result property="adbere" column="adbere"/>
        <result property="spacing" column="spacing"/>
        <result property="score" column="score"/>
        <result property="comment" column="comment"/>
        <result property="technician" column="technician"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="userId" column="user_id"/>
        <result property="deptId" column="dept_id"/>
    </resultMap>

    <sql id="selectScBreastRatingVo">
        select sbr.*,
               bs.manage_tags    as manageTags,
               sbr.user_id,
               sbr.dept_id,
               bsv.id            as varietyId,
               bsv.variety       as varietyName,
               '乳房评分'        as event_type,
               sf.id             as sheepfoldId,
               sf.sheepfold_name as sheepfoldName,
               sbr.event_date
        from sc_breast_rating sbr
                 left join bas_sheep bs on sbr.sheep_id = bs.id
                 left join bas_sheep_variety bsv on bs.variety_id = bsv.id
                 left join da_sheepfold sf on bs.sheepfold_id = sf.id
    </sql>

    <select id="selectScBreastRatingList" resultMap="ScBreastRatingResult">
        <include refid="selectScBreastRatingVo"/>
        <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.varietyId != null">and bsv.id = #{sc.varietyId}</if>
            <if test="sc.sheepfoldId != null">and sf.id = #{sc.sheepfoldId}</if>
            <if test="sc.params != null and sc.params.beginEventDate != null and sc.params.endEventDate != null">
                and sbr.event_date between #{sc.params.beginEventDate} and #{sc.params.endEventDate}
            </if>
            <if test="sc.params != null and sc.params.beginCreateTime != null and sc.params.endCreateTime != null">
                and sbr.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 sbr.technician = #{sc.technician}
            </if>
            <if test="sc.score != null">
                and sbr.score = #{sc.score}
            </if>
            ${sc.params.dataScope}
        </where>
        ORDER BY sbr.create_time DESC
    </select>

    <select id="selectScBreastRatingById" parameterType="Long" resultMap="ScBreastRatingResult">
        <include refid="selectScBreastRatingVo"/>
        where sbr.id = #{id}
    </select>

    <insert id="insertScBreastRating" parameterType="ScBreastRating" useGeneratedKeys="true" keyProperty="id">
        insert into sc_breast_rating
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sheepId != null">sheep_id,</if>
            <if test="eventDate != null">event_date,</if>
            <if test="depth != null">depth,</if>
            <if test="length != null">length,</if>
            <if test="position != null">position,</if>
            <if test="adbere != null">adbere,</if>
            <if test="spacing != null">spacing,</if>
            <if test="score != null">score,</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="eventDate != null">#{eventDate},</if>
            <if test="depth != null">#{depth},</if>
            <if test="length != null">#{length},</if>
            <if test="position != null">#{position},</if>
            <if test="adbere != null">#{adbere},</if>
            <if test="spacing != null">#{spacing},</if>
            <if test="score != null">#{score},</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="updateScBreastRating" parameterType="ScBreastRating">
        update sc_breast_rating
        <trim prefix="SET" suffixOverrides=",">
            <if test="sheepId != null">sheep_id = #{sheepId},</if>
            <if test="eventDate != null">event_date = #{eventDate},</if>
            <if test="depth != null">depth = #{depth},</if>
            <if test="length != null">length = #{length},</if>
            <if test="position != null">position = #{position},</if>
            <if test="adbere != null">adbere = #{adbere},</if>
            <if test="spacing != null">spacing = #{spacing},</if>
            <if test="score != null">score = #{score},</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="deleteScBreastRatingById" parameterType="Long">
        delete
        from sc_breast_rating
        where id = #{id}
    </delete>

    <delete id="deleteScBreastRatingByIds" parameterType="String">
        delete from sc_breast_rating 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>


    <select id="selectLatestBySheepIdBeforeDate" resultMap="ScBreastRatingResult">
        SELECT *
        FROM sc_breast_rating
        WHERE sheep_id = #{sheepId}
          AND event_date &lt;= #{measureDate}
        ORDER BY event_date DESC, create_time DESC LIMIT 1
    </select>
</mapper>