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

    <!-- 羔羊详情结果映射 -->
    <resultMap type="ScLambDetail" id="ScLambDetailResult">
        <result property="id"               column="id" />
        <result property="lambingRecordId"  column="lambing_record_id" />
        <result property="lambEarNumber"    column="lamb_ear_number" />
        <result property="lambBreed"        column="lamb_breed" />
        <result property="gender"           column="gender" />
        <result property="birthWeight"      column="birth_weight" />
        <result property="isRetained"       column="is_retained" />
        <result property="lineage"          column="lineage" />
        <result property="birthday"         column="birthday" />
        <result property="createBy"         column="create_by" />
        <result property="createTime"       column="create_time" />
        <result property="updateBy"         column="update_by" />
        <result property="updateTime"       column="update_time" />
    </resultMap>

    <!-- 基础查询SQL -->
    <sql id="selectScLambDetailVo">
        select id, lambing_record_id, lamb_ear_number, lamb_breed, gender, birth_weight,
               is_retained, lineage, birthday, create_by, create_time, update_by, update_time
        from sc_lamb_detail
    </sql>

    <!-- 查询羔羊详情列表 -->
    <select id="selectScLambDetailList" parameterType="ScLambDetail" resultMap="ScLambDetailResult">
        <include refid="selectScLambDetailVo"/>
        <where>
            <if test="lambingRecordId != null"> and lambing_record_id = #{lambingRecordId}</if>
            <if test="lambEarNumber != null and lambEarNumber != ''"> and lamb_ear_number like concat('%', #{lambEarNumber}, '%')</if>
            <if test="lambBreed != null"> and lamb_breed = #{lambBreed}</if>
            <if test="gender != null"> and gender = #{gender}</if>
            <if test="isRetained != null"> and is_retained = #{isRetained}</if>
            <if test="lineage != null and lineage != ''"> and lineage like concat('%', #{lineage}, '%')</if>
        </where>
        order by create_time desc
    </select>

    <!-- 查询羔羊详情 -->
    <select id="selectScLambDetailById" parameterType="Long" resultMap="ScLambDetailResult">
        <include refid="selectScLambDetailVo"/>
        where id = #{id}
    </select>

    <!-- 根据产羔记录ID查询羔羊详情列表 -->
    <select id="selectScLambDetailByLambingRecordId" parameterType="Long" resultMap="ScLambDetailResult">
        <include refid="selectScLambDetailVo"/>
        where lambing_record_id = #{lambingRecordId}
        order by create_time asc
    </select>

    <!-- 检查羔羊耳号是否已存在 -->
    <select id="checkLambEarNumberExists" resultType="int">
        select count(*) from sc_lamb_detail
        where lamb_ear_number = #{lambEarNumber}
        <if test="excludeId != null">
            and id != #{excludeId}
        </if>
    </select>

    <!-- 新增羔羊详情（同步录入到bas_sheep表） -->
    <insert id="insertScLambDetail" parameterType="ScLambDetail" useGeneratedKeys="true" keyProperty="id">
        <!-- 插入到sc_lamb_detail表 -->
        insert into sc_lamb_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="lambingRecordId != null">lambing_record_id,</if>
            <if test="lambEarNumber != null and lambEarNumber != ''">lamb_ear_number,</if>
            <if test="lambBreed != null">lamb_breed,</if>
            <if test="gender != null">gender,</if>
            <if test="birthWeight != null">birth_weight,</if>
            <if test="isRetained != null">is_retained,</if>
            <if test="lineage != null">lineage,</if>
            <if test="birthday != null">birthday,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="lambingRecordId != null">#{lambingRecordId},</if>
            <if test="lambEarNumber != null and lambEarNumber != ''">#{lambEarNumber},</if>
            <if test="lambBreed != null">#{lambBreed},</if>
            <if test="gender != null ">#{gender},</if>
            <if test="birthWeight != null">#{birthWeight},</if>
            <if test="isRetained != null">#{isRetained},</if>
            <if test="lineage != null">#{lineage},</if>
            <if test="birthday != null">#{birthday},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <!-- 同步插入到bas_sheep表 -->
    <insert id="insertBasSheep" parameterType="ScLambDetail">
        insert into bas_sheep
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="lambEarNumber != null and lambEarNumber != ''">manage_tags,</if>
            <if test="lambBreed != null">variety_id,</if>
            <if test="gender != null">gender,</if>
            <if test="birthday != null">birthday,</if>
            <if test="birthWeight != null">birth_weight,</if>
            <if test="lineage != null">family,</if>
            <if test="motherId != null">mother_id,</if>
            <if test="fatherId != null">father_id,</if>
            <if test="ranchId != null">ranch_id,</if>
            <if test="sheepfoldId != null">sheepfold_id,</if>
            <if test="parity != null">parity,</if>
            <if test="isRetained != null">status_id,</if>
            type_id,
            breed_status_id,
            is_delete,
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="lambEarNumber != null and lambEarNumber != ''">#{lambEarNumber},</if>
            <if test="lambBreed != null">#{lambBreed},</if>
            <if test="gender != null">#{gender},</if>
            <if test="birthday != null">#{birthday},</if>
            <if test="birthWeight != null">#{birthWeight},</if>
            <if test="lineage != null">#{lineage},</if>
            <if test="motherId != null">#{motherId},</if>
            <if test="fatherId != null">#{fatherId},</if>
            <if test="ranchId != null">#{ranchId},</if>
            <if test="sheepfoldId != null">#{sheepfoldId},</if>
            <if test="parity != null">#{parity},</if>
            <if test="isRetained != null">#{isRetained},</if>
            3, <!-- type_id: 3表示羔羊 -->
            1, <!-- breed_status_id: 1表示初始繁育状态 -->
            0, <!-- is_delete: 0表示未删除 -->
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <!-- 批量新增羔羊详情（同步录入到bas_sheep表） -->
    <insert id="insertScLambDetailBatch" parameterType="java.util.List">
        <!-- 批量插入到sc_lamb_detail表 -->
        insert into sc_lamb_detail (lambing_record_id, lamb_ear_number, lamb_breed, gender, birth_weight,
        is_retained, lineage, birthday, create_by, create_time)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.lambingRecordId}, #{item.lambEarNumber}, #{item.lambBreed}, #{item.gender},
            #{item.birthWeight}, #{item.isRetained}, #{item.lineage}, #{item.birthday},
            #{item.createBy}, #{item.createTime})
        </foreach>
    </insert>

    <!-- 批量同步插入到bas_sheep表 -->
    <insert id="insertBasSheepBatch" parameterType="java.util.List">
        insert into bas_sheep (manage_tags, variety_id, gender, birthday, birth_weight, family,
        mother_id, father_id, ranch_id, sheepfold_id, parity, status_id, type_id, breed_status_id, is_delete, create_by, create_time)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.lambEarNumber}, #{item.lambBreed}, #{item.gender}, #{item.birthday},
            #{item.birthWeight}, #{item.lineage}, #{item.motherId}, #{item.fatherId},
            #{item.ranchId}, #{item.sheepfoldId}, #{item.parity}, #{item.isRetained},
            3, 1, 0, #{item.createBy}, #{item.createTime})
        </foreach>
    </insert>

    <!-- 修改羔羊详情（同步更新bas_sheep表） -->
    <update id="updateScLambDetail" parameterType="ScLambDetail">
        update sc_lamb_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="lambingRecordId != null">lambing_record_id = #{lambingRecordId},</if>
            <if test="lambEarNumber != null and lambEarNumber != ''">lamb_ear_number = #{lambEarNumber},</if>
            <if test="lambBreed != null">lamb_breed = #{lambBreed},</if>
            <if test="gender != null">gender = #{gender},</if>
            <if test="birthWeight != null">birth_weight = #{birthWeight},</if>
            <if test="isRetained != null">is_retained = #{isRetained},</if>
            <if test="lineage != null">lineage = #{lineage},</if>
            <if test="birthday != null">birthday = #{birthday},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <!-- 同步更新bas_sheep表 -->
    <update id="updateBasSheep" parameterType="ScLambDetail">
        update bas_sheep
        <trim prefix="SET" suffixOverrides=",">
            <if test="lambEarNumber != null and lambEarNumber != ''">manage_tags = #{lambEarNumber},</if>
            <if test="lambBreed != null">variety_id = #{lambBreed},</if>
            <if test="gender != null ">gender = #{gender},</if>
            <if test="birthday != null">birthday = #{birthday},</if>
            <if test="birthWeight != null">birth_weight = #{birthWeight},</if>
            <if test="lineage != null">family = #{lineage},</if>
            <if test="isRetained != null">status_id = #{isRetained},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where manage_tags = #{lambEarNumber} and is_delete = 0
    </update>

    <!-- 删除羔羊详情（同步删除bas_sheep表） -->
    <delete id="deleteScLambDetailById" parameterType="Long">
        delete from sc_lamb_detail where id = #{id}
    </delete>

    <!-- 同步删除bas_sheep表中的羔羊记录 -->
    <update id="deleteBasSheepByEarNumber" parameterType="String">
        update bas_sheep set is_delete = 1 where manage_tags = #{lambEarNumber}
    </update>

    <!-- 批量删除羔羊详情（同步删除bas_sheep表） -->
    <delete id="deleteScLambDetailByIds" parameterType="String">
        delete from sc_lamb_detail where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 批量同步删除bas_sheep表中的羔羊记录 -->
    <update id="deleteBasSheepByEarNumbers" parameterType="String">
        update bas_sheep set is_delete = 1 where manage_tags in
        <foreach item="earNumber" collection="array" open="(" separator="," close=")">
            #{earNumber}
        </foreach>
    </update>

    <!-- 根据产羔记录ID删除羔羊详情（同步删除bas_sheep表） -->
    <delete id="deleteScLambDetailByLambingRecordId" parameterType="Long">
        delete from sc_lamb_detail where lambing_record_id = #{lambingRecordId}
    </delete>

    <!-- 同步删除bas_sheep表中对应产羔记录的羔羊 -->
    <update id="deleteBasSheepByLambingRecordId" parameterType="Long">
        update bas_sheep set is_delete = 1
        where manage_tags in (
            select lamb_ear_number from sc_lamb_detail
            where lambing_record_id = #{lambingRecordId}
        )
    </update>

    <!-- 根据产羔记录获取母羊和父羊信息 -->
    <select id="getParentInfoByLambingRecordId" parameterType="Long" resultType="java.util.Map">
        SELECT
            lr.sheep_id as motherId,
            mother.ranch_id as ranchId,
            mother.sheepfold_id as sheepfoldId,
            mother.parity as parity,
            father.id as fatherId
        FROM sc_lambing_record lr
                 LEFT JOIN bas_sheep mother ON lr.sheep_id = mother.id
                 LEFT JOIN sc_breed_record br ON lr.sheep_id = br.ewe_id AND lr.parity = mother.parity
                 LEFT JOIN bas_sheep father ON br.ram_id = father.id
        WHERE lr.id = #{lambingRecordId}
    </select>

</mapper>