package thyyxxk.webserver.dao.his.highreport;


import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

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

@Mapper
public interface AllStatisticsDao {
    @Select("<script> " +
            "select " +
            "   d.parent_code as flm, " +
            "   d.flmc, " +
            "   c.audit_code as hsm, " +
            "   d.name as hsmc, " +
            "   a.${suffer} as ksid, " +
            "   f.name as ksmc, " +
            "   sum(a.charge_fee) as totalCharge  " +
            "from " +
            "   zy_detail_charge a (nolock) left join zd_unit_code f on a.${suffer} = f.code, " +
            "   zd_charge_item c " +
            "left join (select s.code, s.name, s.parent_code, t.name as flmc " +
            "           from zy_zd_audit_item s, zy_zd_audit_report t where s.parent_code= t.code ) d " +
            "           on c.audit_code = d.code " +
            "where a.charge_code_mx = c.code  " +
            "and a.gen_time &gt;= #{startTime}  " +
            "and a.gen_time &lt;= #{endTime}  " +
            "group by " +
            "   d.parent_code, " +
            "   d.flmc, " +
            "   c.audit_code, " +
            "   d.name, " +
            "   a.${suffer}, " +
            "   f.name " +
            "union all " +
            "select " +
            "   case when c.bill_item_zy in('001','028') then 'yp1' when c.bill_item_zy= '002' then 'yp2' end as flm, " +
            "   case when c.bill_item_zy in('001','028') then '西成药' when c.bill_item_zy= '002' then '中草药' end flmc, " +
            "   c.drug_kind as hsm, " +
            "   d.name as hsmc, " +
            "   a.${suffer} as ksid, " +
            "   f.name as ksmc, " +
            "   sum(a.charge_fee) as totalCharge  " +
            "from " +
            "   zy_detail_charge a (nolock) left join zd_unit_code f on a.${suffer} = f.code, " +
            "   yp_zd_dict c left join yp_zd_drug_kind d on c.drug_kind = d.code " +
            "where " +
            "   a.charge_code_mx = c.code  " +
            "and a.serial = c.serial " +
            "and a.gen_time &gt;= #{startTime} " +
            "and a.gen_time &lt;= #{endTime} " +
            "group by " +
            "   c.bill_item_zy, " +
            "   d.name, " +
            "   c.drug_kind, " +
            "   a.${suffer}, " +
            "   f.name " +
            "order by flm, hsm " +
            "</script>")
    List<Map<String, Object>> selectInpatientDeptAccountingReport(
            @Param("startTime") String startTime, @Param("endTime") String endTime, @Param("suffer") String suffer);

    @Select("select " +
            "   z.name as ksmc, " +
            "   z.code as ksid, " +
            "   sum ( a.charge_fee ) as totalCharge, " +
            "   max ( d.parent_code ) as flm, " +
            "   max ( e.name ) as flmc, " +
            "   max ( d.name ) as hsmc, " +
            "   c.audit_code as hsm " +
            "from " +
            "   zy_detail_charge a (nolock) " +
            "   join zd_charge_item c on a.charge_code = c.code " +
            "   left join zy_zd_audit_item d on c.audit_code = d.code " +
            "   join zy_zd_audit_report e on d.parent_code = e.code " +
            "   join zy_patient f on a.inpatient_no = f.inpatient_no and a.admiss_times = f.admiss_times " +
            "   left join zd_unit_code z on (case when isnull(a.exec_unit, '') = '' then " +
            "       (case when isnull(f.zk_ward, '') = '' then a.ward_code else f.zk_ward end) else a.exec_unit end) = z.code " +
            "where " +
            "   a.charge_date >= #{startTime} " +
            "   and a.charge_date <= #{endTime} " +
            "   and a.charge_status >= 2 " +
            "group by " +
            "   z.name, " +
            "   z.code, " +
            "   c.audit_code")
    List<Map<String, Object>> selectInpatientDeptAccountingReportZyExecDept(
            @Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select("select " +
            "   z.name as ksmc, " +
            "   z.code as ksid, " +
            "   sum(a.charge_fee) as totalCharge, " +
            "   max(d.parent_code) as flm, " +
            "   max(e.name) as flmc, " +
            "   max(d.name) as hsmc, " +
            "   c.audit_code as hsm " +
            "from " +
            "   zy_detail_charge a (nolock) " +
            "   join zd_charge_item c on a.charge_code = c.code " +
            "   left join zy_zd_audit_item d on c.audit_code = d.code " +
            "   join zy_zd_audit_report e on d.parent_code = e.code " +
            "   left join zd_unit_code z on a.ward_code = z.code " +
            "   join zy_patient f on a.inpatient_no = f.inpatient_no and a.admiss_times = f.admiss_times " +
            "where " +
            "   a.charge_date >= #{startTime} " +
            "   and a.charge_date <= #{endTime} " +
            "   and a.charge_status >= 2 " +
            "group by " +
            "   z.name, " +
            "   z.code, " +
            "   c.audit_code")
    List<Map<String, Object>> selectInpatientDeptAccountingReportZyWardCode(
            @Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select(" select z.name as ksmc, " +
            "        z.code as ksid, " +
            "        max(d.parent_code) as flm, " +
            "        max(e.name) as flmc, " +
            "        isnull(sum(round(convert(decimal(18,3),((convert(decimal(18,3),unit_price)) * (quantity * isnull(drug_win,1)))),2)),0) as totalCharge, " +
            "        max(d.name) as hsmc, " +
            "        c.audit_code as hsm " +
            "   from mz_charge_detail a(nolock) " +
            "   join (select distinct patient_id,times,receipt_no, serial_no,receipt_sn from mz_receipt_serial " +
            "         where charge_date >= #{startTime} " +
            "           and charge_date <= #{endTime}) as b " +
            "     on a.patient_id = b.patient_id " +
            "    and a.times = b.times " +
            "    and a.receipt_no = b.receipt_no " +
            "    and a.real_no = b.receipt_sn " +
            "    and a.serial_no = b.serial_no " +
            "   left join zd_charge_item c " +
            "     on case a.charge_bill_code " +
            "          when 'BILL10' then 'BILL01' " +
            "          when 'BILL11' then 'BILL28' " +
            "          when 'BILL12' then 'BILL02' " +
            "          else a.charge_bill_code " +
            "        end = c.code " +
            "   join zy_zd_audit_item d " +
            "     on isnull(c.audit_code, d.code) = d.code " +
            "   join zy_zd_audit_report e " +
            "     on d.parent_code = e.code " +
            "   left join zd_unit_code z " +
            "     on case when isnull(a.exec_dept,'') = '' or a.exec_dept = 'null' " +
            "             then a.warn_dept " +
            "             else a.exec_dept " +
            "        end = z.code " +
            "  where a.bill_item_code <> 'TC' " +
            "    and a.exec_dept <> '0000000' " +
            "  group by z.name, z.code, c.audit_code")
    List<Map<String, Object>> selectInpatientDeptAccountingReportMzExecDept(
            @Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select(" select z.name as ksmc, " +
            "        z.code as ksid, " +
            "        max(d.parent_code) as flm, " +
            "        max(e.name) as flmc, " +
            "        isnull(sum(round(convert(decimal(18,3),((convert(decimal(18,3),unit_price)) * (quantity * isnull(drug_win,1)))),2)),0) as totalCharge, " +
            "        max(d.name) as hsmc, " +
            "        c.audit_code as hsm " +
            "   from mz_charge_detail a(nolock) " +
            "   join (select distinct patient_id,times,receipt_no, serial_no,receipt_sn from mz_receipt_serial " +
            "         where charge_date >= #{startTime} " +
            "           and charge_date <= #{endTime}) as b " +
            "     on a.patient_id = b.patient_id " +
            "    and a.times = b.times " +
            "    and a.receipt_no = b.receipt_no " +
            "    and a.real_no = b.receipt_sn " +
            "    and a.serial_no = b.serial_no " +
            "   left join zd_charge_item c " +
            "     on case a.charge_bill_code " +
            "          when 'BILL10' then 'BILL01' " +
            "          when 'BILL11' then 'BILL28' " +
            "          when 'BILL12' then 'BILL02' " +
            "          else a.charge_bill_code " +
            "        end = c.code " +
            "   join zy_zd_audit_item d " +
            "     on isnull(c.audit_code, d.code) = d.code " +
            "   join zy_zd_audit_report e " +
            "     on d.parent_code = e.code " +
            "   left join zd_unit_code z " +
            "     on case when isnull(a.warn_dept,'') = '' or a.warn_dept = 'null' " +
            "             then a.exec_dept " +
            "             else a.warn_dept " +
            "        end = z.code " +
            "  where a.bill_item_code <> 'TC' " +
            "    and a.exec_dept <> '0000000' " +
            "  group by z.name, z.code, c.audit_code")
    List<Map<String, Object>> selectInpatientDeptAccountingReportMzWardDept(
            @Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select("<script> ${sql} </script>")
    List<Map<String, Object>> executeSqlByZb(String sql);

    @Select("<script> " +
            "select " +
            "(select rtrim(name) from a_employee_mi where code=a.refer_physician) doctorName,count(*) day from " +
            "(select inpatient_no,admiss_times,convert(varchar(8),charge_date,112) date,refer_physician " +
            "from zy_detail_charge " +
            "where charge_date &gt;= #{startTime} " +
            "and charge_date &lt;= #{endTime} " +
            "<if test=\"dept != null and dept != '' \">" +
            " and ward_code = #{dept} " +
            "</if>" +
            "group by inpatient_no,admiss_times,convert(varchar(8),charge_date,112),refer_physician) a " +
            "group by a.refer_physician " +
            "</script>")
    List<Map<String, Object>> selectZyInDays(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("dept") String dept);

}
