package thyyxxk.webserver.dao.his.reports;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.reports.deptperiodreport.DeptEarn;
import thyyxxk.webserver.entity.reports.deptperiodreport.DoctorEarn;
import thyyxxk.webserver.entity.reports.deptperiodreport.PatientBill;
import thyyxxk.webserver.entity.reports.deptperiodreport.QueryDeptEarnParam;

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

@Mapper
public interface DeptPeriodReportDao {
    @Select("select rtrim(ltrim(ward_code)) code,ltrim(rtrim(ward_name)) name " +
            "from zy_adtward where ward_code not like '8%'")
    List<CodeName> getAllWards();

    @Select("select " +
            "case when isnull(a.refer_physician,'')='' " +
            "then (case when isnull(a.doctor_code,'')='' then a.op_id_code else a.doctor_code end) " +
            "else a.refer_physician end doctor_code, sum(charge_fee) totalCharge, " +
            "deptCode=#{dept}," +
            "deptName=(select rtrim(name) from zd_unit_code where code=#{dept})," +
            "b.name doctor_name " +
            "from zy_detail_charge a left join a_employee_mi b on b.code=(case when isnull(a.refer_physician,'')='' " +
            "then (case when isnull(a.doctor_code,'')='' then a.op_id_code else a.doctor_code end) " +
            "else a.refer_physician end) " +
            "where a.charge_date>=#{start} " +
            "and a.charge_date<#{end} " +
            "and a.ward_code=#{dept} " +
            "group by " +
            "case when isnull(a.refer_physician,'')='' " +
            "then (case when isnull(a.doctor_code,'')='' then a.op_id_code else a.doctor_code end) " +
            "else a.refer_physician end, b.name ")
    List<DeptEarn> getSimpleDeptEarn(@Param("start") Date start,
                                     @Param("end") Date end,
                                     @Param("dept") String dept);

    @Select("select " +
            "deptCode=#{deptCode}, doctorCode=#{doctorCode}, " +
            "deptName=(select rtrim(name) from zd_unit_code where code=#{deptCode})," +
            "doctorName=(select rtrim(name) from a_employee_mi where code=#{doctorCode})," +
            "a.inpatient_no,a.admiss_times,b.name patientName,sum(a.charge_fee) chargeFee " +
            "from zy_detail_charge a " +
            "left join a_patient_mi b on a.inpatient_no=b.inpatient_no " +
            "where a.charge_date>=#{start} " +
            "and a.charge_date<#{end} " +
            "and a.ward_code=#{deptCode} " +
            "and (case when isnull(a.refer_physician,'')='' " +
            "then (case when isnull(a.doctor_code,'')='' then a.op_id_code else a.doctor_code end) " +
            "else a.refer_physician end )=#{doctorCode} " +
            "group by a.inpatient_no,a.admiss_times,b.name " +
            "order by sum(charge_fee) desc")
    List<DoctorEarn> getDoctorEarn(QueryDeptEarnParam param);

    @Select("select " +
            "isnull(c.name,d.name) charge_name," +
            "convert(varchar(30),convert(decimal(20,0), a.order_no)) order_no, " +
            "sum(a.charge_amount) amount, " +
            "sum(charge_fee) chargeFee " +
            "from zy_detail_charge a " +
            "left join zd_charge_item c on a.charge_code_mx=c.code " +
            "left join yp_zd_dict d on a.charge_code_mx=d.code and a.serial=d.serial " +
            "where a.charge_date>=#{start} " +
            "and a.charge_date<#{end} " +
            "and a.ward_code=#{deptCode} " +
            "and a.inpatient_no=#{inpatientNo} " +
            "and a.admiss_times=#{admissTimes} " +
            "and (case when isnull(a.refer_physician,'')='' " +
            "then (case when isnull(a.doctor_code,'')='' then a.op_id_code else a.doctor_code end) " +
            "else a.refer_physician end )=#{doctorCode} " +
            "group by a.order_no,c.name,d.name")
    List<PatientBill> getPatientBill(QueryDeptEarnParam param);

    @Select("select rtrim(name) from a_employee_mi where code=#{code}")
    String getDoctorName(@Param("code") String code);

    @Select("select rtrim(name) from a_patient_mi where inpatient_no=#{code}")
    String getPatientName(@Param("code") String code);
}
