package thyyxxk.webserver.dao.his.casefrontsheet;

import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.casefrontsheet.YshHzRecord;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.login.UserInfo;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 描述： 会诊申请
 * </p>
 *
 * @author xc
 * @date 2021-05-07 11:01
 */
@Mapper
public interface HuiZhenShenQingDao {
    /**
     * 获取科室
     *
     * @return 返回科室
     */
    @Select("select code,rtrim(name) name from zy_ward_code with(nolock) where py_code like #{pyCode} or name like #{pyCode} or code like #{pyCode}")
    List<GetDropdownBox> getZdUnitCode(String pyCode);

    /**
     * 获取会诊类别
     *
     * @return 返回科室
     */
    @Select("select code,rtrim(name) name from ysh_zd_hz_level with(nolock)")
    List<GetDropdownBox> getYshZdHzLevel();

    /**
     * 把表锁住
     */
    @Update("update yz_sequence set act_order_no = act_order_no")
    void lockTable();

    /**
     * 查询医嘱
     *
     * @return 返回医嘱号
     */
    @Select("select act_order_no from yz_sequence with(nolock)")
    float getActOrderNo();

    /**
     * 插入新的医嘱
     *
     * @param actOrderNo 医嘱号
     */
    @Update("update yz_sequence set act_order_no = #{actOrderNo}")
    void updateActOrderNo(float actOrderNo);

    /**
     * 插入会诊申请
     *
     * @param param 要插入的实体类
     */
    @Insert("INSERT INTO ysh_hz_record  " +
            "(inpatient_no, admiss_times, req_times, req_comment, input_id, input_date, req_date, dept_code, ward_code," +
            " req_dept1, status_flag, hz_type, hz_level, hz_zd, hz_md, act_order_no , hz_doctor1,hz_id ) " +
            "VALUES ( #{inpatientNo}, #{admissTimes}, #{reqTimes}, #{reqComment}, #{inputId}, #{inputDate}, #{reqDate}, " +
            "#{deptCode}, #{wardCode}, #{reqDept1}, '1', #{hzType}, #{hzLevel}, #{hzZd}, #{hzMd}, #{actOrderNo} ,#{hzDoctor1},#{hzDoctor1})")
    void chaRuHuiZhenShenQing(YshHzRecord param);

    /**
     * 查看是否有重复的
     *
     * @param inpatientNo 住院号
     * @return 返回一条或者没有
     */
    @Select("select count(1) from ysh_hz_record with(nolock) where inpatient_no = #{inpatientNo}")
    Integer chaKanChongFu(String inpatientNo);

    /**
     * 获取到最大的 req_times 每次插入都要加一
     *
     * @param inpatientNo 住院号
     * @param admissTimes 住院次数
     * @return 返回最大的  req_times
     */
    @Select("select  isnull(max ( req_times ),0) from ysh_hz_record with(nolock) where inpatient_no = #{inpatientNo} and admiss_times =#{admissTimes} ")
    Integer getMaxReqTimes(@Param("inpatientNo") String inpatientNo,
                           @Param("admissTimes") int admissTimes);


    /**
     * 插入一条医嘱
     *
     * @param param 参数
     */
    @Insert("INSERT INTO yz_act_order ( order_code, order_name, frequ_code, infant_flag, order_time, physician, start_time,  " +
            "act_order_no, inpatient_no, admiss_times, drug_flag, enter_oper, enter_time, status_flag, self_buy, pay_self, " +
            "serial, group_no, doctor_flag, exec_unit, dept_code, ward_code, reg_flag,confirm_time,signer,emergency_flag,yb_self_flag)  " +
            "VALUES ( #{orderCode}, #{orderName}, 'ONCE', '0', #{inputDate}, #{inputId}, #{inputDate}, " +
            "#{actOrderNo}, #{inpatientNo}, #{admissTimes}, 'o', #{inputId}, #{inputDate}, '2', '0', '0', '00', '00', '1'," +
            " #{reqDept1}, #{deptCode}, #{wardCode}, '',#{inputDate}, #{inputId},#{emergencyFlag},#{ybSelfFlag})")
    void chaRuYiZhu(YshHzRecord param);

    @Select("<script>" +
            "select inpatient_no,admiss_times,act_order_no, " +
            "(select rtrim(name) from a_employee_mi with(nolock) where code = input_id) input_name," +
            "(select rtrim(name) from zd_unit_code with(nolock) where code = a.dept_code) dept_name, " +
            "(select rtrim(name) from zd_unit_code with(nolock) where  code = req_dept1) req_dept_name,hz_date," +
            "(select rtrim(name) from a_employee_mi with(nolock) where code = hz_doctor1) hz_doctor1_name," +
            "(select rtrim(name) from a_employee_mi with(nolock) where code = hz_doctor2) hz_doctor2_name, " +
            "rtrim(c.name) hz_level_name,hz_comment,status_flag, " +
            "input_date,req_times,hz_type " +
            "from  ysh_hz_record a with(nolock) " +
            "inner join ysh_zd_hz_level c on c.code = a.hz_level " +
            "where  inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} " +
            "<if test=\" actOrderNo != null \">" +
            "and act_order_no = #{actOrderNo} " +
            "</if>" +
            "</script>")
    List<YshHzRecord> chaKanHuiZhenShenQing(@Param("inpatientNo") String inpatientNo,
                                            @Param("admissTimes") int admissTimes,
                                            @Param("actOrderNo") BigDecimal actOrderNo);

    /**
     * 删除 会诊申请
     *
     * @param inpatientNo 住院号
     * @param admissTimes 住院次数
     * @param reqTimes    申请次数
     */
    @Delete("delete ysh_hz_record where inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and req_times = #{reqTimes}")
    void shanChuShenQing(@Param("inpatientNo") String inpatientNo,
                         @Param("admissTimes") int admissTimes,
                         @Param("reqTimes") int reqTimes);

    /**
     * 删除医嘱
     *
     * @param actOrderNo 医嘱号
     */
    @Delete("DELETE FROM yz_act_order WHERE act_order_no = #{actOrderNo} ")
    void shanChuYiZhu(@Param("actOrderNo") BigDecimal actOrderNo);


    @Select(" select code,name, " +
            " dept_name = (select rtrim(name) from zd_unit_code where zd_unit_code.code = dept_code)," +
            " emp_tit_name  = (select rtrim(name) from zd_emp_title where zd_emp_title.code = emp_tit_code) " +
            " from a_employee_mi with(nolock) where dept_code = #{deptCode} and emp_tit_code = #{hzType} and isnull(del_flag,0)=0")
    List<GetDropdownBox> huiZhenYiSheng(@Param("deptCode") String deptCode,
                                        @Param("hzType") String hzType);


    @Select("select code,\n" +
            "       rtrim(name) as name,\n" +
            "       dept_code,\n" +
            "       dept_name    = (select rtrim(name) from zd_unit_code where zd_unit_code.code = dept_code),\n" +
            "       emp_tit_code= right(emp_tit_code, 1),\n" +
            "       empTitName = (select rtrim(name) from zd_emp_title where zd_emp_title.code = emp_tit_code),\n" +
            "       code_rs\n" +
            "from a_employee_mi\n" +
            "where isnull(del_flag\n" +
            "          , 0) = 0\n" +
            "  and emp_tit_code in ('001', '002', '003')\n" +
            "  and (name like #{name}\n" +
            "    or py_code like #{name}\n" +
            "    or d_code like #{name})")
    List<UserInfo> getConsultingDoctorInfo(@Param("name") String name);

    @Select("select rtrim(dis_diag) code, rtrim(dis_diag_comment) name " +
            "from ${tableName} " +
            "where inpatient_no = #{patNo} " +
            "  and admiss_times = #{times} " +
            "order by dis_diag_no")
    List<GetDropdownBox> huoQuHuanZheZhuYaoZhenDuan(@Param("patNo") String patNo,
                                                    @Param("times") Integer times,
                                                    @Param("tableName") String tableName);

}
