package thyyxxk.webserver.dao.his.reports;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.querydata.KeShiFeiiYongTongJi;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 描述： 各个科室收入的情况
 * </p>
 *
 * @author xc
 * @date 2021-10-19 14:50
 */
public interface KeShiShouRuDao {

    /**
     * 查询住院患者的费用
     *
     * @param currentPage  当前页
     * @param pageSize     页大小
     * @param execUnit     执行科室 （大科室要看到子科室的时间）
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @param chargeCodeMx 项目编码
     * @param code         人员的 编码
     * @param codeRs       坑爹的老系统 还会保存不同的 rs
     * @param reqDept      申请科室
     * @return 返回信息
     */
    @Select({"<script>" +
            "select top ${pageSize} * from (select ROW_NUMBER() over (ORDER BY charge_date ) as RowNumber," +
            "rtrim(inpatient_no)    patient, " +
            "          patientName    = (select rtrim(name) from a_patient_mi where a.inpatient_no = a_patient_mi.inpatient_no), " +
            "                           admiss_times           times, " +
            "                           rtrim(charge_code_mx)  charge_code_mx, " +
            "          chargeCodeName = (dbo.get_charge_name(charge_code_mx, serial)), " +
            "                           charge_date, " +
            "                           rtrim(op_id_code)      op_id_code, " +
            "          opName         = (select rtrim(name) from a_employee_mi where code = op_id_code), " +
            "                           rtrim(exec_unit)       exec_unit, " +
            "          exec_unit_name = (select rtrim(name) from zd_unit_code where code = exec_unit), " +
            "                           rtrim(ward_code)       ward_code, " +
            "          ward_code_name = (select rtrim(name) from zd_unit_code where code = ward_code), " +
            "                           convert(decimal(18, " +
            "                               2), " +
            "                                   charge_fee)    charge_fee, " +
            "                           convert(decimal(18, " +
            "                               2), " +
            "                                   charge_amount) charge_amount, " +
            "          source         = '住院'" +
            "from zy_detail_charge a " +
            "where " +
            " charge_date &gt;= #{startTime,jdbcType=TIMESTAMP} and charge_date &lt;= #{endTime,jdbcType=TIMESTAMP} and trans_flag_yb &lt;&gt; 2 " +
            "<when test='list != null and list.size > 0 '>" +
            "  and  a.exec_unit in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item,jdbcType=CHAR}" +
            "</foreach>" +
            "</when>" +
            "<when test=\"chargeCodeMx != null and chargeCodeMx != '' \">" +
            "  and a.charge_code_mx = #{chargeCodeMx,jdbcType=CHAR}" +
            "</when>" +
            "<when test=\"code != null and code != '' \">" +
            " and a.op_id_code in (#{code,jdbcType=CHAR},#{codeRs,jdbcType = CHAR})  " +
            "</when>" +
            "<when test=\"reqDept != null and reqDept != '' \">" +
            " and a.ward_code = #{reqDept,jdbcType=CHAR} " +
            "</when>" +
            ") temp where RowNumber > ${pageSize} * (${currentPage} - 1) " +
            "</script>"})
    List<KeShiFeiiYongTongJi> getKeShiFeiYongZhuYuan(@Param("currentPage") long currentPage,
                                                     @Param("pageSize") long pageSize,
                                                     @Param("list") List<String> execUnit,
                                                     @Param("startTime") String startTime,
                                                     @Param("endTime") String endTime,
                                                     @Param("chargeCodeMx") String chargeCodeMx,
                                                     @Param("code") String code,
                                                     @Param("codeRs") String codeRs,
                                                     @Param("reqDept") String reqDept);

    /**
     * 查询住院患者的费用 的总条数
     *
     * @param execUnit     执行科室 （大科室要看到子科室的时间）
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @param chargeCodeMx 项目编码
     * @param code         人员的 编码
     * @param codeRs       坑爹的老系统 还会保存不同的 rs
     * @param reqDept      申请科室
     * @return 返回 条数和金额
     */
    @Select("<script>" +
            "select count(1) total,sum(a.charge_fee) charge_fee " +
            "from zy_detail_charge a  " +
            "where " +
            "charge_date &gt;= #{startTime,jdbcType=TIMESTAMP} and charge_date &lt;= #{endTime,jdbcType=TIMESTAMP} and trans_flag_yb &lt;&gt; 2 " +
            "<if test=\"list != null and list.size > 0 \">" +
            "  and  a.exec_unit in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item,jdbcType = CHAR}" +
            "</foreach>" +
            "</if>" +
            "<if test=\"chargeCodeMx != null and chargeCodeMx != '' \">" +
            "  and a.charge_code_mx = #{chargeCodeMx,jdbcType = CHAR}" +
            "</if>" +
            "<if test=\"code != null and code != '' \">" +
            " and a.op_id_code in (#{code,jdbcType = CHAR},#{codeRs,jdbcType = CHAR}) " +
            "</if>" +
            "<if test=\"reqDept != null and reqDept != '' \">" +
            " and a.ward_code = #{reqDept,jdbcType = CHAR} " +
            "</if>" +
            "</script>")
    KeShiFeiiYongTongJi getKeShiFeiYongZhuYuanTotal(@Param("list") List<String> execUnit,
                                                    @Param("startTime") String startTime,
                                                    @Param("endTime") String endTime,
                                                    @Param("chargeCodeMx") String chargeCodeMx,
                                                    @Param("code") String code,
                                                    @Param("codeRs") String codeRs,
                                                    @Param("reqDept") String reqDept);

    @Select("select ${fenLei} " +
            "sum(charge_fee) charge_fee," +
            "sum(charge_amount) charge_amount," +
            "count(1) total,source = '住院'" +
            " from zy_detail_charge a " +
            " ${ew.customSqlSegment} ")
    List<KeShiFeiiYongTongJi> getZhuYuanFeiYongXiangMuFenLeiV2(IPage<KeShiFeiiYongTongJi> page,
                                                               @Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper,
                                                               @Param("fenLei") String fenLei);


    /**
     * 这个是获取门诊费用的
     *
     * @param currentPage  当前页
     * @param pageSize     页大小
     * @param execUnitList 执行科室的list1
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @param chargeCodeMx 项目编码
     * @param code         人员 编码
     * @param codeRs       人事的人员编码
     * @param reqDept      申请科室
     * @return 返回信息
     */
    @Select("<script>" +
            "select top ${pageSize} * from (select               ROW_NUMBER() over ( " +
            "    ORDER BY " +
            "        confirm_time )                  as                                            RowNumber, " +
            "                           rtrim(patient_id)                                          patient, " +
            "                           times, " +
            "                           rtrim(name)                                                patient_name, " +
            "                           rtrim(charge_item_code)                                    charge_code_mx, " +
            "          chargeCodeName = dbo.get_charge_name(charge_item_code, group_no), " +
            "                           confirm_time as                                            charge_date, " +
            "                           confirm_id                                                 op_id_code, " +
            "          opName         = (select top 1 rtrim(name) from a_employee_mi where code = confirm_id), " +
            "                           rtrim(exec_dept)                                           exec_unit, " +
            "          execUnitName   = (select top 1 rtrim(name) from zd_unit_code where code = exec_dept), " +
            "                           rtrim(warn_dept)                                           ward_code, " +
            "          ward_code_name = (select top 1 rtrim(name) from zd_unit_code where code = warn_dept), " +
            "                           cast((quantity * drug_win) as decimal(14, 2))              charge_amount, " +
            "                           cast((quantity * drug_win * unit_price) as decimal(14, 2)) charge_fee, " +
            "          source         = N'门诊' " +
            "from mz_charge_detail where  bill_item_code NOT IN ('TC','020') and " +
            "confirm_time &gt;= #{startTime,jdbcType = TIMESTAMP} and confirm_time &lt;= #{endTime,jdbcType = TIMESTAMP}" +
            "<if test=\"list != null and list.size > 0 \">" +
            " and exec_dept in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item,jdbcType = CHAR}" +
            "</foreach>" +
            "</if>" +
            "<if test=\"chargeCodeMx != null and chargeCodeMx != '' \">" +
            " and charge_item_code  = #{chargeCodeMx,jdbcType = CHAR} " +
            "</if>" +
            "<if test=\"code != null and code != '' \">" +
            " and confirm_id in (#{code,jdbcType = CHAR},#{codeRs,jdbcType = CHAR}) " +
            "</if>" +
            "<if test=\"reqDept != null and reqDept != '' \">" +
            " and warn_dept = #{reqDept,jdbcType = CHAR}" +
            "</if>" +
            ") temp where " +
            "RowNumber > ${pageSize} * (${currentPage} - 1) order by RowNumber " +
            "</script>")
    List<KeShiFeiiYongTongJi> getMenZhenFeiYong(@Param("currentPage") long currentPage,
                                                @Param("pageSize") long pageSize,
                                                @Param("list") List<String> execUnitList,
                                                @Param("startTime") String startTime,
                                                @Param("endTime") String endTime,
                                                @Param("chargeCodeMx") String chargeCodeMx,
                                                @Param("code") String code,
                                                @Param("codeRs") String codeRs,
                                                @Param("reqDept") String reqDept);

    /**
     * 获取门诊 费用的总条数
     *
     * @param execUnitList 执行科室
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @param chargeCodeMx 项目编码
     * @param code         人员编码
     * @param codeRs       人事的人员编码
     * @param reqDept      申请科室
     * @return 返回条数 和 总金额
     */
    @Select("<script>" +
            "select count(*) total, sum (cast ((quantity*drug_win*unit_price) as decimal(14,2))) charge_fee  " +
            "from mz_charge_detail where  bill_item_code NOT IN ('TC','020') and " +
            "confirm_time &gt;= #{startTime,jdbcType = TIMESTAMP} and confirm_time &lt;= #{endTime,jdbcType = TIMESTAMP}" +
            "<if test=\"execUnitList != null and execUnitList.size > 0 \">" +
            " and exec_dept in " +
            "<foreach collection='execUnitList' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item}" +
            "</foreach>" +
            "</if>" +
            "<if test=\"chargeCodeMx != null and chargeCodeMx != '' \">" +
            " and charge_item_code  = #{chargeCodeMx,jdbcType = CHAR} " +
            "</if>" +
            "<if test=\"code != null and code != '' \">" +
            " and confirm_id in (#{code,jdbcType = CHAR},#{codeRs,jdbcType = CHAR}) " +
            "</if>" +
            "<if test=\"reqDept != null and reqDept != '' \">" +
            " and warn_dept = #{reqDept,jdbcType = CHAR}" +
            "</if>" +
            "</script>")
    KeShiFeiiYongTongJi getMenZhenFeiYongTotal(@Param("execUnitList") List<String> execUnitList,
                                               @Param("startTime") String startTime,
                                               @Param("endTime") String endTime,
                                               @Param("chargeCodeMx") String chargeCodeMx,
                                               @Param("code") String code,
                                               @Param("codeRs") String codeRs,
                                               @Param("reqDept") String reqDept);

    /**
     * 门诊的项目分类
     *
     * @param page         分页
     * @param execUnitList 执行科室
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @param chargeCodeMx 项目编码
     * @param code         人员编码
     * @param codeRs       人事的人员编码
     * @param reqDept      申请科室
     * @param fenLei       分类
     * @return 返回分类信息
     */
    @Select("<script>" +
            "select " +
            "<choose>" +
            "<when test=\"fenLei == 'charge_item_code' \">" +
            "charge_item_code charge_code_mx," +
            "</when>" +
            "<when test=\"fenLei == 'confirm_id' \">" +
            "confirm_id op_id_code, opName = (select rtrim(name) from a_employee_mi where code = confirm_id)," +
            "</when>" +
            "<when test=\"fenLei == 'warn_dept' \">" +
            "warn_dept , wardCodeName = (select rtrim(name) from zd_unit_code where code = warn_dept), " +
            "</when>" +
            "<otherwise>" +
            "CONVERT(varchar(100), charge_date, 23) charge_date_string ," +
            "</otherwise>" +
            "</choose>" +
            "sum(cast ((quantity*drug_win*unit_price) as decimal(14,2))) charge_fee, " +
            "sum(cast ((quantity*drug_win) as decimal(14,2))) charge_amount,source= '门诊' " +
            "from mz_charge_detail where  bill_item_code NOT IN ('TC','020') and " +
            "confirm_time &gt;= #{startTime,jdbcType = TIMESTAMP} and confirm_time &lt;= #{endTime,jdbcType = TIMESTAMP}" +
            "<if test=\"execUnitList != null and execUnitList.size > 0 \">" +
            " and exec_dept in " +
            "<foreach collection='execUnitList' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item}" +
            "</foreach>" +
            "</if>" +
            "<if test=\"chargeCodeMx != null and chargeCodeMx != '' \">" +
            " and charge_item_code  = #{chargeCodeMx,jdbcType = CHAR} " +
            "</if>" +
            "<if test=\"code != null and code != '' \">" +
            " and confirm_id in (#{code,jdbcType = CHAR},#{codeRs,jdbcType = CHAR}) " +
            "</if>" +
            "<if test=\"reqDept != null and reqDept != '' \">" +
            " and warn_dept = #{reqDept,jdbcType = CHAR} " +
            "</if>" +
            "group by ${fenLei}" +
            "</script>")
    List<KeShiFeiiYongTongJi> getMenZhenFeiYongXiangMuFenLei(IPage<KeShiFeiiYongTongJi> page,
                                                             @Param("execUnitList") List<String> execUnitList,
                                                             @Param("startTime") String startTime,
                                                             @Param("endTime") String endTime,
                                                             @Param("chargeCodeMx") String chargeCodeMx,
                                                             @Param("code") String code,
                                                             @Param("codeRs") String codeRs,
                                                             @Param("reqDept") String reqDept,
                                                             @Param("fenLei") String fenLei);


    @Select("<script>" +
            "select " +
            " ${fenLei} " +
            "sum(cast ((quantity*drug_win*unit_price) as decimal(14,2))) charge_fee, " +
            "sum(cast ((quantity*drug_win) as decimal(14,2))) charge_amount,count(1) total,source= '门诊' " +
            "from mz_charge_detail where  bill_item_code NOT IN ('TC','020') and " +
            "${ew.sqlSegment}" +
            "</script>")
    List<KeShiFeiiYongTongJi> getMenZhenFeiYongXiangMuFenLeiV2(IPage<KeShiFeiiYongTongJi> page,
                                                               @Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper,
                                                               @Param("fenLei") String fenLei);


    /**
     * 获取住院或门诊的患者 信息
     *
     * @param huanZheHaoList 人员编码集合
     * @param tableName      表名
     * @return 返回基本信息
     */
    @Select({"<script>" +
            "select distinct " +
            "<if test=\"tableName == 'zy_patient' \">" +
            "rtrim(inpatient_no) code," +
            "</if>" +
            "<if test=\"tableName == 'mz_patient_mi' \">" +
            "rtrim(patient_id) code," +
            "</if>" +
            "rtrim(name) name from ${tableName} where " +
            "<if test=\"tableName == 'zy_patient' \">" +
            "inpatient_no in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item,jdbcType = CHAR}" +
            "</foreach>" +
            "</if>" +
            "<if test=\"tableName == 'mz_patient_mi' \">" +
            "patient_id in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item,jdbcType = CHAR}" +
            "</foreach>" +
            "</if>" +
            "</script>"})
    List<GetDropdownBox> huanZheXingMing(@Param("list") Set<String> huanZheHaoList,
                                         @Param("tableName") String tableName);


    /**
     * 分会项目的名称
     *
     * @param xiangMuBianMa 项目编码 集合
     * @return 返回名称
     */
    @Select("<script>" +
            "select rtrim(code) code,rtrim(name) name from yp_zd_dict " +
            "where code in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item,jdbcType=CHAR}" +
            "</foreach>" +
            "union " +
            "select rtrim(code) code,rtrim(name) name from zd_charge_item " +
            "where code in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item,jdbcType=CHAR}" +
            "</foreach>" +
            "</script>")
    List<GetDropdownBox> xiangMuMingChen(@Param("list") Set<String> xiangMuBianMa);


    /**
     * 获取科室人员的名称
     *
     * @param luRuRen   录入人的名字
     * @param tableName 表名
     * @return 返回
     */
    @Select("<script>" +
            "select rtrim(code) code,rtrim(name) name" +
            "<if test=\"tableName == 'a_employee_mi' \">" +
            ",rtrim(code_rs) code_rs" +
            "</if>" +
            " from ${tableName} where code in " +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item,jdbcType=CHAR}" +
            "</foreach>" +
            "<if test=\"tableName == 'a_employee_mi' \">" +
            " or " +
            "code_rs in" +
            "<foreach collection='list' item='item' index='index' open='(' close=')' separator=','>" +
            "#{item,jdbcType=CHAR}" +
            "</foreach>" +
            "</if>" +
            "</script>")
    List<GetDropdownBox> huoQuKeShiHuoRenYuan(@Param("list") Set<String> luRuRen,
                                              @Param("tableName") String tableName);

    /**
     * 获取人员的两个编码
     *
     * @param code 人员编码
     * @return 返回人员编码
     */
    @Select("select top(1) rtrim(code) code,rtrim(code_rs) code_rs from a_employee_mi where (code = #{code} or code_rs = #{code})")
    GetDropdownBox huoQuRenYuanDeCodeHeCodeRs(String code);


}
