package org.jeecg.ctop.material.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.ctop.material.constants.Check;
import org.jeecg.ctop.material.entity.vo.KuaiShouOperateAccountVo;
import org.jeecg.ctop.material.entity.vo.KuaiShouOperateProjectVo;
import org.jeecg.ctop.material.mapper.MaterialStareMapper;
import org.jeecg.ctop.material.service.IAdqAdvertiserReportService;
import org.jeecg.ctop.material.service.MaterialStareService;
import org.jeecg.ctop.material.service.support.DashboardLocalCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

/**
 * 快手-运营数据盯盘-经理&个人
 * master
 */
@Slf4j
@RestController
@RequestMapping("/kuaiShouOpreate/kuaiShouMaterialStareController")
public class KuaiShouMaterialStareController {

    private static final long REALTIME_CACHE_TTL = 30 * 1000L;
    private static final long HISTORY_CACHE_TTL = 5 * 60 * 1000L;


    @Autowired
    MaterialStareService materialStareService;

    @Autowired
    private MaterialStareMapper materialStareMapper;

    @Autowired
    private IAdqAdvertiserReportService adqAdvertiserReportService;

    @Autowired
    private DashboardLocalCacheService dashboardLocalCacheService;


    /**
     * @param mediaType 媒体类型 1-头条 2-快手
     * @param type      查询类型 1-汇总 2-运营个人
     * @param startTime
     * @param endTime
     * @param userId
     * @description:数据总览-快手
     * @author: zianY
     * @time: 2022/2/15
     */
    @GetMapping("/kuaiShousumData")
    public Result<JSONObject> kuaiShousumData(@RequestParam(name = "mediaType") String mediaType,
                                              @RequestParam(name = "type") String type,
                                              @RequestParam(name = "startTime") String startTime,
                                              @RequestParam(name = "endTime") String endTime,
                                              @RequestParam(name = "userId") String userId) {
        String cacheKey = "dashboard:stare:sumData:" + safe(mediaType) + ":" + safe(type) + ":" + safe(startTime) + ":" + safe(endTime) + ":" + safe(userId);
        return getCachedResult(cacheKey, resolveCacheTtl(startTime, endTime), () -> materialStareService.kuaiShousumData(mediaType, type, startTime, endTime, userId));
    }


    /**
     * @param mediaType 媒体类型 1-头条 2-快手
     * @param startTime
     * @param endTime
     * @param userId
     * @description: 快手-运营组数据
     * @return: org.jeecg.common.api.vo.Result<com.alibaba.fastjson.JSONObject>
     * @author: zianY
     * @time: 2022/2/15
     */
    @GetMapping("/kuaiShouOperateDateTotal")
    public Result<JSONObject> kuaiShouOperateDateTotal(@RequestParam(name = "mediaType") String mediaType,
                                                       @RequestParam(name = "startTime") String startTime,
                                                       @RequestParam(name = "endTime") String endTime,
                                                       @RequestParam(name = "userId") String userId) {
        if (isTodayRange(startTime, endTime)) {
            return materialStareService.kuaiShouOperateDateTotal(mediaType, startTime, endTime, userId);
        }
        String cacheKey = "dashboard:stare:dateTotal:" + safe(mediaType) + ":" + safe(startTime) + ":" + safe(endTime) + ":" + safe(userId);
        return getCachedResult(cacheKey, resolveCacheTtl(startTime, endTime), () -> materialStareService.kuaiShouOperateDateTotal(mediaType, startTime, endTime, userId));
    }


    /**
     * @param pageNum
     * @param pageSize
     * @param startTime
     * @param endTime
     * @param userId
     * @description: 运营数据详情
     * @return: org.jeecg.common.api.vo.Result<com.alibaba.fastjson.JSONObject>
     * @author: zianY
     * @time: 2022/2/16
     */
    @GetMapping("/getKuaiShouOperateInfo")
    public Result<JSONObject> getKuaiShouOperateInfo(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                     @RequestParam(name = "pageSize", defaultValue = "5") int pageSize,
                                                     @RequestParam(name = "mediaType") String mediaType,
                                                     @RequestParam(name = "startTime") String startTime,
                                                     @RequestParam(name = "endTime") String endTime,
                                                     @RequestParam(name = "userId") String userId) {
        return materialStareService.getKuaiShouOperateInfo(pageNum, pageSize, mediaType, startTime, endTime, userId);
    }

    @GetMapping("/exportKuaiShouOperateInfo")
    public Result<JSONObject> exportKuaiShouOperateInfo(@RequestParam(name = "startTime") String startTime,
                                                        @RequestParam(name = "endTime") String endTime,
                                                        @RequestParam(name = "userId") String userId,
                                                        @RequestParam(name = "mediaType") String mediaType,
                                                        HttpServletResponse response) {
        return materialStareService.exportKuaiShouOperateInfo(startTime, endTime, mediaType, userId, response);
    }


    /**
     * @param pageNum
     * @param pageSize
     * @param mediaType 媒体类型 1-头条 2-快手
     * @param startTime
     * @param endTime
     * @param userId
     * @description:运营数据-项目列表
     * @return: org.jeecg.common.api.vo.Result<com.alibaba.fastjson.JSONObject>
     * @author: zianY
     * @time: 2022/2/17
     */
    @GetMapping("/getKuaiShouOperateProjectInfo")
    public Result<Object> getKuaiShouOperateProjectInfo(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                                        @RequestParam(name = "mediaType", defaultValue = "2") String mediaType,
                                                        @RequestParam(name = "startTime") String startTime,
                                                        @RequestParam(name = "endTime") String endTime,
                                                        @RequestParam(name = "userId") String userId) {
        if (!"16".equals(mediaType)) {
            //查询用户角色
            String roleCode = materialStareMapper.getRoleCodeByUserId(userId);
            //判断是否运营相关角色,并获取媒体类型
            if (!Check.isNull(roleCode)) {
                if ("touTiaoOperationManager".equals(roleCode)) {
                    mediaType = "1";
                } else if ("kuaishouOperationManager".equals(roleCode)) {
                    mediaType = "2";
                } else if (roleCode.toLowerCase().contains("perat")) {
                    String leaderRoleCode = materialStareMapper.getOperatLeaderRole(userId);
                    if (!Check.isNull(leaderRoleCode)) {
                        if ("touTiaoOperationManager".equals(leaderRoleCode)) {
                            mediaType = "1";
                        } else if ("kuaishouOperationManager".equals(leaderRoleCode)) {
                            mediaType = "2";
                        }
                    }
                } else {
                    return Result.successMsg("非运营相关角色,不显示数据", null);
                }
            }
        }
        String resolvedMediaType = mediaType;
        String cacheKey = "dashboard:stare:projectInfo:" + safe(resolvedMediaType) + ":" + safe(startTime) + ":" + safe(endTime) + ":" + safe(userId) + ":" + safe(pageNum) + ":" + safe(pageSize);
        return (Result<Object>) (Result<?>) getCachedResult(cacheKey, resolveCacheTtl(startTime, endTime), () -> materialStareService.getKuaiShouOperateProjectInfo(pageNum, pageSize, resolvedMediaType, startTime, endTime, userId));
    }


    @GetMapping("/getKuaiShouOperateProjectInfo2")
    public Result<Object> getKuaiShouOperateProjectInfo2(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                                         @RequestParam(name = "mediaType", defaultValue = "2") String mediaType,
                                                         @RequestParam(name = "startTime") String startTime,
                                                         @RequestParam(name = "endTime") String endTime,
                                                         @RequestParam(name = "userId") String userId) {

        try {
            List<KuaiShouOperateProjectVo> list = materialStareService.getOperateProjectInfo(pageNum, pageSize, mediaType, startTime, endTime, userId);
            return Result.successMsg("运营项目列表数据查询成功。", new PageInfo<>(list));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return Result.error("查询失败");
    }

    /**
     * @param pageNum
     * @param pageSize
     * @param startTime
     * @param endTime
     * @param projectId
     * @param userId
     * @description:运营数据-账户列表
     * @return: org.jeecg.common.api.vo.Result<com.alibaba.fastjson.JSONObject>
     * @author: zianY
     * @time: 2022/2/18
     */
    @GetMapping("/getKuaiShouOperateAccountInfo")
    public Result<JSONObject> getKuaiShouOperateAccountInfo(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestParam(name = "pageSize", defaultValue = "1000") int pageSize,
                                                            @RequestParam(name = "mediaType", required = false) String mediaType,
                                                            @RequestParam(name = "startTime") String startTime,
                                                            @RequestParam(name = "endTime") String endTime,
                                                            @RequestParam(name = "projectId", required = false) String projectId,
                                                            @RequestParam(name = "userId") String userId) {
        String cacheKey = "dashboard:stare:accountInfo:" + safe(mediaType) + ":" + safe(startTime) + ":" + safe(endTime) + ":" + safe(projectId) + ":" + safe(userId) + ":" + safe(pageNum) + ":" + safe(pageSize);
        return getCachedResult(cacheKey, resolveCacheTtl(startTime, endTime), () -> materialStareService.getKuaiShouOperateAccountInfo(pageNum, pageSize, startTime, endTime, projectId, userId, mediaType));
    }


    /**
     * @param accountId
     * @description: 查询账户余额 以及 账户状态 和 消耗
     * @return: org.jeecg.common.api.vo.Result<com.alibaba.fastjson.JSONObject>
     * @author: zianY
     * @time: 2022/2/21 14:39
     */
    @GetMapping("/getKuaiShouAccountInfo")
    public Result<JSONObject> getKuaiShouAccountInfo(@RequestParam(name = "accountId") long accountId) {
        return materialStareService.getKuaiShouAccountInfo(accountId);
    }


    /**
     * @param accountId
     * @param startTime
     * @param endTime
     * @description: 账户 折线图 查询时间为当天查询时报；查询时间为多天 查询日报
     * @return: org.jeecg.common.api.vo.Result<com.alibaba.fastjson.JSONObject>
     * @author: zianY
     * @time: 2022/2/21
     */
    @GetMapping("/getKuaiShouAccountInfoBrokenLine")
    public Result<JSONObject> getKuaiShouAccountInfoBrokenLine(@RequestParam(name = "accountId") long accountId,
                                                               @RequestParam(name = "startTime") String startTime,
                                                               @RequestParam(name = "endTime") String endTime,
                                                               @RequestParam(name = "type") String type) {
        return materialStareService.getKuaiShouAccountInfoBrokenLine(accountId, startTime, endTime, type);
    }


    /**
     * @param accountId
     * @param searchType searchType：查询类型  0-全部 1-上新 2-有效 3-爆款
     * @param startTime
     * @param endTime
     * @description: 快手 账户 素材上新-有效-爆款
     * @return: org.jeecg.common.api.vo.Result<com.alibaba.fastjson.JSONObject>
     * @author: zianY
     * @time: 2022/2/21
     */
    @GetMapping("/getKuaiShouAccountMaterialNewAndEffectAndFaddish")
    public Result<JSONObject> getKuaiShouAccountMaterialNewAndEffectAndFaddish(@RequestParam(name = "accountId") long accountId,
                                                                               @RequestParam(name = "searchType") String searchType,
                                                                               @RequestParam(name = "startTime") String startTime,
                                                                               @RequestParam(name = "endTime") String endTime,
                                                                               @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return materialStareService.getKuaiShouAccountMaterialNewAndEffectAndFaddish(accountId, searchType, startTime, endTime, pageNum, pageSize);
    }


    /**
     * @param accountId
     * @param startTime
     * @param endTime
     * @param type      查询类型 额。。。 查询选中的指标 --、 给前端组装数据格式  I didn't want to write
     * @description: 查询账户饼图数据信息 分版位；年龄；性别
     * @return: org.jeecg.common.api.vo.Result
     * @author: zianY
     * @time: 2022/2/22
     */
    @GetMapping("/getKuaiShouAccountPieChar")
    public Result getKuaiShouAccountPieChar(@RequestParam(name = "accountId") long accountId,
                                            @RequestParam(name = "startTime") String startTime,
                                            @RequestParam(name = "endTime") String endTime,
                                            @RequestParam(name = "type") String type) {
        return materialStareService.getKuaiShouAccountPieChar(accountId, startTime, endTime, type);
    }


    /**
     * @param mediaType 媒体类型 1-头条 2-快手
     * @param startTime
     * @param endTime
     * @param userId
     * @description:数据总览-快手-运营个人
     * @author: zianY
     * @time: 2022/2/15
     */
    @GetMapping("/kuaiShousumDataPersonal")
    public Result<JSONObject> kuaiShousumDataPersonal(@RequestParam(name = "mediaType", defaultValue = "2") String mediaType,
                                                      @RequestParam(name = "startTime") String startTime,
                                                      @RequestParam(name = "endTime") String endTime,
                                                      @RequestParam(name = "userId") String userId) {
        String cacheKey = "dashboard:stare:sumDataPersonal:" + safe(mediaType) + ":" + safe(startTime) + ":" + safe(endTime) + ":" + safe(userId);
        return getCachedResult(cacheKey, resolveCacheTtl(startTime, endTime), () -> materialStareService.kuaiShousumDataPersonal(mediaType, startTime, endTime, userId));
    }


    /**
     * @param mediaType 媒体类型 1-头条 2-快手
     * @param startTime
     * @param endTime
     * @param userId
     * @description: 运营个人数据
     * @return: org.jeecg.common.api.vo.Result<com.alibaba.fastjson.JSONObject>
     * @author: zianY
     * @time: 2022/3/3
     */
    @GetMapping("/kuaiShouOperateDateTotalPersonal")
    public Result<JSONObject> kuaiShouOperateDateTotalPersonal(@RequestParam(name = "mediaType") String mediaType,
                                                               @RequestParam(name = "startTime") String startTime,
                                                               @RequestParam(name = "endTime") String endTime,
                                                               @RequestParam(name = "userId") String userId) {
        if (!"16".equals(mediaType)) {
            //查询用户角色
            String roleCode = materialStareMapper.getRoleCodeByUserId(userId);
            //判断是否运营相关角色,并获取媒体类型
            if (!Check.isNull(roleCode)) {
                if ("touTiaoOperationManager".equals(roleCode)) {
                    mediaType = "1";
                } else if ("kuaishouOperationManager".equals(roleCode)) {
                    mediaType = "2";
                } else if (roleCode.toLowerCase().contains("perat")) {
                    String leaderRoleCode = materialStareMapper.getOperatLeaderRole(userId);
                    if (!Check.isNull(leaderRoleCode)) {
                        if ("touTiaoOperationManager".equals(leaderRoleCode)) {
                            mediaType = "1";
                        } else if ("kuaishouOperationManager".equals(leaderRoleCode)) {
                            mediaType = "2";
                        }
                    }
                } else {
                    return Result.successMsg("非运营相关角色,不显示数据", null);
                }
            }
        }
        String cacheKey = "dashboard:stare:dateTotalPersonal:" + safe(mediaType) + ":" + safe(startTime) + ":" + safe(endTime) + ":" + safe(userId);
        final String resolvedMediaType = mediaType;
        return getCachedResult(cacheKey, resolveCacheTtl(startTime, endTime), () -> materialStareService.kuaiShouOperateDateTotalPersonal(resolvedMediaType, startTime, endTime, userId));
    }

    @SuppressWarnings("unchecked")
    private Result<JSONObject> getCachedResult(String cacheKey, long ttlMillis, Supplier<Result> loader) {
        return dashboardLocalCacheService.getIfCacheable(cacheKey, ttlMillis, loader, result -> result != null && result.isSuccess());
    }

    private long resolveCacheTtl(String startTime, String endTime) {
        String today = DateUtils.formatDate(new Date());
        if (today.equals(startTime) && today.equals(endTime)) {
            return REALTIME_CACHE_TTL;
        }
        return HISTORY_CACHE_TTL;
    }

    private boolean isTodayRange(String startTime, String endTime) {
        String today = DateUtils.formatDate(new Date());
        return today.equals(startTime) && today.equals(endTime);
    }

    private String safe(Object value) {
        return value == null ? "null" : String.valueOf(value);
    }


    @GetMapping("/realTime/getUnitDateInfo")
    public Result<JSONObject> kuaiShouOperateDateTotalPersonal(@RequestParam(name = "accountId") long accountId,
                                                               @RequestParam(name = "pageNum") Integer pageNum,
                                                               @RequestParam(name = "pageSize") Integer pageSize) {

        return materialStareService.kuaiShouRealTimeUnitDateInfo(accountId, pageNum, pageSize);
    }


    /**
     * 获取用户下级的项目以及账户信息
     *
     * @param
     * @return
     */
    @GetMapping(value = "/participateListByMediaIdAllSubordinate")
    public Result<List<JSONObject>> participateListByMediaIdAllSubordinate(@RequestParam(name = "userId") String userId,
                                                                           @RequestParam(name = "type") String type) {
        JSONObject requestJson = new JSONObject();
        requestJson.put("userId", userId);
        requestJson.put("type", type);
        return materialStareService.participateListByMediaIdAllSubordinate(requestJson);
    }


    //获取当前用户所属公司的所有下级
    @GetMapping("/getCompanyAllSubordinateByUserId")
    public Result getCompanyAllSubordinateByUserId(@RequestParam(name = "userId") String userId) {
        return materialStareService.getCompanyAllSubordinateByUserId(userId);
    }


    @GetMapping("/exportKuaiShouOperateProjectInfo")
    public Result<JSONObject> exportKuaiShouOperateProjectInfo(
            @RequestParam(name = "mediaType") String mediaType,
            @RequestParam(name = "startTime") String startTime,
            @RequestParam(name = "endTime") String endTime,
            @RequestParam(name = "userId") String userId, HttpServletResponse response) {

        try {

            //默认
            JSONObject jsonObject = null;
            if ("2".equals(mediaType)) {
                jsonObject = materialStareMapper.getColumnInfo("e9ca23d68d884d4ebb19d07889727dae", "3001");
            } else {
                jsonObject = materialStareMapper.getColumnInfo("e9ca23d68d884d4ebb19d07889727dae", "3002");
            }
            if (Check.isNull(jsonObject)) {
                return Result.errorMsg("暂无自定义列。");
            }
            JSONArray array = jsonObject.getJSONArray("json");
            //第一步创建workbook
            HSSFWorkbook wb = new HSSFWorkbook();
            //设置样式
            HSSFCellStyle style = wb.createCellStyle();
            style.setAlignment(HorizontalAlignment.CENTER);//水平居中
            //设置字体
            HSSFFont font = wb.createFont();
            font.setFontName("黑体");
            font.setFontHeightInPoints((short) 12);//设置字体大小
            style.setFont(font);
            //2：创建sheet 设置sheet名称
            HSSFSheet sheet = wb.createSheet("项目数据");
            sheet.autoSizeColumn(0, true);
            //3：添加 第一行单元格
            HSSFRow row0 = sheet.createRow(0);
            HSSFCell cell0 = row0.createCell(0);
            //设置 第一行 表头
            cell0.setCellValue("项目数据");
            CellRangeAddress region = new CellRangeAddress(0, 0, 0, array.size() - 1);
            sheet.addMergedRegion(region);//合并行
            cell0.setCellStyle(style);
            //4：添加表头2行
            HSSFRow row = sheet.createRow(1);
            //5：创建第2行的单元格
            for (int i = 0; i < array.size(); i++) {
                JSONObject json = (JSONObject) array.get(i);
                HSSFCell cell = row.createCell(i);     //第一个单元格
                cell.setCellValue(json.getString("title"));         //设定值
                cell.setCellStyle(style);          //内容居中
            }

            List<KuaiShouOperateProjectVo> resultList = materialStareService.getOperateProjectInfo(1, 99999, mediaType, startTime, endTime, userId);

            for (int j = 0; j < resultList.size(); j++) {
                KuaiShouOperateProjectVo projectVo = resultList.get(j);
                //分版位
                if (!Check.isNull(projectVo.getAdSceneDate())) {
                    for (JSONObject jObj : projectVo.getAdSceneDate()) {
                        String name = jObj.get("adSceneName") + "";
                        if (name.contains("激励")) {
                            projectVo.setJiLishiPin(jObj.get("adSceneCost") + "");
                        }
                        if (name.contains("开屏")) {
                            projectVo.setKaiPing(jObj.get("adSceneCost") + "");
                        }
                        if (name.contains("搜索")) {
                            projectVo.setSouSuo(jObj.get("adSceneCost") + "");
                        }
                        if (name.contains("上下")) {
                            projectVo.setShangXiaHua(jObj.get("adSceneCost") + "");
                        }
                        if (name.contains("联盟")) {
                            projectVo.setLianMeng(jObj.get("adSceneCost") + "");
                        }
                        if (name.contains("信息流")) {
                            projectVo.setXinxiLiu(jObj.get("adSceneCost") + "");
                        }

                    }
                }

                Map project = Check.parseObj2Map(projectVo);

                //创建行 （表头占0-1行 所以 从 2开始）
                row = sheet.createRow(j + 2);
                for (int k = 0; k < array.size(); k++) {
                    JSONObject json = (JSONObject) array.get(k);
                    String fieldName = KuaiShouOperateProjectVo.class.getDeclaredField(json.getString("dataIndex")).getName();
                    project.get(fieldName);
                    row.createCell(k).setCellValue(project.get(fieldName) + "");
                }
            }

            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("项目列表.xlsx", "UTF-8"));
            OutputStream output = response.getOutputStream();
            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
            wb.write(bufferedOutPut);
            bufferedOutPut.flush();
            bufferedOutPut.close();
            output.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @GetMapping("/exportKuaiShouOperateAccountInfo")
    public Result<JSONObject> exportKuaiShouOperateAccountInfo(@RequestParam(name = "startTime") String startTime,
                                                               @RequestParam(name = "endTime") String endTime,
                                                               @RequestParam(name = "projectId") String projectId,
                                                               @RequestParam(name = "userId") String userId, HttpServletResponse response) {

        try {
            JSONObject jsonObject = materialStareMapper.getColumnInfo(userId, "102");
            if (Check.isNull(jsonObject)) {
                return Result.errorMsg("暂无自定义列。");
            }
            JSONArray array = jsonObject.getJSONArray("json");
            //第一步创建workbook
            HSSFWorkbook wb = new HSSFWorkbook();
            //设置样式
            HSSFCellStyle style = wb.createCellStyle();
            style.setAlignment(HorizontalAlignment.CENTER);//水平居中
            //设置字体
            HSSFFont font = wb.createFont();
            font.setFontName("黑体");
            font.setFontHeightInPoints((short) 12);//设置字体大小
            style.setFont(font);
            //2：创建sheet 设置sheet名称
            HSSFSheet sheet = wb.createSheet("账户数据");
            sheet.autoSizeColumn(0, true);
            //3：添加 第一行单元格
            HSSFRow row0 = sheet.createRow(0);
            HSSFCell cell0 = row0.createCell(0);
            //设置 第一行 表头
            cell0.setCellValue("账户数据");
            CellRangeAddress region = new CellRangeAddress(0, 0, 0, array.size() - 1);
            sheet.addMergedRegion(region);//合并行
            cell0.setCellStyle(style);
            //4：添加表头2行
            HSSFRow row = sheet.createRow(1);
            //5：创建第2行的单元格
            for (int i = 0; i < array.size(); i++) {
                JSONObject json = (JSONObject) array.get(i);
                HSSFCell cell = row.createCell(i);     //第一个单元格
                cell.setCellValue(json.getString("title"));         //设定值
                cell.setCellStyle(style);          //内容居中

            }

            List<KuaiShouOperateAccountVo> resultList = materialStareService.exportKuaiShouOperateAccountInfo(startTime, endTime, projectId, userId);

            for (int j = 0; j < resultList.size(); j++) {
                KuaiShouOperateAccountVo accountVo = resultList.get(j);
                Map account = Check.parseObj2Map(accountVo);

                //创建行 （表头占0-1行 所以 从 2开始）
                row = sheet.createRow(j + 2);
                for (int k = 0; k < array.size(); k++) {
                    JSONObject json = (JSONObject) array.get(k);
                    String fieldName = KuaiShouOperateAccountVo.class.getDeclaredField(json.getString("dataIndex")).getName();
                    account.get(fieldName);
                    row.createCell(k).setCellValue(account.get(fieldName).toString());
                }
            }

            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("账户列表.xlsx", "UTF-8"));
            OutputStream output = response.getOutputStream();
            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
            wb.write(bufferedOutPut);
            bufferedOutPut.flush();
            bufferedOutPut.close();
            output.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 运营组列表数据
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getOperationGroupData")
    public Result<JSONObject> getOperationGroupData(
            @RequestParam(name = "userId") String userId,
            @RequestParam(name = "mediaType") String mediaType,
            @RequestParam(name = "startTime") String startTime,
            @RequestParam(name = "endTime") String endTime,
            @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize) {
        return materialStareService.getOperationGroupData(userId, mediaType, startTime, endTime, pageNum, pageSize);
    }


    /**
     * 运营组列表数据-导出
     *
     * @return
     */
    @GetMapping("/exportOperationGroupData")
    public Result exportOperationGroupData(@RequestParam(name = "userId") String userId, @RequestParam(name = "mediaType") String mediaType,
                                           @RequestParam(name = "startTime") String startTime,
                                           @RequestParam(name = "endTime") String endTime, HttpServletResponse response) {

        return materialStareService.exportOperationGroupData(userId, mediaType, startTime, endTime, response);
    }


}
