<?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.breed.mapper.ScSheepDeathMapper">

    <resultMap type="ScSheepDeath" id="ScSheepDeathResult">
        <result property="id"    column="id"    />
        <result property="sheepId"    column="sheep_id"    />
        <result property="manageTags"    column="manage_tags"    />
        <result property="eventType"    column="event_type"    />
        <result property="deathDate"    column="death_date"    />
        <result property="diseaseTypeId"    column="disease_type_id"    />
        <result property="diseaseSubtypeId"    column="disease_subtype_id"    />
        <result property="disposalDirection"    column="disposal_direction"    />
        <result property="technician"    column="technician"    />
        <result property="handler"    column="handler"    />
        <result property="workGroup"    column="work_group"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="comment"    column="comment"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDelete"    column="is_delete"    />
    </resultMap>

    <sql id="selectScSheepDeathVo">
        select id, sheep_id, manage_tags, event_type, death_date, disease_type_id, disease_subtype_id, disposal_direction, technician, handler, work_group, create_by, create_time, comment, update_by, update_time, is_delete from sc_sheep_death
    </sql>

    <select id="selectScSheepDeathList" parameterType="ScSheepDeath" resultMap="ScSheepDeathResult">
        <include refid="selectScSheepDeathVo"/>
        <where>
            <if test="sheepId != null "> and sheep_id = #{sheepId}</if>
            <if test="manageTags != null  and manageTags != ''"> and manage_tags = #{manageTags}</if>
            <if test="eventType != null  and eventType != ''"> and event_type = #{eventType}</if>
            <if test="deathDate != null "> and death_date = #{deathDate}</if>
            <if test="diseaseTypeId != null "> and disease_type_id = #{diseaseTypeId}</if>
            <if test="diseaseSubtypeId != null "> and disease_subtype_id = #{diseaseSubtypeId}</if>
            <if test="disposalDirection != null  and disposalDirection != ''"> and disposal_direction = #{disposalDirection}</if>
            <if test="technician != null  and technician != ''"> and technician = #{technician}</if>
            <if test="handler != null  and handler != ''"> and handler = #{handler}</if>
            <if test="workGroup != null  and workGroup != ''"> and work_group = #{workGroup}</if>
            <if test="comment != null  and comment != ''"> and comment = #{comment}</if>
            <if test="isDelete != null "> and is_delete = #{isDelete}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectScSheepDeathById" parameterType="Long" resultMap="ScSheepDeathResult">
        <include refid="selectScSheepDeathVo"/>
        where id = #{id}
    </select>

    <!-- 根据管理耳号查询sheep_file视图信息 -->
    <select id="selectSheepFileByManageTags" parameterType="String" resultType="java.util.Map">
        select
            id as sheepId,
            variety,
            name as sheepType,
            gender,
            day_age as dayAge,
            parity,
            sheepfold_name as sheepfoldName,
            breed as breedStatus,
            post_lambing_day as postLambingDay,
            lactation_day as lactationDay,
            gestation_day as gestationDay
        from sheep_file
        where bs_manage_tags = #{manageTags} and is_delete = 0
            limit 1
    </select>

    <insert id="insertScSheepDeath" parameterType="ScSheepDeath" useGeneratedKeys="true" keyProperty="id">
        insert into sc_sheep_death
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sheepId != null">sheep_id,</if>
            <if test="manageTags != null and manageTags != ''">manage_tags,</if>
            <if test="eventType != null">event_type,</if>
            <if test="deathDate != null">death_date,</if>
            <if test="diseaseTypeId != null">disease_type_id,</if>
            <if test="diseaseSubtypeId != null">disease_subtype_id,</if>
            <if test="disposalDirection != null">disposal_direction,</if>
            <if test="technician != null">technician,</if>
            <if test="handler != null">handler,</if>
            <if test="workGroup != null">work_group,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="comment != null">comment,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDelete != null">is_delete,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sheepId != null">#{sheepId},</if>
            <if test="manageTags != null and manageTags != ''">#{manageTags},</if>
            <if test="eventType != null">#{eventType},</if>
            <if test="deathDate != null">#{deathDate},</if>
            <if test="diseaseTypeId != null">#{diseaseTypeId},</if>
            <if test="diseaseSubtypeId != null">#{diseaseSubtypeId},</if>
            <if test="disposalDirection != null">#{disposalDirection},</if>
            <if test="technician != null">#{technician},</if>
            <if test="handler != null">#{handler},</if>
            <if test="workGroup != null">#{workGroup},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="comment != null">#{comment},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDelete != null">#{isDelete},</if>
        </trim>
    </insert>

    <update id="updateScSheepDeath" parameterType="ScSheepDeath">
        update sc_sheep_death
        <trim prefix="SET" suffixOverrides=",">
            <if test="sheepId != null">sheep_id = #{sheepId},</if>
            <if test="manageTags != null and manageTags != ''">manage_tags = #{manageTags},</if>
            <if test="eventType != null">event_type = #{eventType},</if>
            <if test="deathDate != null">death_date = #{deathDate},</if>
            <if test="diseaseTypeId != null">disease_type_id = #{diseaseTypeId},</if>
            <if test="diseaseSubtypeId != null">disease_subtype_id = #{diseaseSubtypeId},</if>
            <if test="disposalDirection != null">disposal_direction = #{disposalDirection},</if>
            <if test="technician != null">technician = #{technician},</if>
            <if test="handler != null">handler = #{handler},</if>
            <if test="workGroup != null">work_group = #{workGroup},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="comment != null">comment = #{comment},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteScSheepDeathById" parameterType="Long">
        delete from sc_sheep_death where id = #{id}
    </delete>

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

    <!-- 更新羊只繁育状态 - 更新基础表 bas_sheep，使用正确的字段名 -->
    <update id="updateSheepFileStatus">
        UPDATE bas_sheep
        SET breed_status_id = #{status},
            update_time = NOW()
        WHERE id = #{sheepId}
          AND is_delete = 0
    </update>

    <!-- 更新羊只在群状态 - 更新基础表 bas_sheep，使用正确的字段名 -->
    <update id="updateSheepStatus">
        UPDATE bas_sheep
        SET status_id = #{status},
            update_time = NOW()
        WHERE id = #{sheepId}
          AND is_delete = 0
    </update>

</mapper>