package thyyxxk.webserver.dao.his.heliyongyao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.heliyongyao.RationalUsePatient;
import thyyxxk.webserver.entity.heliyongyao.XunHuanJieDian;
import thyyxxk.webserver.entity.heliyongyao.YaoPin;
import thyyxxk.webserver.entity.zhuyuanyisheng.yizhuluru.XinZhenYzActOrder;

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

/**
 * <p>
 * 描述： 合理用药
 * </p>
 *
 * @author xc
 * @date 2022-03-29 11:27
 */
@Mapper
public interface RationalUseDao {

    @Select("select          dept_code  , " +
            "    dept_name = (select rtrim(name) from zd_unit_code where zd_unit_code.code = dept_code), " +
            "                code, " +
            "                rtrim(name)     name, " +
            "                emp_tit_code as type, " +
            "    type_name = (select rtrim(name) from zd_emp_title where zd_emp_title.code = emp_tit_code) " +
            "from a_employee_mi " +
            "where code = #{code}  ")
    Map<String, String> yiShenXinXi(String code);


    @Select("select pati_id = rtrim(a.inpatient_no) + '_' + cast(admiss_times as varchar), " +
            "                 rtrim(a.name) name, " +
            "       birth=(convert(varchar(10), birth_date, 21)), " +
            "                 a.sex, " +
            "                 social_no as  id_card " +
            "from zy_actpatient a, " +
            "     a_patient_mi b " +
            "where a.inpatient_no = b.inpatient_no " +
            "  and a.inpatient_no = #{patNo} " +
            "  and a.admiss_times = #{times}")
    RationalUsePatient huanZheXinXi(@Param("patNo") String patNo,
                                    @Param("times") Integer times);


    @Select("select                     a.act_order_no, " +
            "                           cast(cast(a.act_order_no as decimal) as varchar) id, " +
            "                           order_code, " +
            "                           order_name, " +
            "                           rtrim(frequ_code)                                frequ_code, " +
            "    frequ_code_name      = (select rtrim(comm) " +
            "                            from yz_order_frequency " +
            "                            where code = frequ_code), " +
            "                           instruction, " +
            "                           discription, " +
            "                           infant_flag, " +
            "                           order_time, " +
            "                           physician, " +
            "                           start_time, " +
            "                           drug_specification, " +
            "                           drug_quan, " +
            "    drug_quan_name       = (select name " +
            "                            from yp_zd_unit with (NOLOCK) " +
            "                            where code = mini_unit), " +
            "                           supply_code, " +
            "    supply_code_name     = (select supply_name " +
            "                            from yz_supply_type with (NOLOCK) " +
            "                            where yz_supply_type.supply_code = a.supply_code), " +
            "                           a.inpatient_no, " +
            "                           a.admiss_times, " +
            "                           drug_flag, " +
            "                           enter_oper, " +
            "    enter_oper_name      = (select top (1) rtrim(name) " +
            "                            from a_employee_mi with (NOLOCK) " +
            "                            where code = enter_oper) /* 录入人 */, " +
            "                           enter_time, " +
            "                           mini_unit, " +
            "                           drug_weight, " +
            "                           drug_weight_unit, " +
            "                           rtrim(status_flag)                               status_flag, " +
            "                           status_time, " +
            "                           parent_no, " +
            "    parent_no_name       = (select kfc.order_name from yz_act_order kfc where kfc.act_order_no = a.parent_no), " +
            "                           self_buy, " +
            "                           dose, " +
            "                           dose_unit, " +
            "    dose_unit_name       = (select name " +
            "                            from yp_zd_unit with (NOLOCK) " +
            "                            where dose_unit = code), " +
            "                           drug_occ, " +
            "                           drug_volume, " +
            "                           drug_vol_unit, " +
            "                           exclu_back_time, " +
            "                           exclu_act_order_no, " +
            "                           pay_self, " +
            "                           serial, " +
            "                           group_no, " +
            "                           signer, " +
            "                           cl_code, " +
            "                           doctor_flag, " +
            "                           emergency_flag, " +
            "    exclu_type='', " +
            "    end_time_b=end_time, " +
            "                           exec_unit, " +
            "                           dept_code, " +
            "                           ward_code, " +
            "    new_flag='0', " +
            "    order_no_pattern=0, " +
            "    suprice_flag='', " +
            "    self_flag            ='', " +
            "    pack_size=0.0, " +
            "    parent_no_c=           -1000, " +
            "                           reg_flag, " +
            "    parent_flag='', " +
            "                           yb_self_flag, " +
            "                           kf_flag, " +
            "                           refer_physician, " +
            "                           consult_physician, " +
            "    physician_name       = (select top (1) rtrim(name) " +
            "                            from a_employee_mi with (NOLOCK) " +
            "                            where code = physician) /* 开这个医嘱的医生 */, " +
            "    exec_unit_name       = (select top (1) rtrim(name) " +
            "                            from zd_unit_code with (NOLOCK) " +
            "                            where code = exec_unit), " +
            "    /*管床医生*/ " +
            "    refer_physician_name = (select rtrim(name) " +
            "                            from a_employee_mi with (NOLOCK) " +
            "                            where code = refer_physician), " +
            "    group_no_name        = (select rtrim(group_name) " +
            "                            from yp_zd_group_name with (NOLOCK) " +
            "                            where a.group_no = group_no), " +
            "    signer_name          = (select top (1) rtrim(name) " +
            "                            from a_employee_mi with (NOLOCK) " +
            "                            where code = signer) /*确认人*/, " +
            "                           confirm_time /*确认时间*/, " +
            "                           modifier, " +
            "    modifier_name        = (select top (1) rtrim(name) " +
            "                            from a_employee_mi with (NOLOCK) " +
            "                            where code = modifier) /*停止人*/, " +
            "                           end_time /*停止时间*/, " +
            "                           b.new_ward as                                    zkWardCode, " +
            "                           b.new_dept as                                    zkDeptCode, " +
            "                           kss.yyfs, " +
            "                           kss.ssqk, " +
            "                           kss.yysj, " +
            "    executer1            = (select name from a_employee_mi where code = executer1), " +
            "    executer2=             (select name from a_employee_mi where code = executer2) " +
            "from yz_act_order a with (NOLOCK) " +
            "         left join " +
            "     zy_order_zk b with (NOLOCK) on (a.act_order_no = b.act_order_no) " +
            "         left join " +
            "     yz_act_record_kss kss on (a.act_order_no = kss.act_order_no) " +
            "where convert(varchar(10), order_time, 120) = CONVERT(varchar(10), getdate(), 120) " +
            "  and a.inpatient_no = #{patNo} " +
            "  and a.admiss_times = #{times} " +
            "  and frequ_code = 'ONCE' " +
            "  and status_flag > '1' " +
            "  and serial <> '00' " +
            "union all " +
            "select                     a.act_order_no, " +
            "                           cast(cast(a.act_order_no as decimal) as varchar) id, " +
            "                           order_code, " +
            "                           order_name, " +
            "                           rtrim(frequ_code)                                frequ_code, " +
            "    frequ_code_name      = (select rtrim(comm) " +
            "                            from yz_order_frequency " +
            "                            where code = frequ_code), " +
            "                           instruction, " +
            "                           discription, " +
            "                           infant_flag, " +
            "                           order_time, " +
            "                           physician, " +
            "                           start_time, " +
            "                           drug_specification, " +
            "                           drug_quan, " +
            "    drug_quan_name       = (select name " +
            "                            from yp_zd_unit with (NOLOCK) " +
            "                            where code = mini_unit), " +
            "                           supply_code, " +
            "    supply_code_name     = (select supply_name " +
            "                            from yz_supply_type with (NOLOCK) " +
            "                            where yz_supply_type.supply_code = a.supply_code), " +
            "                           a.inpatient_no, " +
            "                           a.admiss_times, " +
            "                           drug_flag, " +
            "                           enter_oper, " +
            "    enter_oper_name      = (select top (1) rtrim(name) " +
            "                            from a_employee_mi with (NOLOCK) " +
            "                            where code = enter_oper) /* 录入人 */, " +
            "                           enter_time, " +
            "                           mini_unit, " +
            "                           drug_weight, " +
            "                           drug_weight_unit, " +
            "                           rtrim(status_flag)                               status_flag, " +
            "                           status_time, " +
            "                           parent_no, " +
            "    parent_no_name       = (select kfc.order_name from yz_act_order kfc where kfc.act_order_no = a.parent_no), " +
            "                           self_buy, " +
            "                           dose, " +
            "                           dose_unit, " +
            "    dose_unit_name       = (select name " +
            "                            from yp_zd_unit with (NOLOCK) " +
            "                            where dose_unit = code), " +
            "                           drug_occ, " +
            "                           drug_volume, " +
            "                           drug_vol_unit, " +
            "                           exclu_back_time, " +
            "                           exclu_act_order_no, " +
            "                           pay_self, " +
            "                           serial, " +
            "                           group_no, " +
            "                           signer, " +
            "                           cl_code, " +
            "                           doctor_flag, " +
            "                           emergency_flag, " +
            "    exclu_type='', " +
            "    end_time_b=end_time, " +
            "                           exec_unit, " +
            "                           dept_code, " +
            "                           ward_code, " +
            "    new_flag='0', " +
            "    order_no_pattern=0, " +
            "    suprice_flag='', " +
            "    self_flag            ='', " +
            "    pack_size=0.0, " +
            "    parent_no_c=           -1000, " +
            "                           reg_flag, " +
            "    parent_flag='', " +
            "                           yb_self_flag, " +
            "                           kf_flag, " +
            "                           refer_physician, " +
            "                           consult_physician, " +
            "    physician_name       = (select top (1) rtrim(name) " +
            "                            from a_employee_mi with (NOLOCK) " +
            "                            where code = physician) /* 开这个医嘱的医生 */, " +
            "    exec_unit_name       = (select top (1) rtrim(name) " +
            "                            from zd_unit_code with (NOLOCK) " +
            "                            where code = exec_unit), " +
            "    /*管床医生*/ " +
            "    refer_physician_name = (select rtrim(name) " +
            "                            from a_employee_mi with (NOLOCK) " +
            "                            where code = refer_physician), " +
            "    group_no_name        = (select rtrim(group_name) " +
            "                            from yp_zd_group_name with (NOLOCK) " +
            "                            where a.group_no = group_no), " +
            "    signer_name          = (select top (1) rtrim(name) " +
            "                            from a_employee_mi with (NOLOCK) " +
            "                            where code = signer) /*确认人*/, " +
            "                           confirm_time /*确认时间*/, " +
            "                           modifier, " +
            "    modifier_name        = (select top (1) rtrim(name) " +
            "                            from a_employee_mi with (NOLOCK) " +
            "                            where code = modifier) /*停止人*/, " +
            "                           end_time /*停止时间*/, " +
            "                           b.new_ward as                                    zkWardCode, " +
            "                           b.new_dept as                                    zkDeptCode, " +
            "                           kss.yyfs, " +
            "                           kss.ssqk, " +
            "                           kss.yysj, " +
            "    executer1            = (select name from a_employee_mi where code = executer1), " +
            "    executer2=             (select name from a_employee_mi where code = executer2) " +
            "from yz_act_order a with (NOLOCK) " +
            "         left join " +
            "     zy_order_zk b with (NOLOCK) on (a.act_order_no = b.act_order_no) " +
            "         left join " +
            "     yz_act_record_kss kss on (a.act_order_no = kss.act_order_no) " +
            "where " +
            "  a.inpatient_no = #{patNo} " +
            "  and a.admiss_times = #{times} " +
            "  and frequ_code <> 'ONCE' " +
            "  and status_flag in ('2','3','4') " +
            "  and serial <> '00' ")
    List<XinZhenYzActOrder> getHistoricalMedicalOrders(@Param("patNo") String patNo,
                                                       @Param("times") Integer times);

    @Select("select allergen_code code, b.name as name, case b.class when 'A' then '1' when 'B' then '2' end as type " +
            "from patient_allergen_info a, " +
            "     zd_allergen b " +
            "where a.allergen_code = b.code " +
            "  and pat_no = #{patNo} " +
            "  and a.type = 0 " +
            "union all " +
            "select allergen_code, (select name from yp_zd_dict where yp_zd_dict.code = allergen_code) as allergenName, type = 5 " +
            "from patient_allergen_info " +
            "where pat_no = #{patNo} " +
            "  and type = 1")
    List<XunHuanJieDian> guoMin(@Param("patNo") String patNo);

    @Select("select top 1 emr_data_element\n" +
            "from emr_patient_data\n" +
            "where pat_no = #{patNo}\n" +
            "  and times = #{times}\n" +
            "  and emr_category_code = 'ruyuanjiluzhuanyong'\n" +
            "  and del_flag = 0 ")
    String medicalRecordDiagnosticData(@Param("patNo") String patNo,
                                       @Param("times") Integer times);

    @Select("select national_code, rtrim(pzwh) pzwh, isnull(pack_retprice,1) pack_retprice,rtrim(code) + rtrim(serial) as id " +
            "from yp_zd_dict " +
            "${ew.customSqlSegment}")
    List<YaoPin> selectDrug(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

}
