<?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.manage_sheep.mapper.ScAddSheepMapper">

    <resultMap type="com.zhyc.module.produce.manage_sheep.domain.ScAddSheep" id="ScAddSheepResult">
        <result property="id" column="id"/>
        <result property="earNumber" column="ear_number"/>
        <result property="sheepfold" column="sheepfold"/>
        <result property="sheepfoldName" column="sheepfoldName"/>
        <result property="father" column="father"/>
        <result property="mother" column="mother"/>
        <result property="bornWeight" column="born_weight"/>
        <result property="birthday" column="birthday"/>
        <result property="gender" column="gender"/>
        <result property="parity" column="parity"/>
        <result property="varietyId" column="variety_id"/>
        <result property="varietyName" column="varietyName"/>
        <result property="typeId" column="type_id"/>
        <result property="typeName" column="type_name"/>
        <result property="joinDate" column="join_date"/>
        <result property="comment" column="comment"/>
        <result property="technician" column="technician"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <select id="selectScAddSheepList" parameterType="ScAddSheep" resultMap="ScAddSheepResult">
        SELECT
        sas.*,
        sf.sheepfold_name AS sheepfoldName,
        bv.variety AS varietyName,
        st.type_name AS typeName
        FROM sc_add_sheep sas
        LEFT JOIN da_sheepfold sf ON sas.sheepfold = sf.id
        LEFT JOIN bas_sheep_variety bv ON sas.variety_id = bv.id
        LEFT JOIN bas_sheep_type st ON sas.type_id = st.id
        <where>
            <if test="earNumber != null and earNumber != ''">
                AND sas.ear_number LIKE CONCAT('%', #{earNumber}, '%')
            </if>
            <if test="sheepfold != null">
                AND sas.sheepfold = #{sheepfold}
            </if>
            <if test="varietyId != null">
                AND sas.variety_id = #{varietyId}
            </if>
            <if test="typeId != null">
                AND sas.type_id = #{typeId}
            </if>
        </where>
    </select>

    <select id="selectByEarNumber" parameterType="string" resultMap="ScAddSheepResult">
        SELECT *
        FROM sc_add_sheep
        WHERE ear_number = #{earNumber}
    </select>

    <insert id="insert" parameterType="ScAddSheep" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO sc_add_sheep
    (ear_number, sheepfold, ranch_id, father, mother, born_weight, birthday,
     gender, parity, variety_id, type_id, join_date, comment, technician,
     create_by, create_time)
    VALUES (#{earNumber}, #{sheepfold}, #{ranchId}, #{father}, #{mother}, #{bornWeight},
            #{birthday}, #{gender}, #{parity}, #{varietyId}, #{typeId}, #{joinDate},
            #{comment}, #{technician}, #{createBy}, #{createTime})
    </insert>

    <update id="updateScAddSheep" parameterType="ScAddSheep">
        UPDATE sc_add_sheep
        <set>
            ear_number = #{earNumber},
            ranch_id = #{ranchId},
            sheepfold = #{sheepfold},
            father = #{father},
            mother = #{mother},
            born_weight = #{bornWeight},
            birthday = #{birthday},
            gender = #{gender},
            parity = #{parity},
            variety_id = #{varietyId},
            type_id = #{typeId},
            join_date = #{joinDate},
            comment = #{comment},
            technician = #{technician},
            update_by = #{updateBy},
            update_time = NOW()
        </set>
        WHERE id = #{id}
    </update>

    <delete id="deleteScAddSheepByIds">
        DELETE FROM sc_add_sheep WHERE id IN
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>