<?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="pastureId"          column="pasture_id"/>
        <result property="pastureName"        column="pasture_name"/>
        <result property="batchNo"            column="batch_no"/>
        <result property="gender"             column="gender"/>
        <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, pasture_id, pasture_name, batch_no, gender, 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="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}, '%')
            </if>
            <if test="assetStatus != null">
                and asset_status = #{assetStatus}
            </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, pasture_id, pasture_name, batch_no, gender, 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}, #{pastureId}, #{pastureName}, #{batchNo}, #{gender}, #{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},
            pasture_id = #{pastureId},
            pasture_name = #{pastureName},
            batch_no = #{batchNo},
            gender = #{gender},
            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>
