<?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.bonus.boot.manager.physical.dao.EmployeeHealthCheckDao">
    <insert id="insertAppointStatus">
        insert into pm_base_phy_appont(user_id,hospital_id,appoint_status,if_active,sex,if_career_appoint)
        values (#{id},#{hospitalId},'3','1','0','0')
    </insert>
    <update id="updateExcelData">
        update  pm_physical_result set
            physical_time = #{physicalTime},physical_status = #{phyStatus}
        where physical_id = (select id from pm_base_physical where idcard = #{idNumber} and is_active = '1')
    </update>
    <update id="updateAppointStatus">
        update  pm_base_phy_appont set
            appoint_status = #{status}
        where user_id = (select id from pm_base_physical where idcard = #{idNumber} and is_active = '1' and if_career_appoint = '2')
    </update>
    <update id="updatePDFAddress">
        update pm_physical_result set physical_address = #{filePath} where physical_id = (select id from pm_base_physical where idcard = #{idNumber} and is_active = '1')
    </update>
    <update id="updateNoCheckStatus">
        update  pm_base_phy_appont set
            appoint_status = #{status}
        where id = #{ids}
    </update>
    <update id="updateAppointCancel">
        update pm_base_phy_appont set
            if_cancel = #{status}
        where user_id = (select id from pm_base_physical where idcard = #{idNumber} and is_active = '1') and if_cancel = '2' and if_career_appoint = '2'
    </update>

    <select id="getList" resultType="com.bonus.boot.manager.physical.entity.EmployeeHealthCheckBean">
        select DISTINCT
            pbp.phy_name as personName,
            pbp.idcard as idNumber,
            pbp.age as age,
            pbp.telep_number as phone,
            if(pbp.sex = '0','男','女') as sex,
            if(pbp.if_higher = '1','是','否') as higherJob,
            sdd.`name` as personType,
            pbsj.job_name as specialPostName,
            ppo.`NAME` as department,
            CASE
            WHEN pbpa.appoint_status = '1' THEN
            '已预约已体检'
            WHEN pbpa.appoint_status = '2' THEN
            '已预约未体检'
            WHEN pbpa.appoint_status = '3' THEN
            '未预约已体检'
            WHEN pbpa.appoint_status = '4' THEN
            '已作废'
            WHEN ppr.physical_time IS NOT NULL THEN
            '未预约已体检' ELSE ''
            END AS physicalStatus,
            ppr.physical_time as physicalTime,
            ifnull(ppr.physical_free,0) as physicalFree,
            ppr.physical_address as physicalAddress
        from  pm_physical_result ppr
                  left join pm_base_physical pbp on pbp.id = ppr.physical_id and pbp.is_active = '1'
                  left join sys_dic_detail sdd on sdd.id = pbp.if_job
                  left join pm_base_special_job pbsj on pbsj.id = pbp.special_job and pbsj.is_active = '1'
                  left join pm_phy_organization ppo on ppo.id = pbp.depart_id and ppo.IS_ACTIVE = '1'
                  left join pm_base_phy_appont pbpa on pbpa.user_id = pbp.id and pbpa.if_active = '1' and (pbpa.if_cancel = '2' or pbpa.if_cancel = '3') and pbpa.if_career_appoint = '2'
        <if test="hospitalId !=null and hospitalId !='null' and hospitalId !=''">
            and pbpa.hospital_id like concat ('%',#{hospitalId},'%')
        </if>
        where ppr.is_active = '1'

        <if test="physicalTime !=null and physicalTime !='null' and physicalTime !=''">
            and ppr.create_time like concat ('%',#{physicalTime},'%')
        </if>
        <if test="physicalStatus !=null and physicalStatus !='null' and physicalStatus !=''">
            and pbpa.appoint_status  = #{physicalStatus}
        </if>
        <if test="physicalStatus == 3">
            or ppr.physical_time IS NOT NULL
        </if>
        <if test="keyWord !=null and keyWord !='null' and keyWord !=''">
            and(
            pbp.phy_name like concat ('%',#{keyWord},'%') or
            pbp.idcard like concat ('%',#{keyWord},'%') or
            pbp.telep_number like concat ('%',#{keyWord},'%') or
            ppo.`NAME` like concat ('%',#{keyWord},'%')
            )
        </if>
    </select>
    <select id="getAllPhysicalPerson" resultType="com.bonus.boot.manager.career.entity.CareerBean">
        select DISTINCT phy_name as personName,idcard as idNumber,id
        from pm_base_physical
        where is_active = '1'
    </select>
    <select id="getAllAppointPerson" resultType="com.bonus.boot.manager.career.entity.CareerBean">
        select DISTINCT pbp.phy_name as personName,pbp.idcard as idNumber,p.phy_appont_time as bookingTime
        from pm_base_phy_appont p
        left join pm_base_physical pbp on pbp.id = p.user_id and pbp.is_active = '1'
        where p.if_active = '1'
    </select>
    <select id="getAllIdNumber" resultType="com.bonus.boot.manager.career.entity.CareerBean">
        select DISTINCT idcard as idNumber
        from pm_base_physical
        where is_active = '1'
    </select>
    <select id="getAllAppointIdNumber" resultType="com.bonus.boot.manager.career.entity.CareerBean">
        select DISTINCT pbp.idcard as idNumber
        from pm_base_phy_appont p
                 left join pm_base_physical pbp on pbp.id = p.user_id and pbp.is_active = '1'
        where p.if_active = '1'
    </select>
    <select id="getIsExistAppoint" resultType="java.lang.Integer">
        select count(1)
        from pm_base_phy_appont
        where user_id = (select id from pm_base_physical where idcard = #{idNumber} and is_active = '1') and if_cancel = '2' and if_career_appoint = '2'
    </select>
    <select id="getNoCheckLsit" resultType="com.bonus.boot.manager.career.entity.CareerBean">
        select id
        from pm_base_phy_appont
        where hospital_id = #{hospitalId} and phy_appont_time between #{beginDateNext} and #{endDateNext} and if_active = '1' and (appoint_status is null or appoint_status = '2') and if_cancel = '2'
    </select>
    <select id="getPersonPhoneNumber" resultType="java.lang.String">
        select telep_number as phone
        from pm_base_physical
        where idcard = #{idNumber} and is_active = '1'
        limit 1
    </select>
</mapper>

