package cn.hnthyy.thmz.mapper.his.mz;

import cn.hnthyy.thmz.entity.his.mz.MzyRequest;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

public interface MzyRequestMapper {
    /**
     * 根据日期查询所有号表
     *
     * @param requestDay
     * @return
     */
    @Select(" select id,request_day,ampm,rtrim(unit_code) unit_code,group_code,rtrim(doctor_code) doctor_code,charge_type,op_id,total_num,left_num,bespeak_no,check_fee,sys_date,del_flag from mzy_request_new where request_day = #{requestDay,jdbcType=DATE}")
    List<MzyRequest> selectMzyRequestByRequestDay(@Param("requestDay") String requestDay);

    /**
     * 按日期和时间区间查询可以挂号的科室编号
     *
     * @param requestDay
     * @return
     */
    @Select("select rtrim(unit_code) unit_code from mzy_request_new where request_day = #{requestDay,jdbcType=DATE} and ampm =#{ampm,jdbcType=CHAR} and left_num>0 group by unit_code")
    List<String> selectUnitCodeByRequestDay(@Param("requestDay") String requestDay, @Param("ampm") String ampm);


    /**
     * 按日期和时间区间以及部门编码与号别类型查询可以挂号的医生编号及费用类型
     *
     * @param requestDay
     * @return
     */
    @Select("select id,rtrim(doctor_code) doctor_code,rtrim(charge_type) charge_type,check_fee,left_num from mzy_request_new where request_day = #{requestDay,jdbcType=DATE} and unit_code=#{unitCode,jdbcType=CHAR} and ampm =#{ampm,jdbcType=CHAR} and charge_type=#{chargeType,jdbcType=CHAR} and left_num > 0")
    List<MzyRequest> selectDoctorCodeByRequestDay(@Param("requestDay") String requestDay, @Param("unitCode") String unitCode, @Param("ampm") String ampm, @Param("chargeType") String chargeType);

    /**
     * 按日期和时间区间以及部门编码查询可以挂号的号别
     *
     * @param requestDay
     * @param unitCode
     * @param ampm
     * @return
     */
    @Select("select rtrim(charge_type) charge_type from mzy_request_new where request_day = #{requestDay,jdbcType=DATE} and unit_code=#{unitCode,jdbcType=CHAR} and ampm =#{ampm,jdbcType=CHAR}  and left_num > 0  group by charge_type")
    List<String> selectChargeTypeByRequestDay(@Param("requestDay") String requestDay, @Param("unitCode") String unitCode, @Param("ampm") String ampm);

    /**
     * 按照主键查询所选号表数据
     *
     * @param id 主键
     */
    @Select(" select id,request_day,ampm,rtrim(unit_code) unit_code,group_code,rtrim(doctor_code) doctor_code,charge_type,op_id,total_num,left_num,bespeak_no,check_fee,sys_date,del_flag" +
            " from mzy_request_new where id = #{id,jdbcType=BIGINT}")
    MzyRequest selectMzyRequestById(@Param("id") Long id);

    /**
     * 更新新号表剩余卡号与当前就诊号
     *
     * @param mzyRequest
     * @return
     */
    @Update("update mzy_request_new set left_num=#{leftNum,jdbcType=INTEGER}, bespeak_no=#{bespeakNo,jdbcType=SMALLINT} where id = #{id,jdbcType=BIGINT}")
    int updateMzyRequest(MzyRequest mzyRequest);


    /**
     * 更新原号表剩余卡号与当前就诊号  （临时功能，全部切换新系统不会使用）
     *
     * @param mzyRequest
     * @return
     */
    @Update("update mzy_request set left_num=#{leftNum,jdbcType=INTEGER}, bespeak_no=#{bespeakNo,jdbcType=SMALLINT} where " +
            "request_day = #{requestDay,jdbcType=DATE} and unit_code=#{unitCode,jdbcType=CHAR} and ampm =#{ampm,jdbcType=CHAR}  and charge_type=#{chargeType}  and doctor_code=#{doctorCode}")
    int updateOriMzyRequest(MzyRequest mzyRequest);

    /**
     * 同步排班表
     *
     * @return
     */
    @Insert("insert into mzy_request_new(request_day,ampm,unit_code,group_code,doctor_code,charge_type,op_id,total_num,left_num,bespeak_no,check_fee,sys_date,del_flag) " +
            "select request_day,ampm,unit_code,group_code,doctor_code,charge_type,op_id,total_num,left_num,bespeak_no,check_fee,sys_date,del_flag " +
            " from mzy_request where request_day >=(select top 1 request_day from mzy_request_new order by request_day desc ) and request_day <> '7019-10-27 00:00:00.000' order by request_day asc")
    int synchronizationRequest();



    /**
     * 查询当天最新的排班信息
     *
     * @return
     */
    @Select("select request_day,ampm,unit_code,group_code,doctor_code,charge_type,op_id,total_num,left_num,bespeak_no,check_fee,sys_date,del_flag " +
            " from mzy_request where request_day =#{today,jdbcType=DATE}")
    List<MzyRequest> selectRequestForToday(Date today);

    /**
     * 根据所以筛选条件查询号表
     *
     * @param mzyRequest
     * @return
     */
    @Select("select * from mzy_request_new where  ampm = #{ampm} and unit_code = #{unitCode}  and request_day = #{requestDay,jdbcType=DATE} and doctor_code =#{doctorCode} and charge_type =#{chargeType}  ")
    MzyRequest selectMzyRequestByAllParams(MzyRequest mzyRequest);

    /**
     * 保存排班表
     *
     * @return
     */
    @Insert("if not exists (select id  from mzy_request_new where request_day=#{requestDay,jdbcType=DATE} and ampm = #{ampm,jdbcType=CHAR} and unit_code=#{unitCode} and group_code=#{groupCode} and charge_type=#{chargeType} and doctor_code=#{doctorCode} ) "+
            "insert into mzy_request_new(request_day,ampm,unit_code,group_code,doctor_code,charge_type,op_id,total_num,left_num,bespeak_no,check_fee,sys_date,del_flag) " +
            "values(#{requestDay,jdbcType=DATE},#{ampm},#{unitCode},#{groupCode},#{doctorCode},#{chargeType},#{opId},#{totalNum},#{leftNum},#{bespeakNo},#{checkFee},#{sysDate,jdbcType=DATE},#{delFlag})")
    int insertRequest(MzyRequest mzyRequest);



    /**
     * 按日期和时间区间以及部门编码以及收费类型查询老排班表中的最新数据
     *
     * @param requestDay
     * @param unitCode
     * @param ampm
     * @return
     */
    @Select({"<script>",
            "select * from mzy_request_new where request_day = #{requestDay,jdbcType=DATE} and unit_code=#{unitCode,jdbcType=CHAR} and ampm =#{ampm,jdbcType=CHAR}  and charge_type=#{chargeType} and left_num>0 ",
            "<when test='doctorCode!=null'>",
            " and doctor_code=#{doctorCode} ",
            "</when>",
            "</script>"})
    List<MzyRequest> selectRequestByRequestDay(@Param("requestDay") String requestDay, @Param("unitCode") String unitCode, @Param("ampm") String ampm, @Param("chargeType") String chargeType, @Param("doctorCode") String doctorCode);

    /**
     * 退号时需要将号表数据返回
     *
     * @param id
     * @return
     */
    @Update("update mzy_request_new set bespeak_no=bespeak_no-1,left_num=left_num+1 where id=#{id}")
    int updateLeftNumById(@Param("id") Long id);


    /**
     * 按时间区间查询号表数据
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    @Select({"<script>",
            "  SELECT id,request_day,ampm,unit_code,group_code,doctor_code,charge_type,total_num,op_id,left_num,check_fee,bespeak_no " +
                    "    FROM mzy_request_new " +
                    " where " +
                    "         request_day <![CDATA[>=]]> #{beginDate,jdbcType=TIMESTAMP} and " +
                    "         request_day <![CDATA[<=]]> #{endDate,jdbcType=TIMESTAMP} " +
            "<when test='unitCode!=null'>",
            " and unit_code=#{unitCode} ",
            "</when>",
            "<when test='ampm!=null'>",
            " and ampm=#{ampm} ",
            "</when>",
            "order by request_day,unit_code,doctor_code,ampm ",
            "</script>"})
    List<MzyRequest> selectByTimes(@Param("beginDate") Date beginDate, @Param("endDate") Date endDate, @Param("unitCode") String unitCode, @Param("ampm") String ampm);


    /**
     * 更新号表
     *
     * @param mzyRequest
     * @return
     */
    @Update("if not exists (select id  from mzy_request_new where request_day=#{requestDay,jdbcType=DATE} and ampm = #{ampm,jdbcType=CHAR} and unit_code=#{unitCode} and group_code=#{groupCode} and charge_type=#{chargeType} and doctor_code=#{doctorCode} and id <> #{id,jdbcType=BIGINT} ) "+
            "update mzy_request_new set request_day=#{requestDay,jdbcType=DATE},ampm=#{ampm},unit_code=#{unitCode},doctor_code=#{doctorCode},charge_type=#{chargeType},total_num=#{totalNum},left_num=#{leftNum,jdbcType=INTEGER}, bespeak_no=#{bespeakNo,jdbcType=SMALLINT},check_fee=#{checkFee} where id = #{id,jdbcType=BIGINT}")
    int updateMzyRequestNew(MzyRequest mzyRequest);



    /**
     * 科室号源统计
     * @param beginDate
     * @param endDate
     * @param unitCode
     * @return
     */
    @Select("select CONVERT (varchar (100), request_day, 23) scheduleDate,sum(left_num) leftNum,sum(total_num) totalNum from mzy_request_new where unit_code =#{unitCode} and request_day >=#{beginDate,jdbcType=TIMESTAMP} and request_day <=#{endDate,jdbcType=TIMESTAMP} and isnull(doctor_code,'')<>''  and charge_type not in (16,24) group by request_day")
    List<Map<String,Object>> selectNumByDateAndDept(@Param("beginDate") Date beginDate, @Param("endDate") Date endDate, @Param("unitCode") String unitCode);



    /**
     * 查询可以挂号的科室内医生信息
     * @param requestDay 挂号日期
     * @param ampms 当上下午不为空时，查询不在入参数集合的所有的号源
     * @param unitCode 科室编码
     * @return
     */
    @Select({"<script>",
            "select doctor_code doctorCode,charge_type chargeType,check_fee checkFee,sum(left_num) leftNum from mzy_request_new where unit_code=#{unitCode} and request_day=#{requestDay,jdbcType=TIMESTAMP}  and del_flag=0  and charge_type not in (16,24)" ,
            "<when test='ampms!=null'>",
            " and ampm  not in " ,
            "<foreach item='item' index='index' collection='ampms' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</when>",
                    "  group by doctor_code,charge_type,check_fee",
            "</script>"})
    List<Map<String,Object>> selectDoctorByDateAndDept(@Param("requestDay") Date requestDay, @Param("ampms") List<String> ampms, @Param("unitCode") String unitCode);




    /**
     * 查询可以挂号的科室内医生信息
     * @param requestDay 挂号日期
     * @param ampms 当上下午不为空时，查询不在入参数集合的所有的号源
     * @param unitCode 科室编码
     * @param doctorCode 医生编码
     * @return
     */
    @Select({"<script>",
            "select id mzyRequestId,ampm,doctor_code doctorCode,charge_type chargeType,check_fee checkFee,left_num leftNum from mzy_request_new where unit_code=#{unitCode} and request_day=#{requestDay,jdbcType=TIMESTAMP} and doctor_code  =#{doctorCode} and del_flag=0  and charge_type not in (16,24)" ,
            "<when test='ampms!=null'>",
            " and ampm  not in " ,
            "<foreach item='item' index='index' collection='ampms' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</when>",
            "</script>"})
    List<Map<String,Object>> selectRequestByDateAndDeptAndDoctor(@Param("requestDay") Date requestDay, @Param("ampms") List<String> ampms, @Param("unitCode") String unitCode, @Param("doctorCode") String doctorCode);

    /**
     * 删除号表数据
     * @param id
     * @return
     */

    @Delete("delete from mzy_request_new where id=#{id,jdbcType=BIGINT}")
    int  deleteMzyRequestById(@Param("id") Long id);

}
