<?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.xinelu.familydoctor.applet.mapper.ResidentRescindApplyMapper">

    <resultMap id="BaseResultMap" type="com.xinelu.familydoctor.applet.pojo.entity.ResidentRescindApplyEntity">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="applyNo" column="apply_no" jdbcType="VARCHAR"/>
            <result property="patientId" column="patient_id" jdbcType="VARCHAR"/>
            <result property="signNo" column="sign_no" jdbcType="VARCHAR"/>
            <result property="identity" column="identity" jdbcType="VARCHAR"/>
            <result property="orgNo" column="org_no" jdbcType="VARCHAR"/>
            <result property="orgName" column="org_name" jdbcType="VARCHAR"/>
            <result property="teamNo" column="team_no" jdbcType="VARCHAR"/>
            <result property="teamName" column="team_name" jdbcType="VARCHAR"/>
            <result property="userNo" column="user_no" jdbcType="VARCHAR"/>
            <result property="userName" column="user_name" jdbcType="VARCHAR"/>
            <result property="applyTime" column="apply_time" jdbcType="TIMESTAMP"/>
            <result property="bookingStatus" column="booking_status" jdbcType="VARCHAR"/>
            <result property="cancelTime" column="cancel_time" jdbcType="TIMESTAMP"/>
            <result property="rescindType" column="rescind_type" jdbcType="VARCHAR"/>
            <result property="rescindReason" column="rescind_reason" jdbcType="VARCHAR"/>
            <result property="approvalStatus" column="approval_status" jdbcType="VARCHAR"/>
            <result property="refuseReason" column="refuse_reason" jdbcType="VARCHAR"/>
            <result property="approvalTime" column="approval_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,apply_no,patient_id,sign_no,identity,
        org_no,org_name,team_no,
        team_name,user_no,user_name,
        apply_time,booking_status,cancel_time,rescind_type,rescind_reason,
        approval_status,refuse_reason,approval_time
    </sql>
    <sql id="Base_Column_List_fd">
        p.id,p.apply_no,p.patient_id,p.sign_no,p.identity,
        p.org_no,p.org_name,p.team_no,
        p.team_name,p.user_no,p.user_name,
        p.apply_time,booking_status,cancel_time,p.rescind_type,p.rescind_reason,
        p.approval_status,p.refuse_reason,p.approval_time
    </sql>

    <!-- 取消解约申请 -->
    <update id="cancel">
        update resident_rescind_apply set booking_status = '1', cancel_time = now() where apply_no = #{applyNo}
    </update>

    <select id="findByBody" resultType="com.xinelu.familydoctor.applet.pojo.vo.ResidentRescindApplyVo">
        select <include refid="Base_Column_List_fd"></include>,b.patient_name as residentName,b.address,b.phone
        from resident_rescind_apply p
        left join patient_info b on p.identity = b.card_no
        where 1=1
        <if test="identity != null and identity != ''">
            and p.identity = #{identity}
        </if>
        <if test="residentName != null and residentName != ''">
            and b.patient_name like concat(#{residentName},'%')
        </if>
        <if test="orgNo != null and orgNo != ''">
            and p.org_no = #{orgNo}
        </if>
        <if test="teamNo != null and teamNo != ''">
            and p.team_no = #{teamNo}
        </if>
        <if test="userNo != null and userNo != ''">
            and p.user_no = #{userNo}
        </if>
        <if test="startDate != null">
            and p.apply_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and p.apply_time &lt;= #{endDate}
        </if>
        <if test="patientId != null and patientId != ''">
            and p.patient_id = #{patientId}
        </if>
        <if test="bookingStatus != null and bookingStatus != ''">
            and p.booking_status = #{bookingStatus}
        </if>
        <if test="approvalStatus != null and approvalStatus != ''">
            and p.approval_status = #{approvalStatus}
        </if>
        <if test="address != null and address != ''">
            and b.address like concat('%', #{address},'%')
        </if>
        order by p.apply_time desc
    </select>

    <!-- 解约申请详情 -->
    <select id="detail" resultType="com.xinelu.familydoctor.applet.pojo.vo.ResidentRescindApplyVo">
        select <include refid="Base_Column_List"></include> from resident_rescind_apply where apply_no = #{applyNo} limit 1
    </select>

    <insert id="insert">
        insert into resident_rescind_apply
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="applyNo != null">apply_no,
            </if>
            <if test="patientId != null and patientId != ''">patient_id,
            </if>
            <if test="signNo != null and signNo != ''">sign_no,
            </if>
            <if test="identity != null and identity != ''">identity,
            </if>
            <if test="orgNo != null and orgNo != ''">org_no,
            </if>
            <if test="orgName != null and orgName != ''">org_name,
            </if>
            <if test="teamNo != null and teamNo != ''">team_no,
            </if>
            <if test="teamName != null and teamName != ''">team_name,
            </if>
            <if test="userNo != null and userNo != ''">user_no,
            </if>
            <if test="userName != null and userName != ''">user_name,
            </if>
            <if test="applyTime != null">apply_time,
            </if>
            <if test="bookingStatus != null and bookingStatus != ''">booking_status,
            </if>
            <if test="cancelTime != null">cancel_time,
            </if>
            <if test="rescindType != null and rescindType != ''">rescind_type,
            </if>
            <if test="rescindReason != null and rescindReason != ''">rescind_reason,
            </if>
            <if test="approvalStatus != null and approvalStatus != ''">approval_status,
            </if>
            <if test="refuseReason != null">refuse_reason,
            </if>
            <if test="approvalTime != null">approval_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="applyNo != null">#{applyNo},
            </if>
            <if test="patientId != null and patientId != ''">#{patientId},
            </if>
            <if test="signNo != null and signNo != ''">#{signNo},
            </if>
            <if test="identity != null and identity != ''">#{identity},
            </if>
            <if test="orgNo != null and orgNo != ''">#{orgNo},
            </if>
            <if test="orgName != null and orgName != ''">#{orgName},
            </if>
            <if test="teamNo != null and teamNo != ''">#{teamNo},
            </if>
            <if test="teamName != null and teamName != ''">#{teamName},
            </if>
            <if test="userNo != null and userNo != ''">#{userNo},
            </if>
            <if test="userName != null and userName != ''">#{userName},
            </if>
            <if test="applyTime != null">#{applyTime},
            </if>
            <if test="bookingStatus != null and bookingStatus != ''">#{bookingStatus},
            </if>
            <if test="cancelTime != null">#{cancelTime},
            </if>
            <if test="rescindType != null and rescindType != ''">#{rescindType},
            </if>
            <if test="rescindReason != null and rescindReason != ''">#{rescindReason},
            </if>
            <if test="approvalStatus != null and approvalStatus != ''">#{approvalStatus},
            </if>
            <if test="refuseReason != null">#{refuseReason},
            </if>
            <if test="approvalTime != null">#{approvalTime},
            </if>
        </trim>
    </insert>

    <update id="update">
        update resident_rescind_apply
        <trim prefix="SET" suffixOverrides=",">
            <if test="patientId != null and patientId != ''">patient_id =
                #{patientId},
            </if>
            <if test="signNo != null and signNo != ''">sign_no = #{signNo}
            </if>
            <if test="identity != null and identity != ''">identity =
                #{identity},
            </if>
            <if test="orgNo != null and orgNo != ''">org_no =
                #{orgNo},
            </if>
            <if test="orgName != null and orgName != ''">org_name =
                #{orgName},
            </if>
            <if test="teamNo != null and teamNo != ''">team_no =
                #{teamNo},
            </if>
            <if test="teamName != null and teamName != ''">team_name =
                #{teamName},
            </if>
            <if test="userNo != null and userNo != ''">user_no =
                #{userNo},
            </if>
            <if test="userName != null and userName != ''">user_name =
                #{userName},
            </if>
            <if test="applyTime != null">apply_time =
                #{applyTime},
            </if>
            <if test="bookingStatus != null and bookingStatus != ''">booking_status =
                #{bookingStatus},
            </if>
            <if test="cancelTime != null">cancel_time =
                #{cancelTime},
            </if>
            <if test="rescindType != null and rescindType != ''">rescind_type =
                #{rescindType},
            </if>
            <if test="rescindReason != null and rescindReason != ''">rescind_reason =
                #{rescindReason},
            </if>
            <if test="approvalStatus != null and approvalStatus != ''">approval_status =
                #{approvalStatus},
            </if>
            <if test="refuseReason != null">refuse_reason =
                #{refuseReason},
            </if>
            <if test="approvalTime != null">approval_time =
                #{approvalTime},
            </if>
        </trim>
        where apply_no = #{applyNo}
    </update>

    <update id="updateEx">
        update resident_rescind_apply
        <set>
            <if test="approvalStatus != null and approvalStatus != ''">
                approval_status = #{approvalStatus},
            </if>
            <if test="refuseReason != null and refuseReason != ''">
                refuse_reason = #{refuseReason},
            </if>
            approval_time = now()
        </set>
        where apply_no = #{applyNo};
    </update>
</mapper>
