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.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.querydata.ConsumablesStatistics;

import java.util.List;

/**
 * <p>
 * 描述 查询耗材统计
 * </p>
 *
 * @author xc
 * @date 2021-04-06 14:35
 */
@Mapper
public interface ConsumablesStatisticsDao {
    /**
     * 查询耗材使用统计  这个sql太复杂无法分页
     * 使用了 UNION ALL 就需要用 select * from (里面放sql) temp
     *
     * @param param 时间区间 费用类型 项目编码 查询的条件
     * @return 指定数据
     */
    @Select("<script>" +
            "select * from (" +
            "select a.charge_code_mx project_code,b.name entry_name,b.cl_code hospital_code,b.yb_comment origin_brand, " +
            "sum(a.charge_amount) number,sum(a.charge_fee) amount_of_money,source='住院' " +
            "from zy_detail_charge a,zd_charge_item b " +
            "<where>" +
            "a.charge_code_mx=b.code  " +
            "<if test=\"startTime!='' and startTime!=null\"> " +
            "and a.charge_date&gt;=#{startTime} and a.charge_date&lt;#{endTime} " +
            "</if>" +
            "<if test=\"classCode!=''  and classCode!=null\">" +
            "and b.class_code=#{classCode} " +
            "</if>" +
            "<if test=\"projectCode!='' and projectCode!=null\">" +
            "and a.charge_code_mx = #{projectCode} " +
            "</if>" +
            "</where>" +
            "group by a.charge_code_mx,b.name,b.cl_code,b.yb_comment having sum(a.charge_amount)&lt;&gt;0 " +
            "UNION ALL " +
            " select a.charge_item_code project_code,b.name entry_name,b.cl_code hospital_code,b.yb_comment origin_brand, " +
            "sum(quantity) number,sum(quantity*unit_price*drug_win) amount_of_money,source='门诊' " +
            "from mz_charge_detail a,zd_charge_item b " +
            "<where>" +
            "a.charge_item_code=b.code " +
            "<if test=\"startTime!='' and startTime!=null\"> " +
            "and a.charge_date&gt;=#{startTime} and a.charge_date&lt;#{endTime} " +
            "</if>" +
            "<if test=\"classCode!=''  and classCode!=null\">" +
            "and b.class_code=#{classCode} " +
            "</if>" +
            "<if test=\" projectCode!='' and projectCode!=null\">" +
            "and a.charge_item_code=#{projectCode} " +
            "</if>" +
            "</where>" +
            "group by a.charge_item_code,b.name,b.cl_code,b.yb_comment having sum(a.quantity)&lt;&gt;0 " +
            ") temp " +
            "</script>")
    List<ConsumablesStatistics> query(ConsumablesStatistics param);

    /**
     * 获取收费的类型
     *
     * @return 返回收费的类型
     */
    @Select("select code,name from zd_charge_class")
    List<GetDropdownBox> chargeClassName();
}
