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

    <resultMap type="ViewBreedRamFile" id="ViewBreedRamFileResult">
        <result property="id"    column="id"    />
        <result property="ordinaryEarNumber"    column="ordinary_ear_number"    />
        <result property="ranchId"    column="ranch_id"    />
        <result property="ranchName"    column="ranch_name"    />
        <result property="sheepfoldId"    column="sheepfold_id"    />
        <result property="sheepfoldName"    column="sheepfold_name"    />
        <result property="electronicTags"    column="electronic_tags"    />
        <result property="varietyId"    column="variety_id"    />
        <result property="variety"    column="variety"    />
        <result property="sheepCategory"    column="sheep_category"    />
        <result property="gender"    column="gender"    />
        <result property="birthday"    column="birthday"    />
        <result property="monthAge"    column="month_age"    />
        <result property="birthWeight"    column="birth_weight"    />
        <result property="weaningDate"    column="weaning_date"    />
        <result property="weaningDayAge"    column="weaning_day_age"    />
        <result property="weaningWeight"    column="weaning_weight"    />
        <result property="weaningDailyGain"    column="weaning_daily_gain"    />
        <result property="currentWeight"    column="current_weight"    />
        <result property="breedingStatusId"    column="breeding_status_id"    />
        <result property="breedingStatus"    column="breeding_status"    />
        <result property="fatherId"    column="father_id"    />
        <result property="fatherEarNumber"    column="father_ear_number"    />
        <result property="motherId"    column="mother_id"    />
        <result property="motherEarNumber"    column="mother_ear_number"    />
        <result property="grandfatherId"    column="grandfather_id"    />
        <result property="grandfatherEarNumber"    column="grandfather_ear_number"    />
        <result property="grandmotherId"    column="grandmother_id"    />
        <result property="grandmotherEarNumber"    column="grandmother_ear_number"    />
        <result property="maternalGrandfatherEarNumber"    column="maternal_grandfather_ear_number"    />
        <result property="maternalGrandmotherEarNumber"    column="maternal_grandmother_ear_number"    />
        <result property="matingDate"    column="mating_date"    />
        <result property="matingTypeId"    column="mating_type_id"    />
        <result property="pregnancyCheckDate"    column="pregnancy_check_date"    />
        <result property="lambingDate"    column="lambing_date"    />
        <result property="lambingDay"    column="lambing_day"    />
        <result property="matingDay"    column="mating_day"    />
        <result property="gestationDay"    column="gestation_day"    />
        <result property="expectedDate"    column="expected_date"    />
        <result property="postLambingDay"    column="post_lambing_day"    />
        <result property="lactationDay"    column="lactation_day"    />
        <result property="anestrusDay"    column="anestrous_day"    />
        <result property="matingCounts"    column="mating_counts"    />
        <result property="matingTotal"    column="mating_total"    />
        <result property="miscarriageCounts"    column="miscarriage_counts"    />
        <result property="comment"    column="comment"    />
        <result property="controlled"    column="controlled"    />
        <result property="bodyConditionScore"    column="body_condition_score"    />
        <result property="udderScore"    column="udder_score"    />
        <result property="source"    column="source"    />
        <result property="sourceDate"    column="source_date"    />
        <result property="sourceRanchId"    column="source_ranch_id"    />
        <result property="sourceRanch"    column="source_ranch"    />
        <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"    />
        <result property="sexualStatus"    column="sexual_status"    />
        <result property="scrotumCircumference"    column="scrotum_circumference"    />
        <result property="spermCollectionTime"    column="sperm_collection_time"    />
        <result property="spermVolume"    column="sperm_volume"    />
        <result property="spermVitality"    column="sperm_vitality"    />
        <result property="spermDensity"    column="sperm_density"    />
        <result property="semenQuality"    column="semen_quality"    />
        <result property="totalMatedEwes"    column="total_mated_ewes"    />
        <result property="totalPregnantEwes"    column="total_pregnant_ewes"    />
        <result property="naturalPregnancyCheckEwes"    column="natural_pregnancy_check_ewes"    />
        <result property="naturalConceptionRate"    column="natural_conception_rate"    />
        <result property="artificialPregnancyCheckEwes"    column="artificial_pregnancy_check_ewes"    />
        <result property="artificialConceptionRate"    column="artificial_conception_rate"    />
        <result property="offspringCount"    column="offspring_count"    />
        <result property="prolificacy"    column="prolificacy"    />
    </resultMap>

    <sql id="selectViewBreedRamFileVo">
        select * from view_breed_ram_file
    </sql>

    <select id="selectViewBreedRamFileList" parameterType="ViewBreedRamFile" resultMap="ViewBreedRamFileResult">
        <include refid="selectViewBreedRamFileVo"/>
        <where>
            is_delete = 0
            <if test="ordinaryEarNumber != null and ordinaryEarNumber != ''">
                and ordinary_ear_number like concat('%', #{ordinaryEarNumber}, '%')
            </if>
            <if test="ranchId != null">
                and ranch_id = #{ranchId}
            </if>
            <if test="ranchName != null and ranchName != ''">
                and ranch_name like concat('%', #{ranchName}, '%')
            </if>
            <if test="sheepfoldId != null">
                and sheepfold_id = #{sheepfoldId}
            </if>
            <if test="electronicTags != null and electronicTags != ''">
                and electronic_tags like concat('%', #{electronicTags}, '%')
            </if>
            <if test="variety != null and variety != ''">
                and variety like concat('%', #{variety}, '%')
            </if>
            <if test="breedingStatusId != null">
                and breeding_status_id = #{breedingStatusId}
            </if>
        </where>
        order by id desc
    </select>

    <select id="selectViewBreedRamFileById" parameterType="Long" resultMap="ViewBreedRamFileResult">
        <include refid="selectViewBreedRamFileVo"/>
        where id = #{id} and is_delete = 0
    </select>

    <select id="selectViewBreedRamFileByOrdinaryEarNumber" parameterType="String" resultMap="ViewBreedRamFileResult">
        <include refid="selectViewBreedRamFileVo"/>
        where ordinary_ear_number = #{ordinaryEarNumber} and is_delete = 0
        limit 1
    </select>

    <select id="selectViewBreedRamFileListByRanchId" parameterType="Long" resultMap="ViewBreedRamFileResult">
        <include refid="selectViewBreedRamFileVo"/>
        where ranch_id = #{ranchId} and is_delete = 0
        order by id desc
    </select>

    <select id="selectViewBreedRamFileListBySheepfoldId" parameterType="Long" resultMap="ViewBreedRamFileResult">
        <include refid="selectViewBreedRamFileVo"/>
        where sheepfold_id = #{sheepfoldId} and is_delete = 0
        order by id desc
    </select>

    <select id="selectCoreFlockViewBreedRamFileList" parameterType="ViewBreedRamFile" resultMap="ViewBreedRamFileResult">
        select vbrf.* from view_breed_ram_file vbrf
        inner join breed_ram_file brf on vbrf.id = brf.id
        where brf.is_core_flock = 1 and vbrf.is_delete = 0
        <if test="ranchId != null">
            and vbrf.ranch_id = #{ranchId}
        </if>
        <if test="varietyId != null">
            and vbrf.variety_id = #{varietyId}
        </if>
        order by vbrf.id desc
    </select>

    <select id="selectBreedingUseViewBreedRamFileList" parameterType="ViewBreedRamFile" resultMap="ViewBreedRamFileResult">
        select vbrf.* from view_breed_ram_file vbrf
        inner join breed_ram_file brf on vbrf.id = brf.id
        where brf.is_breeding_use = 1 and vbrf.is_delete = 0
        <if test="ranchId != null">
            and vbrf.ranch_id = #{ranchId}
        </if>
        <if test="varietyId != null">
            and vbrf.variety_id = #{varietyId}
        </if>
        order by vbrf.id desc
    </select>

</mapper>