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

    <resultMap id="BaseResultMap" type="com.xinelu.familydoctor.applet.pojo.entity.ResidentServiceApplyEntity">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="bookingNo" column="booking_no" jdbcType="VARCHAR"/>
        <result property="patientId" column="patient_id" jdbcType="VARCHAR"/>
        <result property="residentName" column="resident_name" jdbcType="VARCHAR"/>
        <result property="identity" column="identity" jdbcType="VARCHAR"/>
        <result property="packageNo" column="package_no" jdbcType="VARCHAR"/>
        <result property="packageName" column="package_name" jdbcType="VARCHAR"/>
        <result property="formNo" column="form_no" jdbcType="VARCHAR"/>
        <result property="formName" column="form_name" jdbcType="VARCHAR"/>
        <result property="serviceWay" column="service_way" jdbcType="VARCHAR"/>
        <result property="bookingTime" column="booking_time" jdbcType="TIMESTAMP"/>
        <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="completeTime" column="complete_time" jdbcType="TIMESTAMP"/>
        <result property="approvalStatus" column="approval_status" jdbcType="VARCHAR"/>
        <result property="refuseReason" column="refuse_reason" jdbcType="VARCHAR"/>
        <result property="approvalTime" column="approval_time" jdbcType="TIMESTAMP"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
    </resultMap>

    <resultMap id="ServiceApplyRecordMap" type="com.xinelu.familydoctor.applet.pojo.vo.ResidentServiceApplyVo">
        <result property="bookingNo" column="booking_no" jdbcType="VARCHAR"/>
        <result property="patientId" column="patient_id" jdbcType="VARCHAR"/>
        <result property="residentName" column="resident_name" jdbcType="VARCHAR"/>
        <result property="identity" column="identity" jdbcType="VARCHAR"/>
        <result property="packageNo" column="package_no" jdbcType="VARCHAR"/>
        <result property="packageName" column="package_name" jdbcType="VARCHAR"/>
        <result property="formNo" column="form_no" jdbcType="VARCHAR"/>
        <result property="formName" column="form_name" jdbcType="VARCHAR"/>
        <result property="serviceWay" column="service_way" jdbcType="VARCHAR"/>
        <result property="bookingTime" column="booking_time" jdbcType="TIMESTAMP"/>
        <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="completeTime" column="complete_time" jdbcType="TIMESTAMP"/>
        <result property="approvalStatus" column="approval_status" jdbcType="VARCHAR"/>
        <result property="refuseReason" column="refuse_reason" jdbcType="VARCHAR"/>
        <result property="approvalTime" column="approval_time" jdbcType="TIMESTAMP"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
        <!-- 服务评价 -->
        <association property="orderEvaluateInfo" notNullColumn="service_code" javaType="com.xinelu.familydoctor.applet.pojo.vo.OrderEvaluateInfoVo">
            <result property="id" column="id"/>
            <result property="openid" column="openid"/>
            <result property="unionid" column="unionid"/>
            <result property="orderNo" column="order_no"/>
            <result property="serviceCode" column="service_code"/>
            <result property="evaluateContent" column="evaluate_content"/>
            <result property="evaluateChannel" column="evaluate_channel"/>
            <result property="evaluateSatisfaction" column="evaluate_satisfaction"/>
            <result property="compositeScore" column="composite_score"/>
            <result property="orderSource" column="order_source"/>
            <result property="createBy" column="create_by"/>
            <result property="createTime" column="create_time"/>
            <result property="updateBy" column="update_by"/>
            <result property="updateTime" column="update_time"/>
            <collection property="orderEvaluatePictureInfoList" notNullColumn="evaluate_picture_url" javaType="java.util.List" resultMap="OrderEvaluatePictureInfoMap"/>
        </association>
    </resultMap>

    <!--评价图片-->
    <resultMap type="com.xinelu.manage.domain.orderevaluatepictureinfo.OrderEvaluatePictureInfo" id="OrderEvaluatePictureInfoMap">
        <result property="id" column="id"/>
        <result property="orderEvaluateId" column="order_evaluate_id"/>
        <result property="evaluatePictureUrl" column="evaluate_picture_url"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,booking_no,patient_id,
        resident_name,identity,package_no,
        package_name,form_no,form_name,
        service_way,booking_time,org_no,
        org_name,team_no,team_name,
        user_no,user_name,apply_time,
        booking_status,cancel_time,complete_time,
        approval_status,refuse_reason,approval_time,
        remark
    </sql>

    <sql id="Base_Column_Service_Apply">
        s.id,s.booking_no,s.patient_id,
        s.resident_name,s.identity,s.package_no,
        s.package_name,s.form_no,s.form_name,
        s.service_way,s.booking_time,s.org_no,
        s.org_name,s.team_no,s.team_name,
        s.user_no,s.user_name,s.apply_time,
        s.booking_status,s.cancel_time,s.complete_time,
        s.approval_status,s.refuse_reason,s.approval_time,
        s.remark
    </sql>

    <sql id="Base_Column_Evaluate_Info">
        oei.id, oei.openid, oei.unionid, oei.order_no, oei.service_code,
        oei.evaluate_content, oei.evaluate_channel, oei.evaluate_satisfaction, oei.composite_score, oei.order_source,
        oei.create_by, oei.create_time, oei.update_by, oei.update_time
    </sql>

    <sql id="Base_Column_Evaluate_Picture">
        oepi.id, oepi.order_evaluate_id, oepi.evaluate_picture_url, oepi.create_by, oepi.create_time, oepi.update_by, oepi.update_time
    </sql>

    <sql id="Insert_Column_List">
        booking_no,patient_id,
        resident_name,identity,package_no,
        package_name,form_no,form_name,
        service_way,booking_time,org_no,
        org_name,team_no,team_name,
        user_no,user_name,apply_time,
        booking_status,cancel_time,complete_time,
        approval_status,refuse_reason,approval_time,
        remark
    </sql>

    <!-- 新增 -->
    <insert id="insert">
        insert into resident_service_apply
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="bookingNo != null and bookingNo != ''">booking_no,
            </if>
            <if test="patientId != null and patientId != ''">patient_id,
            </if>
            <if test="residentName != null and residentName != ''">resident_name,
            </if>
            <if test="identity != null and identity != ''">identity,
            </if>
            <if test="packageNo != null">package_no,
            </if>
            <if test="packageName != null">package_name,
            </if>
            <if test="formNo != null">form_no,
            </if>
            <if test="formName != null">form_name,
            </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="bookingTime != null">booking_time,
            </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="approvalStatus != null and approvalStatus != ''">approval_status,
            </if>
            <if test="refuseReason != null">refuse_reason,
            </if>
            <if test="approvalTime != null">approval_time,
            </if>
            <if test="remark != null">remark,
            </if>
            <if test="serviceWay != null">service_way,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="bookingNo != null and bookingNo != ''">#{bookingNo},
            </if>
            <if test="patientId != null and patientId != ''">#{patientId},
            </if>
            <if test="residentName != null and residentName != ''">#{residentName},
            </if>
            <if test="identity != null and identity != ''">#{identity},
            </if>
            <if test="packageNo != null">#{packageNo},
            </if>
            <if test="packageName != null">#{packageName},
            </if>
            <if test="formNo != null">#{formNo},
            </if>
            <if test="formName != null">#{formName},
            </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="bookingTime != null">#{bookingTime},
            </if>
            <if test="applyTime != null">#{applyTime},
            </if>
            <if test="bookingStatus != null and bookingStatus != ''">#{bookingStatus},
            </if>
            <if test="cancelTime != null">#{cancelTime},
            </if>
            <if test="approvalStatus != null and approvalStatus != ''">#{approvalStatus},
            </if>
            <if test="refuseReason != null">#{refuseReason},
            </if>
            <if test="approvalTime != null">#{approvalTime},
            </if>
            <if test="remark != null">#{remark},
            </if>
            <if test="serviceWay != null">#{serviceWay},
            </if>
        </trim>
    </insert>

    <!-- 批量插入 -->
    <insert id="batchInsert">
        insert into resident_service_apply (<include refid="Insert_Column_List"></include>)
        values
        <foreach collection="list" separator="," item="item">
            (#{item.bookingNo}, #{item.patientId}, #{item.residentName}, #{item.identity},
            #{item.packageNo}, #{item.packageName}, #{item.formNo},
            #{item.formName}, #{item.serviceWay}, #{item.bookingTime}, #{item.orgNo},#{item.orgName},
            #{item.teamNo},#{item.teamName}, #{item.userNo}, #{item.userName},
            #{item.applyTime}, #{item.bookingStatus}, #{item.cancelTime},
            #{item.completeTime}, #{item.approvalStatus}, #{item.refuseReason},
            #{item.approvalTime},#{item.remark}
            )
        </foreach>
    </insert>

    <!-- 取消服务申请 -->
    <update id="cancel">
        update resident_service_apply
        set booking_status = '2',
            cancel_time    = now()
        where booking_no = #{bookingNo}
    </update>

    <select id="findByBody" resultType="com.xinelu.familydoctor.applet.pojo.vo.ResidentServiceApplyVo">
        select <include refid="Base_Column_Service_Apply"></include>,p.address,p.phone
        from resident_service_apply s
        left join patient_info p on p.card_no = s.identity
        where 1=1
        <if test="identity != null and identity != ''">
            and s.identity = #{identity}
        </if>
        <if test="residentName != null and residentName != ''">
            and s.resident_name like concat(#{residentName},'%')
        </if>
        <if test="orgNo != null and orgNo != ''">
            and s.org_no = #{orgNo}
        </if>
        <if test="teamNo != null and teamNo != ''">
            and s.team_no = #{teamNo}
        </if>
        <if test="userNo != null and userNo != ''">
            and s.user_no = #{userNo}
        </if>
        <if test="formNo != null and formNo != ''">
            and s.form_no = #{formNo}
        </if>
        <if test="startDate != null">
            and s.booking_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and s.booking_time &lt;= #{endDate}
        </if>
        <if test="bookingStatus != null and bookingStatus != ''">
            and s.booking_status = #{bookingStatus}
        </if>
        <if test="approvalStatus != null and approvalStatus != ''">
            and s.approval_status = #{approvalStatus}
        </if>
        <if test="completed != null and completed != ''">
            <choose>
                <!-- 已完成 -->
                <when test="completed == 1">
                    and s.booking_status = '1'
                </when>
                <!-- 未完成 -->
                <when test="completed == 2">
                    and s.booking_status != '1'
                </when>
                <otherwise></otherwise>
            </choose>
        </if>
        order by s.booking_time desc
    </select>

    <!-- 查询未完成的服务申请 -->
    <select id="getServiceBookingByIdentity" resultType="com.xinelu.familydoctor.applet.pojo.vo.ResidentServiceApplyVo">
        select <include refid="Base_Column_List"></include> from resident_service_apply where identity = #{identity} and
        booking_status = '0' and approval_status ='0' order by booking_time desc limit 1
    </select>

    <!-- 服务申请详情 -->
    <select id="detail" resultType="com.xinelu.familydoctor.applet.pojo.vo.ResidentServiceApplyVo">
        select <include refid="Base_Column_List"></include> from resident_service_apply where booking_no = #{bookingNo}
    </select>

    <!-- 服务记录列表 -->
    <select id="getRecord" resultMap="ServiceApplyRecordMap">
        select <include refid="Base_Column_Service_Apply"></include>,
               <include refid="Base_Column_Evaluate_Info"></include>,
               <include refid="Base_Column_Evaluate_Picture"></include>
        from resident_service_apply s
        left join order_evaluate_info oei on oei.service_code = s.booking_no and oei.order_source = 'FAMILY_DOCTOR'
        left join order_evaluate_picture_info oepi on oepi.order_evaluate_id = oei.id
        where 1=1
        <if test="identity != null and identity != ''">
          and s.identity = #{identity}
        </if>
        <if test="startDate != null">
            and s.booking_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and s.booking_time &lt;= #{endDate}
        </if>
        <choose>
            <when test="isEvaluate == 1">and oei.id is not null</when>
            <when test="isEvaluate == 2">and oei.id is null</when>
        </choose>
    </select>

    <update id="updateEx">
        update resident_service_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 booking_no = #{applyNo};
    </update>

    <!-- 更新服务完成时间 -->
    <update id="updateComplete">
        update resident_service_apply set complete_time = now(),booking_status = '1' where booking_no = #{bookingNo}
    </update>

</mapper>
