package thyyxxk.webserver.dao.his.zhuyuanyisheng;

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.casefrontsheet.YshHzRecord;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.inpatient.inpatientcost.ZyDetailChargeTable;
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();

    /**
     * 插入会诊申请
     *
     * @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 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 ( '06054', #{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);


    /**
     * 获取到最大的 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);


    @Select("select code, emp_tit_code " +
            "from a_employee_mi " +
            "where dept_code = #{deptCode} " +
            "  and isnull(del_flag, 0) = 0 " +
            "  and emp_tit_code in ('001', '002', '003')")
    List<String> obtainDepartmentDoctor(String deptCode);

    @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, " +
            "(select rtrim(name) from ysh_zd_hz_level where code = hz_level) as hz_level_name,hz_comment,status_flag, " +
            "input_date,req_times,hz_type " +
            "from  ysh_hz_record a with(nolock) " +
            "where  inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} " +
            "<if test=\" actOrderNo != null \">" +
            "and act_order_no = #{actOrderNo} " +
            "</if>" +
            " order by act_order_no desc " +
            "</script>")
    List<YshHzRecord> chaKanHuiZhenShenQing(@Param("inpatientNo") String inpatientNo,
                                            @Param("admissTimes") int admissTimes,
                                            @Param("actOrderNo") BigDecimal actOrderNo);

    @Select("select ledger_sn, detail_sn from zy_detail_charge " +
            "where " +
            "inpatient_no = #{patNo} " +
            "and admiss_times = #{times} " +
            "and order_no = #{orderNo} and ori_detail_sn is null " +
            "and charge_fee > 0")
    List<ZyDetailChargeTable> selectChargeByOrderNo(String patNo,
                                                    Integer times,
                                                    BigDecimal orderNo);


    /**
     * 删除 会诊申请
     *
     * @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);

    @Delete("delete  from yz_zy_patient_fee where inpatient_no = '${patNo}' AND admiss_times = ${times} and act_order_no = #{orderNo}")
    void deletePatientFee(String patNo, Integer times, BigDecimal orderNo);


    @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, " +
            "       rtrim(name) as name, " +
            "       dept_code, " +
            "       dept_name    = (select rtrim(name) from zd_unit_code where zd_unit_code.code = dept_code), " +
            "       emp_tit_code= right(emp_tit_code, 1), " +
            "       empTitName = (select rtrim(name) from zd_emp_title where zd_emp_title.code = emp_tit_code), " +
            "       code_rs " +
            "from a_employee_mi " +
            "where isnull(del_flag " +
            "          , 0) = 0 " +
            "  and emp_tit_code in ('001', '002', '003')" +
            "  and nullif(yb_code,'') is not null  " +
            "  and (name like #{name} " +
            "    or py_code like #{name} " +
            "    or d_code like #{name})")
    List<UserInfo> getConsultingDoctorInfo(@Param("name") String name);


    @Select("select data_element " +
            "from emr_data_element " +
            "where pat_no = #{patNo} " +
            "  and times = #{times}")
    JSONObject getPatientEmrDiag(String patNo,
                                 Integer times);

}
