package thyyxxk.webserver.service.reports;


import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.reports.ConsumablesStatisticsDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.querydata.ConsumablesStatistics;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.ToolExcel;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 描述
 * </p>
 *
 * @author xc
 * @date 2021-04-06 14:35
 */
@Service
@Slf4j
public class ConsumablesStatisticsService {

    private final ConsumablesStatisticsDao dao;

    @Autowired
    public ConsumablesStatisticsService(ConsumablesStatisticsDao dao) {
        this.dao = dao;
    }

    /**
     * 查询耗材使用统计  根据条件动态查询 这个sql语句太复杂了 mybatis-plus分页查询很慢
     *
     * @param param 条件 分页
     * @return 指定数据
     */
    public ResultVo<List<ConsumablesStatistics>> query(ConsumablesStatistics param) {
        List<ConsumablesStatistics> list = dao.query(param);
        if (list.size() > 0) {
            return ResultVoUtil.success(list);
        }
        return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
    }

    /**
     * @param response 固定
     * @param param    查询条件
     */
    public void exportExcel(HttpServletResponse response, ConsumablesStatistics param) {
        log.info("导出耗材使用统计excel --> {}", JSON.toJSONString(param));
        List<ConsumablesStatistics> list = dao.query(param);
        ToolExcel excel = new ToolExcel();
        excel.addTitle("projectCode", "项目编码");
        excel.addTitle("entryName", "项目名称");
        excel.addTitle("originBrand", "产地品牌");
        excel.addTitle("hospitalCode", "耗材院内编码");
        excel.addTitle("source", "来源");
        excel.addTitle("number", "数量");
        excel.addTitle("amountOfMoney", "金额");
        excel.exportExcel(list, response);
    }

    /**
     * 获取费用类型
     */
    public ResultVo<List<GetDropdownBox>> getDropdownBox() {
        return ResultVoUtil.success(dao.chargeClassName());
    }


}
