<?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="followTaskMapper">



    <!-- 查询任务 -->
    <select id="selectFollowTasks" parameterType="pd" resultType="pd">
        select t.*,n.useForm ,n.followUpSubject,n.taskMaterial,n.taskActionTaskType from s_gxhpz_follow_task t left join s_gxhpz_follow_task_node n on t.taskId = n.taskId where 1=1
        <if test="id != null">
            and t.id = #{id}
        </if>
        <if test="taskActionTaskType != null and taskActionTaskType!=''">
            and n.taskActionTaskType = #{taskActionTaskType}
        </if>
        <if test="appointmentDate != null">
            and t.appointmentDate = #{appointmentDate}
        </if>
        <if test="subscribebeginTime != null and subscribebeginTime!='' and  subscribeendTime != null and subscribeendTime!=''">
            and t.appointmentDate between #{subscribebeginTime} and #{subscribeendTime}
        </if>
        <if test="businessBelonging != null and businessBelonging != ''">
            and t.businessBelonging = #{businessBelonging}
        </if>
        <if test="taskName != null and taskName != ''">
            and t.taskName = #{taskName}
        </if>
        <if test="patientId != null and patientId != ''">
            and t.patientId = #{patientId}
        </if>
        <if test="taskTheme != null and taskTheme != ''">
            and t.taskTheme = #{taskTheme}
        </if>
        <if test="taskFollower != null and taskFollower != ''">
            and t.taskFollower = #{taskFollower}
        </if>
        <if test="templeName != null and templeName != ''">
            and t.templeName = #{templeName}
        </if>
        <if test="planId != null and planId != ''">
            and t.planId = #{planId}
        </if>
        <if test="drugsLinkId != null and drugsLinkId != ''">
            and t.drugsLinkId = #{drugsLinkId}
        </if>
        <if test="patientName != null and patientName != ''">
            and t.patientName = #{patientName}
        </if>
        <if test="mdmCode != null and mdmCode != '' ">
            and t.mdmCode = #{mdmCode}
        </if>
        <if test="taskStatus != null and taskStatus != ''">
            and t.taskStatus = #{taskStatus}
        </if>
        <if test="query != null and query != ''">
            AND  (t.patientName LIKE CONCAT('%', #{query}, '%') OR t.patientId =#{query})
        </if>
        <if test="product != null and product != ''">
            AND (t.genericName LIKE CONCAT('%', #{product}, '%') OR t.productName LIKE CONCAT('%', #{product}, '%') OR t.mdmCode LIKE CONCAT('%', #{product}, '%'))
        </if>

        and t.taskStatus != '未下发'
        <!-- 省略其他字段条件... -->
    </select>

    <!-- 根据ID查询单个任务 -->
    <select id="selectOneById" parameterType="pd" resultType="pd">
        select t.*,
               mr.id as mrId,
               mr.combined_medication_adverse_reaction AS combinedMedicationAdverseReaction,
               mr.combined_medication_other_adverse_reactions AS combinedMedicationOtherAdverseReactions,
               mr.combined_medication_other_adverse_reactions_image AS combinedMedicationOtherAdverseReactionsImage,
               mr.combined_medication_other_adverse_reactions_handling AS combinedMedicationOtherAdverseReactionsHandling,
               mr.is_review AS isReview,
               mr.imaging_insurance_type AS imagingInsuranceType,
               mr.ct_check AS ctCheck,
               mr.ct_text AS ctText,
               mr.ultrasound_check AS ultrasoundCheck,
               mr.ultrasound_text AS ultrasoundText,
               mr.mri_check AS mriCheck,
               mr.mri_text AS mriText,
               mr.other_imaging_examinations AS otherImagingExaminations,
               mr.imaging_examinations_image AS imagingExaminationsImage,
               mr.tumor_marker_check AS tumorMarkerCheck,
               mr.cea AS cea,
               mr.afp AS afp,
               mr.ca_199 AS ca199,
               mr.ca125 AS ca125,
               mr.ca153 AS ca153,
               mr.hcg AS hcg,
               mr.nse AS nse,
               mr.cyfra21_1 AS cyfra211,
               mr.tg AS tg,
               mr.psa AS psa,
               mr.tumor_marker_image AS tumorMarkerImage,
               mr.lab_test_indicators AS labTestIndicators,
               mr.lab_test_indicators_image AS labTestIndicatorsImage,
               mr.treatment_type AS treatmentType,
               mr.pre_medication_plan AS preMedicationPlan,
               mr.current_treatment_plan AS currentTreatmentPlan,
               mr.post_progression_treatment_plan AS postProgressionTreatmentPlan,
               mr.has_medication_indications AS hasMedicationIndications,
               mr.drug_effectiveness AS drugEffectiveness,
               mr.clinical_interaction AS clinicalInteraction,
               mr.clinical_interaction_description AS clinicalInteractionDescription,
               mr.unnecessary_repeated_medication AS unnecessaryRepeatedMedication,
               mr.repeated_medication_first_use_date AS repeatedMedicationFirstUseDate,
               mr.unnecessary_repeated_medication_description AS unnecessaryRepeatedMedicationDescription,
               mr.medication_error AS medicationError,
               mr.medication_error_time AS medicationErrorTime,
               mr.medication_error_description AS medicationErrorDescription,
               mr.medication_record AS medicationRecord,
               mr.combined_medication_record AS combinedMedicationRecord,
               mr.pain_nrs_score AS painNrsScore,
               mr.physical_condition_score AS physicalConditionScore,
               mr.is_consultation AS isConsultation,
               mr.consultation_type AS consultationType,
               mr.specific_question AS specificQuestion,
               mr.pharmacist_response AS pharmacistResponse,
               mr.patient_condition_assessment AS patientConditionAssessment,
               mr.project_description AS projectDescription,
               p.name,
               p.dateBirth,
               p.age,
               p.gender,
               p.realNameStatus,
               p.id as patientId,
               p.phoneNumber,
               p.address,p.idCardImg,
               p.documentNumber,
               p.documentType,
               p.contactPhone,
               p.contactName,
               p.addr,
               p.dl,
               n.useForm ,
               n.templateId ,
               n.followUpSubject,
               n.taskMaterial,
               n.taskValidity,
               DATE_FORMAT(t.appointmentDate, '%Y-%m-%d %H:%i:%s') AS appointmentDate1,
               DATE_FORMAT(t.next_follow_time, '%Y-%m-%d %H:%i:%s') AS next_follow_time2,
            DATE_FORMAT(t.timeFirstDiagnosis, '%Y-%m-%d') AS timeFirstDiagnosis2,
        DATE_FORMAT(p.timeFirstDiagnosis, '%Y-%m-%d %H:%i:%s') AS FirstDiagnosis
        from s_gxhpz_follow_task t
            left join s_gxhpz_follow_up_records mr on t.id = mr.taskId
            left join s_dtp_ysfw_archive_management p on t.patientId = p.id
            left join s_gxhpz_follow_task_node n on t.taskId = n.taskId
            where t.id = #{id}
    </select>


    <!-- 停止任务 -->
    <update id="stopFollowTask" parameterType="pd">
        update s_gxhpz_follow_task
        <set>
            <if test="taskStatus != null">
                taskStatus = #{taskStatus},
            </if>
            <if test="updatedAt != null and updatedAt != ''">
                updatedAt = #{updatedAt},
            </if>
            <if test="operator != null and operator != ''">
                operator = #{operator},
            </if>
        </set>
        where planId = #{planId}
    </update>
    <!-- 关闭任务 -->
    <update id="closeTask" parameterType="pd">
        update s_gxhpz_follow_task
        <set>
            <if test="taskStatus != null">
                taskStatus = #{taskStatus},
            </if>
            <if test="updatedAt != null and updatedAt != ''">
                updatedAt = #{updatedAt},
            </if>
            <if test="operator != null and operator != ''">
                operator = #{operator},
            </if>
        </set>
        where id = #{id}
    </update>

    <!-- 插入新任务 -->
    <insert id="insertFollowTask" parameterType="pd">
        insert into s_gxhpz_follow_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="templeName != null and templeName != ''">
                 templeName,
            </if>
            <if test="appointmentDate != null">
                appointmentDate,
            </if>
            <if test="businessBelonging != null and businessBelonging != ''">
                businessBelonging,
            </if>
            <if test="taskName != null and taskName != ''">
                taskName,
            </if>
            <if test="taskTheme != null and taskTheme != ''">
                taskTheme,
            </if>
            <if test="planId != null and planId != ''">
                planId,
            </if>
            <if test="taskId != null and taskId != ''">
                taskId,
            </if>
            <if test="patientId != null and patientId != ''">
                patientId,
            </if>
            <if test="patientName != null and patientName != ''">
                patientName,
            </if>
            <if test="gender != null">
                gender,
            </if>
            <if test="age != null">
                age,
            </if>
            <!-- 省略其他字段... -->
            <!-- 新增字段 -->
            <if test="followUpSummary != null and followUpSummary != ''">
                followUpSummary,
            </if>
            <if test="followUpSummaryid != null and followUpSummaryid != ''">
                followUpSummaryid,
            </if>
            <if test="disease != null and disease != ''">
                disease,
            </if>
            <if test="storeName != null and storeName != ''">
                storeName,
            </if>
            <if test="storeId != null">
                storeId,
            </if>
            <if test="drugsLinkId != null and drugsLinkId != ''">
                 drugsLinkId,
            </if>
            <if test="nodeId != null and nodeId != ''">
                nodeId,
            </if>
            <if test="genericName != null and genericName != ''">
                genericName,
            </if>
            <if test="productName != null and productName != ''">
                productName,
            </if>
            <if test="mdmCode != null and mdmCode != ''">
                mdmCode,
            </if>
            <if test="taskFollower != null and taskFollower != ''">
                taskFollower,
            </if>
            <if test="returnMethod != null and returnMethod != ''">
                returnMethod,
            </if>
            <if test="iscoordinate != null and iscoordinate != ''">
                iscoordinate,
            </if>
            <if test="InterviewRecord != null and InterviewRecord != ''">
                InterviewRecord,
            </if>
            <if test="timeFirstDiagnosis != null and timeFirstDiagnosis != ''">
                timeFirstDiagnosis,
            </if>
            <if test="permanentStopTime != null">
                permanentStopTime,
            </if>
            <if test="returnObject != null and returnObject != ''">
                returnObject,
            </if>
            <if test="returnImgUrl != null and returnImgUrl != ''">
                returnImgUrl,
            </if>
            <if test="medicationStatus != null and medicationStatus != ''">
                medicationStatus,
            </if>
            <if test="stoped != null">
                stoped,
            </if>
            <if test="charitable != null and charitable != ''">
                charitable,
            </if>
            <if test="perpetual_stopdrug_cause != null and perpetual_stopdrug_cause != ''">
                perpetual_stopdrug_cause,
            </if>
            <if test="other_channels != null and other_channels != ''">
                other_channels,
            </if>
            <if test="delay_purchase_doctor != null and delay_purchase_doctor != ''">
                delay_purchase_doctor,
            </if>
            <if test="delayed_purchase_drugs != null and delayed_purchase_drugs != ''">
                delayed_purchase_drugs,
            </if>
            <if test="is_adverse_reaction != null and is_adverse_reaction != ''">
                is_adverse_reaction,
            </if>
            <if test="adverse_reaction_time != null">
                adverse_reaction_time,
            </if>
            <if test="good_response_symptom_id != null and good_response_symptom_id != ''">
                good_response_symptom_id,
            </if>
            <if test="tar != null and tar != ''">
                tar,
            </if>
            <if test="nutrition_evaluation_id != null and nutrition_evaluation_id != ''">
                nutrition_evaluation_id,
            </if>
            <if test="medication_record_id != null and medication_record_id != ''">
                medication_record_id,
            </if>
            <if test="other != null and other != ''">
                other,
            </if>
            <if test="actualFollowUpTime != null">
                actualFollowUpTime,
            </if>
            <if test="callConnectedCount != null">
                callConnectedCount,
            </if>
            <if test="nextCallTime != null">
                nextCallTime,
            </if>
            <if test="lastOutboundFlag != null and lastOutboundFlag != ''">
                lastOutboundFlag,
            </if>
            <if test="outboundCallCount != null">
                outboundCallCount,
            </if>
            <if test="nextOutboundCallCount != null">
                nextOutboundCallCount,
            </if>
            <if test="taskStatus != null and taskStatus != ''">
                taskStatus,
            </if>
            <if test="completionTime != null">
                completionTime,
            </if>
            <if test="lastOutboundStatus != null and lastOutboundStatus != ''">
                lastOutboundStatus,
            </if>
            <if test="operator != null and operator != ''">
                operator,
            </if>
            <if test="interval_this_time != null">
                interval_this_time,
            </if>

            <if test="next_taskTheme != null and next_taskTheme != ''">
                next_taskTheme,
            </if>
            <if test="next_follow_time != null">
                next_follow_time,
            </if>
            <if test="createdAt != null">
                createdAt,
            </if>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <!-- 基础字段 -->
            <if test="templeName != null and templeName != ''">
                #{templeName},
            </if>
            <if test="appointmentDate != null">
                #{appointmentDate},
            </if>
            <if test="businessBelonging != null and businessBelonging != ''">
                #{businessBelonging},
            </if>
            <if test="taskName != null and taskName != ''">
                #{taskName},
            </if>
            <if test="taskTheme != null and taskTheme != ''">
                #{taskTheme},
            </if>
            <if test="planId != null and planId != ''">
                #{planId},
            </if>
            <if test="taskId != null and taskId != ''">
                #{taskId},
            </if>
            <if test="patientId != null and patientId != ''">
                #{patientId},
            </if>
            <if test="patientName != null and patientName != ''">
                #{patientName},
            </if>
            <if test="gender != null">
                #{gender},
            </if>
            <if test="age != null">
                #{age},
            </if>

            <!-- 新增字段 -->
            <if test="followUpSummary != null and followUpSummary != ''">
                #{followUpSummary},
            </if>
            <if test="followUpSummaryid != null and followUpSummaryid != ''">
                #{followUpSummaryid},
            </if>

            <if test="disease != null and disease != ''">
                #{disease},
            </if>
            <if test="storeName != null and storeName != ''">
                #{storeName},
            </if>
            <if test="storeId != null">
                #{storeId},
            </if>
            <if test="drugsLinkId != null and drugsLinkId != ''">
                #{drugsLinkId},
            </if>
            <if test="nodeId != null and nodeId != ''">
                #{nodeId},
            </if>
            <if test="genericName != null and genericName != ''">
                #{genericName},
            </if>
            <if test="productName != null and productName != ''">
                #{productName},
            </if>
            <if test="mdmCode != null and mdmCode != ''">
                #{mdmCode},
            </if>
            <if test="taskFollower != null and taskFollower != ''">
                #{taskFollower},
            </if>
            <if test="returnMethod != null and returnMethod != ''">
                #{returnMethod},
            </if>
            <if test="iscoordinate != null and iscoordinate != ''">
                #{iscoordinate},
            </if>
            <if test="InterviewRecord != null and InterviewRecord != ''">
                #{InterviewRecord},
            </if>
            <if test="timeFirstDiagnosis != null and timeFirstDiagnosis != ''">
                #{timeFirstDiagnosis},
            </if>
            <if test="permanentStopTime != null">
                #{permanentStopTime},
            </if>

            <if test="returnObject != null and returnObject != ''">
                #{returnObject},
            </if>
            <if test="returnImgUrl != null and returnImgUrl != ''">
                #{returnImgUrl},
            </if>
            <if test="medicationStatus != null and medicationStatus != ''">
                #{medicationStatus},
            </if>
            <if test="stoped != null">
                #{stoped},
            </if>
            <if test="charitable != null and charitable != ''">
                #{charitable},
            </if>
            <if test="perpetual_stopdrug_cause != null and perpetual_stopdrug_cause != ''">
                #{perpetual_stopdrug_cause},
            </if>
            <if test="other_channels != null and other_channels != ''">
                #{other_channels},
            </if>
            <if test="delay_purchase_doctor != null and delay_purchase_doctor != ''">
                #{delay_purchase_doctor},
            </if>
            <if test="delayed_purchase_drugs != null and delayed_purchase_drugs != ''">
                #{delayed_purchase_drugs},
            </if>
            <if test="is_adverse_reaction != null and is_adverse_reaction != ''">
                #{is_adverse_reaction},
            </if>
            <if test="adverse_reaction_time != null">
                #{adverse_reaction_time},
            </if>
            <if test="good_response_symptom_id != null and good_response_symptom_id != ''">
                #{good_response_symptom_id},
            </if>
            <if test="tar != null and tar != ''">
                #{tar},
            </if>
            <if test="nutrition_evaluation_id != null and nutrition_evaluation_id != ''">
                #{nutrition_evaluation_id},
            </if>
            <if test="medication_record_id != null and medication_record_id != ''">
                #{medication_record_id},
            </if>
            <if test="other != null and other != ''">
                #{other},
            </if>
            <if test="actualFollowUpTime != null">
                #{actualFollowUpTime},
            </if>
            <if test="callConnectedCount != null">
                #{callConnectedCount},
            </if>
            <if test="nextCallTime != null">
                #{nextCallTime},
            </if>
            <if test="lastOutboundFlag != null and lastOutboundFlag != ''">
                #{lastOutboundFlag},
            </if>
            <if test="outboundCallCount != null">
                #{outboundCallCount},
            </if>
            <if test="nextOutboundCallCount != null">
                #{nextOutboundCallCount},
            </if>
            <if test="taskStatus != null and taskStatus != ''">
                #{taskStatus},
            </if>
            <if test="completionTime != null">
                #{completionTime},
            </if>
            <if test="lastOutboundStatus != null and lastOutboundStatus != ''">
                #{lastOutboundStatus},
            </if>
            <if test="operator != null and operator != ''">
                #{operator},
            </if>
            <if test="interval_this_time != null">
                #{interval_this_time},
            </if>
            <if test="next_taskTheme != null and next_taskTheme != ''">
                #{next_taskTheme},
            </if>
            <if test="next_follow_time != null">
                #{next_follow_time},
            </if>
            <if test="createdAt != null">
                #{createdAt},
            </if>

        </trim>
    </insert>


    <!-- 更新任务 -->
    <update id="updateFollowTask" parameterType="pd">
        update s_gxhpz_follow_task
        <set>
            <if test="templeName != null and templeName != ''">
                templeName = #{templeName},
            </if>
            <if test="appointmentDate != null">
                appointmentDate = #{appointmentDate},
            </if>
            <if test="businessBelonging != null and businessBelonging != ''">
                businessBelonging = #{businessBelonging},
            </if>
            <if test="taskName != null and taskName != ''">
                taskName = #{taskName},
            </if>
            <if test="taskTheme != null and taskTheme != ''">
                taskTheme = #{taskTheme},
            </if>
            <if test="planId != null and planId != ''">
                planId = #{planId},
            </if>
            <if test="taskId != null and taskId != ''">
                taskId = #{taskId},
            </if>
            <if test="nodeId != null and nodeId != ''">
                nodeId = #{nodeId},
            </if>
            <if test="patientId != null and patientId != ''">
                patientId = #{patientId},
            </if>
            <if test="patientName != null and patientName != ''">
                patientName = #{patientName},
            </if>
            <if test="gender != null">
                gender = #{gender},
            </if>
            <if test="age != null">
                age = #{age},
            </if>
            <!-- 省略其他字段... -->
            <!-- 新增字段 -->
            <if test="followUpSummary != null and followUpSummary != ''">
                followUpSummary = #{followUpSummary},
            </if>
            <if test="followUpSummaryid != null and followUpSummaryid != ''">
                followUpSummaryid = #{followUpSummaryid},
            </if>

            <if test="disease != null and disease != ''">
                disease = #{disease},
            </if>
            <if test="storeName != null and storeName != ''">
                storeName = #{storeName},
            </if>
            <if test="storeId != null">
                storeId = #{storeId},
            </if>
            <if test="drugsLinkId != null and drugsLinkId != ''">
                drugsLinkId = #{drugsLinkId},
            </if>
            <if test="genericName != null and genericName != ''">
                genericName = #{genericName},
            </if>
            <if test="productName != null and productName != ''">
                productName = #{productName},
            </if>
            <if test="mdmCode != null and mdmCode != ''">
                mdmCode = #{mdmCode},
            </if>
            <if test="taskFollower != null and taskFollower != ''">
                taskFollower = #{taskFollower},
            </if>
            <if test="returnMethod != null and returnMethod != ''">
                returnMethod = #{returnMethod},
            </if>
            <if test="iscoordinate != null and iscoordinate != ''">
                iscoordinate = #{iscoordinate},
            </if>
            <if test="InterviewRecord != null and InterviewRecord != ''">
                InterviewRecord = #{InterviewRecord},
            </if>
            <if test="timeFirstDiagnosis != null and timeFirstDiagnosis != ''">
                timeFirstDiagnosis = #{timeFirstDiagnosis},
            </if>
            <if test="permanentStopTime != null and permanentStopTime != ''">
                permanentStopTime = #{permanentStopTime},
            </if>
            <if test="returnObject != null and returnObject != ''">
                returnObject = #{returnObject},
            </if>
            <if test="returnImgUrl != null and returnImgUrl != ''">
                returnImgUrl = #{returnImgUrl},
            </if>
            <if test="medicationStatus != null and medicationStatus != ''">
                medicationStatus = #{medicationStatus},
            </if>
            <if test="stoped != null">
                stoped = #{stoped},
            </if>
            <if test="charitable != null and charitable != ''">
                charitable = #{charitable},
            </if>
            <if test="perpetual_stopdrug_cause != null and perpetual_stopdrug_cause != ''">
                perpetual_stopdrug_cause = #{perpetual_stopdrug_cause},
            </if>
            <if test="other_channels != null and other_channels != ''">
                other_channels = #{other_channels},
            </if>
            <if test="delay_purchase_doctor != null and delay_purchase_doctor != ''">
                delay_purchase_doctor = #{delay_purchase_doctor},
            </if>
            <if test="delayed_purchase_drugs != null and delayed_purchase_drugs != ''">
                delayed_purchase_drugs = #{delayed_purchase_drugs},
            </if>
            <if test="is_adverse_reaction != null and is_adverse_reaction != ''">
                is_adverse_reaction = #{is_adverse_reaction},
            </if>
            <if test="adverse_reaction_time != null and adverse_reaction_time != ''">
                adverse_reaction_time = #{adverse_reaction_time},
            </if>
            <if test="good_response_symptom_id != null and good_response_symptom_id != ''">
                good_response_symptom_id = #{good_response_symptom_id},
            </if>
            <if test="tar != null and tar != ''">
                tar = #{tar},
            </if>
            <if test="nutrition_evaluation_id != null and nutrition_evaluation_id != ''">
                nutrition_evaluation_id = #{nutrition_evaluation_id},
            </if>
            <if test="medication_record_id != null and medication_record_id != ''">
                medication_record_id = #{medication_record_id},
            </if>
            <if test="other != null and other != ''">
                other = #{other},
            </if>
            <if test="actualFollowUpTime != null">
                actualFollowUpTime = #{actualFollowUpTime},
            </if>
            <if test="interval_this_time != null and interval_this_time != ''">
                interval_this_time = #{interval_this_time},
            </if>

            <if test="next_taskTheme != null and next_taskTheme != ''">
                next_taskTheme = #{next_taskTheme},
            </if>
            <if test="next_follow_time != null and next_follow_time != ''">
                next_follow_time = #{next_follow_time},
            </if>
            <if test="callConnectedCount != null">
                callConnectedCount = #{callConnectedCount},
            </if>
            <if test="nextCallTime != null">
                nextCallTime = #{nextCallTime},
            </if>
            <if test="lastOutboundFlag != null and lastOutboundFlag != ''">
                lastOutboundFlag = #{lastOutboundFlag},
            </if>
            <if test="outboundCallCount != null">
                outboundCallCount = #{outboundCallCount},
            </if>
            <if test="nextOutboundCallCount != null">
                nextOutboundCallCount = #{nextOutboundCallCount},
            </if>
            <if test="taskStatus != null and taskStatus != ''">
                taskStatus = #{taskStatus},
            </if>
            <if test="completionTime != null">
                completionTime = #{completionTime},
            </if>
            <if test="lastOutboundStatus != null and lastOutboundStatus != ''">
                lastOutboundStatus = #{lastOutboundStatus},
            </if>
            <if test="updatedAt != null">
                updatedAt = #{updatedAt},
            </if>
            <if test="operator != null and operator != ''">
                operator= #{operator},
            </if>
        </set>
        where id = #{id}
    </update>

    <!-- 删除任务 -->
    <delete id="deleteFollowTasksByIds" parameterType="java.util.List">
        <if test="list != null and list.size() > 0">
            delete from s_gxhpz_follow_task
            where id in
            <foreach item="id" index="index" collection="list" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </delete>


    <!-- 更据drugsLinkId删除任务 -->
    <delete id="deleteTaskByDrugsLinkId" parameterType="pd">
        delete from s_gxhpz_follow_task where drugsLinkId =#{drugsLinkId}
    </delete>

    <!-- 更据计划planId 任务taskId 删除job任务 -->
    <delete id="deleteSysJobByPlanId" parameterType="pd">
        delete from sys_job where plan_id =#{plan_id}
    </delete>
    <!--查询定时任务-->
    <select id="getJobByTaskId" parameterType="pd" resultType="pd">
        select  * from sys_job where task_id = #{taskId} ORDER BY create_time DESC
            LIMIT 1;
    </select>
    <!--查询模版节点配置的任务的有效期天数-->
    <select id="getNodeConfigByNodeId" parameterType="pd" resultType="pd">
        select  * from s_dtp_yypz_nodeconfig where id = #{nodeId}
    </select>
    <!--根据患者id和药查询计划-->
    <select id="getPlanByMdmCode" parameterType="pd" resultType="pd">
        select  * from s_gxhpz_follow_plan where mdmCode = #{mdmCode} and storeId = #{storeId} and patientId= #{patientId}
    </select>
    <!--根据计划id查询下面的所有任务-->
    <select id="getTaskByPlanId" parameterType="pd" resultType="pd">
        select  * from s_gxhpz_follow_task where planId = #{planId} and storeId = #{storeId} and patientId= #{patientId}
    </select>
    <!--关闭任务-->
    <update id="updateJobByTaskId" parameterType="SysJob">
        update sys_job
        <set>
            <if test="status !=null">status = #{status},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate(),
        </set>
        where task_id = #{taskId}
    </update>
    <!--关闭患者当前所有计划-->
    <update id="closePatientCurrentPlan" parameterType="SysJob">
        update s_gxhpz_follow_plan
        <set>
            <if test="status !=null">status = #{status},</if>
            <if test="updateBy != null and updateBy != ''">updatedBy = #{updateBy},</if>
            updatedTime = sysdate(),
        </set>
        where   patientId= #{patientId}  and storeId= #{storeId}
    </update>
    <!--关闭患者当前所有计划-->
    <update id="closePatientCurrentTask" parameterType="SysJob">
        update s_gxhpz_follow_task
        <set>
            <if test="taskStatus !=null">taskStatus = #{taskStatus},</if>
            <if test="operator != null and operator != ''">operator = #{operator},</if>
            updatedAt = sysdate(),
        </set>
        where   patientId= #{patientId}  and storeId= #{storeId}
    </update>

    <!--关闭计划更据类型z-->
    <update id="closeByPlanTypePlan" parameterType="SysJob">
        update s_gxhpz_follow_plan
        <set>
            <if test="status !=null">status = #{status},</if>
            <if test="updateBy != null and updateBy != ''">updatedBy = #{updateBy},</if>
            updatedTime = sysdate(),
        </set>
        where   patientId= #{patientId} and businessBelonging= #{businessBelonging} and storeId= #{storeId}
    </update>
    <!--关闭任务根类型z-->
    <update id="closeTaskByTypePlan" parameterType="SysJob">
        update s_gxhpz_follow_task
        <set>
            <if test="taskStatus !=null">taskStatus = #{taskStatus},</if>
            <if test="operator != null and operator != ''">operator = #{operator},</if>
            updatedAt = sysdate(),
        </set>
        where   patientId= #{patientId} and businessBelonging= #{businessBelonging} and storeId= #{storeId}
    </update>


    <!--关闭计划根据药z-->
    <update id="closePLanCurrentDrugsPlan" parameterType="SysJob">
        update s_gxhpz_follow_plan
        <set>
            <if test="status !=null">status = #{status},</if>
            <if test="updateBy != null and updateBy != ''">updatedBy = #{updateBy},</if>
            updatedTime = sysdate(),
        </set>
        where  mdmCode = #{mdmCode} and patientId= #{patientId}  and storeId= #{storeId}
    </update>
    <!--关闭任务根据药z-->
    <update id="closeTaskCurrentDrugsPlan" parameterType="SysJob">
        update s_gxhpz_follow_task
        <set>
            <if test="taskStatus !=null">taskStatus = #{taskStatus},</if>
            <if test="operator != null and operator != ''">operator = #{operator},</if>
            updatedAt = sysdate(),
        </set>
        where mdmCode = #{mdmCode} and patientId= #{patientId}  and storeId= #{storeId}
    </update>

    <!--关闭定时任务表的计划根据指定药查询出来的计划z-->
    <update id="updateJobByDrugsClosePlanId" parameterType="SysJob">
        update sys_job
        <set>
            <if test="status !=null">status = #{status},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate(),
        </set>
        where plan_id = #{planId}
    </update>

    <!--关闭定时任务计划-->
    <update id="updateJobByPlanId" parameterType="SysJob">
        update sys_job
            <set>
                <if test="status !=null">status = #{status},</if>
                <if test="remark != null and remark != ''">remark = #{remark},</if>
                <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
                update_time = sysdate(),
            </set>
        where plan_id = #{planId}
    </update>
    <!--关闭该患者的所有定时任务-->
    <update id="updateJobByPatientId" parameterType="SysJob">
        update sys_job
        <set>
            <if test="status !=null">status = #{status},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate(),
        </set>
        where store_id = #{storeId} and patient_id = #{patientId}
    </update>
    <!-- 关闭该患者的所有计划 -->
    <update id="closeFollowPlanByPatientId" parameterType="pd">
        update s_gxhpz_follow_plan
        <set>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="updatedBy != null and updatedBy != ''">
                updatedBy = #{updatedBy},
            </if>
            <if test="updatedTime != null and updatedTime != ''">
                updatedTime = #{updatedTime},
            </if>
        </set>
        where storeId = #{storeId} and patientId = #{patientId}

    </update>

    <!-- 关闭该患者的所有任务 -->
    <update id="stopFollowTaskByPatientId" parameterType="pd">
        update s_gxhpz_follow_task
        <set>
            <if test="taskStatus != null">
                taskStatus = #{taskStatus},
            </if>
            <if test="updatedAt != null and updatedAt != ''">
                updatedAt = #{updatedAt},
            </if>
            <if test="operator != null and operator != ''">
                operator = #{operator},
            </if>
        </set>
        where storeId = #{storeId} and patientId = #{patientId}

    </update>
    <!-- 关闭任务taskid -->
    <update id="closeTaskByTaskId" parameterType="pd">
        update s_gxhpz_follow_task
        <set>
            <if test="taskStatus != null">
                taskStatus = #{taskStatus},
            </if>
            <if test="updatedAt != null and updatedAt != ''">
                updatedAt = #{updatedAt},
            </if>
            <if test="operator != null and operator != ''">
                operator = #{operator},
            </if>
        </set>
        where storeId = #{storeId} and patientId = #{patientId} and taskId=#{taskId}

    </update>





    <!-- 开启生成的任务 -->
    <update id="openTaskByTaskId" parameterType="pd">
        update s_gxhpz_follow_task
        <set>
            <if test="taskStatus != null">
                taskStatus = #{taskStatus},
            </if>
            <if test="updatedAt != null and updatedAt != ''">
                updatedAt = #{updatedAt},
            </if>
            <if test="operator != null and operator != ''">
                operator = #{operator},
            </if>
            <if test="appointmentDate != null and appointmentDate != ''">
                appointmentDate = #{appointmentDate},
            </if>

        </set>
        where storeId = #{storeId} and patientId = #{patientId} and taskId=#{taskId}

    </update>
    <!-- 激活创建的计划 -->
    <update id="openPlanByPlanId" parameterType="pd">
        update s_gxhpz_follow_plan
        <set>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="updatedTime != null and updatedTime != ''">
                updatedTime = #{updatedTime},
            </if>
            <if test="updatedBy != null and updatedBy   != ''">
                updatedBy = #{updatedBy},
            </if>
        </set>
        where storeId = #{storeId} and patientId = #{patientId} and planId=#{planId}

    </update>

    <!-- 更新job表的任务执行次数 -->
    <update id="updateJobStatusByTaskId" parameterType="pd">
        update sys_job
        <set>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="currentExecutionCount != null">
                currentExecutionCount = #{currentExecutionCount},
            </if>
            <if test="remainingExecutions != null">
                remainingExecutions = #{remainingExecutions},
            </if>
            <if test="updateTime != null and updateTime != ''">
                update_time = #{updateTime},
            </if>

            <if test="updateBy != null and updateBy != ''">
                update_by = #{updateBy},
            </if>
        </set>
        where store_id = #{storeId} and patient_id = #{patientId} and task_id=#{taskId}

    </update>
</mapper>
