<?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="followPlanMapper">
    <!-- 查询多个计划 -->
    <select id="selectFollowPlans" parameterType="pd" resultType="pd">
        select * from s_gxhpz_follow_plan where 1=1
        <if test="id != null">
            and id = #{id}
        </if>
        <if test="planId != null and planId != ''">
            and planId = #{planId}
        </if>

        <if test="productName != null and productName != ''">
            and productName = #{productName}
        </if>
        <if test="mdmCode != null and mdmCode != ''">
            and mdmCode = #{mdmCode}
        </if>
        <if test="specification != null and specification != ''">
            and specification = #{specification}
        </if>
        <if test="patientId != null">
            and patientId = #{patientId}
        </if>
        <if test="businessBelonging != null and businessBelonging != ''">
            and businessBelonging = #{businessBelonging}
        </if>
        <if test="planName != null and planName != ''">
            and planName = #{planName}
        </if>
        <if test="storeId != null">
            and storeId = #{storeId}
        </if>
        <!-- 省略其他字段条件... -->
    </select>


    <!-- 通过selectPlanByDrugsLinkId查询计划信息 -->
    <select id="selectPlanByDrugsLinkId" parameterType="pd" resultType="pd">
        select up.planId,up.specification,dr.productName,dr.mdmCode,dr.taskId from s_gxhpz_follow_plan up  left join s_gxhpz_follow_task dr on up.planId=dr.planId where 1=1
        <if test="drugsLinkId !=null and drugsLinkId !='' ">
            and dr.drugsLinkId = #{drugsLinkId}
        </if>
        <if test="storeId !=null">
            and up.storeId = #{storeId}
        </if>
        <if test="patientId !=null and patientId !='' ">
            and up.patientId = #{patientId}
        </if>

    </select>


    <!-- 根据药品编码和患者id查询计划 只能出现一条数据 -->
    <select id="selectPlanByCPB" parameterType="pd" resultType="pd">
        select p.*,DATE_FORMAT(p.updatedTime, '%Y-%m-%d %H:%i:%s')
            AS updatedTime1 ,DATE_FORMAT(p.createdTime, '%Y-%m-%d %H:%i:%s')
            AS createdTime1  from s_gxhpz_follow_plan  p where 1=1
        <if test="mdmCode != null and mdmCode != ''">
           and  p.mdmCode = #{mdmCode}
        </if>
        <if test="patientId != null and patientId != ''">
            and p.patientId = #{patientId}
        </if>
        <if test="businessBelonging != null and businessBelonging != ''">
            and p.businessBelonging=#{businessBelonging}
        </if>
    </select>
    <!-- 根据药品编码和患者id查询计划 多条数据-->
    <select id="selectPlanListByCPB" parameterType="pd" resultType="pd">
        select p.*,DATE_FORMAT(p.updatedTime, '%Y-%m-%d %H:%i:%s')
        AS updatedTime1 ,DATE_FORMAT(p.createdTime, '%Y-%m-%d %H:%i:%s')
        AS createdTime1  from s_gxhpz_follow_plan  p where 1=1
        <if test="mdmCode != null and mdmCode != ''">
            p.mdmCode = #{mdmCode}
        </if>
        <if test="patientId != null and patientId != ''">
            and p.patientId = #{patientId}
        </if>
        <if test="businessBelonging != null and businessBelonging != ''">
            and p.businessBelonging=#{businessBelonging}
        </if>
    </select>

    <!-- 根据ID查询单个计划 -->
    <select id="selectOneById" parameterType="pd" resultType="pd">
        select * from s_gxhpz_follow_plan where id = #{id}
    </select>

    <!-- 关闭计划 -->
    <update id="closeFollowPlan" 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 planId = #{planId}
    </update>

    <!-- 插入新计划 -->
    <insert id="insertFollowPlan" parameterType="pd">
        insert into s_gxhpz_follow_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productName != null and productName != ''">
                productName,
            </if>
            <if test="mdmCode != null and mdmCode != ''">
                mdmCode,
            </if>
            <if test="specification != null and specification != ''">
                specification,
            </if>
            <if test="patientId != null">
                patientId,
            </if>
            <if test="businessBelonging != null and businessBelonging != ''">
                businessBelonging,
            </if>
            <if test="planName != null and planName != ''">
                planName,
            </if>
            <if test="storeId != null">
                storeId,
            </if>
            <if test="drugsLinkId != null and drugsLinkId != ''">
                drugsLinkId,
            </if>
            <if test="storeName != null and storeName != ''">
                storeName,
            </if>
            <if test="templateId != null and templateId != ''">
                templateId,
            </if>
            <if test="taskId != null and taskId != ''">
                taskId,
            </if>
            <if test="planId != null and planId != ''">
                 planId,
            </if>
            <if test="follow_up_person != null and follow_up_person != ''">
                follow_up_person,
            </if>
            <if test="follow_up_person_id != null and follow_up_person_id != ''">
                follow_up_person_id,
            </if>
            <if test="createdBy != null and createdBy != ''">
                createdBy,
            </if>
            <if test="status != null">
                status,
            </if>
            <if test="updatedBy != null and updatedBy != ''">
                updatedBy,
            </if>
            <if test="createdTime != null">
                createdTime,
            </if>
            <if test="updatedTime != null and updatedTime != ''">
                updatedTime,
            </if>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productName != null and productName != ''">
                #{productName},
            </if>
            <if test="mdmCode != null and mdmCode != ''">
                #{mdmCode},
            </if>
            <if test="specification != null and specification != ''">
                #{specification},
            </if>
            <if test="patientId != null">
                #{patientId},
            </if>
            <if test="businessBelonging != null and businessBelonging != ''">
                #{businessBelonging},
            </if>
            <if test="planName != null and planName != ''">
                #{planName},
            </if>
            <if test="storeId != null">
                #{storeId},
            </if>
            <if test="drugsLinkId != null and drugsLinkId != ''">
                #{drugsLinkId},
            </if>
            <if test="storeName != null and storeName != ''">
                #{storeName},
            </if>
            <if test="templateId != null and templateId != ''">
                #{templateId},
            </if>
            <if test="taskId != null and taskId != ''">
                #{taskId},
            </if>
            <if test="planId != null and planId != ''">
                 #{planId},
            </if>
            <if test="follow_up_person != null and follow_up_person != ''">
                #{follow_up_person},
            </if>
            <if test="follow_up_person_id != null and follow_up_person_id != ''">
                #{follow_up_person_id},
            </if>
            <if test="createdBy != null and createdBy != ''">
                #{createdBy},
            </if>
            <if test="status != null">
                #{status},
            </if>
            <if test="updatedBy != null and updatedBy != ''">
                #{updatedBy},
            </if>
            <if test="createdTime != null">
                #{createdTime},
            </if>
            <if test="updatedTime != null and updatedTime != ''">
                #{updatedTime},
            </if>
        </trim>
    </insert>

    <!-- 更新计划 -->
    <update id="updateFollowPlan" parameterType="pd">
        update s_gxhpz_follow_plan
        <set>
            <if test="productName != null and productName != ''">
                productName = #{productName},
            </if>
            <if test="mdmCode != null and mdmCode != ''">
                mdmCode = #{mdmCode},
            </if>
            <if test="specification != null and specification != ''">
                specification = #{specification},
            </if>
            <if test="patientId != null">
                patientId = #{patientId},
            </if>
            <if test="businessBelonging != null and businessBelonging != ''">
                businessBelonging = #{businessBelonging},
            </if>
            <if test="planName != null and planName != ''">
                planName = #{planName},
            </if>
            <if test="storeId != null">
                storeId = #{storeId},
            </if>
            <if test="drugsLinkId != null and drugsLinkId != ''">
                drugsLinkId = #{drugsLinkId},
            </if>
            <if test="storeName != null and storeName != ''">
                storeName = #{storeName},
            </if>
            <if test="templateId != null and templateId != ''">
                templateId = #{templateId},
            </if>
            <if test="taskId != null and taskId != ''">
                taskId = #{taskId},
            </if>
            <if test="planId != null and planId != ''">
                 planId = #{planId},
            </if>
            <if test="follow_up_person != null and follow_up_person != ''">
                follow_up_person = #{follow_up_person},
            </if>
            <if test="follow_up_person_id != null and follow_up_person_id != ''">
                follow_up_person_id = #{follow_up_person_id},
            </if>
            <if test="createdBy != null and createdBy != ''">
                createdBy = #{createdBy},
            </if>
            <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 planId = #{planId}
    </update>

    <!-- 删除计划 -->
    <delete id="deleteFollowPlansByIds" parameterType="java.util.List">
        <if test="list != null and list.size() > 0">
            delete from s_gxhpz_follow_plan
            where id in
            <foreach item="id" index="index" collection="list" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </delete>
    <!-- 更据drugsLinkId删除任务 -->
    <delete id="deletePlanByDrugsLinkId" parameterType="pd">
        delete from s_gxhpz_follow_plan where drugsLinkId =#{drugsLinkId}
    </delete>
</mapper>
