<?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.base.mapper.BasSheepMapper">

    <resultMap type="com.zhyc.module.base.domain.BasSheep" id="BasSheepResult">
        <result property="id" column="id"/>
        <result property="manageTags" column="manage_tags"/>
        <result property="ranchId" column="ranch_id"/>
        <result property="sheepfoldId" column="sheepfold_id"/>
        <result property="electronicTags" column="electronic_tags"/>
        <result property="varietyId" column="variety_id"/>
        <result property="family" column="family"/>
        <result property="typeId" column="type_id"/>
        <result property="gender" column="gender"/>
        <result property="birthday" column="birthday"/>
        <result property="birthWeight" column="birth_weight"/>
        <result property="parity" column="parity"/>
        <result property="statusId" column="status_id"/>
        <result property="weaningDate" column="weaning_date"/>
        <result property="weaningWeight" column="weaning_weight"/>
        <result property="breedStatusId" column="breed_status_id"/>
        <result property="fatherId" column="father_id"/>
        <result property="motherId" column="mother_id"/>
        <result property="receptorId" column="receptor_id"/>
        <result property="matingDate" column="mating_date"/>
        <result property="matingTypeId" column="mating_type_id"/>
        <result property="pregDate" column="preg_date"/>
        <result property="lambingDate" column="lambing_date"/>
        <result property="lambingDay" column="lambing_day"/>
        <result property="expectedDate" column="expected_date"/>
        <result property="controlled" column="controlled"/>
        <result property="matingCounts" column="mating_counts"/>
        <result property="matingTotal" column="mating_total"/>
        <result property="miscarriageCounts" column="miscarriage_counts"/>
        <result property="body" column="body"/>
        <result property="breast" column="breast"/>
        <result property="source" column="source"/>
        <result property="sourceDate" column="source_date"/>
        <result property="sourceRanchId" column="source_ranch_id"/>
        <result property="comment" column="comment"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="isDelete" column="is_delete"/>
    </resultMap>

    <sql id="selectBasSheepVo">
        select id,
               manage_tags,
               ranch_id,
               sheepfold_id,
               electronic_tags,
               variety_id,
               family,
               type_id,
               gender,
               birthday,
               birth_weight,
               parity,
               status_id,
               weaning_date,
               weaning_weight,
               breed_status_id,
               father_id,
               mother_id,
               receptor_id,
               mating_date,
               mating_type_id,
               preg_date,
               lambing_date,
               lambing_day,
               expected_date,
               controlled,
               mating_counts,
               mating_total,
               miscarriage_counts,
               body,
               breast,
               source,
               source_date,
               source_ranch_id,
               comment,
               update_by,
               update_time,
               create_by,
               create_time,
               is_delete
        from bas_sheep
    </sql>

    <select id="selectBasSheepList" parameterType="BasSheep" resultMap="BasSheepResult">
        <include refid="selectBasSheepVo"/>
        <where>
            <if test="manageTags != null  and manageTags != ''">and manage_tags = #{manageTags}</if>
            <if test="ranchId != null ">and ranch_id = #{ranchId}</if>
            <if test="sheepfoldId != null ">and sheepfold_id = #{sheepfoldId}</if>
            <if test="electronicTags != null  and electronicTags != ''">and electronic_tags = #{electronicTags}</if>
            <if test="varietyId != null ">and variety_id = #{varietyId}</if>
            <if test="family != null  and family != ''">and family = #{family}</if>
            <if test="typeId != null ">and type_id = #{typeId}</if>
            <if test="gender != null ">and gender = #{gender}</if>
            <if test="birthday != null ">and birthday = #{birthday}</if>
            <if test="birthWeight != null ">and birth_weight = #{birthWeight}</if>
            <if test="currentWeight != null">current_weight = #{currentWeight},</if>
            <if test="parity != null ">and parity = #{parity}</if>
            <if test="statusId != null ">and status_id = #{statusId}</if>
            <if test="weaningDate != null ">and weaning_date = #{weaningDate}</if>
            <if test="weaningWeight != null ">and weaning_weight = #{weaningWeight}</if>
            <if test="breedStatusId != null ">and breed_status_id = #{breedStatusId}</if>
            <if test="fatherId != null ">and father_id = #{fatherId}</if>
            <if test="motherId != null ">and mother_id = #{motherId}</if>
            <if test="receptorId != null ">and receptor_id = #{receptorId}</if>
            <if test="matingDate != null ">and mating_date = #{matingDate}</if>
            <if test="matingTypeId != null ">and mating_type_id = #{matingTypeId}</if>
            <if test="pregDate != null ">and preg_date = #{pregDate}</if>
            <if test="lambingDate != null ">and lambing_date = #{lambingDate}</if>
            <if test="lambingDay != null ">and lambing_day = #{lambingDay}</if>
            <if test="expectedDate != null ">and expected_date = #{expectedDate}</if>
            <if test="controlled != null ">and controlled = #{controlled}</if>
            <if test="matingCounts != null ">and mating_counts = #{matingCounts}</if>
            <if test="matingTotal != null ">and mating_total = #{matingTotal}</if>
            <if test="miscarriageCounts != null ">and miscarriage_counts = #{miscarriageCounts}</if>
            <if test="body != null ">and body = #{body}</if>
            <if test="breast != null ">and breast = #{breast}</if>
            <if test="source != null  and source != ''">and source = #{source}</if>
            <if test="sourceDate != null ">and source_date = #{sourceDate}</if>
            <if test="sourceRanchId != null ">and source_ranch_id = #{sourceRanchId}</if>
            <if test="comment != null  and comment != ''">and comment = #{comment}</if>
            <if test="isDelete == null">and is_delete = 0</if>
            <if test="isDelete != null">and is_delete = #{isDelete}</if>
        </where>
    </select>

    <select id="selectBasSheepById" parameterType="java.lang.Long" resultType="com.zhyc.module.base.domain.BasSheep">
        SELECT s.id,
               s.manage_tags     AS manageTags,
               s.electronic_tags AS electronicTags,
               s.sheepfold_id    AS sheepfoldId,
               sf.sheepfold_name AS sheepfoldName,
               s.variety_id      AS varietyId,
               bv.variety        AS varietyName
        FROM bas_sheep s
                 LEFT JOIN da_sheepfold sf ON s.sheepfold_id = sf.id
                 LEFT JOIN bas_sheep_variety bv ON s.variety_id = bv.id
        WHERE s.id = #{id}
    </select>

    <select id="searchEarNumbers" resultType="java.lang.String">
        SELECT DISTINCT manage_tags
        FROM bas_sheep b
        <where>
        manage_tags LIKE CONCAT(#{manageTags}, '%')
          AND is_delete = 0
        ${params.dataScope}
        </where>
        ORDER by manage_tags
        LIMIT 50
    </select>

    <select id="selectBasSheepByManageTags" parameterType="String" resultMap="BasSheepResult">
        SELECT s.*,
               bv.variety AS varietyName
        FROM bas_sheep s
                 LEFT JOIN bas_sheep_variety bv ON s.variety_id = bv.id
        <where>s.manage_tags = #{manageTags}
            AND s.is_delete = 0
        </where>

    </select>

    <select id="selectBasSheepByManageTagsList" resultMap="BasSheepResult">
        SELECT s.*,
               bv.variety AS varietyName
        FROM bas_sheep s
                 LEFT JOIN bas_sheep_variety bv ON s.variety_id = bv.id
        <where>manage_tags IN
        <foreach collection="manageTagsList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
            AND s.is_delete = 0
        ${sheep.params.dataScope}
        </where>

    </select>

    <select id="selectBasSheepBySheepfold" parameterType="BasSheep" resultMap="BasSheepResult">
        <include refid="selectBasSheepVo"/>
        <where>
            sheepfold_id = #{id}
            and is_delete = 0
        </where>
    </select>

    <select id="getSheepByRanchId" resultMap="BasSheepResult">
        <include refid="selectBasSheepVo"/>
        <where>
            ranch_id = #{ranchId}
            and is_delete = 0
        </where>
    </select>

    <select id="selectBasSheepListByIds" parameterType="list" resultMap="BasSheepResult">
        SELECT * FROM bas_sheep WHERE id IN
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <insert id="insertBasSheep" parameterType="BasSheep" useGeneratedKeys="true" keyProperty="id">
        insert into bas_sheep
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="manageTags != null">manage_tags,</if>
            <if test="ranchId != null">ranch_id,</if>
            <if test="sheepfoldId != null">sheepfold_id,</if>
            <if test="electronicTags != null">electronic_tags,</if>
            <if test="varietyId != null">variety_id,</if>
            <if test="family != null">family,</if>
            <if test="typeId != null">type_id,</if>
            <if test="gender != null">gender,</if>
            <if test="birthday != null">birthday,</if>
            <if test="birthWeight != null">birth_weight,</if>
            <if test="parity != null">parity,</if>
            <if test="statusId != null">status_id,</if>
            <if test="weaningDate != null">weaning_date,</if>
            <if test="weaningWeight != null">weaning_weight,</if>
            <if test="breedStatusId != null">breed_status_id,</if>
            <if test="fatherId != null">father_id,</if>
            <if test="motherId != null">mother_id,</if>
            <if test="receptorId != null">receptor_id,</if>
            <if test="matingDate != null">mating_date,</if>
            <if test="matingTypeId != null">mating_type_id,</if>
            <if test="pregDate != null">preg_date,</if>
            <if test="lambingDate != null">lambing_date,</if>
            <if test="lambingDay != null">lambing_day,</if>
            <if test="expectedDate != null">expected_date,</if>
            <if test="controlled != null">controlled,</if>
            <if test="matingCounts != null">mating_counts,</if>
            <if test="matingTotal != null">mating_total,</if>
            <if test="miscarriageCounts != null">miscarriage_counts,</if>
            <if test="body != null">body,</if>
            <if test="breast != null">breast,</if>
            <if test="source != null">source,</if>
            <if test="sourceDate != null">source_date,</if>
            <if test="sourceRanchId != null">source_ranch_id,</if>
            <if test="comment != null">comment,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="isDelete != null">is_delete,</if>
            <if test="userId != null">user_id,</if>
            <if test="deptId != null">dept_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="manageTags != null">#{manageTags},</if>
            <if test="ranchId != null">#{ranchId},</if>
            <if test="sheepfoldId != null">#{sheepfoldId},</if>
            <if test="electronicTags != null">#{electronicTags},</if>
            <if test="varietyId != null">#{varietyId},</if>
            <if test="family != null">#{family},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="gender != null">#{gender},</if>
            <if test="birthday != null">#{birthday},</if>
            <if test="birthWeight != null">#{birthWeight},</if>
            <if test="parity != null">#{parity},</if>
            <if test="statusId != null">#{statusId},</if>
            <if test="weaningDate != null">#{weaningDate},</if>
            <if test="weaningWeight != null">#{weaningWeight},</if>
            <if test="breedStatusId != null">#{breedStatusId},</if>
            <if test="fatherId != null">#{fatherId},</if>
            <if test="motherId != null">#{motherId},</if>
            <if test="receptorId != null">#{receptorId},</if>
            <if test="matingDate != null">#{matingDate},</if>
            <if test="matingTypeId != null">#{matingTypeId},</if>
            <if test="pregDate != null">#{pregDate},</if>
            <if test="lambingDate != null">#{lambingDate},</if>
            <if test="lambingDay != null">#{lambingDay},</if>
            <if test="expectedDate != null">#{expectedDate},</if>
            <if test="controlled != null">#{controlled},</if>
            <if test="matingCounts != null">#{matingCounts},</if>
            <if test="matingTotal != null">#{matingTotal},</if>
            <if test="miscarriageCounts != null">#{miscarriageCounts},</if>
            <if test="body != null">#{body},</if>
            <if test="breast != null">#{breast},</if>
            <if test="source != null">#{source},</if>
            <if test="sourceDate != null">#{sourceDate},</if>
            <if test="sourceRanchId != null">#{sourceRanchId},</if>
            <if test="comment != null">#{comment},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="isDelete != null">#{isDelete},</if>
            <if test="userId != null">#{userId},</if>
            <if test="deptId != null">#{deptId},</if>
        </trim>
    </insert>

    <update id="updateBasSheep" parameterType="BasSheep">
        update bas_sheep
        <trim prefix="SET" suffixOverrides=",">
            <if test="manageTags != null">manage_tags = #{manageTags},</if>
            <if test="ranchId != null">ranch_id = #{ranchId},</if>
            <if test="sheepfoldId != null">sheepfold_id = #{sheepfoldId},</if>
            <if test="electronicTags != null">electronic_tags = #{electronicTags},</if>
            <if test="varietyId != null">variety_id = #{varietyId},</if>
            <if test="family != null">family = #{family},</if>
            <if test="typeId != null">type_id = #{typeId},</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="currentWeight != null">current_weight = #{currentWeight},</if>
            <if test="parity != null">parity = #{parity},</if>
            <if test="statusId != null">status_id = #{statusId},</if>
            <if test="weaningDate != null">weaning_date = #{weaningDate},</if>
            <if test="weaningWeight != null">weaning_weight = #{weaningWeight},</if>
            <if test="breedStatusId != null">breed_status_id = #{breedStatusId},</if>
            <if test="fatherId != null">father_id = #{fatherId},</if>
            <if test="motherId != null">mother_id = #{motherId},</if>
            <if test="receptorId != null">receptor_id = #{receptorId},</if>
            <if test="matingDate != null">mating_date = #{matingDate},</if>
            <if test="matingTypeId != null">mating_type_id = #{matingTypeId},</if>
            <if test="pregDate != null">preg_date = #{pregDate},</if>
            <if test="lambingDate != null">lambing_date = #{lambingDate},</if>
            <if test="lambingDay != null">lambing_day = #{lambingDay},</if>
            <if test="expectedDate != null">expected_date = #{expectedDate},</if>
            <if test="controlled != null">controlled = #{controlled},</if>
            <if test="matingCounts != null">mating_counts = #{matingCounts},</if>
            <if test="matingTotal != null">mating_total = #{matingTotal},</if>
            <if test="miscarriageCounts != null">miscarriage_counts = #{miscarriageCounts},</if>
            <if test="body != null">body = #{body},</if>
            <if test="breast != null">breast = #{breast},</if>
            <if test="source != null">source = #{source},</if>
            <if test="sourceDate != null">source_date = #{sourceDate},</if>
            <if test="sourceRanchId != null">source_ranch_id = #{sourceRanchId},</if>
            <if test="comment != null">comment = #{comment},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBasSheepById" parameterType="Long">
        delete
        from bas_sheep
        where id = #{id}
    </delete>

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

    <select id="existsByManageTag" resultType="int">
        SELECT COUNT(*)
        FROM bas_sheep
        WHERE manage_tags = #{tag}
          AND is_delete = 0
    </select>

    <select id="existsByElectronicTag" resultType="int">
        SELECT COUNT(*)
        FROM bas_sheep
        WHERE electronic_tags = #{tag}
          AND is_delete = 0
    </select>
</mapper>