<?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.frozen.mapper.DdFeMapper">

    <resultMap type="com.zhyc.module.frozen.domain.DdFe" id="DdFeResult">
        <result property="id" column="id"/>
        <result property="code" column="code"/>
        <result property="freezeDate" column="freeze_date"/>
        <result property="drId" column="dr_id"/>
        <result property="drBreed" column="dr_breed"/>
        <result property="deId" column="de_id"/>
        <result property="deBreed" column="de_breed"/>
        <result property="embBreed" column="emb_breed"/>
        <result property="grade" column="grade"/>
        <result property="qty" column="qty"/>
        <result property="sexCtl" column="sex_ctl"/>
        <result property="status" column="status"/>
        <result property="tech" column="tech"/>
        <result property="tankId" column="tank_id"/>
        <result property="bucketId" column="bucket_id"/>
        <result property="rackId" column="rack_id"/>
        <result property="outDate" column="out_date"/>
        <result property="discardTxt" column="discard_txt"/>
        <result property="remark" column="remark"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="selectDdFeVo">
        select id,
               code,
               freeze_date,
               dr_id,
               dr_breed,
               de_id,
               de_breed,
               emb_breed,
               grade,
               qty,
               sex_ctl,
               status,
               tech,
               tank_id,
               bucket_id,
               rack_id,
               out_date,
               discard_txt,
               remark,
               create_by,
               create_time
        from dd_fe
    </sql>

    <select id="selectDdFeList" parameterType="DdFe" resultMap="DdFeResult">
        <include refid="selectDdFeVo"/>
        <where>
            <if test="code != null  and code != ''">and code like concat('%', #{code}, '%')</if>
            <if test="grade != null  and grade != ''">and grade = #{grade}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
            <if test="tech != null and tech != ''">
                AND tech LIKE CONCAT('%', #{tech}, '%')
            </if>
            <if test="outDate != null ">and out_date = #{outDate}</if>
            <if test="params.beginFreezeDate != null and params.endFreezeDate != null">
                and freeze_date between #{params.beginFreezeDate} and #{params.endFreezeDate}
            </if>
            <if test="params.beginOutDate != null and params.endOutDate != null">
                and out_date between #{params.beginOutDate} and #{params.endOutDate}
            </if>
            <if test="status != null and status != ''">and status = #{status}</if>
        </where>
    </select>

    <select id="selectDdFeById" parameterType="Long" resultMap="DdFeResult">
        <include refid="selectDdFeVo"/>
        where id = #{id}
    </select>

    <insert id="insertDdFe" parameterType="DdFe" useGeneratedKeys="true" keyProperty="id">
        insert into dd_fe
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null">code,</if>
            <if test="freezeDate != null">freeze_date,</if>
            <if test="drId != null">dr_id,</if>
            <if test="drBreed != null">dr_breed,</if>
            <if test="deId != null">de_id,</if>
            <if test="deBreed != null">de_breed,</if>
            <if test="embBreed != null">emb_breed,</if>
            <if test="grade != null">grade,</if>
            <if test="qty != null">qty,</if>
            <if test="sexCtl != null">sex_ctl,</if>
            <if test="status != null">status,</if>
            <if test="tech != null">tech,</if>
            <if test="tankId != null">tank_id,</if>
            <if test="bucketId != null">bucket_id,</if>
            <if test="rackId != null">rack_id,</if>
            <if test="outDate != null">out_date,</if>
            <if test="discardTxt != null">discard_txt,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null">#{code},</if>
            <if test="freezeDate != null">#{freezeDate},</if>
            <if test="drId != null">#{drId},</if>
            <if test="drBreed != null">#{drBreed},</if>
            <if test="deId != null">#{deId},</if>
            <if test="deBreed != null">#{deBreed},</if>
            <if test="embBreed != null">#{embBreed},</if>
            <if test="grade != null">#{grade},</if>
            <if test="qty != null">#{qty},</if>
            <if test="sexCtl != null">#{sexCtl},</if>
            <if test="status != null">#{status},</if>
            <if test="tech != null">#{tech},</if>
            <if test="tankId != null">#{tankId},</if>
            <if test="bucketId != null">#{bucketId},</if>
            <if test="rackId != null">#{rackId},</if>
            <if test="outDate != null">#{outDate},</if>
            <if test="discardTxt != null">#{discardTxt},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="updateDdFe" parameterType="DdFe">
        update dd_fe
        <trim prefix="SET" suffixOverrides=",">
            <if test="code != null">code = #{code},</if>
            <if test="freezeDate != null">freeze_date = #{freezeDate},</if>
            <if test="drId != null">dr_id = #{drId},</if>
            <if test="drBreed != null">dr_breed = #{drBreed},</if>
            <if test="deId != null">de_id = #{deId},</if>
            <if test="deBreed != null">de_breed = #{deBreed},</if>
            <if test="embBreed != null">emb_breed = #{embBreed},</if>
            <if test="grade != null">grade = #{grade},</if>
            <if test="qty != null">qty = #{qty},</if>
            <if test="sexCtl != null">sex_ctl = #{sexCtl},</if>
            <if test="status != null">status = #{status},</if>
            <if test="tech != null">tech = #{tech},</if>
            <if test="tankId != null">tank_id = #{tankId},</if>
            <if test="bucketId != null">bucket_id = #{bucketId},</if>
            <if test="rackId != null">rack_id = #{rackId},</if>
            <if test="outDate != null">out_date = #{outDate},</if>
            <if test="discardTxt != null">discard_txt = #{discardTxt},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDdFeById" parameterType="Long">
        delete
        from dd_fe
        where id = #{id}
    </delete>

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

    <select id="selectFlushByEwe" resultType="map">
        SELECT grade_a       gradeA,
               grade_b       gradeB,
               grade_c       gradeC,
               grade_d       gradeD,
               donor_male_no ramId
        FROM sc_embryo_flush
        WHERE donor_female_no = #{eweNo}
        ORDER BY flush_time DESC LIMIT 1
    </select>

    <update id="updateDiscard" parameterType="DdFe">
        UPDATE dd_fe
        SET status      = #{status},
            discard_txt = #{discardTxt}
        WHERE id = #{id}
    </update>

    <select id="existsByCode" resultType="int">
        SELECT COUNT(*) FROM dd_fe WHERE code = #{code}
    </select>
</mapper>