<?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="grandpa" column="grandpa"/>
        <result property="grandma" column="grandma"/>
        <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"/>
        <result property="userId" column="user_id"/>
        <result property="deptId" column="dept_id"/>
    </resultMap>
    <sql id="selectScAddSheepVo">
        SELECT
            sas.id,
            sas.ear_number,
            sas.sheepfold,
            sas.ranch_id,
            sas.father,
            sas.mother,
            sas.grandpa,
            sas.grandma,
            sas.born_weight,
            sas.birthday,
            sas.gender,
            sas.parity,
            sas.variety_id,
            sas.type_id,
            sas.join_date,
            sas.comment,
            sas.technician,
            sas.create_by,
            sas.create_time,
            sas.user_id,
            sas.dept_id,
            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
    </sql>
    <select id="selectScAddSheepList" parameterType="ScAddSheep" resultMap="ScAddSheepResult">
        <include refid="selectScAddSheepVo"/>
        <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>
                ${params.dataScope}
        </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
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="earNumber != null">ear_number,</if>
            <if test="sheepfold != null">sheepfold,</if>
            <if test="ranchId != null">ranch_id,</if>
            <if test="father != null">father,</if>
            <if test="mother != null">mother,</if>
            <if test="grandpa != null">grandpa,</if>
            <if test="grandma != null">grandma,</if>
            <if test="bornWeight != null">born_weight,</if>
            <if test="birthday != null">birthday,</if>
            <if test="gender != null">gender,</if>
            <if test="parity != null">parity,</if>
            <if test="varietyId != null">variety_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="joinDate != null">join_date,</if>
            <if test="comment != null">comment,</if>
            <if test="technician != null">technician,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="userId != null">user_id,</if>
            <if test="deptId != null">dept_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="earNumber != null">#{earNumber},</if>
            <if test="sheepfold != null">#{sheepfold},</if>
            <if test="ranchId != null">#{ranchId},</if>
            <if test="father != null">#{father},</if>
            <if test="mother != null">#{mother},</if>
            <if test="grandpa != null">#{grandpa},</if>
            <if test="grandma != null">#{grandma},</if>
            <if test="bornWeight != null">#{bornWeight},</if>
            <if test="birthday != null">#{birthday},</if>
            <if test="gender != null">#{gender},</if>
            <if test="parity != null">#{parity},</if>
            <if test="varietyId != null">#{varietyId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="joinDate != null">#{joinDate},</if>
            <if test="comment != null">#{comment},</if>
            <if test="technician != null">#{technician},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="userId != null">#{userId},</if>
            <if test="deptId != null">#{deptId},</if>
        </trim>
    </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>