<?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.applet.mapper.appletlogin.AppletLoginMapper">

    <!-- 被护理人基本信息以及其疾病信息结果映射 -->
    <resultMap type="com.xinelu.applet.vo.appletlogin.RegisterPatientInfoVO" id="RegisterPatientInfo">
        <result property="patientCode" column="nurseStationId"/>
        <result property="patientName" column="patientName"/>
        <result property="phone" column="phone"/>
        <result property="areaCode" column="areaCode"/>
        <result property="areaName" column="areaName"/>
        <result property="cardNo" column="cardNo"/>
        <result property="address" column="address"/>
        <result property="communityAliasName" column="communityAliasName"/>
        <result property="homeLongitude" column="homeLongitude"/>
        <result property="homeLatitude" column="homeLatitude"/>
        <result property="unionid" column="unionid"/>
        <result property="openid" column="openid"/>
        <collection property="diseaseInfoList" javaType="java.util.List" resultMap="PatientDiseaseInfoResult"/>
    </resultMap>

    <!-- 护理人疾病信息结果映射 -->
    <resultMap type="DiseaseInfo" id="PatientDiseaseInfoResult">
        <result property="id" column="id"/>
        <result property="diseaseCode" column="disease_code"/>
        <result property="diseaseName" column="disease_name"/>
    </resultMap>

    <!-- 护理站及护理信息结果映射 -->
    <resultMap type="com.xinelu.applet.vo.appletlogin.NurserStationItemInfoVO"
               id="NurserStationItemInfoVO">
        <result property="stationId" column="stationId"/>
        <result property="stationItemId" column="stationItemId"/>
        <result property="stationItemPriceId" column="stationItemPriceId"/>
        <result property="nurseItemCode" column="nurse_item_code"/>
        <result property="nurseItemName" column="nurse_item_name"/>
        <result property="advanceAppointDuration" column="advance_appoint_duration"/>
        <result property="serveDurationUnit" column="serve_duration_unit"/>
        <result property="nurseItemPrice" column="nurseItemPrice"/>
        <result property="nurseItemContent" column="nurse_item_content"/>
        <result property="itemPictureUrl" column="item_picture_url"/>
        <result property="nurseStationName" column="nurse_station_name"/>
        <collection property="poserInfoList" javaType="java.util.List" resultMap="PoserInfoResult"/>
    </resultMap>
    <!-- 海报模块信息结果映射 -->
    <resultMap type="com.xinelu.applet.vo.nearbynursingstation.PoserInfoHomeVO" id="PoserInfoResult">
        <result property="id" column="poserInfoId"/>
        <result property="poserName" column="poser_name"/>
        <result property="posterPictureUrl" column="poster_picture_url"/>
        <result property="posterVideoUrl" column="poster_video_url"/>
        <result property="moduleType" column="module_type"/>
        <result property="jumpLink" column="jump_link"/>
        <result property="dictCode" column="dict_code"/>
        <result property="dictLabel" column="dict_label"/>
        <result property="dictValue" column="dict_value"/>
        <result property="dictType" column="dict_type"/>
    </resultMap>
    <select id="getPatientInfoByOpenId" parameterType="string" resultType="patientInfo">
        SELECT
        id,
        community_code,
        area_code,
        patient_code,
        patient_name,
        card_no,
        user_id,
        unionid,
        openid,
        phone,
        address,
        urgent_contact_name,
        urgent_contact_phone,
        community_alias_name,
        home_longitude,
        home_latitude,
        head_picture_url,
        bind_openid
        FROM
        patient_info
        <where>
            del_flag = 0
            <if test="openId != null and openId != ''">
                and bind_openid = #{openId}
            </if>
        </where>
        limit 1
    </select>

    <select id="getPatientInfoByPatientId" parameterType="long" resultType="patientInfo">
        SELECT
        id,
        community_code,
        area_code,
        patient_code,
        patient_name,
        card_no,
        user_id,
        unionid,
        openid,
        phone,
        address,
        urgent_contact_name,
        urgent_contact_phone,
        community_alias_name,
        home_longitude,
        home_latitude,
        head_picture_url
        FROM
        patient_info
        <where>
            del_flag = 0
            <if test="patient != null">
                and id = #{patient}
            </if>
        </where>
    </select>

    <select id="getAllDiseaseInfo" resultType="com.xinelu.applet.vo.diseaseinfo.DiseasesInfoVO">
        SELECT id,
               id diseaseId,
               user_id,
               disease_code,
               disease_name,
               sort
        FROM disease_info
        order by sort asc
    </select>

    <select id="getAllNurseTypeInfo" resultType="nurseType">
        SELECT id,
               user_id,
               nurse_type_code,
               nurse_type_name
        FROM nurse_type
    </select>

    <select id="getPatientByPhoneAndCardNo" resultType="int">
        select count(1) patientCount from patient_info
        <where>
            and del_flag = 0
            <if test="phone != null and phone != ''">
                and phone = #{phone}
            </if>
            <if test="cardNo != null and cardNo != ''">
                and card_no = #{cardNo}
            </if>
        </where>
    </select>

    <select id="getPatientInfoList"
            resultType="com.xinelu.applet.vo.appletlogin.PatientVO">
        select id patient_id, patient_name, unionid, openid, address, phone from patient_info
        <where>
            <if test="openId != null and openId != ''">
                openid = #{openId}
            </if>
        </where>
        order by id asc
    </select>

    <select id="getItemConsumableList"
            resultType="com.xinelu.applet.vo.appletlogin.NurserStationItemConsumableVO">
        SELECT
        nsic.consumable_detail,
        nsic.consumable_unit,
        IFNULL(nsic.consumable_price, 0) consumable_price,
        nsic.consumable_count
        FROM
        nurse_station_item_consumable nsic
        <where>
            <if test="stationItemId != null">
                and nsic.nurse_station_item_id = #{stationItemId}
            </if>
        </where>
    </select>

    <select id="getNurserStationItemInfo"
            resultType="com.xinelu.applet.vo.appletlogin.NurserStationItemInfoVO">
        SELECT
        nsi.nurse_station_id stationId,
        nsi.id stationItemId,
        nsip.id stationItemPriceId,
        nsi.nurse_item_code,
        nsi.nurse_item_name,
        IFNULL(nsi.advance_appoint_duration, '') advance_appoint_duration,
        nsi.nurse_item_content,
        nsi.item_picture_url,
        nsip.serve_duration_unit,
        IFNULL(nsip.price, 0) nurseItemPrice,
        nsi.appointment_limit_count,
        IFNULL(nsi.appointment_time_interval, '') appointment_time_interval,
        ns.morning_open_start_time,
        ns.morning_open_end_time,
        ns.afternoon_open_start_time,
        ns.afternoon_open_end_time,
        nsip.commission_amount
        FROM
        nurse_station_item nsi
        LEFT JOIN nurse_station_item_price nsip ON nsi.id = nsip.nurse_station_item_id
        LEFT JOIN nurse_station ns ON ns.id = nsi.nurse_station_id
        <where>
            <if test="stationId != null">
                and nsi.nurse_station_id = #{stationId}
            </if>
            <if test="stationItemId != null">
                and nsi.id = #{stationItemId}
            </if>
            <if test="stationItemPriceId != null">
                and nsip.id = #{stationItemPriceId}
            </if>
        </where>
    </select>

    <insert id="insertBatchOrderConsumable">
        insert into appointment_order_consumable(
        appoint_order_details_id,
        order_consumable_name,
        order_consumable_price,
        order_consumable_count,
        consumable_unit,
        del_flag,
        create_time
        ) values
        <foreach item="orderConsumable" index="index" collection="list" separator=",">
            (
            #{orderConsumable.appointOrderDetailsId},
            #{orderConsumable.consumableDetail},
            #{orderConsumable.consumablePrice},
            #{orderConsumable.consumableCount},
            #{orderConsumable.consumableUnit},
            0,
            now()
            )
        </foreach>
    </insert>

    <select id="getRegisterPatientInfoById" resultMap="RegisterPatientInfo">
        SELECT
        pi.id patientId,
        pi.area_code areaCode,
        sa.area_name areaName,
        pi.patient_code patientCode,
        pi.patient_name patientName,
        pi.card_no cardNo,
        pi.unionid,
        pi.openid,
        pi.phone,
        pi.address,
        pi.community_alias_name communityAliasName,
        pi.home_longitude homeLongitude,
        pi.home_latitude homeLatitude,
        di.id,
        di.disease_code,
        di.disease_name
        FROM
        patient_info pi
        left join sys_area sa on pi.area_code = sa.area_code
        left join patient_disease_info pdi on pi.id = pdi.patient_id
        left join disease_info di on pdi.disease_id = di.id
        <where>
            <if test="patientId != null">
                pi.id = #{patientId}
            </if>
        </where>
    </select>

    <select id="getPatientNurseTypeInfo" resultType="nurseType">
        SELECT
        nt.id,
        nt.nurse_type_code,
        nt.nurse_type_name
        FROM
        nurse_type nt
        LEFT JOIN patient_nurse_type pnt ON pnt.nurse_type_id = nt.id
        <where>
            <if test="patientId != null">
                pnt.patient_id = #{patientId}
            </if>
        </where>
    </select>

    <select id="getPatientInfoByPhone" parameterType="string"
            resultType="com.xinelu.manage.vo.patientinfo.PatientInfoVO">
        SELECT
        id,
        community_code,
        area_code,
        patient_code,
        patient_name,
        card_no,
        user_id,
        unionid,
        openid,
        phone,
        address,
        urgent_contact_name,
        urgent_contact_phone,
        community_alias_name,
        home_longitude,
        home_latitude,
        head_picture_url,
        (select count(1) from nurse_station_person where phone = #{phone}) personCount
        FROM
        patient_info
        <where>
            del_flag = 0
            <if test="phone != null and phone != ''">
                and phone = #{phone}
            </if>
            <if test="openId != null and openId != ''">
                and openid = #{openId}
            </if>
        </where>
    </select>


    <select id="selectPatientDiseaseInfos" parameterType="com.xinelu.applet.vo.diseaseinfo.DiseasesInfoVO"
            resultType="com.xinelu.applet.vo.diseaseinfo.DiseasesInfoVO">
        select
        id,
        patient_id,
        disease_id,
        user_id,
        disease_name
        from patient_disease_info
        <where>
            <if test="patientId != null ">
                and patient_id = #{patientId}
            </if>
        </where>
    </select>

    <select id="getNurserStationItemAndPoser" resultMap="NurserStationItemInfoVO">
        SELECT
        nsi.nurse_item_name,
        nsi.nurse_item_content,
        nsi.item_picture_url,
        IFNULL(nsip.price, 0) nurseItemPrice,
        ns.nurse_station_name,
        pi.poster_picture_url,
        pi.poster_video_url,
        pi.module_type,
        pi.jump_link
        FROM
        nurse_station_item nsi
        LEFT JOIN nurse_station_item_price nsip ON nsi.id = nsip.nurse_station_item_id
        LEFT JOIN nurse_station ns ON ns.id = nsi.nurse_station_id
        LEFT JOIN poser_info pi ON pi.nurse_item_id = nsi.id
        <where>
            <if test="stationId != null">
                and nsi.nurse_station_id = #{stationId}
            </if>
            <if test="stationItemId != null">
                and nsi.id = #{stationItemId}
            </if>
            <if test="stationItemPriceId != null">
                and nsip.id = #{stationItemPriceId}
            </if>
        </where>
    </select>

    <select id="getAppointOrderCountByIdAndTime" resultType="integer">
        SELECT
        count( 1 ) orderCount
        FROM
        appointment_order ao
        INNER JOIN appointment_order_details aod ON ao.order_no = aod.order_no
        WHERE
        aod.nurse_station_item_id = #{itemId}
        AND aod.service_start_time = #{serviceStartTime}
        AND aod.service_date = #{serviceDate}
        AND ao.order_status IN
        <foreach item="orderStatus" collection="orderStatusList" open="(" separator="," close=")">
            #{orderStatus}
        </foreach>
    </select>
    <update id="deletePatientInfoById">
        update patient_info
        set del_flag    = 1,
            update_time = now()
        where id = #{patientId}
    </update>
</mapper>
