<?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.huimv.apiservice.dao.PigDao">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.huimv.apiservice.entity.YearPigBaseEntity" id="yearPigBaseMap">
        <result property="id" column="id"/>
        <result property="eartag" column="eartag"/>
        <result property="dayAge" column="day_age"/>
        <result property="unitId" column="unit_id"/>
        <result property="weight" column="weight"/>
        <result property="fosterStatus" column="foster_status"/>
        <result property="healthStatus" column="health_status"/>
        <result property="whereDidYouGo" column="where_did_you_go"/>
        <result property="breed" column="breed"/>
        <result property="sex" column="sex"/>
        <result property="birthday" column="birthday"/>
        <result property="pigstyId" column="pigsty_id"/>
        <result property="periodId" column="period_id"/>
        <result property="deleted" column="deleted"/>
        <result property="gmtCreate" column="gmt_create"/>
        <result property="gmtModified" column="gmt_modified"/>
    </resultMap>


    <resultMap id="pigBaseVoMap" type="com.huimv.apiservice.entity.vo.PigBaseVo">
        <result property="eartag" column="c_eartag" />
        <result property="weight" column="weight" />
        <result property="dayAge" column="day_age" />
        <result property="breed" column="breed" />
        <result property="birthday" column="birthday" />
        <result property="feeder" column="feeder" />
        <result property="pigsty" column="pigsty" />
        <result property="periodNumber" column="period_number" />
        <result property="inFenceTime" column="in_fence_time" />
        <result property="outFenceTime" column="out_fence_time" />

        <association property="mother" javaType="com.huimv.apiservice.entity.vo.PigPedigreeMotherVo">
            <result property="eartag" column="m_eartag" />
            <result property="breed" column="m_breed" />
            <result property="childbirthWeight" column="m_weight" />
            <result property="originPlace" column="m_origin_place" />
            <result property="pregnancyDate" column="pregnancy_date" />
            <result property="childCount" column="child_count" />
        </association>

        <association property="father" javaType="com.huimv.apiservice.entity.vo.PigPedigreeFatherVo">
            <result property="eartag" column="f_eartag" />
            <result property="breed" column="f_breed" />
            <result property="originPlace" column="f_origin_place" />
        </association>
    </resultMap>


    <select id="selectFeederInfoByPigEarTagNo" parameterType="string" resultType="com.huimv.apiservice.entity.EmployeeEntity">
        SELECT me.*
        FROM mgt_employee me
        JOIN mgt_pigsty mp
        ON me.id = mp.feeder_id
        JOIN mgt_pigsty_pig mpp
        ON mpp.pigsty_id = mp.id
        WHERE mpp.eartag = #{pigEarTagNo}
    </select>


    <select id="selectPastureNameByPigstyId" resultType="java.lang.String">
        SELECT mc.name
        FROM mgt_pigsty zs
        JOIN mgt_pasture mc
        ON zs.pasture_id = mc.id
        WHERE zs.id = #{pigstyId}
    </select>

    <select id="selectHealthStatus" parameterType="string" resultType="java.lang.Boolean">
        SELECT health_status FROM mgt_year_pig_base WHERE eartag = #{pigEarTagNo} AND deleted = 0
    </select>

    <select id="selectPagePigBaseVoByBreedName" resultMap="pigBaseVoMap">
        SELECT child.eartag c_eartag,child.weight,child.day_age,child.breed,child.birthday,feeder.`name` feeder,zs.number pigsty,p.number period_number,child.in_fence_time,child.out_fence_time,
               m.eartag m_eartag,m.breed m_breed,mother.childbirth_weight m_weight,m.origin_place m_origin_place,mother.pregnancy_date pregnancy_date,mother.childbirth_count child_count,
               father.eartag f_eartag,father.breed f_breed,father.origin_place f_origin_place
        FROM mgt_year_pig_base child
        LEFT JOIN mgt_pigsty zs ON child.pigsty_id = zs.id
        LEFT JOIN mgt_period p ON child.period_id = p.id
        LEFT JOIN mgt_employee feeder ON zs.feeder_id = feeder.id
        LEFT JOIN mgt_sow_childbirth mother ON child.mother_eartag = mother.eartag
        LEFT JOIN mgt_year_pig_base m ON child.mother_eartag = m.eartag
        LEFT JOIN mgt_year_pig_base father ON child.father_eartag = father.eartag
        WHERE child.deleted = 0 AND child.breed = #{breedName}
        <if test="periodId != null">
            AND child.period_id = #{periodId}
        </if>
    </select>

</mapper>