package cn.com.ctop.kuaishou.modules.report.controller;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.ExportExcelUtils;
import cn.com.ctop.kuaishou.modules.report.service.IKuaiShouYiCheReportService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/ctop/yiche/report/")
public class KuaiShouYiCheReportController {
    @Autowired
    private IKuaiShouYiCheReportService yiCheReportService;


    @PostMapping("hourReport")
    public Result<JSONObject> advertiserReport(@RequestBody JSONObject requestJson) {
        Result<JSONObject> result = new Result<>();
        JSONObject hourReport = yiCheReportService.getHourReport(requestJson);
        result.setResult(hourReport);
        return result;
    }


    @PostMapping("dailyReport")
    public Result<JSONObject> dailyReport(@RequestBody JSONObject requestJson) {
        Result<JSONObject> result = new Result<>();
        JSONObject hourReport = yiCheReportService.getDailyReport(requestJson);
        result.setResult(hourReport);
        return result;
    }


    /**
     * 导出报表
     *
     * @return
     */
    @RequestMapping(value = "/exportHourReport")
    @ResponseBody
    public void exportHourReport(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject requestJson) throws Exception {

        JSONObject hourReport = yiCheReportService.getHourReport(requestJson);
        JSONArray androidArr = hourReport.getJSONArray("android");

        // 安卓相关
        List<List<Object>> androidList = new ArrayList<>();
        if (!Check.isNull(androidArr)) {
            for (int i = 0; i < androidArr.size(); i++) {
                JSONObject jsonObject = androidArr.getJSONObject(i);

                List<Object> android = new ArrayList();
                android.add(jsonObject.getString("systemType"));
                android.add(jsonObject.getString("authName"));
                android.add(jsonObject.getBigDecimal("charge"));
                android.add(jsonObject.getLong("activation"));
                android.add(jsonObject.getBigDecimal("activationCost"));
                android.add(jsonObject.getBigDecimal("estimatedCost"));
                androidList.add(android);
            }
        }


        // ios 相关
        JSONArray iosArr = hourReport.getJSONArray("ios");
        List<List<Object>> iosList = new ArrayList<>();
        if (!Check.isNull(iosArr)) {
            for (int i = 0; i < iosArr.size(); i++) {
                JSONObject jsonObject = iosArr.getJSONObject(i);
                List<Object> ios = new ArrayList();
                ios.add(jsonObject.getString("systemType"));
                ios.add(jsonObject.getString("authName"));
                ios.add(jsonObject.getBigDecimal("charge"));
                ios.add(jsonObject.getLong("activation"));
                ios.add(jsonObject.getBigDecimal("activationCost"));
                ios.add(jsonObject.getBigDecimal("estimatedCost"));
                iosList.add(ios);
            }
        }


        String[] androidHeaders = {"投放设备", "分类", "现金花费", "账户激活量", "激活成本", "预估成本"};
        String[] iosHeaders = {"投放设备", "分类", "现金花费", "账户激活量", "激活成本", "预估成本"};
        OutputStream os = response.getOutputStream();
        ExportExcelUtils eeu = new ExportExcelUtils();
        XSSFWorkbook workbook = new XSSFWorkbook();
        eeu.exportExcel(workbook, 0, "安卓时报统计", androidHeaders, androidList);
        eeu.exportExcel(workbook, 1, "ios时报统计", iosHeaders, iosList);
        this.setResponseHeader(response, "易车时报.xls");
        workbook.write(os);
        os.flush();
        os.close();
    }


    @RequestMapping(value = "/exportDailyReport")
    @ResponseBody
    public void exportDailyReport(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject requestJson) throws Exception {
        JSONObject dailyReport = yiCheReportService.getDailyReport(requestJson);
        JSONObject androidJson = dailyReport.getJSONObject("android");

        // 安卓相关
        List<List<Object>> androidList = new ArrayList<>();
        if (!Check.isNull(androidJson)) {
            List<Object> android = new ArrayList();
            android.add(androidJson.getString("agent"));
            android.add(androidJson.getString("media"));
            android.add(androidJson.getString("systemType"));
            if (!Check.isNull(androidJson.getString("statDate"))) {
                android.add(androidJson.getString("statDate").replace("-", "/"));
            }
            android.add(androidJson.getBigDecimal("charge"));
            android.add(androidJson.getBigDecimal("cashCost"));
            android.add(androidJson.getLong("photoShow"));
            android.add(androidJson.getLong("photoClick"));
            android.add(androidJson.getString("clickRate"));
            android.add(androidJson.getString("impression1kCost"));
            android.add(androidJson.getLong("bclick"));
            android.add(androidJson.getString("eventJinJianApp"));
            android.add(androidJson.getLong("activation"));
            android.add(androidJson.getLong("youMengactivation"));
            android.add(androidJson.getLong("tdActivation"));
            android.add(androidJson.getBigDecimal("activationCost"));
            android.add(androidJson.getBigDecimal("youMengactivationCost"));
            android.add(androidJson.getBigDecimal("tdActivationCost"));
            android.add(androidJson.getBigDecimal("actionCost"));
            android.add(androidJson.getBigDecimal("clickCost"));
            android.add(androidJson.getString("actionRatio"));
            android.add(androidJson.getString("eventJinJianAppRatio"));
            android.add(androidJson.getString("activationRatio"));
            androidList.add(android);

        }


        JSONObject iosJson = dailyReport.getJSONObject("ios");

        // 安卓相关
        List<List<Object>> iosList = new ArrayList<>();
        if (!Check.isNull(iosJson)) {
            List<Object> ios = new ArrayList();
            ios.add(iosJson.getString("agent"));
            ios.add(iosJson.getString("media"));
            ios.add(iosJson.getString("systemType"));
            if (!Check.isNull(iosJson.getString("statDate"))) {
                ios.add(iosJson.getString("statDate").replace("-", "/"));
            }
            ios.add(iosJson.getBigDecimal("charge"));
            ios.add(iosJson.getBigDecimal("cashCost"));
            ios.add(iosJson.getLong("photoShow"));
            ios.add(iosJson.getLong("photoClick"));
            ios.add(iosJson.getString("clickRate"));
            ios.add(iosJson.getString("impression1kCost"));
            ios.add(iosJson.getLong("bclick"));
            ios.add(iosJson.getString("eventJinJianApp"));
            ios.add(iosJson.getLong("activation"));
            ios.add(iosJson.getLong("youMengactivation"));
            ios.add(iosJson.getLong("tdActivation"));
            ios.add(iosJson.getBigDecimal("activationCost"));
            ios.add(iosJson.getBigDecimal("youMengactivationCost"));
            ios.add(iosJson.getBigDecimal("tdActivationCost"));
            ios.add(iosJson.getBigDecimal("actionCost"));
            ios.add(iosJson.getBigDecimal("clickCost"));
            ios.add(iosJson.getString("actionRatio"));
            ios.add(iosJson.getString("eventJinJianAppRatio"));
            ios.add(iosJson.getString("activationRatio"));
            iosList.add(ios);
        }


        // 应用直达
        JSONObject directApplicationJson = dailyReport.getJSONObject("directApplication");
        List<List<Object>> directApplicationList = new ArrayList<>();
        if (!Check.isNull(directApplicationJson)) {
            List<Object> directApplication = new ArrayList();
            directApplication.add(directApplicationJson.getString("agent"));
            directApplication.add(directApplicationJson.getString("media"));
            directApplication.add(directApplicationJson.getString("systemType"));
            directApplication.add(directApplicationJson.getString("statDate"));
            directApplication.add(directApplicationJson.getBigDecimal("charge"));
            directApplication.add(directApplicationJson.getBigDecimal("cashCost"));
            directApplication.add(directApplicationJson.getLong("photoShow"));
            directApplication.add(directApplicationJson.getLong("photoClick"));
            directApplication.add(directApplicationJson.getString("clickRate"));
            directApplication.add(directApplicationJson.getString("impression1kCost"));
            directApplication.add(directApplicationJson.getLong("bclick"));
            directApplication.add(directApplicationJson.getString("eventJinJianApp"));
            directApplication.add(directApplicationJson.getLong("activation"));
            directApplication.add(directApplicationJson.getLong("youMengactivation"));
            directApplication.add(directApplicationJson.getLong("tdActivation"));
            directApplication.add(directApplicationJson.getBigDecimal("activationCost"));
            directApplication.add(directApplicationJson.getBigDecimal("youMengactivationCost"));
            directApplication.add(directApplicationJson.getBigDecimal("tdActivationCost"));
            directApplication.add(directApplicationJson.getBigDecimal("actionCost"));
            directApplication.add(directApplicationJson.getBigDecimal("clickCost"));
            directApplication.add(directApplicationJson.getString("actionRatio"));
            directApplication.add(directApplicationJson.getString("eventJinJianAppRatio"));
            directApplication.add(directApplicationJson.getString("activationRatio"));
            directApplicationList.add(directApplication);
        }


        // 渠道包分析
        JSONArray channelSubcontractingArr = dailyReport.getJSONArray("channelSubcontracting");
        List<List<Object>> channelSubcontractingList = new ArrayList<>();
        if (!Check.isNull(channelSubcontractingArr)) {
            for (int i = 0; i < channelSubcontractingArr.size(); i++) {
                JSONObject channelSubcontractingJson = channelSubcontractingArr.getJSONObject(i);
                List<Object> channelSubcontracting = new ArrayList();
                channelSubcontracting.add(channelSubcontractingJson.getString("agent"));
                channelSubcontracting.add(channelSubcontractingJson.getString("systemType"));
                channelSubcontracting.add(channelSubcontractingJson.getString("statDate"));
                channelSubcontracting.add(channelSubcontractingJson.getString("campaignName"));
                channelSubcontracting.add(channelSubcontractingJson.getBigDecimal("charge"));
                channelSubcontracting.add(channelSubcontractingJson.getBigDecimal("cashCost"));
                channelSubcontracting.add(channelSubcontractingJson.getLong("photoShow"));
                channelSubcontracting.add(channelSubcontractingJson.getLong("photoClick"));
                channelSubcontracting.add(channelSubcontractingJson.getString("clickRate"));
                channelSubcontracting.add(channelSubcontractingJson.getString("impression1kCost"));
                channelSubcontracting.add(channelSubcontractingJson.getLong("bclick"));
                channelSubcontracting.add(channelSubcontractingJson.getString("eventJinJianApp"));
                channelSubcontracting.add(channelSubcontractingJson.getLong("activation"));
                channelSubcontracting.add(channelSubcontractingJson.getLong("youMengactivation"));
                channelSubcontracting.add(channelSubcontractingJson.getLong("tdActivation"));
                channelSubcontracting.add(channelSubcontractingJson.getBigDecimal("activationCost"));
                channelSubcontracting.add(channelSubcontractingJson.getBigDecimal("youMengactivationCost"));
                channelSubcontracting.add(channelSubcontractingJson.getBigDecimal("tdActivationCost"));
                channelSubcontracting.add(channelSubcontractingJson.getBigDecimal("actionCost"));
                channelSubcontracting.add(channelSubcontractingJson.getBigDecimal("clickCost"));
                channelSubcontracting.add(channelSubcontractingJson.getString("actionRatio"));
                channelSubcontracting.add(channelSubcontractingJson.getString("eventJinJianAppRatio"));
                channelSubcontracting.add(channelSubcontractingJson.getString("activationRatio"));
                channelSubcontracting.add(channelSubcontractingJson.getString("keyAction"));
                channelSubcontractingList.add(channelSubcontracting);
            }

        }

        String[] iosHeaders = {"代理商", "媒体", "设备", "日期", "账面花费", "现金花费", "曝光", "点击", "点击率", "千次曝光花费", "行为量", "下载完成数", "账户激活量", "友盟激活量", "TD激活量", "账户激活成本", "友盟激活成本", "TD激活成本", "平均行为单价", "平均点击单价", "行为率", "下载完成行为率", "下载完成激活率"};
        String[] androidHeaders = {"代理商", "媒体", "设备", "日期", "账面花费", "现金花费", "曝光", "点击", "点击率", "千次曝光花费", "行为量", "下载完成数", "账户激活量", "友盟激活量", "TD激活量", "账户激活成本", "友盟激活成本", "TD激活成本", "平均行为单价", "平均点击单价", "行为率", "下载完成行为率", "下载完成激活率"};
        String[] directApplicationHeaders = {"代理商", "媒体", "设备", "日期", "账面花费", "现金花费", "曝光", "点击", "点击率", "千次曝光花费", "行为量", "下载完成数", "账户激活量", "友盟激活量", "TD激活量", "账户激活成本", "友盟激活成本", "TD激活成本", "平均行为单价", "平均点击单价", "行为率", "下载完成行为率", "下载完成激活率"};
        String[] channelSubcontractingHeaders = {"代理商", "设备", "日期", "渠道包", "账面花费", "现金花费", "曝光", "点击", "点击率", "千次曝光花费", "行为量", "下载完成数", "账户激活量", "友盟激活量", "TD激活量", "账户激活成本", "友盟激活成本", "TD激活成本", "平均行为单价", "平均点击单价", "行为率", "下载完成行为率", "下载完成激活率", "关键行为数"};

        OutputStream os = response.getOutputStream();
        ExportExcelUtils eeu = new ExportExcelUtils();
        XSSFWorkbook workbook = new XSSFWorkbook();
        eeu.exportExcel(workbook, 0, "安卓汇总", androidHeaders, androidList);
        eeu.exportExcel(workbook, 1, "ios汇总", iosHeaders, iosList);
        eeu.exportExcel(workbook, 2, "应用直达", directApplicationHeaders, directApplicationList);
        eeu.exportExcel(workbook, 3, "渠道包分包", channelSubcontractingHeaders, channelSubcontractingList);


        this.setResponseHeader(response, "易车日报.xls");
        workbook.write(os);
        os.flush();
        os.close();
    }


    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


}
