package thyyxxk.webserver.dao.his.reports;

import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.datamodify.YzActOrder;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.reports.illegalchargesanalysis.IllegalChargeData;
import thyyxxk.webserver.entity.reports.illegalchargesanalysis.IllegalChargeTemplate;

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

/**
 * @author dj
 */
@Mapper
public interface IllegalChargesAnalysisDao {

    @Select("select *, physicianName=(select rtrim(name) from a_employee_mi where code=physician) " +
            "from yz_act_order where act_order_no in (#{orderNo}, #{orderNo2})")
    List<YzActOrder> selectYzActOrder(@Param("orderNo") BigDecimal orderNo,
                                      @Param("orderNo2") BigDecimal orderNo2);

    @Select("select *, physicianName=(select rtrim(name) from a_employee_mi where code=physician) " +
            "from yz_inact_order where act_order_no in (#{orderNo}, #{orderNo2})")
    List<YzActOrder> selectYzInActOrder(@Param("orderNo") BigDecimal orderNo,
                                        @Param("orderNo2") BigDecimal orderNo2);

    @Select("select * from t_illegal_charge_template where id like #{code} or id like 'admin%' order by id")
    List<IllegalChargeTemplate> selectMyTemplates(@Param("code") String code);

    @Select("select 1 from t_illegal_charge_template where id=#{id}")
    Integer selectCurrentId(@Param("id") String id);

    @Insert("insert into t_illegal_charge_template (id, label, parent, personal, type) values " +
            "(#{id}, #{label}, #{parent}, #{personal}, #{type})")
    void insertNewTemplate(IllegalChargeTemplate template);

    @Delete("delete from t_illegal_charge_template where id like #{id}")
    void deleteTemplate(@Param("id") String id);

    @Select("select distinct rtrim(code) as code, rtrim(name) as name from zd_charge_item where " +
            "isnull(del_flag,0) ${include} and ${method} like #{content} union " +
            "select distinct rtrim(code) as code, rtrim(name) as name from yp_zd_dict where " +
            "isnull(del_flag,0) ${include} and ${method} like #{content} ")
    List<CodeName> selectChargeItem(@Param("method") String method,
                                    @Param("content") String content,
                                    @Param("include") String include);

    @Update("update t_illegal_charge_template set label=#{label}, value=#{value},attribute=#{attribute} where id=#{id}")
    void updateTemplate(IllegalChargeTemplate template);

    @Select("select rtrim(a.inpatient_no) as inpatientNo,a.admiss_times,rtrim(a.exec_unit) as execUnit, " +
            "rtrim(c.name) as name,b.med_type,rtrim(a.ward_code) as deptName,rtrim(a.dept_code) wardName, " +
            "diag=(select rtrim(d.dis_diag_comment) from zy_dis_diag_yb d where d.inpatient_no=a.inpatient_no " +
            "and d.admiss_times=a.admiss_times and d.dis_diag_no=1),a.detail_sn,a.charge_date, " +
            "rtrim(a.charge_code_mx) as chargeCode,b.insuplc_admdvs, " +
            "chargeName=case when a.charge_code in ('BILL01','BILL02','BILL28') then " +
            "(select top 1 d.name from yp_zd_dict d where d.code=a.charge_code_mx) else " +
            "(select d.name from zd_charge_item d where d.code=a.charge_code_mx) end, " +
            "chargeDay=convert(varchar(10),a.charge_date,21),a.charge_fee,a.charge_amount,a.trans_flag_yb " +
            "from zy_detail_charge a, t_si_pat_info b, a_patient_mi c where a.charge_fee>0 " +
            "and isnull(a.trans_flag_yb,0)!=2 and a.charge_date>=#{start} and a.charge_date<=#{end} " +
            "and isnull(a.ori_detail_sn,0)!=-1 and a.charge_code_mx=#{code} and a.inpatient_no=c.inpatient_no " +
            "and a.inpatient_no=b.pat_no and a.admiss_times=b.times and b.ledger_sn=1 and a.ledger_sn>0")
    List<IllegalChargeData> selectChargeDataForSameTime(@Param("start") String start,
                                                        @Param("end") String end,
                                                        @Param("code") String code);

    @Select("select *, " +
            "name=(select rtrim(name) from a_patient_mi where inpatient_no=temp.inpatient_no), " +
            "medtype=(select max(med_type) from t_si_pat_info t where t.pat_no=temp.inpatient_no and " +
            "t.times=temp.admiss_times), " +
            "insuplcAdmdvs=(select max(insuplc_admdvs) from t_si_pat_info t where t.pat_no=temp.inpatient_no and " +
            "t.times=temp.admiss_times), " +
            "wardName=(select rtrim(name) from zd_unit_code where code=temp.ward_code), " +
            "diag=(select rtrim(dis_diag_comment) from zy_dis_diag_yb where zy_dis_diag_yb.inpatient_no=temp.inpatient_no and " +
            "zy_dis_diag_yb.admiss_times=temp.admiss_times and dis_diag_no=1), " +
            "chargeName=rtrim(isnull((select name from zd_charge_item where code=temp.chargeCode), " +
            "(select top 1 name from yp_zd_dict where code=temp.chargeCode))) " +
            "from (select inpatient_no,admiss_times,rtrim(charge_code_mx) as chargeCode,ward_code, " +
            "convert(varchar(10), charge_date, 21) as chargeDay, sum(charge_amount) as chargeAmount, " +
            "sum((charge_fee)) as chargeFee from zy_detail_charge " +
            "where charge_fee>0 and isnull(trans_flag_yb,0)!=2 and charge_date>=#{start} and " +
            "charge_date<=#{end} and charge_code_mx=#{code} and isnull(ori_detail_sn,0)!=-1 group by " +
            "inpatient_no, admiss_times, charge_code_mx,ward_code, convert(varchar(10), charge_date, 21)) " +
            "temp where chargeAmount>#{num} ")
    List<IllegalChargeData> selectChargeDataForOverLimit(@Param("start") String start,
                                                         @Param("end") String end,
                                                         @Param("code") String code,
                                                         @Param("num") int num);


    @Select("<script>" +
            "select                   rtrim(a.inpatient_no)                              inpatient_no, " +
            "                         rtrim(a.name)                                      name, " +
            "                         a.admiss_times, " +
            "                         a.admiss_date, " +
            "                         b.dis_date, " +
            "                         DATEDIFF(day, " +
            "                                  b.dis_date, " +
            "                                  a.admiss_date)                            differ_date, " +
            "                         (select name " +
            "                          from zd_unit_code " +
            "                          where code = isnull(a.small_dept, a.admiss_dept)) dept_name, " +
            "    responce_type_name = (select name " +
            "                          from zy_zd_responce_type " +
            "                          where a.responce_type = zy_zd_responce_type.code), " +
            "                         c.dis_diag, " +
            "                         c.dis_diag_comment " +
            "from zy_actpatient a, " +
            "     zy_inactpatient b, " +
            "     ba_first_page1 c " +
            "where a.inpatient_no = b.inpatient_no " +
            "  and a.admiss_times = b.admiss_times + 1 " +
            "  and a.inpatient_no not like 'JT%' " +
            "  and DATEDIFF(day, b.dis_date, a.admiss_date) &lt; 2 " +
            "  and a.inpatient_no = c.inpatient_no " +
            "  and a.admiss_times = c.admiss_times " +
            "  and c.dis_diag_no = 1 " +
            "<if test=\"start !=null and start != '' \">" +
            " and a.admiss_date &gt;= #{start} and a.admiss_date &lt;= #{end} " +
            "</if>" +
            "<if test=\"responces.size > 0\" >" +
            "and a.responce_type in " +
            "<foreach collection='responces' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item}" +
            "</foreach>" +
            "</if>" +
            "union " +
            "select                   rtrim(a.inpatient_no)                              inpatient_no, " +
            "                         rtrim(a.name)                                      name, " +
            "                         a.admiss_times, " +
            "                         a.admiss_date, " +
            "                         b.dis_date, " +
            "                         DATEDIFF(day, " +
            "                                  b.dis_date, " +
            "                                  a.admiss_date)                            differ_date, " +
            "                         (select name " +
            "                          from zd_unit_code " +
            "                          where code = isnull(a.small_dept, a.admiss_dept)) dept_name, " +
            "    responce_type_name = (select name " +
            "                          from zy_zd_responce_type " +
            "                          where a.responce_type = zy_zd_responce_type.code), " +
            "                         c.dis_diag, " +
            "                         c.dis_diag_comment " +
            "from zy_inactpatient a, " +
            "     zy_inactpatient b, " +
            "     ba_first_page1 c " +
            "where a.inpatient_no = b.inpatient_no " +
            "  and a.admiss_times = b.admiss_times + 1 " +
            "  and a.inpatient_no not like 'JT%' " +
            "  and DATEDIFF(day, b.dis_date, a.admiss_date) &lt; 2 " +
            "  and a.inpatient_no = c.inpatient_no " +
            "  and a.admiss_times = c.admiss_times " +
            "  and c.dis_diag_no = 1 " +
            "<if test=\"start !=null and start != '' \">" +
            " and a.admiss_date &gt;= #{start} and a.admiss_date &lt;= #{end} " +
            "</if>" +
            "<if test=\"responces.size > 0\" >" +
            "and a.responce_type in " +
            "<foreach collection='responces' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item}" +
            "</foreach>" +
            "</if>" +
            "order by a.inpatient_no, " +
            "         a.admiss_times" +
            "</script>")
    List<IllegalChargeData> queryHuanZheLiangCiRuYuanRiQi(@Param("start") String start,
                                                          @Param("end") String end,
                                                          @Param("responces") List<String> responces);

    @Select("select inpatient_no, " +
            "       rtrim(name) name, " +
            "       dept_name   = (select rtrim(name) name from zd_unit_code where code = small_dept), " +
            "       admiss_date, " +
            "       dis_date, " +
            "       differ_date = DATEDIFF(day, admiss_date, dis_date)," +
            "       responce_type_name = (select name from zy_zd_responce_type where responce_type = zy_zd_responce_type.code) " +
            "from zy_inactpatient " +
            "where DATEDIFF(day, admiss_date, dis_date) <= #{tianShu} " +
            "  and admiss_date >= #{startTime} " +
            "  and admiss_date < #{endTime} " +
            "  and inpatient_no not like 'S%' " +
            "  order by differ_date")
    List<IllegalChargeData> zhuYuanTianShu(@Param("startTime") String startTime,
                                           @Param("endTime") String endTime,
                                           @Param("tianShu") Integer tianShu);
}

