package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.common.module.vo.BytedanceImageCostVOEntity;
import cn.com.ctop.common.module.vo.KuaishouImageCostVOEntity;
import cn.com.ctop.common.module.vo.SheetInfoVo;
import cn.com.ctop.toutiao.modules.report.service.IBytedanceReportMaterialDailyService;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.ctop.service.IImageReportDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("image")
public class ImageCostController {
    @Autowired
    private IImageReportDailyService imageReportDailyService;
    @PostMapping("bytedance/cost")
    public Map<String,Object> bytedanceImageCost(@RequestBody JSONObject data){
        return imageReportDailyService.bytedanceImageCost(data);
    }

    @PostMapping("bytedance/costTotal")
    public Map<String,Object> bytedanceImageCostTotal(@RequestBody JSONObject data){
        return imageReportDailyService.bytedanceImageCostTotal(data);
    }
    @PostMapping("bytedance/cost/export")
    public void bytedanceImageCostExport(@RequestBody JSONObject data, HttpServletResponse response) throws IOException {
        SheetInfoVo vo = imageReportDailyService.getbytedanceImageCostVo(data);
        OutputStream outputStream = response.getOutputStream();
        String date = DateUtils.formatDate(new Date());
        HttpUtils.setResponseHeader(response, "头条图片消耗报表" + date + ".xlsx");
        ExcelWriter excelWriter = EasyExcelFactory.getWriter(outputStream);
        Sheet sheet = new Sheet(1, 0, BytedanceImageCostVOEntity.class);
        sheet.setSheetName(vo.getSheetName());
        excelWriter.write(vo.getDetails(), sheet);

        excelWriter.finish();
        outputStream.flush();
        outputStream.close();
    }

    @PostMapping("kuaishou/cost")
    public Map<String,Object> kuaishouImageCost(@RequestBody JSONObject data){
        return imageReportDailyService.kuaishouImageCost(data);
    }

    @PostMapping("kuaishou/costTotal")
    public Map<String,Object> kuaishouImageCostTotal(@RequestBody JSONObject data){
        return imageReportDailyService.kuaishouImageCostTotal(data);
    }

    @PostMapping("kuaishou/cost/export")
    public void kuaishouImageCostExport(@RequestBody JSONObject data, HttpServletResponse response) throws IOException {
        SheetInfoVo vo = imageReportDailyService.getkuaishouImageCostVo(data);
        OutputStream outputStream = response.getOutputStream();
        String date = DateUtils.formatDate(new Date());
        HttpUtils.setResponseHeader(response, "快手图片消耗报表" + date + ".xlsx");
        ExcelWriter excelWriter = EasyExcelFactory.getWriter(outputStream);
        Sheet sheet = new Sheet(1, 0, KuaishouImageCostVOEntity.class);
        sheet.setSheetName(vo.getSheetName());
        excelWriter.write(vo.getDetails(), sheet);
        excelWriter.finish();
        outputStream.flush();
        outputStream.close();
    }
}
