package cn.com.ctop.common.module.service.impl;

import cn.com.ctop.common.module.service.IUReportExportService;
import com.bstek.ureport.export.ExportConfigureImpl;
import com.bstek.ureport.export.ExportManager;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class UReportExportServiceImpl{

    @Autowired
    RestTemplate restTemplate;
    private static final String  outputDir=System.getProperty("user.dir")+File.separator + "uReport"+File.separator+"report.xlsx";

    public void exportExcel(String fileName) {

        String yesterday= DateUtils.addDay(DateUtils.date2Str(),-1);
        Map<String, Object> parameters=new HashMap<>();
        parameters.put("startDate",yesterday);
        parameters.put("endDate",yesterday);
        parameters.put("statDate,",yesterday);
        parameters.put("date,",yesterday);
        parameters.put("statDatetime,",yesterday);
        File file=new File(outputDir);
        OutputStream outputStream=null;
        try {
            outputStream=new FileOutputStream(file,true);
            ExportConfigureImpl exportConfigure=new ExportConfigureImpl(fileName,parameters,outputStream);
            getExportManager().exportExcel(exportConfigure);
        } catch (FileNotFoundException e) {
            log.error("定制化报表下载错误>>>UReportExportServiceImpl");
        }finally {
            if(outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    private static ExportManager getExportManager(){
        return (ExportManager)SpringContextUtils.getBean(ExportManager.BEAN_ID);
    }
}

