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

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

import java.io.*;

@Service
@Slf4j
public class UReportExportRest implements IUReportExportService {

    @Autowired
    private RestTemplate restTemplate;

    private static final String  outputDir="/data/file/report"+ File.separator + "uReport"+File.separator;

    @Override
    public void exportExcel(String fileName) {

        String yesterday= DateUtils.addDay(DateUtils.date2Str(),-1);
        StringBuilder sb=new StringBuilder();
        sb.append("&startDate=").append(yesterday)
                .append("&endDate=").append(yesterday)
                .append("&statDate=").append(yesterday)
                .append("&date=").append(yesterday)
                .append("&statDatetime=").append(yesterday);
        String exportDataUrl = "http://139.186.29.76:8804/jeecg-boot/ureport/excel?_u=mysql:";
        byte[] result =restTemplate.postForObject(exportDataUrl +fileName+sb.toString(),null,byte[].class);
        File file=new File(outputDir+fileName.replace("ureport.xml","")+"xlsx");
        OutputStream outputStream=null;
        try {
            if(result!=null){
                outputStream=new FileOutputStream(file,true);
                outputStream.write(result);
                outputStream.flush();
            }
        } catch (IOException e) {
            log.error("定制化报表下载错误>>>UReportExportServiceImpl");
        } finally {
            if(outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
