package com.ruixuan.jiaoyang.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.core.controller.BaseController;
import com.ruixuan.common.core.page.TableDataInfo;
import com.ruixuan.common.utils.Check;
import com.ruixuan.common.utils.DateUtils;
import com.ruixuan.data.utils.ExportExcelUtils;
import com.ruixuan.jiaoyang.service.IJiaoYangReportService;
import com.ruixuan.system.service.ISysDeptService;
import com.ruixuan.system.service.ISysRoleService;
import com.ruixuan.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.SneakyThrows;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/jy/report")
public class JiaoYangReportController extends BaseController {
    @Autowired
    private IJiaoYangReportService jiaoYangReportService;
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 商品数据
     *
     * @param itemId
     * @param itemTitle
     * @param orderStartDate
     * @param orderEndDate
     * @param fieId
     * @param sort
     * @return
     */
    @GetMapping("/item")
    @ApiOperation(value = "商品列表")
    public TableDataInfo item(
            @ApiParam("商品Id") @RequestParam(value = "itemId", required = false) String itemId,
            @ApiParam("商品名称") @RequestParam(value = "itemTitle", required = false) String itemTitle,
            @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
            @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
            @ApiParam("排序字段") @RequestParam(value = "fieId", required = false) String fieId,
            @ApiParam("排序方式") @RequestParam(value = "sort", required = false) String sort) {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate)) {
            Long start = DateUtils.strDateToInt(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Long end = DateUtils.strDateToInt(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(itemId)) {
            requestMap.put("itemId", itemId);
        }
        if (!Check.isNull(itemTitle)) {
            requestMap.put("itemTitle", itemTitle);
        }

        requestMap.put("fieId", fieId);
        requestMap.put("sort", sort);

        TableDataInfo dataInfo = new TableDataInfo();
        if (Check.isNullMap(requestMap)) {
            dataInfo.setCode(-1);
            dataInfo.setMsg("入参不能为空");
            return dataInfo;
        }
        startPage();
        List<JSONObject> list = jiaoYangReportService.item(requestMap);
        return getDataTable(list);
    }


    /**
     * 商品详情
     *
     * @param itemId
     * @param orderStartDate
     * @param orderEndDate
     * @param promoterId
     * @param promoterNickName
     * @return
     */
    @GetMapping("/itemDetail")
    @ApiOperation(value = "商品详情")
    public TableDataInfo itemDetail(
            @ApiParam("商品ID") @RequestParam(value = "itemId", required = false) String itemId,
            @ApiParam("订单开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
            @ApiParam("订单结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
            @ApiParam("达人Id") @RequestParam(value = "promoterId", required = false) String promoterId,
            @ApiParam("达人名称") @RequestParam(value = "promoterNickName", required = false) String promoterNickName) {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate)) {
            Long start = DateUtils.strDateToInt(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Long end = DateUtils.strDateToInt(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(promoterId)) {
            requestMap.put("promoterId", promoterId);
        }
        if (!Check.isNull(itemId)) {
            requestMap.put("itemId", itemId);
        }
        if (!Check.isNull(promoterNickName)) {
            requestMap.put("itemTitle", promoterNickName);
        }

        TableDataInfo dataInfo = new TableDataInfo();
        if (Check.isNullMap(requestMap)) {
            dataInfo.setCode(-1);
            dataInfo.setMsg("入参不能为空");
            return dataInfo;
        }
        startPage();
        List<JSONObject> list = jiaoYangReportService.itemDetail(requestMap);
        return getDataTable(list);
    }


    /**
     * 达人数据
     *
     * @param orderStartDate
     * @param orderEndDate
     * @param promoterNickName
     * @param promoterId
     * @return
     */
    @GetMapping("/promoter")
    @ApiOperation(value = "达人订单")
    public TableDataInfo promoter(@ApiParam("订单开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
                                  @ApiParam("订单结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
                                  @ApiParam("达人名称") @RequestParam(value = "promoterNickName", required = false) String promoterNickName,
                                  @ApiParam("达人ID") @RequestParam(value = "promoterId", required = false) Long promoterId,
                                  @ApiParam("类型") @RequestParam(value = "type", required = false) Long type,
                                  Long userId) {

        Map<String, Object> requestMap = new HashMap<>();

        String roleId = sysRoleService.getRoleBYUserId(userId);
        if ("jy_bd".equals(roleId) || "jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId) || "jy_scissors".equals(roleId)) {
            List<Long> userIds = new ArrayList<>();
            if ("jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId)) {
                Long deptId = sysDeptService.getDeptIdByUserId(userId);
                userIds = sysDeptService.getDeptUserListByDeptId(deptId);
            } else {
                userIds.add(userId);
            }
            List<Long> promoterIds = jiaoYangReportService.getPromoterIdsByUserIds(userIds);
            if (Check.isNull(promoterIds)) {
                return getDataTable(new ArrayList<JSONObject>());
            }
            requestMap.put("promoterIds", promoterIds);
        }


        if (!Check.isNull(orderStartDate)) {
            Long start = DateUtils.strDateToInt(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Long end = DateUtils.strDateToInt(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(promoterNickName)) {
            requestMap.put("promoterNickName", promoterNickName);
        }
        if (!Check.isNull(promoterId)) {
            requestMap.put("promoterId", promoterId);
        }
        if (!Check.isNull(type)) {
            requestMap.put("type", type);
        }
        TableDataInfo dataInfo = new TableDataInfo();
        if (Check.isNullMap(requestMap)) {
            dataInfo.setCode(-1);
            dataInfo.setMsg("入参不能为空");
            return dataInfo;
        }
        startPage();
        List<JSONObject> list = jiaoYangReportService.promoter(requestMap);
        return getDataTable(list);
    }


    /**
     * 达人带货详情
     *
     * @param promoterId
     * @param orderStartDate
     * @param orderEndDate
     * @param itemTitle
     * @param itemId
     * @return
     */
    @GetMapping("/promoterDetail")
    @ApiOperation(value = "达人带货详情")
    public TableDataInfo promoterDetail(
            @ApiParam("达人Id") @RequestParam(value = "promoterId", required = false) String promoterId,
            @ApiParam("订单开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
            @ApiParam("订单结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
            @ApiParam("商品名称") @RequestParam(value = "itemTitle", required = false) String itemTitle,
            @ApiParam("商品ID") @RequestParam(value = "itemId", required = false) String itemId, Long userId) {
        Map<String, Object> requestMap = new HashMap<>();
        String roleId = sysRoleService.getRoleBYUserId(userId);
        if ("jy_bd".equals(roleId) || "jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId) || "jy_scissors".equals(roleId)) {
            List<Long> userIds = new ArrayList<>();
            if ("jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId)) {
                Long deptId = sysDeptService.getDeptIdByUserId(userId);
                userIds = sysDeptService.getDeptUserListByDeptId(deptId);
            } else {
                userIds.add(userId);
            }
            List<Long> promoterIds = jiaoYangReportService.getPromoterIdsByUserIds(userIds);
            if (Check.isNull(promoterIds)) {
                return getDataTable(new ArrayList<>());
            }
            requestMap.put("promoterIds", promoterIds);
        }
        if (!Check.isNull(orderStartDate)) {
            Long start = DateUtils.strDateToInt(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Long end = DateUtils.strDateToInt(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(promoterId)) {
            requestMap.put("promoterId", promoterId);
        }
        if (!Check.isNull(itemId)) {
            requestMap.put("itemId", itemId);
        }
        if (!Check.isNull(itemTitle)) {
            requestMap.put("itemTitle", itemTitle);
        }

        TableDataInfo dataInfo = new TableDataInfo();
        if (Check.isNullMap(requestMap)) {
            dataInfo.setCode(-1);
            dataInfo.setMsg("入参不能为空");
            return dataInfo;
        }
        startPage();
        List<JSONObject> list = jiaoYangReportService.anchorOrderDetail(requestMap);
        return getDataTable(list);
    }


    @GetMapping("/promoterStatistics")
    @ApiOperation(value = "订单统计")
    public JSONObject promoterStatistics(
            @ApiParam("达人Id") @RequestParam(value = "promoterId", required = false) String promoterId,
            @ApiParam("订单开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
            @ApiParam("订单结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate, Long userId) {
        Map<String, Object> requestMap = new HashMap<>();
        String roleId = sysRoleService.getRoleBYUserId(userId);
        if ("jy_bd".equals(roleId) || "jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId) || "jy_scissors".equals(roleId)) {
            List<Long> userIds = new ArrayList<>();
            if ("jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId)) {
                Long deptId = sysDeptService.getDeptIdByUserId(userId);
                userIds = sysDeptService.getDeptUserListByDeptId(deptId);
            } else {
                userIds.add(userId);
            }
            List<Long> promoterIds = jiaoYangReportService.getPromoterIdsByUserIds(userIds);
            if (Check.isNull(promoterIds)) {
                return new JSONObject();
            }
            requestMap.put("promoterIds", promoterIds);
        }

        if (!Check.isNull(orderStartDate)) {
            Long start = DateUtils.strDateToInt(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Long end = DateUtils.strDateToInt(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(promoterId)) {
            requestMap.put("promoterId", promoterId);
        }

        JSONObject returnJson = new JSONObject();
        if (Check.isNullMap(requestMap)) {
            returnJson.put("code", -1);
            returnJson.put("message", "入参不能为空");

            return returnJson;
        }
        List<JSONObject> list = jiaoYangReportService.promoterStatistics(requestMap);
        returnJson.put("code", 0);
        returnJson.put("data", list);
        return returnJson;
    }


    @GetMapping("/exportClipCooperation")
    public void exportClipCooperation(HttpServletRequest request, HttpServletResponse response, String statDate, Long promoterId, String promoterName) throws IOException {
        Long date = Long.valueOf(statDate.replace("-", ""));
        List<JSONObject> list = jiaoYangReportService.getClipCooperation(date, promoterId, promoterName);
        List<List<Object>> exportList = new ArrayList<>();
        if (!Check.isNull(list)) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject data = list.get(i);
                List<Object> export = new ArrayList();
                export.add(data.getString("clipId"));
                export.add(data.getString("clipName"));
                export.add(data.getString("commissionRateView"));
                export.add(data.getString("fansNum"));
                export.add(data.getString("status"));
                export.add(data.getString("shopScore"));
                export.add(data.getString("clipGmv30d"));
                export.add(data.getString("authItemGmv30d"));
                export.add(data.getString("videoCount"));
                export.add(data.getString("videoShow"));
                export.add(data.getString("terminationReason"));
                exportList.add(export);
            }
        }
        String[] headers = {"达人ID", "达人名称", "佣金率", "粉丝数", "状态", "口碑评分", "二创GMV", "授权商品GMV", "二创作者挂车短视频发布量", "二创作者挂车短视频曝光量", "状态备注"};
        OutputStream os = response.getOutputStream();
        ExportExcelUtils eeu = new ExportExcelUtils();
        XSSFWorkbook workbook = new XSSFWorkbook();
        eeu.exportExcel(workbook, 0, "二创信息", headers, exportList);
        this.setResponseHeader(response, "二创信息.xls");
        try {
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        os.flush();
        os.close();
    }


    @GetMapping("/getClipCooperation")
    public TableDataInfo list(String statDate, Long promoterId, String promoterName) {
        Long date = Long.valueOf(statDate.replace("-", ""));
        startPage();
        List<JSONObject> list = jiaoYangReportService.getClipCooperation(date, promoterId, promoterName);
        return getDataTable(list);
    }


    @GetMapping("/indexOrderInfo")
    @ApiOperation(value = "首页-订单数据")
    public JSONObject orderTotal(
            @ApiParam("时间类型") @RequestParam(value = "dateType", required = false) Integer dateType,
            @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
            @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
            @ApiParam("结束时间") @RequestParam(value = "userId", required = false) Long userId
    ) {
        Map<String, Object> requestMap = new HashMap<>();
        JSONObject returnJson = new JSONObject();

        String roleId = sysRoleService.getRoleBYUserId(userId);
        if ("jy_bd".equals(roleId) || "jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId) || "jy_scissors".equals(roleId)) {
            List<Long> userIds = new ArrayList<>();
            if ("jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId)) {
                Long deptId = sysDeptService.getDeptIdByUserId(userId);
                userIds = sysDeptService.getDeptUserListByDeptId(deptId);
            } else {
                userIds.add(userId);
            }
            List<Long> promoterIds = jiaoYangReportService.getPromoterIdsByUserIds(userIds);
            if (Check.isNull(promoterIds)) {
                return returnJson;
            }
            requestMap.put("promoterIds", promoterIds);
        }
        if (dateType == 0) {
            if (!Check.isNull(orderStartDate)) {
                Long start = DateUtils.strDateToInt(orderStartDate); //当天开始时间
                requestMap.put("start", start);
            }
            if (!Check.isNull(orderEndDate)) {
                Long end = DateUtils.strDateToInt(orderEndDate); // 当天结束时间
                requestMap.put("end", end);
            }
            JSONObject orderTotal = jiaoYangReportService.getOrderTotal(requestMap);
            returnJson.put("orderTotal", orderTotal);
            Integer hour = DateUtils.getNowHour() - 1; // 当前小时
            if (hour >= 0) {
                JSONObject ratioJson = new JSONObject();
                Long nowDate = DateUtils.strDateToInt(orderStartDate);
                Long yesDate = DateUtils.strDateToInt(DateUtils.getAnotherDay("yyyy-MM-dd", orderStartDate, -1)); // 昨日日期
                Map<String, Object> ratioMap = new HashMap<>();
                ratioMap.put("nowDate", nowDate);

                ratioMap.put("yesDate", yesDate);
                ratioMap.put("hour", hour);
                Double ringRatio = jiaoYangReportService.getOrderTotalRatio(ratioMap); //环比
                ratioJson.put("ringRatio", ringRatio);
                yesDate = DateUtils.strDateToInt(DateUtils.getAnotherDay("yyyy-MM-dd", orderStartDate, -6)); // 昨日日期
                ratioMap.put("yesDate", yesDate);
                Double yearRatio = jiaoYangReportService.getOrderTotalRatio(ratioMap); //同比
                ratioJson.put("yearRatio", yearRatio);
                returnJson.put("ratio", ratioJson);
            }
        } else {
            if (!Check.isNull(orderStartDate)) {
                Long start = DateUtils.strDateToInt(orderStartDate); //昨日开始时间
                requestMap.put("start", start);
            }
            if (!Check.isNull(orderEndDate)) {
                Long end = DateUtils.strDateToInt(orderEndDate); // 前日开始时间
                requestMap.put("end", end);
            }
            JSONObject orderTotal = jiaoYangReportService.getOrderTotal(requestMap);
            returnJson.put("orderTotal", orderTotal);
        }
        return returnJson;
    }


    /**
     * 首页-服务费数据
     *
     * @param dateType
     * @param orderStartDate
     * @param orderEndDate
     * @return
     */
    @GetMapping("/indexServiceAmountInfo")
    @ApiOperation(value = "首页-服务费数据")
    public JSONObject indexServiceAmountInfo(
            @ApiParam("时间类型") @RequestParam(value = "dateType", required = false) Integer dateType,
            @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
            @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
            Long userId
    ) {
        Map<String, Object> requestMap = new HashMap<>();
        JSONObject returnJson = new JSONObject();

        String roleId = sysRoleService.getRoleBYUserId(userId);
        if ("jy_bd".equals(roleId) || "jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId) || "jy_scissors".equals(roleId)) {
            List<Long> userIds = new ArrayList<>();
            if ("jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId)) {
                Long deptId = sysDeptService.getDeptIdByUserId(userId);
                userIds = sysDeptService.getDeptUserListByDeptId(deptId);
            } else {
                userIds.add(userId);
            }
            List<Long> promoterIds = jiaoYangReportService.getPromoterIdsByUserIds(userIds);
            if (Check.isNull(promoterIds)) {
                return returnJson;
            }
            requestMap.put("promoterIds", promoterIds);
        }


        if (!Check.isNull(orderStartDate)) {
            Long start = DateUtils.strDateToInt(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Long end = DateUtils.strDateToInt(orderEndDate);
            requestMap.put("end", end);
        }
        JSONObject serviceAmountTotal = jiaoYangReportService.getServiceAmountTotal(requestMap);
        returnJson.put("serviceAmountTotal", serviceAmountTotal);
        return returnJson;
    }


    /**
     * 首页-金额数据
     *
     * @param dateType
     * @param orderStartDate
     * @param orderEndDate
     * @return
     */
    @GetMapping("/indexAmountInfo")
    @ApiOperation(value = "首页-金额数据")
    public JSONObject indexAmountInfo(
            @ApiParam("时间类型") @RequestParam(value = "dateType", required = false) Integer dateType,
            @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
            @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
            Long userId) {
        Map<String, Object> requestMap = new HashMap<>();
        JSONObject returnJson = new JSONObject();

        String roleId = sysRoleService.getRoleBYUserId(userId);
        if ("jy_bd".equals(roleId) || "jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId) || "jy_scissors".equals(roleId)) {
            List<Long> userIds = new ArrayList<>();
            if ("jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId)) {
                Long deptId = sysDeptService.getDeptIdByUserId(userId);
                userIds = sysDeptService.getDeptUserListByDeptId(deptId);
            } else {
                userIds.add(userId);
            }
            List<Long> promoterIds = jiaoYangReportService.getPromoterIdsByUserIds(userIds);
            if (Check.isNull(promoterIds)) {
                return returnJson;
            }
            requestMap.put("promoterIds", promoterIds);
        }
        if (dateType == 0) {
            if (!Check.isNull(orderStartDate)) {
                Long start = DateUtils.strDateToInt(orderStartDate); //当天开始时间
                requestMap.put("start", start);
            }
            if (!Check.isNull(orderEndDate)) {
                Long end = DateUtils.strDateToInt(orderEndDate); // 当天结束时间
                requestMap.put("end", end);
            }
            JSONObject amountTotal = jiaoYangReportService.getAmountTotal(requestMap);
            returnJson.put("amountTotal", amountTotal);
            Integer hour = DateUtils.getNowHour() - 1; // 当前小时
            if (hour >= 0) {
                JSONObject amountRatioJson = new JSONObject();
                Long nowDate = DateUtils.strDateToInt(orderStartDate);
                Long yesDate = DateUtils.strDateToInt(DateUtils.getAnotherDay("yyyy-MM-dd", orderStartDate, -1)); // 昨日日期
                Map<String, Object> ratioMap = new HashMap<>();
                ratioMap.put("nowDate", nowDate);

                ratioMap.put("yesDate", yesDate);
                ratioMap.put("hour", hour);
                Double amountRingRatio = jiaoYangReportService.getAmountTotalRatio(ratioMap); //环比
                amountRatioJson.put("amountRingRatio", amountRingRatio);
                yesDate = DateUtils.strDateToInt(DateUtils.getAnotherDay("yyyy-MM-dd", orderStartDate, -6)); // 昨日日期
                ratioMap.put("yesDate", yesDate);
                Double amountYearRatio = jiaoYangReportService.getAmountTotalRatio(ratioMap); //同比
                amountRatioJson.put("amountYearRatio", amountYearRatio);
                returnJson.put("amountRatio", amountRatioJson);
            }
        } else {
            if (!Check.isNull(orderStartDate)) {
                Long start = DateUtils.strDateToInt(orderStartDate); //昨日开始时间
                requestMap.put("start", start);
            }
            if (!Check.isNull(orderEndDate)) {
                Long end = DateUtils.strDateToInt(orderEndDate); // 前日开始时间
                requestMap.put("end", end);
            }
            JSONObject amountTotal = jiaoYangReportService.getAmountTotal(requestMap);
            returnJson.put("amountTotal", amountTotal);
        }
        return returnJson;
    }


    @GetMapping("/indexPromoterRank")
    @ApiOperation(value = "首页-达人排行")
    public TableDataInfo indexPromoterRank(
            @ApiParam("时间类型") @RequestParam(value = "dateType", required = true) Integer dateType,
            @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = true) String orderStartDate,
            @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = true) String orderEndDate,
            @ApiParam("userId") @RequestParam(value = "userId", required = false) Long userId
    ) {

        Map<String, Object> requestMap = new HashMap<>();

        /*String roleId = sysRoleService.getRoleBYUserId(userId);
        if ("jy_bd".equals(roleId) || "jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId) || "jy_scissors".equals(roleId)) {
            List<Long> userIds = new ArrayList<>();
            if ("jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId)) {
                Long deptId = sysDeptService.getDeptIdByUserId(userId);
                userIds = sysDeptService.getDeptUserListByDeptId(deptId);
            } else {
                userIds.add(userId);
            }
            List<Long> promoterIds = jiaoYangReportService.getPromoterIdsByUserIds(userIds);
            if (Check.isNull(promoterIds)) {
                return getDataTable(
                        new ArrayList<>()
                );
            }
            requestMap.put("promoterIds", promoterIds);
        }*/
        if (dateType == 0) {

            if (!Check.isNull(orderStartDate)) {
                Long start = DateUtils.strDateToInt(orderStartDate); //当天开始时间
                requestMap.put("start", start);
            }
            if (!Check.isNull(orderEndDate)) {
                Long end = DateUtils.strDateToInt(orderEndDate); // 当天结束时间
                requestMap.put("end", end);
            }
            Integer hour = DateUtils.getNowHour(); // 当前小时
            Long nowDate = DateUtils.strDateToInt(orderStartDate); // 当天开始时间戳
            Long yesDate = DateUtils.strDateToInt(DateUtils.getAnotherDay("yyyy-MM-dd", orderStartDate, -1)); // 昨日日期
            requestMap.put("nowDate", nowDate);
            requestMap.put("hour", hour);
            requestMap.put("yesDate", yesDate);
            startPage();
            List<JSONObject> promoterRankRatioList = jiaoYangReportService.getPromoterRankRatioList(requestMap);
            return getDataTable(promoterRankRatioList);

        } else {

            if (!Check.isNull(orderStartDate)) {
                Long start = DateUtils.strDateToInt(orderStartDate);
                requestMap.put("start", start);
            }
            if (!Check.isNull(orderEndDate)) {
                Long end = DateUtils.strDateToInt(orderEndDate);
                requestMap.put("end", end);
            }
            startPage();
            List<JSONObject> orderRatioList = jiaoYangReportService.getPromoterRatioList(requestMap);
            return getDataTable(orderRatioList);

        }
    }


    /**
     * 首页-商品排行
     *
     * @param dateType
     * @param orderStartDate
     * @param orderEndDate
     * @return
     */
    @GetMapping("/indexOrderRank")
    @ApiOperation(value = "首页-商品排行")
    public TableDataInfo indexOrderRank(
            @ApiParam("时间类型") @RequestParam(value = "dateType", required = true) Integer dateType,
            @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = true) String orderStartDate,
            @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = true) String orderEndDate) {

        Map<String, Object> requestMap = new HashMap<>();
        if (dateType == 0) {
            Integer hour = DateUtils.getNowHour(); // 当前小时
            Long nowDate = DateUtils.strDateToInt(orderStartDate); // 当天开始时间戳
            Long yesDate = DateUtils.strDateToInt(DateUtils.getAnotherDay("yyyy-MM-dd", orderStartDate, -1)); // 昨日日期
            requestMap.put("nowDate", nowDate);
            requestMap.put("yesDate", yesDate);
            requestMap.put("hour", hour);
            startPage();
            List<JSONObject> orderRankRatioList = jiaoYangReportService.getOrderRankRatioList(requestMap);
            return getDataTable(orderRankRatioList);
        } else {
            if (!Check.isNull(orderStartDate)) {
                Long start = DateUtils.strDateToInt(orderStartDate);
                requestMap.put("start", start);
            }
            if (!Check.isNull(orderEndDate)) {
                Long end = DateUtils.strDateToInt(orderEndDate);
                requestMap.put("end", end);
            }
            startPage();
            List<JSONObject> orderRankRatioList = jiaoYangReportService.getOrderRankList(requestMap);
            return getDataTable(orderRankRatioList);
        }
    }

    /**
     * 首页-统计图
     *
     * @param orderStartDate
     * @param orderEndDate
     * @param type
     * @return
     */
    @GetMapping("/indexStatistics")
    @ApiOperation(value = "首页-统计图")
    public List<JSONObject> indexStatistics(
            @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
            @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
            @ApiParam("类型") @RequestParam(value = "type", required = false) String type,
            Long userId) {
        Map<String, Object> requestMap = new HashMap<>();
        String roleId = sysRoleService.getRoleBYUserId(userId);
        if ("jy_bd".equals(roleId) || "jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId) || "jy_scissors".equals(roleId)) {
            List<Long> userIds = new ArrayList<>();
            if ("jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId)) {
                Long deptId = sysDeptService.getDeptIdByUserId(userId);
                userIds = sysDeptService.getDeptUserListByDeptId(deptId);
            } else {
                userIds.add(userId);
            }
            List<Long> promoterIds = jiaoYangReportService.getPromoterIdsByUserIds(userIds);
            if (Check.isNull(promoterIds)) {
                return new ArrayList<>();
            }
            requestMap.put("promoterIds", promoterIds);
        }
        String nowDate = DateUtils.getNowDateStr();
        Long start = null;
        Long end = null;
        if (nowDate.equals(orderStartDate) && nowDate.equals(orderEndDate)) {
            String endStr = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
            end = DateUtils.strDateToInt(endStr);
            start = DateUtils.strDateToInt(DateUtils.getAnotherDay("yyyy-MM-dd", endStr, -6));
        } else {
            start = DateUtils.strDateToInt(orderStartDate);
            end = DateUtils.strDateToInt(orderEndDate);
        }
        if (!Check.isNull(start)) {
            requestMap.put("start", start);
        }
        if (!Check.isNull(end)) {
            requestMap.put("end", end);
        }
        if (!Check.isNull(type)) {
            requestMap.put("type", type);
        }
        List<JSONObject> list = jiaoYangReportService.indexStatistics(requestMap);
        return list;
    }

    /**
     * 首页-时段对比
     *
     * @param orderStartDate
     * @param orderEndDate
     * @return
     */
    @SneakyThrows
    @GetMapping("/indexTimeIntervalRatio")
    @ApiOperation(value = "首页-时段对比")
    public JSONObject indexTimeIntervalRatio(
            @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
            @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
            Long userId
    ) {
        Map<String, Object> requestMap = new HashMap<>();
        String roleId = sysRoleService.getRoleBYUserId(userId);
        if ("jy_bd".equals(roleId) || "jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId) || "jy_scissors".equals(roleId)) {
            List<Long> userIds = new ArrayList<>();
            if ("jy_bd_manage".equals(roleId) || "jy_scissors_manage".equals(roleId)) {
                Long deptId = sysDeptService.getDeptIdByUserId(userId);
                userIds = sysDeptService.getDeptUserListByDeptId(deptId);
            } else {
                userIds.add(userId);
            }
            List<Long> promoterIds = jiaoYangReportService.getPromoterIdsByUserIds(userIds);
            if (Check.isNull(promoterIds)) {
                return new JSONObject();
            }
            requestMap.put("promoterIds", promoterIds);
        }
        String nowDate = DateUtils.getNowDateStr();
        String thisCycleEnd = null; // 当前时段结束日期
        String thisCycleStart = null; // 当前时段开始日期
        String lastCycleEnd = null; // 上一时段结束日期
        String lastCycleStart = null; // 上一时段开始日期
        if (nowDate.equals(orderStartDate) && nowDate.equals(orderEndDate)) {
            thisCycleEnd = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
            thisCycleStart = DateUtils.getAnotherDay("yyyy-MM-dd", thisCycleEnd, -6);
            lastCycleEnd = DateUtils.getAnotherDay("yyyy-MM-dd", thisCycleStart, -1);
            lastCycleStart = DateUtils.getAnotherDay("yyyy-MM-dd", lastCycleEnd, -6);
        } else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            int differDay = DateUtils.differentDaysByMillisecond(simpleDateFormat.parse(orderStartDate), simpleDateFormat.parse(orderEndDate));
            thisCycleEnd = orderEndDate;
            thisCycleStart = orderStartDate;
            lastCycleEnd = DateUtils.getAnotherDay("yyyy-MM-dd", orderStartDate, -1);
            lastCycleStart = DateUtils.getAnotherDay("yyyy-MM-dd", lastCycleEnd, -differDay);
        }
        if (!Check.isNull(thisCycleStart)) {
            Long thisCycleStartTemp = DateUtils.strDateToInt(thisCycleStart);
            requestMap.put("thisCycleStartTemp", thisCycleStartTemp);
        }
        if (!Check.isNull(thisCycleEnd)) {
            Long thisCycleEndTemp = DateUtils.strDateToInt(thisCycleEnd);
            requestMap.put("thisCycleEndTemp", thisCycleEndTemp);
        }
        if (!Check.isNull(lastCycleStart)) {
            Long lastCycleStartTemp = DateUtils.strDateToInt(lastCycleStart);
            requestMap.put("lastCycleStartTemp", lastCycleStartTemp);
        }
        if (!Check.isNull(lastCycleEnd)) {
            Long lastCycleEndTemp = DateUtils.strDateToInt(lastCycleEnd);
            requestMap.put("lastCycleEndTemp", lastCycleEndTemp);
        }
        JSONObject timeIntervalRatioJson = jiaoYangReportService.getTimeIntervalRatio(requestMap);
        return timeIntervalRatioJson;
    }

    //发送响应流方法
    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();
        }
    }

}
