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

    <resultMap type="com.ruoyi.web.modules.industryservice.domain.BizYakHealthSourceRecord" id="BizYakHealthSourceRecordResult">
        <id     property="id"                      column="id"/>
        <result property="externalId"              column="external_id"/>
        <result property="recordType"              column="record_type"/>
        <result property="recordTime"              column="record_time"/>
        <result property="regionKey"               column="region_key"/>
        <result property="farmId"                  column="farm_id"/>
        <result property="farmName"                column="farm_name"/>
        <result property="enclosureId"             column="enclosure_id"/>
        <result property="enclosureName"           column="enclosure_name"/>
        <result property="cattleId"                column="cattle_id"/>
        <result property="earTagNumber"            column="ear_tag_number"/>
        <result property="yakNo"                   column="yak_no"/>
        <result property="sourceCreateTime"        column="source_create_time"/>
        <result property="quarantineUnitId"        column="quarantine_unit_id"/>
        <result property="quarantineUnitName"      column="quarantine_unit_name"/>
        <result property="quarantinePersonnelId"   column="quarantine_personnel_id"/>
        <result property="quarantinePersonnelName" column="quarantine_personnel_name"/>
        <result property="quarantineItem"          column="quarantine_item"/>
        <result property="quarantineResult"        column="quarantine_result"/>
        <result property="quarantineReport"        column="quarantine_report"/>
        <result property="certificate"             column="certificate"/>
        <result property="scenePicture"            column="scene_picture"/>
        <result property="age"                     column="age"/>
        <result property="etiology"                column="etiology"/>
        <result property="diagnosisResult"         column="diagnosis_result"/>
        <result property="medicationName"          column="medication_name"/>
        <result property="medicationMethod"        column="medication_method"/>
        <result property="clinic"                  column="clinic"/>
        <result property="treatmentTime"           column="treatment_time"/>
        <result property="treatmentCycle"          column="treatment_cycle"/>
        <result property="treatmentEffect"         column="treatment_effect"/>
        <result property="conditionChange"         column="condition_change"/>
        <result property="recoveryTime"            column="recovery_time"/>
        <result property="recoveryHealthStatus"    column="recovery_health_status"/>
        <result property="recordNo"                column="record_no"/>
        <result property="sourceType"              column="source_type"/>
        <result property="taskId"                  column="task_id"/>
        <result property="operationType"           column="operation_type"/>
        <result property="batchId"                 column="batch_id"/>
        <result property="cattleIds"               column="cattle_ids"/>
        <result property="executorUserId"          column="executor_user_id"/>
        <result property="executorName"            column="executor_name"/>
        <result property="executeResult"           column="execute_result"/>
        <result property="detailJson"              column="detail_json"/>
        <result property="materialUsageJson"       column="material_usage_json"/>
        <result property="attachments"             column="attachments"/>
        <result property="exceptionFlag"           column="exception_flag"/>
        <result property="exceptionDesc"           column="exception_desc"/>
        <result property="remark"                  column="remark"/>
        <result property="analyzedFlag"            column="analyzed_flag"/>
        <result property="lastSyncTime"            column="last_sync_time"/>
        <result property="createTime"              column="create_time"/>
        <result property="updateTime"              column="update_time"/>
    </resultMap>

    <sql id="selectVo">
        select id, external_id, record_type, record_time, region_key, farm_id, farm_name,
               enclosure_id, enclosure_name, cattle_id, ear_tag_number, yak_no, source_create_time,
               quarantine_unit_id, quarantine_unit_name, quarantine_personnel_id, quarantine_personnel_name,
               quarantine_item, quarantine_result, quarantine_report, certificate, scene_picture,
               age, etiology, diagnosis_result, medication_name, medication_method, clinic,
               treatment_time, treatment_cycle, treatment_effect, condition_change, recovery_time, recovery_health_status,
               record_no, source_type, task_id, operation_type, batch_id, cattle_ids,
               executor_user_id, executor_name, execute_result, detail_json, material_usage_json, attachments,
               exception_flag, exception_desc, remark,
               analyzed_flag, last_sync_time, create_time, update_time
        from biz_yak_health_source_record
    </sql>

    <select id="selectByExternalId" resultMap="BizYakHealthSourceRecordResult">
        <include refid="selectVo"/>
        where external_id = #{externalId}
    </select>

    <select id="selectUnanalyzedByType" resultMap="BizYakHealthSourceRecordResult">
        <include refid="selectVo"/>
        where record_type = #{recordType} and analyzed_flag = '0'
        order by record_time asc, id asc
    </select>

    <insert id="insertBizYakHealthSourceRecord" parameterType="com.ruoyi.web.modules.industryservice.domain.BizYakHealthSourceRecord"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_yak_health_source_record (
            external_id, record_type, record_time, region_key, farm_id, farm_name,
            enclosure_id, enclosure_name, cattle_id, ear_tag_number, yak_no, source_create_time,
            quarantine_unit_id, quarantine_unit_name, quarantine_personnel_id, quarantine_personnel_name,
            quarantine_item, quarantine_result, quarantine_report, certificate, scene_picture,
            age, etiology, diagnosis_result, medication_name, medication_method, clinic,
            treatment_time, treatment_cycle, treatment_effect, condition_change, recovery_time, recovery_health_status,
            record_no, source_type, task_id, operation_type, batch_id, cattle_ids,
            executor_user_id, executor_name, execute_result, detail_json, material_usage_json, attachments,
            exception_flag, exception_desc, remark,
            analyzed_flag, last_sync_time, create_time
        ) values (
            #{externalId}, #{recordType}, #{recordTime}, #{regionKey}, #{farmId}, #{farmName},
            #{enclosureId}, #{enclosureName}, #{cattleId}, #{earTagNumber}, #{yakNo}, #{sourceCreateTime},
            #{quarantineUnitId}, #{quarantineUnitName}, #{quarantinePersonnelId}, #{quarantinePersonnelName},
            #{quarantineItem}, #{quarantineResult}, #{quarantineReport}, #{certificate}, #{scenePicture},
            #{age}, #{etiology}, #{diagnosisResult}, #{medicationName}, #{medicationMethod}, #{clinic},
            #{treatmentTime}, #{treatmentCycle}, #{treatmentEffect}, #{conditionChange}, #{recoveryTime}, #{recoveryHealthStatus},
            #{recordNo}, #{sourceType}, #{taskId}, #{operationType}, #{batchId}, #{cattleIds},
            #{executorUserId}, #{executorName}, #{executeResult}, #{detailJson}, #{materialUsageJson}, #{attachments},
            #{exceptionFlag}, #{exceptionDesc}, #{remark},
            #{analyzedFlag}, #{lastSyncTime}, sysdate()
        )
    </insert>

    <update id="updateBizYakHealthSourceRecord" parameterType="com.ruoyi.web.modules.industryservice.domain.BizYakHealthSourceRecord">
        update biz_yak_health_source_record set
            record_time = #{recordTime},
            region_key = #{regionKey},
            farm_id = #{farmId},
            farm_name = #{farmName},
            enclosure_id = #{enclosureId},
            enclosure_name = #{enclosureName},
            cattle_id = #{cattleId},
            ear_tag_number = #{earTagNumber},
            yak_no = #{yakNo},
            source_create_time = #{sourceCreateTime},
            quarantine_unit_id = #{quarantineUnitId},
            quarantine_unit_name = #{quarantineUnitName},
            quarantine_personnel_id = #{quarantinePersonnelId},
            quarantine_personnel_name = #{quarantinePersonnelName},
            quarantine_item = #{quarantineItem},
            quarantine_result = #{quarantineResult},
            quarantine_report = #{quarantineReport},
            certificate = #{certificate},
            scene_picture = #{scenePicture},
            age = #{age},
            etiology = #{etiology},
            diagnosis_result = #{diagnosisResult},
            medication_name = #{medicationName},
            medication_method = #{medicationMethod},
            clinic = #{clinic},
            treatment_time = #{treatmentTime},
            treatment_cycle = #{treatmentCycle},
            treatment_effect = #{treatmentEffect},
            condition_change = #{conditionChange},
            recovery_time = #{recoveryTime},
            recovery_health_status = #{recoveryHealthStatus},
            record_no = #{recordNo},
            source_type = #{sourceType},
            task_id = #{taskId},
            operation_type = #{operationType},
            batch_id = #{batchId},
            cattle_ids = #{cattleIds},
            executor_user_id = #{executorUserId},
            executor_name = #{executorName},
            execute_result = #{executeResult},
            detail_json = #{detailJson},
            material_usage_json = #{materialUsageJson},
            attachments = #{attachments},
            exception_flag = #{exceptionFlag},
            exception_desc = #{exceptionDesc},
            remark = #{remark},
            last_sync_time = #{lastSyncTime},
            update_time = sysdate()
        where id = #{id}
    </update>

    <update id="markAnalyzedByIds">
        update biz_yak_health_source_record set analyzed_flag = '1', update_time = sysdate()
        where id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>
