<?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.ruoyi.web.modules.industryservice.mapper.BizYakAssetMapper">

    <resultMap type="com.ruoyi.web.modules.industryservice.domain.BizYakAsset" id="BizYakAssetResult">
        <id     property="id"                   column="id"/>
        <result property="externalId"           column="external_id"/>
        <result property="yakNo"                column="yak_no"/>
        <result property="earTagNumber"         column="ear_tag_number"/>
        <result property="pastureId"            column="pasture_id"/>
        <result property="pastureName"          column="pasture_name"/>
        <result property="farmExternalId"       column="farm_external_id"/>
        <result property="enclosureExternalId"  column="enclosure_external_id"/>
        <result property="enclosureId"          column="enclosure_id"/>
        <result property="enclosureName"        column="enclosure_name"/>
        <result property="batchNo"              column="batch_no"/>
        <result property="batchExternalId"        column="batch_external_id"/>
        <result property="batchId"              column="batch_id"/>
        <result property="entryCycle"           column="entry_cycle"/>
        <result property="gender"               column="gender"/>
        <result property="cattleVariety"        column="cattle_variety"/>
        <result property="cattlePicture"        column="cattle_picture"/>
        <result property="registrant"           column="registrant"/>
        <result property="remark"               column="remark"/>
        <result property="birthDate"            column="birth_date"/>
        <result property="ageMonths"            column="age_months"/>
        <result property="entryDate"            column="entry_date"/>
        <result property="entryWeightKg"        column="entry_weight_kg"/>
        <result property="source"               column="source"/>
        <result property="breedingMethod"       column="breeding_method"/>
        <result property="assetStatus"          column="asset_status"/>
        <result property="statusChangeDate"     column="status_change_date"/>
        <result property="statusChangeReason"   column="status_change_reason"/>
        <result property="penLocation"          column="pen_location"/>
        <result property="expectedOutDate"      column="expected_out_date"/>
        <result property="supplementPlan"       column="supplement_plan"/>
        <result property="realtimeTemp"         column="realtime_temp"/>
        <result property="realtimeSteps"        column="realtime_steps"/>
        <result property="envTemp"              column="env_temp"/>
        <result property="location"             column="location"/>
        <result property="physioCollectTime"    column="physio_collect_time"/>
        <result property="fatherYakNo"          column="father_yak_no"/>
        <result property="motherYakNo"          column="mother_yak_no"/>
        <result property="lastSyncTime"         column="last_sync_time"/>
        <result property="delFlag"              column="del_flag"/>
        <result property="createTime"           column="create_time"/>
        <result property="updateTime"           column="update_time"/>
    </resultMap>

    <sql id="selectVo">
        select id, external_id, yak_no, ear_tag_number, pasture_id, pasture_name,
               farm_external_id, enclosure_external_id, enclosure_id, enclosure_name,
               batch_no, batch_external_id, batch_id, entry_cycle,
               gender, cattle_variety, cattle_picture, registrant, remark,
               birth_date, age_months, entry_date, entry_weight_kg, source, breeding_method,
               asset_status, status_change_date, status_change_reason, pen_location,
               expected_out_date, supplement_plan, realtime_temp, realtime_steps, env_temp,
               location, physio_collect_time, father_yak_no, mother_yak_no,
               last_sync_time, del_flag, create_time, update_time
        from biz_yak_asset
    </sql>

    <select id="selectBizYakAssetById" parameterType="long" resultMap="BizYakAssetResult">
        <include refid="selectVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="selectBizYakAssetByYakNo" parameterType="string" resultMap="BizYakAssetResult">
        <include refid="selectVo"/>
        where yak_no = #{yakNo} and del_flag = '0'
    </select>

    <select id="selectBizYakAssetByEarTagNumber" parameterType="string" resultMap="BizYakAssetResult">
        <include refid="selectVo"/>
        where ear_tag_number = #{earTagNumber} and del_flag = '0'
    </select>

    <select id="selectBizYakAssetByExternalId" parameterType="long" resultMap="BizYakAssetResult">
        <include refid="selectVo"/>
        where external_id = #{externalId} and del_flag = '0'
    </select>

    <select id="selectBizYakAssetList" parameterType="com.ruoyi.web.modules.industryservice.domain.BizYakAsset" resultMap="BizYakAssetResult">
        <include refid="selectVo"/>
        <where>
            del_flag = '0'
            <if test="keyword != null and keyword != ''">
                and (yak_no like concat('%', #{keyword}, '%')
                  or ear_tag_number like concat('%', #{keyword}, '%'))
            </if>
            <if test="assetStatus != null">
                and asset_status = #{assetStatus}
            </if>
            <if test="gender != null and gender != ''">
                and gender = #{gender}
            </if>
            <if test="pastureId != null">
                and pasture_id = #{pastureId}
            </if>
        </where>
        order by status_change_date desc, id desc
    </select>

    <insert id="insertBizYakAsset" parameterType="com.ruoyi.web.modules.industryservice.domain.BizYakAsset" useGeneratedKeys="true" keyProperty="id">
        insert into biz_yak_asset (
            external_id, yak_no, ear_tag_number, pasture_id, pasture_name,
            farm_external_id, enclosure_external_id, enclosure_id, enclosure_name,
            batch_no, batch_external_id, batch_id, entry_cycle,
            gender, cattle_variety, cattle_picture, registrant, remark,
            birth_date, age_months, entry_date, entry_weight_kg, source, breeding_method,
            asset_status, status_change_date, status_change_reason, pen_location,
            expected_out_date, supplement_plan, realtime_temp, realtime_steps, env_temp,
            location, physio_collect_time, father_yak_no, mother_yak_no,
            last_sync_time, del_flag, create_time
        ) values (
            #{externalId}, #{yakNo}, #{earTagNumber}, #{pastureId}, #{pastureName},
            #{farmExternalId}, #{enclosureExternalId}, #{enclosureId}, #{enclosureName},
            #{batchNo}, #{batchExternalId}, #{batchId}, #{entryCycle},
            #{gender}, #{cattleVariety}, #{cattlePicture}, #{registrant}, #{remark},
            #{birthDate}, #{ageMonths}, #{entryDate}, #{entryWeightKg}, #{source}, #{breedingMethod},
            #{assetStatus}, #{statusChangeDate}, #{statusChangeReason}, #{penLocation},
            #{expectedOutDate}, #{supplementPlan}, #{realtimeTemp}, #{realtimeSteps}, #{envTemp},
            #{location}, #{physioCollectTime}, #{fatherYakNo}, #{motherYakNo},
            #{lastSyncTime}, #{delFlag}, sysdate()
        )
    </insert>

    <update id="updateBizYakAsset" parameterType="com.ruoyi.web.modules.industryservice.domain.BizYakAsset">
        update biz_yak_asset set
            external_id = #{externalId},
            yak_no = #{yakNo},
            ear_tag_number = #{earTagNumber},
            pasture_id = #{pastureId},
            pasture_name = #{pastureName},
            farm_external_id = #{farmExternalId},
            enclosure_external_id = #{enclosureExternalId},
            enclosure_id = #{enclosureId},
            enclosure_name = #{enclosureName},
            batch_no = #{batchNo},
            batch_external_id = #{batchExternalId},
            batch_id = #{batchId},
            entry_cycle = #{entryCycle},
            gender = #{gender},
            cattle_variety = #{cattleVariety},
            cattle_picture = #{cattlePicture},
            registrant = #{registrant},
            remark = #{remark},
            birth_date = #{birthDate},
            age_months = #{ageMonths},
            entry_date = #{entryDate},
            entry_weight_kg = #{entryWeightKg},
            source = #{source},
            breeding_method = #{breedingMethod},
            asset_status = #{assetStatus},
            status_change_date = #{statusChangeDate},
            status_change_reason = #{statusChangeReason},
            pen_location = #{penLocation},
            expected_out_date = #{expectedOutDate},
            supplement_plan = #{supplementPlan},
            realtime_temp = #{realtimeTemp},
            realtime_steps = #{realtimeSteps},
            env_temp = #{envTemp},
            location = #{location},
            physio_collect_time = #{physioCollectTime},
            father_yak_no = #{fatherYakNo},
            mother_yak_no = #{motherYakNo},
            last_sync_time = #{lastSyncTime},
            update_time = sysdate()
        where id = #{id} and del_flag = '0'
    </update>
</mapper>
