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

    <resultMap type="ScMiscarriageRecord" id="ScMiscarriageRecordResult">
        <result property="id"    column="id"    />
        <result property="sheepId"    column="sheep_id"    />
        <result property="datetime"    column="datetime"    />
        <result property="comment"    column="comment"    />
        <result property="technician"    column="technician"    />
        <result property="reason"    column="reason"    />
        <result property="exposeType"    column="expose_type"    />
        <result property="status"    column="status"    />
        <result property="miscaLamb"    column="misca_lamb"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <!-- 关联查询字段 -->
        <result property="bsManageTags"    column="bs_manage_tags"    />
        <result property="variety"    column="variety"    />
        <result property="matingTypeId"    column="mating_type_id"    />
        <result property="matingTypeName"    column="mating_type_name"    />
        <result property="matingDate"    column="mating_date"    />
        <result property="parity"    column="parity"    />
        <result property="ramVariety"    column="ram_variety"    />
        <result property="monthAge"    column="month_age"    />
        <result property="pregnantDays"    column="pregnant_days"    />
        <result property="sheepfoldName"    column="sheepfold_name"    />
        <result property="drRanch"    column="dr_ranch"    />
    </resultMap>

    <sql id="selectScMiscarriageRecordVo">
        select
            smr.id,
            smr.sheep_id,
            smr.datetime,
            smr.comment,
            smr.technician,
            smr.reason,
            smr.expose_type,
            smr.status,
            smr.misca_lamb,
            smr.create_by,
            smr.create_time,
            sf.bs_manage_tags,
            sf.variety,
            sf.mating_type_id,
            CASE sf.mating_type_id
                WHEN 1 THEN '人工授精'
                WHEN 2 THEN '自然配种'
                WHEN 3 THEN '胚胎移植'
                ELSE '未知'
                END as mating_type_name,
            sf.mating_date,
            sf.parity,
            sf.variety as ram_variety,
            sf.month_age,
            CASE
                WHEN sf.mating_date IS NOT NULL AND smr.datetime IS NOT NULL
                    THEN DATEDIFF(smr.datetime, sf.mating_date)
                ELSE NULL
                END as pregnant_days,
            sf.sheepfold_name,
            sf.dr_ranch
        from sc_miscarriage_record smr
                 left join sheep_file sf on smr.sheep_id = sf.bs_manage_tags
    </sql>

    <select id="selectScMiscarriageRecordList" parameterType="ScMiscarriageRecord" resultMap="ScMiscarriageRecordResult">
        <include refid="selectScMiscarriageRecordVo"/>
        <where>
            <if test="sheepId != null and sheepId != ''"> and smr.sheep_id = #{sheepId}</if>
            <if test="bsManageTags != null and bsManageTags != ''"> and sf.bs_manage_tags like concat('%', #{bsManageTags}, '%')</if>
            <if test="datetime != null"> and smr.datetime = #{datetime}</if>
            <if test="comment != null and comment != ''"> and smr.comment like concat('%', #{comment}, '%')</if>
            <if test="technician != null and technician != ''"> and smr.technician like concat('%', #{technician}, '%')</if>
            <if test="reason != null and reason != ''"> and smr.reason = #{reason}</if>
            <if test="exposeType != null"> and smr.expose_type = #{exposeType}</if>
            <if test="status != null"> and smr.status = #{status}</if>
            <if test="miscaLamb != null"> and smr.misca_lamb = #{miscaLamb}</if>
            <if test="variety != null and variety != ''"> and sf.variety like concat('%', #{variety}, '%')</if>
        </where>
        order by smr.create_time desc
    </select>

    <select id="selectScMiscarriageRecordById" parameterType="Long" resultMap="ScMiscarriageRecordResult">
        <include refid="selectScMiscarriageRecordVo"/>
        where smr.id = #{id}
    </select>

    <insert id="insertScMiscarriageRecord" parameterType="ScMiscarriageRecord" useGeneratedKeys="true" keyProperty="id">
        insert into sc_miscarriage_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sheepId != null">sheep_id,</if>
            <if test="datetime != null">datetime,</if>
            <if test="comment != null">comment,</if>
            <if test="technician != null">technician,</if>
            <if test="reason != null">reason,</if>
            <if test="exposeType != null">expose_type,</if>
            <if test="status != null">status,</if>
            <if test="miscaLamb != null">misca_lamb,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sheepId != null">#{sheepId},</if>
            <if test="datetime != null">#{datetime},</if>
            <if test="comment != null">#{comment},</if>
            <if test="technician != null">#{technician},</if>
            <if test="reason != null">#{reason},</if>
            <if test="exposeType != null">#{exposeType},</if>
            <if test="status != null">#{status},</if>
            <if test="miscaLamb != null">#{miscaLamb},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="updateScMiscarriageRecord" parameterType="ScMiscarriageRecord">
        update sc_miscarriage_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="sheepId != null">sheep_id = #{sheepId},</if>
            <if test="datetime != null">datetime = #{datetime},</if>
            <if test="comment != null">comment = #{comment},</if>
            <if test="technician != null">technician = #{technician},</if>
            <if test="reason != null">reason = #{reason},</if>
            <if test="exposeType != null">expose_type = #{exposeType},</if>
            <if test="status != null">status = #{status},</if>
            <if test="miscaLamb != null">misca_lamb = #{miscaLamb},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteScMiscarriageRecordById" parameterType="Long">
        delete from sc_miscarriage_record where id = #{id}
    </delete>

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

    <select id="selectSheepByManageTags" parameterType="String" resultType="map">
        select
            bs_manage_tags,
            variety,
            parity,
            month_age,
            sheepfold_name,
            dr_ranch,
            gender
        from sheep_file
        where bs_manage_tags = #{manageTags}
    </select>

</mapper>