package cn.com.ctop.bytedance.controller;

import cn.com.ctop.bytedance.service.IReportService;
import cn.com.ctop.bytedance.vo.ByteDanceAdvertiserReportDTO;
import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yumeng
 * 2019年11月12日10:04:00
 */
@RestController
@RequestMapping("report")
public class ReportController {
    /**
     * 头条广告主报表信息
     * 只支持查询2016-10-26及以后的日期
     * 单次获取最大条数1000
     *
     * @return
     */
    @GetMapping("bytedance/advertiser")
    public Map<String, Object> advertiserReport(@RequestBody ByteDanceAdvertiserReportDTO conditions) {
        return reportService.getAdvertiserReport(conditions);
    }

    /**
     * 头条广告计划报表信息
     * 只支持查询2016-10-26及以后的日期
     * 单次获取最大条数1000
     * @return 广告组报表数据
     */
    @GetMapping("bytedance/campaign")
    public Map<String, Object> campaignReport(@RequestBody ByteDanceAdvertiserReportDTO conditions) {
        return reportService.getCampaignReport(conditions);
    }

    /**
     * 头条广告计划报表信息
     * @param conditions 查询条件
     * @return 广告计划报表数据
     */
    @GetMapping("bytedance/ad")
    public Map<String, Object> adReport(@RequestBody ByteDanceAdvertiserReportDTO conditions) {
        return reportService.getAdReport(conditions);
    }

    /**
     * 头条广告创意报表信息
     * @param conditions
     * @return
     */
    @GetMapping("bytedance/creative")
    public Map<String, Object> creativeReport(@RequestBody ByteDanceAdvertiserReportDTO conditions) {
        return reportService.getCreativeReport(conditions);
    }

    /**
     * 代理商数据
     * @param conditions
     * @return
     * TODO 尚未完善
     */
    @GetMapping("bytedance/agent")
    public Map<String, Object> agentReport(@RequestBody JSONObject conditions) {
        return reportService.getAgentReport(conditions);
    }

    /**
     * 360借条报表数据
     */
    @GetMapping("jietiao/kuaishou/insert")
    public Map<String, Object> jietiaoKuaishouInsert(String accountId) {
        Map<String, Object> result = new HashMap<>();
        CtopOauthToken token = tokenService.getOauthTokenByAccountId(accountId);
        Date now = new Date();
        String startDate = DateUtils.formatDate(now, "yyyy-MM-dd");
        kuaishouInterfaceService.getCampaignDailyReport(token.getAccessToken(), token.getAccountId(), startDate, startDate, 1000, 1);
        return result;
    }

    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IKuaishouInterfaceService kuaishouInterfaceService;

    /**
     * 360借条报表客户后台数据
     */
    @GetMapping("jietiao/customer/insert")
    public Map<String, Object> jietiaoCustomerInsert() {
        Map<String, Object> result = new HashMap<>();
        String csvPath = "D:\\工作文件\\360借条\\测试数据\\客户后台数据.csv";
        reportService.insertCustomerInfo(csvPath, "utf8");
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS.getCode());
        return result;
    }

    /**
     * 360借条报表平台数据
     */
    @GetMapping("jietiao/plateform/insert")
    public Map<String, Object> jietiaoPlatformInsert() {
        Map<String, Object> result = new HashMap<>();
        String csvPath = "D:\\工作文件\\360借条\\测试数据\\快手后台数据.csv";
        reportService.insertPlatformInfo(csvPath, "gb2312", 4L);
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS.getCode());
        return result;
    }

    @PostMapping("debitAuto")
    Map<String, Object> debitAuto(@RequestBody JSONObject data) {
        return reportService.debitAuto(data);
    }

    @GetMapping("download/file")
    public void downLoadFile(String path, HttpServletResponse response) {
        try {
            File file = new File(path);
            InputStream fis = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Length", "" + file.length());
            response.setContentType("text/csv");
            response.setHeader("Content-Disposition", "attachment;fileName=template.xlsx");

            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Autowired
    private IReportService reportService;
}
