package com.ruixuan.bytedance.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruixuan.bytedance.service.IBytedancePromoterOrderListService;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 团长订单Controller
 *
 * @author ruoyi
 * @date 2023-08-28
 */
@Api(tags = "---------头条")
@RestController
@RequestMapping("/bytedance/order")
public class BytedancePromoterOrderListController extends BaseController {
    @Autowired
    private IBytedancePromoterOrderListService bytedancePromoterOrderListService;


    /**
     * 【达人订单管理】
     * -列表
     */
    @GetMapping("/bytedanceOrderList")
    @ApiOperation(value = "达人订单管理-列表")
    public TableDataInfo bytedanceOrderList(@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) String promoterId) {

        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(promoterNickName)) {
            requestMap.put("promoterNickName", promoterNickName);
        }
        if (!Check.isNull(promoterId)) {
            requestMap.put("promoterId", promoterId);
        }
        TableDataInfo dataInfo = new TableDataInfo();
        if (Check.isNullMap(requestMap)) {
            dataInfo.setCode(-1);
            dataInfo.setMsg("入参不能为空");
            return dataInfo;
        }
        startPage();
        List<JSONObject> list = bytedancePromoterOrderListService.getBytedanceOrderList(requestMap, true);
        return getDataTable(list);
    }

    /**
     * 【达人订单管理】
     * -列表-导出
     */
    @GetMapping(value = "/exportBytedanceOrderList")
    @ApiOperation(value = "达人订单管理列表-导出")
    public void exportPromoterTotal(HttpServletRequest request, HttpServletResponse response,
                                    @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) String promoterId
    ) throws Exception {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(promoterNickName)) {
            requestMap.put("promoterNickName", promoterNickName);
        }
        if (!Check.isNull(promoterId)) {
            requestMap.put("promoterId", promoterId);
        }
        List<JSONObject> list = bytedancePromoterOrderListService.getBytedanceOrderList(requestMap, false);
        List<List<Object>> exportList = new ArrayList<>();
        if (!Check.isNull(list)) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject date = list.get(i);
                List<Object> export = new ArrayList();
                export.add(date.getString("promoterNickName") + "（" + date.getString("promoterId") + "）");
                export.add(date.getInteger("orderNum"));
                export.add(date.getInteger("validOrderNUm"));
                export.add(date.getString("validRadio"));
                export.add(date.getString("payAmount"));
                export.add(date.getString("validPayAmount"));
                export.add(date.getString("userName"));
                exportList.add(export);
            }
        }
        String[] headers = {"达人名称", "订单数", "有效订单数", "有效订单率", "GVM", "有效GMV", "绑定人"};
        OutputStream os = response.getOutputStream();
        ExportExcelUtils eeu = new ExportExcelUtils();
        XSSFWorkbook workbook = new XSSFWorkbook();
        eeu.exportExcel(workbook, 0, "达人信息", headers, exportList);
        this.setResponseHeader(response, "达人信息.xls");
        workbook.write(os);
        os.flush();
        os.close();
    }

    /**
     * 【达人订单管理】
     * -详情
     */
    @GetMapping("/anchorBytedanceOrderDetail")
    @ApiOperation(value = "达人订单管理-详情")
    public TableDataInfo anchorBytedanceOrderDetail(
            @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) {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(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 = bytedancePromoterOrderListService.anchorBytedanceOrderDetail(requestMap);
        return getDataTable(list);
    }

    /**
     * 【达人订单管理】
     * -订单统计
     */
    @GetMapping("/bytedanceOrderStatistics")
    @ApiOperation(value = "达人订单管理-订单统计")
    public JSONObject bytedanceOrderStatistics(
            @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) {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(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 = bytedancePromoterOrderListService.bytedanceOrderStatistics(requestMap);
        returnJson.put("code", 0);
        returnJson.put("data", list);
        return returnJson;
    }

    /**
     * 【达人订单管理】
     * -详情-导出
     */
    @GetMapping(value = "/exportBytedancePromoter")
    @ApiOperation(value = "达人订单管理-详情导出")
    public void exportBytedancePromoter(HttpServletRequest request, HttpServletResponse response,
                                        @ApiParam("达人Id") @RequestParam(value = "promoterId", required = false) String promoterId,
                                        @ApiParam("达人名") @RequestParam(value = "promoterNickName", required = false) String promoterNickName,
                                        @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) throws Exception {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(promoterNickName)) {
            requestMap.put("promoterNickName", promoterNickName);
        }
        if (!Check.isNull(promoterId)) {
            requestMap.put("promoterId", promoterId);
        }
        if (!Check.isNull(itemId)) {
            requestMap.put("itemId", itemId);
        }
        if (!Check.isNull(itemTitle)) {
            requestMap.put("itemTitle", itemTitle);
        }

        List<JSONObject> list = bytedancePromoterOrderListService.exportBytedancePromoter(requestMap);
        List<List<Object>> exportList = new ArrayList<>();
        if (!Check.isNull(list)) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject date = list.get(i);
                List<Object> export = new ArrayList();
                String promoter = date.getString("promoterNickName") + "（" + date.getString("promoterId") + "）";
                export.add(promoter);
                String item = date.getString("itemTitle") + "（" + date.getString("itemId") + "）";
                export.add(item);
                export.add(date.getString("imageUrl"));
                export.add(date.getInteger("orderNum"));
                export.add(date.getInteger("validOrderNUm"));
                export.add(date.getString("validRadio"));
                exportList.add(export);
            }
        }
        String[] headers = {"达人", "商品", "商品封面", "订单数", "有效订单数", "有效订单率"};
        OutputStream os = response.getOutputStream();
        ExportExcelUtils eeu = new ExportExcelUtils();
        XSSFWorkbook workbook = new XSSFWorkbook();
        eeu.exportExcel(workbook, 0, "达人信息", headers, exportList);
        this.setResponseHeader(response, "达人信息.xls");
        workbook.write(os);
        os.flush();
        os.close();
    }

    /*---------------------------------------------------------------------------------------------------------*/


    /**
     * 【渠道管理】
     * -列表
     */
    @GetMapping("/bytedanceBDReportList")
    @ApiOperation(value = "渠道管理-列表")
    public TableDataInfo bytedanceBDReportList(
            @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
            @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
            @ApiParam("用户ID") @RequestParam(value = "collectSampleId", required = false) Long collectSampleId) {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(collectSampleId)) {
            requestMap.put("collectSampleId", collectSampleId);
        }
        TableDataInfo dataInfo = new TableDataInfo();
        if (Check.isNullMap(requestMap)) {
            dataInfo.setCode(500);
            dataInfo.setMsg("入参不能为空");
            return dataInfo;
        }
        startPage();
        List<JSONObject> list = bytedancePromoterOrderListService.bytedanceBDReportList(requestMap);
        return getDataTable(list);
    }


    /**
     * 【渠道管理】
     * -列表-导出
     */
    @GetMapping("/exportBytedanceBDReportList")
    @ApiOperation(value = "渠道管理列表-导出")
    public void exportBdReportList(HttpServletRequest request, HttpServletResponse response,
                                   @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
                                   @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
                                   @ApiParam("用户ID") @RequestParam(value = "collectSampleId", required = false) Long collectSampleId) throws IOException {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(collectSampleId)) {
            requestMap.put("collectSampleId", collectSampleId);
        }

        List<JSONObject> list = bytedancePromoterOrderListService.bytedanceBDReportList(requestMap);
        List<List<Object>> exportList = new ArrayList<>();
        if (!Check.isNull(list)) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject date = list.get(i);
                List<Object> export = new ArrayList();
                export.add(date.getString("collectSampleId"));
                export.add(date.getString("collectSampleName"));
                export.add(date.getString("promoterCount"));
                export.add(date.getString("sampleCount"));
                export.add(date.getString("sampleRate"));
                export.add(date.getString("notSampleCount"));
                export.add(date.getString("sendTotalCount"));
                export.add(date.getString("sendPromoterCount"));
                export.add(date.getString("sendItemCount"));
                export.add(date.getString("orderNum"));
                export.add(date.getString("voucherOrderNum"));
                export.add(date.getString("validOrderNum"));
                export.add(date.getString("voucherValidOrderNum"));
                export.add(date.getString("validOrderRate"));
                export.add(date.getString("orderAmount"));
                export.add(date.getString("voucherOrderAmount"));
                export.add(date.getString("regimentalPromotionAmount"));
                export.add(date.getString("voucherRegimentalPromotionAmount"));
                export.add(date.getString("totalRegimentalSettleAmount"));
                export.add(date.getString("voucherTotalRegimentalSettleAmount"));
                exportList.add(export);
            }
        }
        String[] headers = {"渠道ID", "渠道姓名", "（总）达人数", "（总）领样达人数", "（总）领样率", "（总）未领样数", "发样数", "发样达人数", "发样商品数", "订单数", "（凭）订单数", "有效订单数", "（凭）有效订单数", "有效订单率", "订单金额", "凭（订单金额）", "预估服务费", "（凭）预估服务费", "结算服务费", "（凭）结算服务费"};
        OutputStream os = response.getOutputStream();
        ExportExcelUtils eeu = new ExportExcelUtils();
        XSSFWorkbook workbook = new XSSFWorkbook();
        eeu.exportExcel(workbook, 0, "bd业绩汇总", headers, exportList);
        this.setResponseHeader(response, "bd业绩汇总.xls");
        workbook.write(os);
        os.flush();
        os.close();
    }

    /**
     * 【渠道管理】
     * -详情总计
     */
    @GetMapping("/bytedanceBDTotal")
    @ApiOperation(value = "渠道管理-详情总计")
    public TableDataInfo bytedanceBDTotal(
            @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
            @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
            @ApiParam("用户ID") @RequestParam(value = "collectSampleId", required = false) Long collectSampleId) {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(collectSampleId)) {
            requestMap.put("collectSampleId", collectSampleId);
        }

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

    /**
     * 【渠道管理】
     * -详情集合
     */
    @GetMapping("/bytedanceBDDetailList")
    @ApiOperation(value = "渠道管理-详情集合")
    public TableDataInfo bytedanceBDDetailList(
            @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
            @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
            @ApiParam("用户ID") @RequestParam(value = "collectSampleId", required = false) Long collectSampleId,
            @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)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(collectSampleId)) {
            requestMap.put("collectSampleId", collectSampleId);
        }
        if (Check.isNull(fieId)) {
            fieId = "orderNum";
            sort = "ASC";
        }
        requestMap.put("fieId", fieId);
        requestMap.put("sort", sort);
        TableDataInfo dataInfo = new TableDataInfo();
        if (Check.isNullMap(requestMap)) {
            dataInfo.setCode(500);
            dataInfo.setMsg("入参不能为空");
            return dataInfo;
        }
        startPage();
        List<JSONObject> list = bytedancePromoterOrderListService.bytedanceBDDetailList(requestMap);
        return getDataTable(list);
    }

    /**
     * 【渠道管理】
     * -详情-导出
     */
    @GetMapping("/exportBdDetail")
    @ApiOperation(value = "渠道管理-详情导出")
    public void exportBdDetail(HttpServletRequest request, HttpServletResponse response,
                               @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
                               @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
                               @ApiParam("用户ID") @RequestParam(value = "collectSampleId", required = false) Long collectSampleId) throws IOException {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(collectSampleId)) {
            requestMap.put("collectSampleId", collectSampleId);
        }
        requestMap.put("fieId", "orderNum");
        requestMap.put("sort", "desc");
        List<JSONObject> list = bytedancePromoterOrderListService.bytedanceBDDetailList(requestMap);
        List<List<Object>> exportList = new ArrayList<>();
        if (!Check.isNull(list)) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject date = list.get(i);
                List<Object> export = new ArrayList();
                export.add(date.getString("promoterId"));
                export.add(date.getString("promoterName"));
                export.add(date.getString("sampleCount"));
                export.add(date.getString("orderNum"));
                export.add(date.getString("validOrderNum"));
                export.add(date.getString("validOrderRate"));
                export.add(date.getString("regimentalPromotionAmount"));
                export.add(date.getString("totalRegimentalSettleAmount"));
                export.add(date.getString("promoterUrl"));
                exportList.add(export);
            }
        }
        String[] headers = {"达人ID", "达人名称", "领样数", "订单数", "有效订单数", "有效率", "预估服务费收入", "结算服务费收入", "达人头像"};
        OutputStream os = response.getOutputStream();
        ExportExcelUtils eeu = new ExportExcelUtils();
        XSSFWorkbook workbook = new XSSFWorkbook();
        eeu.exportExcel(workbook, 0, "bd业绩", headers, exportList);
        this.setResponseHeader(response, "bd业绩.xls");
        workbook.write(os);
        os.flush();
        os.close();
    }
    /*---------------------------------------------------------------------------------------------------------*/

    /**
     * 【商品管理】
     * -列表
     */
    @GetMapping("/bytedanceItemList")
    @ApiOperation(value = "商品-列表")
    public TableDataInfo bytedanceItemList(
            @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 = true) String fieId,
            @ApiParam("排序方式") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam("用户ID") @RequestParam(value = "userId", required = false) String userId

    ) {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(itemId)) {
            requestMap.put("itemId", itemId);
        }
        if (!Check.isNull(itemTitle)) {
            requestMap.put("itemTitle", itemTitle);
        }
        if (!Check.isNull(userId)) {
            requestMap.put("userId", userId);
        }

        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 = bytedancePromoterOrderListService.bytedanceItemList(requestMap);
        return getDataTable(list);
    }

    /**
     * 【商品管理】
     * -列表(详情)-导出
     */
    @GetMapping("/exportBytedanceItemList")
    @ApiOperation(value = "商品-列表导出")
    public void bytedanceItemList(
            HttpServletRequest request, HttpServletResponse response,
            @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("用户ID") @RequestParam(value = "userId", required = false) String userId) throws IOException {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(itemId)) {
            requestMap.put("itemId", itemId);
        }
        if (!Check.isNull(itemTitle)) {
            requestMap.put("itemTitle", itemTitle);
        }
        if (!Check.isNull(userId)) {
            requestMap.put("userId", userId);
        }

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

        if (Check.isNullMap(requestMap)) {
            return;
        }

        List<JSONObject> list = bytedancePromoterOrderListService.bytedanceItemList(requestMap);
        List<List<Object>> exportList = new ArrayList<>();
        if (!Check.isNull(list)) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject date = list.get(i);
                List<Object> export = new ArrayList();
                export.add(date.getString("userName"));
                export.add(date.getLong("itemId"));
                export.add(date.getString("itemTitle"));
                export.add(date.getString("imageUrl"));
                export.add(date.getString("itemCommissionRate"));
                export.add(date.getInteger("orderNum"));
                export.add(date.getInteger("validOrderNUm"));
                export.add(date.getString("validRadio"));
                exportList.add(export);
            }
        }
        String[] headers = {"所属人", "商品ID", "商品名称", "商品封面", "佣金率", "订单数", "有效订单数", "有效订单率"};
        OutputStream os = response.getOutputStream();
        ExportExcelUtils eeu = new ExportExcelUtils();
        XSSFWorkbook workbook = new XSSFWorkbook();
        eeu.exportExcel(workbook, 0, "订单信息", headers, exportList);
        this.setResponseHeader(response, "订单信息.xls");
        workbook.write(os);
        os.flush();
        os.close();
    }


    /**
     * 【商品管理】
     * -列表-TOP3
     */
    @GetMapping("/getBytedanceTopPromoterByItemId")
    @ApiOperation(value = "商品-列表TOP3")
    public TableDataInfo getBytedanceTopPromoterByItemId(
            @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) {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(itemId)) {
            requestMap.put("itemId", itemId);
        }

        List<JSONObject> list = bytedancePromoterOrderListService.getBytedanceTopPromoterByItemId(requestMap);
        return getDataTable(list);
    }

    /**
     * 【商品管理】
     * -详情
     */
    @GetMapping("/bytedanceItemDetail")
    @ApiOperation(value = "商品-详情")
    public TableDataInfo bytedanceItemDetail(
            @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)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(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 = bytedancePromoterOrderListService.bytedanceItemDetail(requestMap);
        return getDataTable(list);
    }

    /**
     * 【商品管理】
     * -详情-导出
     */
    @GetMapping("/exportBytedanceItemDetail")
    @ApiOperation(value = "商品-详情导出")
    public void exportBytedanceItemDetail(
            HttpServletRequest request, HttpServletResponse response,
            @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
    ) throws IOException {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(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);
        }
        if (Check.isNullMap(requestMap)) {
            return;
        }
        List<JSONObject> list = bytedancePromoterOrderListService.bytedanceItemDetail(requestMap);
        List<List<Object>> exportList = new ArrayList<>();
        if (!Check.isNull(list)) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject date = list.get(i);
                List<Object> export = new ArrayList();
                export.add(date.getString("promoterNickName"));
                export.add(date.getString("promoterId"));
                export.add(date.getLong("orderNum"));
                export.add(date.getString("payAmount"));
                export.add(date.getString("regimentalPromotionRate"));
                export.add(date.getString("validOrderNUm"));
                export.add(date.getString("validRadio"));
                exportList.add(export);
            }
        }
        String[] headers = {"达人名称", "达人Id", "订单数", "有效订单数", "有效率", "付款金额", "服务费率"};
        OutputStream os = response.getOutputStream();
        ExportExcelUtils eeu = new ExportExcelUtils();
        XSSFWorkbook workbook = new XSSFWorkbook();
        eeu.exportExcel(workbook, 0, "订单信息", headers, exportList);
        this.setResponseHeader(response, "订单信息.xls");
        workbook.write(os);
        os.flush();
        os.close();
    }

    /*---------------------------------------------------------------------------------------------------------*/


    /**
     * 【招商管理】
     * -列表
     */
    @GetMapping("/adminBytedanceReportList")
    @ApiOperation(value = "招商管理列表")
    public TableDataInfo adminBytedanceReportList(
            @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
            @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
            @ApiParam("结束时间") @RequestParam(value = "bindStartDate", required = false) String bindStartDate,
            @ApiParam("结束时间") @RequestParam(value = "bindEndDate", required = false) String bindEndDate,
            @ApiParam("用户ID") @RequestParam(value = "userId", required = false) Long userId
    ) {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate) && !Check.isNull(orderEndDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
            Integer end = DateUtils.getIntegerTime(orderEndDate);
            requestMap.put("end", end);
        }

        if (!Check.isNull(bindStartDate) && !Check.isNull(bindEndDate)) {
            Integer start = DateUtils.getIntegerTime(bindStartDate);
            requestMap.put("start", start);
            Integer end = DateUtils.getIntegerTime(bindEndDate);
            requestMap.put("end", end);
        }

        if (!Check.isNull(userId)) {
            requestMap.put("userId", userId);
        }

        TableDataInfo dataInfo = new TableDataInfo();
        if (Check.isNullMap(requestMap)) {
            dataInfo.setCode(-1);
            dataInfo.setMsg("入参不能为空");
            return dataInfo;
        }
        List<JSONObject> list = new ArrayList<>();
        startPage();
        if (!Check.isNull(orderStartDate) && !Check.isNull(orderEndDate)) {
            list = bytedancePromoterOrderListService.adminBytedanceReportList(requestMap);
        } else if (!Check.isNull(bindStartDate) && !Check.isNull(bindEndDate)) {
            list = bytedancePromoterOrderListService.adminBytedanceReportListByBind(requestMap);
        }

        return getDataTable(list);
    }


    /**
     * 【招商管理】
     * -列表-导出
     */
    @GetMapping("/exportAdminBytedanceReportList")
    @ApiOperation(value = "招商管理列表-导出")
    public void exportAdminBytedanceReportList(
            HttpServletRequest request, HttpServletResponse response,
            @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
            @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
            @ApiParam("结束时间") @RequestParam(value = "bindStartDate", required = false) String bindStartDate,
            @ApiParam("结束时间") @RequestParam(value = "bindEndDate", required = false) String bindEndDate,
            @ApiParam("用户ID") @RequestParam(value = "userId", required = false) Long userId

    ) throws IOException {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate) && !Check.isNull(orderEndDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
            Integer end = DateUtils.getIntegerTime(orderEndDate);
            requestMap.put("end", end);
        }

        if (!Check.isNull(bindStartDate) && !Check.isNull(bindEndDate)) {
            Integer start = DateUtils.getIntegerTime(bindStartDate);
            requestMap.put("start", start);
            Integer end = DateUtils.getIntegerTime(bindEndDate);
            requestMap.put("end", end);
        }

        if (!Check.isNull(userId)) {
            requestMap.put("userId", userId);
        }

        List<JSONObject> list = new ArrayList<>();
        if (!Check.isNull(orderStartDate) && !Check.isNull(orderEndDate)) {
            list = bytedancePromoterOrderListService.adminBytedanceReportList(requestMap);
        } else if (!Check.isNull(bindStartDate) && !Check.isNull(bindEndDate)) {
            list = bytedancePromoterOrderListService.adminBytedanceReportListByBind(requestMap);
        }

        List<List<Object>> exportList = new ArrayList<>();
        if (!Check.isNull(list)) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject date = list.get(i);
                List<Object> export = new ArrayList();
                export.add(date.getString("userName"));
                export.add(date.getString("itemCount"));
                export.add(date.getString("sendSampleCount"));
                export.add(date.getString("orderNum"));
                export.add(date.getString("validOrderNUm"));
                export.add(date.getString("validRadio"));
                export.add(date.getString("payAmount"));
                export.add(date.getString("regimentalPromotionAmount"));
                export.add(date.getString("totalRegimentalSettleAmount"));
                exportList.add(export);
            }
        }
        String[] headers = {"招商", "商品数", "发样数", "订单数", "有效订单数", "有效率", "实付金额", "预估服务费收入", "结算服务费收入"};
        OutputStream os = response.getOutputStream();
        ExportExcelUtils eeu = new ExportExcelUtils();
        XSSFWorkbook workbook = new XSSFWorkbook();
        eeu.exportExcel(workbook, 0, "订单信息", headers, exportList);
        this.setResponseHeader(response, "订单信息.xls");
        workbook.write(os);
        os.flush();
        os.close();
    }

    /**
     * 【招商管理】
     * -详情汇总
     */
    @GetMapping("/bytedanceUserItemTotal")
    @ApiOperation(value = "招商管理-详情汇总")
    public JSONObject bytedanceUserItemTotal(
            @ApiParam("userId") @RequestParam(value = "userId", required = false) Long userId,
            @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
            @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate
    ) {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(userId)) {
            requestMap.put("userId", userId);
        }
        if (!Check.isNull(orderStartDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(orderEndDate);
            requestMap.put("end", end);
        }
        JSONObject returnJson = bytedancePromoterOrderListService.bytedanceUserItemTotal(requestMap);
        return returnJson;
    }

    /**
     * 【招商管理】
     * -详情集合
     */
    @GetMapping("/bytedanceUserItemDetail")
    @ApiOperation(value = "招商管理-详情集合")
    public TableDataInfo bytedanceUserItemDetail(
            @ApiParam("userId") @RequestParam(value = "userId", required = false) Long userId,
            @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
            @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
            @ApiParam("商品Id") @RequestParam(value = "itemId", required = false) Long itemId,
            @ApiParam("商品名称") @RequestParam(value = "itemTitle", required = false) String itemTitle,
            @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(userId)) {
            requestMap.put("userId", userId);
        }
        if (!Check.isNull(orderStartDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(itemId)) {
            requestMap.put("itemId", itemId);
        }
        if (!Check.isNull(itemTitle)) {
            requestMap.put("itemTitle", itemTitle);
        }
        if (Check.isNull(fieId)) {
            fieId = "orderNum";
            sort = "ASC";
        }
        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 = bytedancePromoterOrderListService.bytedanceUserItemDetail(requestMap);
        return getDataTable(list);
    }

    /**
     * 【招商管理】
     * -详情导出
     */
    @GetMapping("/exportBytedanceUserItemDetail")
    @ApiOperation(value = "招商管理-详情导出")
    public void exportBytedanceUserItemDetail(HttpServletRequest request, HttpServletResponse response,
                                              @ApiParam("userId") @RequestParam(value = "userId", required = false) Long userId,
                                              @ApiParam("开始时间") @RequestParam(value = "orderStartDate", required = false) String orderStartDate,
                                              @ApiParam("结束时间") @RequestParam(value = "orderEndDate", required = false) String orderEndDate,
                                              @ApiParam("商品Id") @RequestParam(value = "itemId", required = false) Long itemId,
                                              @ApiParam("商品名称") @RequestParam(value = "itemTitle", required = false) String itemTitle
    ) throws IOException {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(userId)) {
            requestMap.put("userId", userId);
        }
        if (!Check.isNull(orderStartDate)) {
            Integer start = DateUtils.getIntegerTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Integer end = DateUtils.getIntegerTime(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(itemId)) {
            requestMap.put("itemId", itemId);
        }
        if (!Check.isNull(itemTitle)) {
            requestMap.put("itemTitle", itemTitle);
        }
        List<JSONObject> list = bytedancePromoterOrderListService.bytedanceUserItemDetail(requestMap);
        List<List<Object>> exportList = new ArrayList<>();
        if (!Check.isNull(list)) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject date = list.get(i);
                List<Object> export = new ArrayList();
                export.add(date.getString("userName"));
                export.add(date.getLong("itemId"));
                export.add(date.getString("itemTitle"));
                export.add(date.getString("reservePrice"));
                export.add(date.getString("itemCommissionRate"));
                export.add(date.getString("orderNum"));
                export.add(date.getString("validOrderNUm"));
                export.add(date.getString("validRadio"));
                export.add(date.getString("invalidOrderNUm"));
                export.add(date.getString("payAmount"));
                export.add(date.getString("regimentalPromotionRate"));
                export.add(date.getString("regimentalPromotionAmount"));
                export.add(date.getString("totalRegimentalSettleAmount"));
                export.add(date.getString("imageUrl"));
                exportList.add(export);
            }
        }
        String[] headers = {"所属人", "商品ID", "商品名称", "商品单价", "佣金率", "订单数", "有效订单数", "有效订单率", "失效订单数", "付款金额", "服务费率", "预估服务费收入", "结算服务费收入", "商品封面"};
        OutputStream os = response.getOutputStream();
        ExportExcelUtils eeu = new ExportExcelUtils();
        XSSFWorkbook workbook = new XSSFWorkbook();
        eeu.exportExcel(workbook, 0, "认领列表", headers, exportList);
        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();
        }
    }
}

