package com.ruixuan.isc.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.isc.service.IKuaishouPromoterService;
import com.ruixuan.isc.service.ISupplyChainService;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
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.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/isv/supply_chain")
public class SupplyChainController extends BaseController {

    @Autowired
    private ISupplyChainService supplyChainService;

    @Autowired
    private IKuaishouPromoterService promoterService;

    /**
     * 达人订单
     *
     * @param orderStartDate
     * @param orderEndDate
     * @param promoterNickName
     * @param promoterId
     * @return
     */
    @GetMapping("/orderList")
    @ApiOperation(value = "达人订单")
    public TableDataInfo orderList(@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) {

        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate)) {
            Long start = DateUtils.getStartLongTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Long end = DateUtils.getEndLongTime(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 = supplyChainService.getOrderList(requestMap);
        return getDataTable(list);
    }

    /**
     * 达人带货详情
     *
     * @param promoterId
     * @param orderStartDate
     * @param orderEndDate
     * @param itemTitle
     * @param itemId
     * @return
     */
    @GetMapping("/anchorOrderDetail")
    @ApiOperation(value = "达人带货详情")
    public TableDataInfo anchorOrderDetail(
            @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)) {
            Long start = DateUtils.getStartLongTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Long end = DateUtils.getEndLongTime(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 = supplyChainService.anchorOrderDetail(requestMap);
        return getDataTable(list);
    }


    /**
     * 订单统计
     *
     * @param promoterId
     * @param orderStartDate
     * @param orderEndDate
     * @return
     */
    @GetMapping("/orderStatistics")
    @ApiOperation(value = "订单统计")
    public JSONObject orderStatistics(
            @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)) {
            Long start = DateUtils.getStartLongTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Long end = DateUtils.getEndLongTime(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 = supplyChainService.orderStatistics(requestMap);
        returnJson.put("code", 0);
        returnJson.put("data", list);
        return returnJson;
    }


    /**
     * 商品列表
     *
     * @param itemId
     * @param itemTitle
     * @param orderStartDate
     * @param orderEndDate
     * @return
     */
    @GetMapping("/itemList")
    @ApiOperation(value = "商品列表")
    public TableDataInfo itemList(
            @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 = "bindStartDate", required = false) String bindStartDate,
            @ApiParam("结束时间") @RequestParam(value = "bindEndDate", required = false) String bindEndDate,
            @ApiParam("排序字段") @RequestParam(value = "fieId", required = false) 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) && !Check.isNull(orderEndDate)) {
            if (!Check.isNull(orderStartDate)) {
                Long start = DateUtils.getStartLongTime(orderStartDate);
                requestMap.put("start", start);
            }
            if (!Check.isNull(orderEndDate)) {
                Long end = DateUtils.getEndLongTime(orderEndDate);
                requestMap.put("end", end);
            }
        }

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

        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 = new ArrayList<>();
        if (!Check.isNull(orderStartDate) && !Check.isNull(orderEndDate)) {
            list = supplyChainService.itemList(requestMap);
        } else if (!Check.isNull(bindStartDate) && !Check.isNull(bindEndDate)) {
            list = supplyChainService.itemBindList(requestMap);
        }

        return getDataTable(list);
    }


    @RequestMapping(value = "/exportOrder")
    @ResponseBody
    public void exportOrder(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject requestJson) throws Exception {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(requestJson.getString("orderStartDate"))) {
            Long start = DateUtils.getStartLongTime(requestJson.getString("orderStartDate"));
            requestMap.put("start", start);
        }
        if (!Check.isNull(requestJson.getString("orderEndDate"))) {
            Long end = DateUtils.getEndLongTime(requestJson.getString("orderEndDate"));
            requestMap.put("end", end);
        }
        if (!Check.isNull(requestJson.getString("bindStartDate"))) {
            Long start = DateUtils.getStartLongTime(requestJson.getString("bindStartDate"));
            requestMap.put("start", start);
            requestMap.put("bindStartDate", requestJson.getString("bindStartDate"));
        }
        if (!Check.isNull(requestJson.getString("bindEndDate"))) {
            Long end = DateUtils.getEndLongTime(requestJson.getString("bindEndDate"));
            requestMap.put("end", end);
            requestMap.put("bindEndDate", requestJson.getString("bindEndDate"));
        }


        if (!Check.isNull(requestJson.getString("promoterNickName"))) {
            requestMap.put("promoterNickName", requestJson.getString("promoterNickName"));
        }
        if (!Check.isNull(requestJson.getLong("promoterId"))) {
            requestMap.put("promoterId", requestJson.getLong("promoterId"));
        }
        if (!Check.isNull(requestJson.getLong("itemId"))) {
            requestMap.put("itemId", requestJson.getLong("itemId"));
        }
        if (!Check.isNull(requestJson.getString("itemTitle"))) {
            requestMap.put("itemTitle", requestJson.getString("itemTitle"));
        }
        if (!Check.isNull(requestJson.getLong("userId"))) {
            requestMap.put("userId", requestJson.getLong("userId"));
        }
        List<JSONObject> list = new ArrayList<>();
        if(!Check.isNull(requestJson.getString("orderStartDate")) && !Check.isNull(requestJson.getString("orderEndDate"))){
            list = supplyChainService.exportOrder(requestMap);
        }else if(!Check.isNull(requestJson.getString("bindStartDate")) && !Check.isNull(requestJson.getString("bindEndDate"))){
            list = supplyChainService.exportBindOrder(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"));
                export.add(date.getString("deliveryNum"));
                export.add(date.getString("deliveryRadio"));
                export.add(date.getString("notDeliveryNum"));
                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();
    }


    /**
     * 根据商品id获取top达人
     *
     * @param itemId
     * @param orderStartDate
     * @param orderEndDate
     * @return
     */
    @GetMapping("/getTopPromoterByItemId")
    @ApiOperation(value = "商品列表")
    public TableDataInfo getTopPromoterByItemId(
            @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)) {
            Long start = DateUtils.getStartLongTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Long end = DateUtils.getEndLongTime(orderEndDate);
            requestMap.put("end", end);
        }
        if (!Check.isNull(itemId)) {
            requestMap.put("itemId", itemId);
        }

        List<JSONObject> list = supplyChainService.getTopPromoterByItemId(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.getStartLongTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Long end = DateUtils.getEndLongTime(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 = supplyChainService.itemDetail(requestMap);
        return getDataTable(list);
    }


    /**
     * 产品服务费
     *
     * @param itemId
     * @param itemTitle
     * @param orderStartDate
     * @param orderEndDate
     * @return
     */
    @GetMapping("/itemServiceCharge")
    @ApiOperation(value = "产品服务费")
    public TableDataInfo itemServiceCharge(
            @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) {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(orderStartDate)) {
            Long start = DateUtils.getStartLongTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Long end = DateUtils.getEndLongTime(orderEndDate);
            requestMap.put("end", end);
        }
        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 = supplyChainService.itemServiceCharge(requestMap);
        return getDataTable(list);
    }

    /**
     * 首页-订单数据
     *
     * @param dateType
     * @param orderStartDate
     * @param orderEndDate
     * @return
     */

    @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) {
        Map<String, Object> requestMap = new HashMap<>();
        JSONObject returnJson = new JSONObject();
        if (dateType == 0) {
            if (!Check.isNull(orderStartDate)) {
                Long start = DateUtils.getStartLongTime(orderStartDate); //当天开始时间
                requestMap.put("start", start);
            }
            if (!Check.isNull(orderEndDate)) {
                Long end = DateUtils.getEndLongTime(orderEndDate); // 当天结束时间
                requestMap.put("end", end);
            }
            JSONObject orderTotal = supplyChainService.getOrderTotal(requestMap);
            returnJson.put("orderTotal", orderTotal);
            Integer hour = DateUtils.getNowHour() - 1; // 当前小时
            if (hour >= 0) {
                JSONObject ratioJson = new JSONObject();
                Long nowStartHourTemp = DateUtils.getStartLongTime(orderStartDate); // 当天开始时间戳
                Long nowEndHourTemp = DateUtils.getNowEndHourTemp(orderStartDate, hour); // 当前结束时间戳
                String yesterday = DateUtils.getAnotherDay("yyyy-MM-dd", orderStartDate, -1); // 昨日日期
                Long yesterdayStartHourTemp = DateUtils.getStartLongTime(yesterday); //昨日开始时间戳
                Long yesterdayEndHourTemp = DateUtils.getNowEndHourTemp(yesterday, hour); // 昨日结束时间戳

                Map<String, Object> ratioMap = new HashMap<>();
                ratioMap.put("nowStartHourTemp", nowStartHourTemp);
                ratioMap.put("nowEndHourTemp", nowEndHourTemp);
                ratioMap.put("yesterdayStartHourTemp", yesterdayStartHourTemp);
                ratioMap.put("yesterdayEndHourTemp", yesterdayEndHourTemp);
                Double ringRatio = supplyChainService.getOrderTotalRatio(ratioMap); //环比
                ratioJson.put("ringRatio", ringRatio);
                String yearDay = DateUtils.getAnotherDay("yyyy-MM-dd", orderStartDate, -6); // 昨日日期
                Long yearStartHourTemp = DateUtils.getStartLongTime(yearDay); //前7日开始时间戳
                Long yearEndHourTemp = DateUtils.getNowEndHourTemp(yearDay, hour);//前7日结束时间戳
                ratioMap.put("yesterdayStartHourTemp", yearStartHourTemp);
                ratioMap.put("yesterdayEndHourTemp", yearEndHourTemp);
                Double yearRatio = supplyChainService.getOrderTotalRatio(ratioMap); //同比
                ratioJson.put("yearRatio", yearRatio);
                returnJson.put("ratio", ratioJson);
            }
        } else {
            if (!Check.isNull(orderStartDate)) {
                Long start = DateUtils.getStartLongTime(orderStartDate); //昨日开始时间
                requestMap.put("start", start);
            }
            if (!Check.isNull(orderEndDate)) {
                Long end = DateUtils.getEndLongTime(orderEndDate); // 前日开始时间
                requestMap.put("end", end);
            }
            JSONObject orderTotal = supplyChainService.getOrderTotal(requestMap);
            returnJson.put("orderTotal", orderTotal);
        }
        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) {
        Map<String, Object> requestMap = new HashMap<>();
        JSONObject returnJson = new JSONObject();
        if (dateType == 0) {
            if (!Check.isNull(orderStartDate)) {
                Long start = DateUtils.getStartLongTime(orderStartDate); //当天开始时间
                requestMap.put("start", start);
            }
            if (!Check.isNull(orderEndDate)) {
                Long end = DateUtils.getEndLongTime(orderEndDate); // 当天结束时间
                requestMap.put("end", end);
            }
            JSONObject amountTotal = supplyChainService.getAmountTotal(requestMap);
            returnJson.put("amountTotal", amountTotal);
            Integer hour = DateUtils.getNowHour() - 1; // 当前小时
            if (hour >= 0) {
                JSONObject amountRatioJson = new JSONObject();
                Long nowStartHourTemp = DateUtils.getStartLongTime(orderStartDate); // 当天开始时间戳
                Long nowEndHourTemp = DateUtils.getNowEndHourTemp(orderStartDate, hour); // 当前结束时间戳
                String yesterday = DateUtils.getAnotherDay("yyyy-MM-dd", orderStartDate, -1); // 昨日日期
                Long yesterdayStartHourTemp = DateUtils.getStartLongTime(yesterday); //昨日开始时间戳
                Long yesterdayEndHourTemp = DateUtils.getNowEndHourTemp(yesterday, hour); // 昨日结束时间戳

                Map<String, Object> ratioMap = new HashMap<>();
                ratioMap.put("nowStartHourTemp", nowStartHourTemp);
                ratioMap.put("nowEndHourTemp", nowEndHourTemp);
                ratioMap.put("yesterdayStartHourTemp", yesterdayStartHourTemp);
                ratioMap.put("yesterdayEndHourTemp", yesterdayEndHourTemp);
                Double amountRingRatio = supplyChainService.getAmountTotalRatio(ratioMap); //环比
                amountRatioJson.put("amountRingRatio", amountRingRatio);
                String yearDay = DateUtils.getAnotherDay("yyyy-MM-dd", orderStartDate, -6); // 昨日日期
                Long yearStartHourTemp = DateUtils.getStartLongTime(yearDay); //前7日开始时间戳
                Long yearEndHourTemp = DateUtils.getNowEndHourTemp(yearDay, hour);//前7日结束时间戳
                ratioMap.put("yesterdayStartHourTemp", yearStartHourTemp);
                ratioMap.put("yesterdayEndHourTemp", yearEndHourTemp);
                Double amountYearRatio = supplyChainService.getAmountTotalRatio(ratioMap); //同比
                amountRatioJson.put("amountYearRatio", amountYearRatio);
                returnJson.put("amountRatio", amountRatioJson);
            }
        } else {
            if (!Check.isNull(orderStartDate)) {
                Long start = DateUtils.getStartLongTime(orderStartDate); //昨日开始时间
                requestMap.put("start", start);
            }
            if (!Check.isNull(orderEndDate)) {
                Long end = DateUtils.getEndLongTime(orderEndDate); // 前日开始时间
                requestMap.put("end", end);
            }
            JSONObject amountTotal = supplyChainService.getAmountTotal(requestMap);
            returnJson.put("amountTotal", amountTotal);
        }
        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) {
        Map<String, Object> requestMap = new HashMap<>();
        JSONObject returnJson = new JSONObject();

        if (!Check.isNull(orderStartDate)) {
            Long start = DateUtils.getStartLongTime(orderStartDate); //昨日开始时间
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Long end = DateUtils.getEndLongTime(orderEndDate); // 前日开始时间
            requestMap.put("end", end);
        }
        JSONObject serviceAmountTotal = supplyChainService.getServiceAmountTotal(requestMap);
        returnJson.put("serviceAmountTotal", serviceAmountTotal);
        return returnJson;
    }

    /**
     * 首页-商品排行
     *
     * @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,
            @ApiParam("媒体，1-抖音；2-快手") @RequestParam(value = "mediaId", required = false) String mediaId) {

        Map<String, Object> requestMap = new HashMap<>();
        if (dateType == 0) {
            if ("1".equals(mediaId)) {
                return indexOrderRankBytedance(dateType, orderStartDate, orderEndDate);
            } else {
                Integer hour = DateUtils.getNowHour(); // 当前小时
                Long nowStartHourTemp = DateUtils.getStartLongTime(orderStartDate); // 当天开始时间戳
                Long nowEndHourTemp = DateUtils.getNowEndHourTemp(orderStartDate, hour); // 当前结束时间戳
                String yesterday = DateUtils.getAnotherDay("yyyy-MM-dd", orderStartDate, -1); // 昨日日期
                Long yesterdayStartHourTemp = DateUtils.getStartLongTime(yesterday); //昨日开始时间戳
                Long yesterdayEndHourTemp = DateUtils.getNowEndHourTemp(yesterday, hour); // 昨日结束时间戳
                requestMap.put("nowStartHourTemp", nowStartHourTemp);
                requestMap.put("nowEndHourTemp", nowEndHourTemp);
                requestMap.put("yesterdayStartHourTemp", yesterdayStartHourTemp);
                requestMap.put("yesterdayEndHourTemp", yesterdayEndHourTemp);
                startPage();
                List<JSONObject> orderRankRatioList = supplyChainService.getOrderRankRatioList(requestMap);
                return getDataTable(orderRankRatioList);
            }
        } else {
            if ("1".equals(mediaId)) {
                return indexOrderRankBytedance(dateType, orderStartDate, orderEndDate);
            } else {
                if (!Check.isNull(orderStartDate)) {
                    Long start = DateUtils.getStartLongTime(orderStartDate);
                    requestMap.put("start", start);
                }
                if (!Check.isNull(orderEndDate)) {
                    Long end = DateUtils.getEndLongTime(orderEndDate);
                    requestMap.put("end", end);
                }
                startPage();
                List<JSONObject> orderRankRatioList = supplyChainService.getOrderRankList(requestMap);
                return getDataTable(orderRankRatioList);
            }
        }
    }

    private TableDataInfo indexOrderRankBytedance(Integer dateType, String orderStartDate, String orderEndDate) {
        Map<String, Object> requestMap = new HashMap<>();
        if (dateType == 0) {
            Long nowStartHourTemp = DateUtils.strDateToInt(orderStartDate);
            String yesterday = DateUtils.getAnotherDay("yyyy-MM-dd", orderStartDate, -1); // 昨日日期
            Long yesterdayStartHourTemp = DateUtils.strDateToInt(yesterday); //昨日时间
            requestMap.put("nowStartHourTemp", nowStartHourTemp);
            requestMap.put("nowEndHourTemp", nowStartHourTemp);
            requestMap.put("yesterdayStartHourTemp", yesterdayStartHourTemp);
            requestMap.put("yesterdayEndHourTemp", yesterdayStartHourTemp);
            startPage();
            List<JSONObject> orderRankRatioList = supplyChainService.getOrderRankRatioListBytedance(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 = supplyChainService.getOrderRankListBytedance(requestMap);
            return getDataTable(orderRankRatioList);
        }
    }

    @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,
            @ApiParam("媒体，1-抖音；2-快手") @RequestParam(value = "mediaId", required = false) String mediaId
    ) {

        Map<String, Object> requestMap = new HashMap<>();
        if (dateType == 0) {
            if ("1".equals(mediaId)) {
                return indexPromoterRankBytedance(dateType, orderStartDate, orderEndDate, userId);
            } else {
                if (Check.isNotNull(userId)) {
                    List<String> promoterIds = promoterService.selectPromoterIdList(Arrays.asList(userId), mediaId);
                    if (Check.isNull(promoterIds)) {
                        return getDataTable(null);
                    }
                    requestMap.put("promoterIds", promoterIds);
                }
                if (!Check.isNull(orderStartDate)) {
                    Long start = DateUtils.getStartLongTime(orderStartDate); //当天开始时间
                    requestMap.put("start", start);
                }
                if (!Check.isNull(orderEndDate)) {
                    Long end = DateUtils.getEndLongTime(orderEndDate); // 当天结束时间
                    requestMap.put("end", end);
                }
                Integer hour = DateUtils.getNowHour(); // 当前小时
                Long nowStartHourTemp = DateUtils.getStartLongTime(orderStartDate); // 当天开始时间戳
                Long nowEndHourTemp = DateUtils.getNowEndHourTemp(orderStartDate, hour); // 当前结束时间戳
                String yesterday = DateUtils.getAnotherDay("yyyy-MM-dd", orderStartDate, -1); // 昨日日期
                Long yesterdayStartHourTemp = DateUtils.getStartLongTime(yesterday); //昨日开始时间戳
                Long yesterdayEndHourTemp = DateUtils.getNowEndHourTemp(yesterday, hour); // 昨日结束时间戳
                requestMap.put("nowStartHourTemp", nowStartHourTemp);
                requestMap.put("nowEndHourTemp", nowEndHourTemp);
                requestMap.put("yesterdayStartHourTemp", yesterdayStartHourTemp);
                requestMap.put("yesterdayEndHourTemp", yesterdayEndHourTemp);
                startPage();
                List<JSONObject> promoterRankRatioList = supplyChainService.getPromoterRankRatioList(requestMap);
                return getDataTable(promoterRankRatioList);
            }
        } else {
            if ("1".equals(mediaId)) {
                return indexPromoterRankBytedance(dateType, orderStartDate, orderEndDate, userId);
            } else {
                if (!Check.isNull(orderStartDate)) {
                    Long start = DateUtils.getStartLongTime(orderStartDate);
                    requestMap.put("start", start);
                }
                if (!Check.isNull(orderEndDate)) {
                    Long end = DateUtils.getEndLongTime(orderEndDate);
                    requestMap.put("end", end);
                }
                startPage();
                List<JSONObject> orderRatioList = supplyChainService.getPromoterRatioList(requestMap);
                return getDataTable(orderRatioList);
            }
        }
    }

    public TableDataInfo indexPromoterRankBytedance(Integer dateType, String orderStartDate, String orderEndDate, Long userId) {
        Map<String, Object> requestMap = new HashMap<>();
        if (dateType == 0) {
            if (Check.isNotNull(userId)) {
                List<String> promoterIds = promoterService.selectPromoterIdList(Arrays.asList(userId), "1");
                if (Check.isNull(promoterIds)) {
                    return getDataTable(null);
                }
                requestMap.put("promoterIds", promoterIds);
            }
            Long nowStartHourTemp = DateUtils.strDateToInt(orderStartDate); // 当天开始时间戳
            String yesterday = DateUtils.getAnotherDay("yyyy-MM-dd", orderStartDate, -1); // 昨日日期
            Long yesterdayStartHourTemp = DateUtils.strDateToInt(yesterday); //昨日开始时间戳
            requestMap.put("nowStartHourTemp", nowStartHourTemp);
            requestMap.put("nowEndHourTemp", nowStartHourTemp);
            requestMap.put("yesterdayStartHourTemp", yesterdayStartHourTemp);
            requestMap.put("yesterdayEndHourTemp", yesterdayStartHourTemp);
            startPage();
            List<JSONObject> promoterRankRatioList = supplyChainService.getPromoterRankRatioListBytedance(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 = supplyChainService.getPromoterRatioListBytedance(requestMap);
            return getDataTable(orderRatioList);
        }
    }

    @RequestMapping(value = "/exportPromoterTotal")
    @ResponseBody
    public void exportPromoterTotal(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject requestJson) throws Exception {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(requestJson.getString("orderStartDate"))) {
            Long start = DateUtils.getStartLongTime(requestJson.getString("orderStartDate"));
            requestMap.put("start", start);
        }
        if (!Check.isNull(requestJson.getString("orderEndDate"))) {
            Long end = DateUtils.getEndLongTime(requestJson.getString("orderEndDate"));
            requestMap.put("end", end);
        }
        if (!Check.isNull(requestJson.getString("promoterNickName"))) {
            requestMap.put("promoterNickName", requestJson.getString("promoterNickName"));
        }
        if (!Check.isNull(requestJson.getLong("promoterId"))) {
            requestMap.put("promoterId", requestJson.getLong("promoterId"));
        }
        if (!Check.isNull(requestJson.getLong("itemId"))) {
            requestMap.put("itemId", requestJson.getLong("itemId"));
        }
        if (!Check.isNull(requestJson.getString("itemTitle"))) {
            requestMap.put("itemTitle", requestJson.getString("itemTitle"));
        }
        List<JSONObject> list = supplyChainService.exportPromoterTotal(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();
                Long promoterId = date.getLong("promoterId");
                String promoterNickName = date.getString("promoterNickName");
                String name = promoterNickName + "（" + promoterId + "）";
                export.add(name);
                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("regimentalPromotionAmount"));
                export.add(date.getString("deliveryNum"));
                export.add(date.getString("deliveryRadio"));
                export.add(date.getString("notDeliveryNum"));
                String userName = supplyChainService.getUserName(date.getLong("promoterId"));
                if (!Check.isNull(userName)) {
                    export.add(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();
    }


    @RequestMapping(value = "/exportPromoter")
    @ResponseBody
    public void exportPromoter(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject requestJson) throws Exception {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(requestJson.getString("orderStartDate"))) {
            Long start = DateUtils.getStartLongTime(requestJson.getString("orderStartDate"));
            requestMap.put("start", start);
        }
        if (!Check.isNull(requestJson.getString("orderEndDate"))) {
            Long end = DateUtils.getEndLongTime(requestJson.getString("orderEndDate"));
            requestMap.put("end", end);
        }
        if (!Check.isNull(requestJson.getString("promoterNickName"))) {
            requestMap.put("promoterNickName", requestJson.getString("promoterNickName"));
        }
        if (!Check.isNull(requestJson.getLong("promoterId"))) {
            requestMap.put("promoterId", requestJson.getLong("promoterId"));
        }
        if (!Check.isNull(requestJson.getLong("itemId"))) {
            requestMap.put("itemId", requestJson.getLong("itemId"));
        }
        if (!Check.isNull(requestJson.getString("itemTitle"))) {
            requestMap.put("itemTitle", requestJson.getString("itemTitle"));
        }
        List<JSONObject> list = supplyChainService.exportPromoter(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 promoterNickName = date.getString("promoterNickName");
                Long promoterId = date.getLong("promoterId");
                String promoter = promoterNickName + "（" + promoterId + "）";
                export.add(promoter);
                Long itemId = date.getLong("itemId");
                String itemTitle = date.getString("itemTitle");
                String item = itemTitle + "（" + itemId + "）";
                export.add(item);
                export.add(date.getString("imageUrl"));
                export.add(date.getInteger("orderNum"));
                export.add(date.getInteger("validOrderNUm"));
                export.add(date.getString("validRadio"));
                export.add(date.getString("deliveryNum"));
                export.add(date.getString("deliveryRadio"));
                export.add(date.getString("activityItemStatus"));
                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();
    }


    /**
     * 获取供应链人员列表
     *
     * @return
     */
    @GetMapping("/getSupplyChainUserList")
    @ApiOperation(value = "获取供应链成员列表")
    public TableDataInfo getSupplyChainUserList() {
        List<JSONObject> list = supplyChainService.getSupplyChainUserList();
        return getDataTable(list);
    }

    @GetMapping("/getSupplyChainCourtshipUserList")
    @ApiOperation(value = "获取供应链成员列表")
    public TableDataInfo getSupplyChainCourtshipUserList() {
        List<JSONObject> list = supplyChainService.getSupplyChainCourtshipUserList();
        return getDataTable(list);
    }


    /**
     * 获取商品绑定人员列表
     *
     * @param status
     * @param userId
     * @param itemId
     * @param itemTitle
     * @param activityId
     * @return
     */
    @GetMapping("/bindUserItemList")
    @ApiOperation(value = "商品认领列表")
    public TableDataInfo bindUserItemList(
            @ApiParam("状态") @RequestParam(value = "status", required = false) Integer status,
            @ApiParam("userId") @RequestParam(value = "userId", required = false) Long userId,
            @ApiParam("商品id") @RequestParam(value = "itemId", required = false) Long itemId,
            @ApiParam("商品名称") @RequestParam(value = "itemTitle", required = false) String itemTitle,
            @ApiParam("活动ID") @RequestParam(value = "activityId", required = false) Long activityId
    ) {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(status)) {
            requestMap.put("status", status);
        }
        if (!Check.isNull(userId)) {
            requestMap.put("userId", userId);
        }
        if (!Check.isNull(itemId)) {
            requestMap.put("itemId", itemId);
        }
        if (!Check.isNull(itemTitle)) {
            requestMap.put("itemTitle", itemTitle);
        }
        if (!Check.isNull(activityId)) {
            requestMap.put("activityId", activityId);
        }
        startPage();
        List<JSONObject> list = supplyChainService.bindUserItemList(requestMap);
        return getDataTable(list);
    }


    /**
     * 商品解绑
     *
     * @param itemId
     * @return
     */
    @GetMapping("/unbindUser")
    @ApiOperation(value = "商品解绑")
    public JSONObject unbindUser(
            @ApiParam("商品id") @RequestParam(value = "itemId", required = false) Long itemId) {
        JSONObject returnJson = new JSONObject();
        if (Check.isNull(itemId)) {
            returnJson.put("code", -1);
            returnJson.put("message", "请传入商品ID");
            return returnJson;
        }
        boolean isTrueOrFalse = supplyChainService.unbindUser(itemId);
        if (isTrueOrFalse) {
            returnJson.put("code", 0);
            returnJson.put("message", "解绑成功");
            return returnJson;
        } else {
            returnJson.put("code", -1);
            returnJson.put("message", "解绑失败");
            return returnJson;
        }
    }


    /**
     * 绑定商品
     *
     * @param itemId
     * @return
     */
    @GetMapping("/bindUser")
    @ApiOperation(value = "绑定商品")
    public JSONObject bindUser(
            @ApiParam("商品ID") @RequestParam(value = "itemId", required = false) Long itemId,
            @ApiParam("用户ID") @RequestParam(value = "userId", required = false) Long userId,
            @ApiParam("用户名称") @RequestParam(value = "nikeName", required = false) String nikeName) {
        JSONObject returnJson = new JSONObject();
        if (Check.isNull(itemId)) {
            returnJson.put("code", -1);
            returnJson.put("message", "请传入商品ID");
            return returnJson;
        }
        if (Check.isNull("userId")) {
            returnJson.put("code", -1);
            returnJson.put("message", "请传入用户ID");
            return returnJson;
        }
        if (Check.isNull("nikeName")) {
            returnJson.put("code", -1);
            returnJson.put("message", "请传入用户名称");
            return returnJson;
        }
        boolean isTrueOrFalse = supplyChainService.bindUser(itemId, userId, nikeName);
        if (isTrueOrFalse) {
            returnJson.put("code", 0);
            returnJson.put("message", "认领成功");
            return returnJson;
        } else {
            returnJson.put("code", -1);
            returnJson.put("message", "认领失败");
            return returnJson;
        }
    }


    /**
     * 供应链管理员统计列表
     *
     * @param orderStartDate
     * @param orderEndDate
     * @return
     */
    @GetMapping("/adminReportList")
    @ApiOperation(value = "供应链管理员统计列表")
    public TableDataInfo adminReportList(
            @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)) {
            Long start = DateUtils.getStartLongTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Long end = DateUtils.getEndLongTime(orderEndDate);
            requestMap.put("end", end);
        }

        if (!Check.isNull(bindStartDate)) {
            Long start = DateUtils.getStartLongTime(bindStartDate);
            requestMap.put("start", start);
        }

        if (!Check.isNull(bindEndDate)) {
            Long end = DateUtils.getEndLongTime(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 = supplyChainService.adminReportList(requestMap);
        } else if (!Check.isNull(bindStartDate) && !Check.isNull(bindEndDate)) {
            list = supplyChainService.adminReportListByBind(requestMap);
        }

        return getDataTable(list);
    }


    @PostMapping("/exportAdminReportList")
    @ApiOperation(value = "供应链管理员统计列表")
    public void exportAdminReportList(
            HttpServletRequest request, HttpServletResponse response,
            @RequestBody JSONObject requestJson
    ) throws IOException {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(requestJson.getString("orderStartDate"))) {
            Long start = DateUtils.getStartLongTime(requestJson.getString("orderStartDate"));
            requestMap.put("start", start);
        }
        if (!Check.isNull(requestJson.getString("orderEndDate"))) {
            Long end = DateUtils.getEndLongTime(requestJson.getString("orderEndDate"));
            requestMap.put("end", end);
        }

        if (!Check.isNull(requestJson.getString("bindStartDate"))) {
            Long start = DateUtils.getStartLongTime(requestJson.getString("bindStartDate"));
            requestMap.put("start", start);
        }

        if (!Check.isNull(requestJson.getString("bindEndDate"))) {
            Long end = DateUtils.getEndLongTime(requestJson.getString("bindEndDate"));
            requestMap.put("end", end);
        }

        if (!Check.isNull(requestJson.getString("userId"))) {
            requestMap.put("userId", requestJson.getString("userId"));
        }
        List<JSONObject> list = new ArrayList<>();
        if (!Check.isNull(requestJson.getString("orderStartDate")) && !Check.isNull(requestJson.getString("orderEndDate"))) {
            list = supplyChainService.exportAdminReportList(requestMap);
        } else if (!Check.isNull(requestJson.getString("bindStartDate")) && !Check.isNull(requestJson.getString("bindEndDate"))) {
            list = supplyChainService.exportAdminReportListByBind(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("deliveryNum"));
                export.add(date.getString("notDeliveryNum"));
                export.add(date.getString("deliveryRadio"));
                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();
    }


    /**
     * 认领商品列表
     *
     * @param userId
     * @param orderStartDate
     * @param orderEndDate
     * @param itemId
     * @param itemTitle
     * @return
     */
    @GetMapping("/userItemDetail")
    @ApiOperation(value = "认领商品列表")
    public TableDataInfo userItemDetail(
            @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)) {
            Long start = DateUtils.getStartLongTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Long end = DateUtils.getEndLongTime(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 = supplyChainService.userItemDetail(requestMap);
        return getDataTable(list);
    }

    /**
     * 认领商品列表导出
     *
     * @return
     */
    @PostMapping("/exportUserItemDetail")
    @ApiOperation(value = "认领商品列表")
    @ResponseBody
    public void exportUserItemDetail(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject requestJson) throws IOException {


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

        if (!Check.isNull(requestJson.getLong("userId"))) {
            requestMap.put("userId", requestJson.getLong("userId"));
        }
        if (!Check.isNull(requestJson.getString("orderStartDate"))) {
            Long start = DateUtils.getStartLongTime(requestJson.getString("orderStartDate"));
            requestMap.put("start", start);
        }
        if (!Check.isNull(requestJson.getString("orderEndDate"))) {
            Long end = DateUtils.getEndLongTime(requestJson.getString("orderEndDate"));
            requestMap.put("end", end);
        }
        if (!Check.isNull(requestJson.getLong("itemId"))) {
            requestMap.put("itemId", requestJson.getLong("itemId"));
        }
        if (!Check.isNull(requestJson.getString("itemTitle"))) {
            requestMap.put("itemTitle", requestJson.getString("itemTitle"));
        }

        List<JSONObject> list = supplyChainService.exportUserItemDetail(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("deliveryNum"));
                export.add(date.getString("deliveryRadio"));
                export.add(date.getString("notDeliveryNum"));
                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();
    }


    @GetMapping("/userItemTotal")
    @ApiOperation(value = "认领商品列表汇总")
    public JSONObject userItemTotal(
            @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)) {
            Long start = DateUtils.getStartLongTime(orderStartDate);
            requestMap.put("start", start);
        }
        if (!Check.isNull(orderEndDate)) {
            Long end = DateUtils.getEndLongTime(orderEndDate);
            requestMap.put("end", end);
        }
        JSONObject returnJson = supplyChainService.userItemTotal(requestMap);
        return returnJson;
    }


    /**
     * 商品认领汇总
     *
     * @return
     */
    @GetMapping("/itemBindSummary")
    @ApiOperation(value = "商品认领汇总")
    public JSONObject itemBindSummary() {
        JSONObject json = supplyChainService.itemBindSummary();
        return json;
    }


    /**
     * 首页-时段对比
     *
     * @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) {
        Map<String, Object> requestMap = new HashMap<>();
        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.getStartLongTime(thisCycleStart);
            requestMap.put("thisCycleStartTemp", thisCycleStartTemp);
        }
        if (!Check.isNull(thisCycleEnd)) {
            Long thisCycleEndTemp = DateUtils.getEndLongTime(thisCycleEnd);
            requestMap.put("thisCycleEndTemp", thisCycleEndTemp);
        }
        if (!Check.isNull(lastCycleStart)) {
            Long lastCycleStartTemp = DateUtils.getStartLongTime(lastCycleStart);
            requestMap.put("lastCycleStartTemp", lastCycleStartTemp);
        }
        if (!Check.isNull(lastCycleEnd)) {
            Long lastCycleEndTemp = DateUtils.getEndLongTime(lastCycleEnd);
            requestMap.put("lastCycleEndTemp", lastCycleEndTemp);
        }
        JSONObject timeIntervalRatioJson = supplyChainService.getTimeIntervalRatio(requestMap);
        return timeIntervalRatioJson;
    }


    /**
     * 首页-统计图
     *
     * @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) {
        Map<String, Object> requestMap = new HashMap<>();
        String nowDate = DateUtils.getNowDateStr();
        String start = null;
        String end = null;
        if (nowDate.equals(orderStartDate) && nowDate.equals(orderEndDate)) {
            end = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
            start = DateUtils.getAnotherDay("yyyy-MM-dd", end, -6);
        } else {
            start = orderStartDate;
            end = orderEndDate;
        }
        if (!Check.isNull(start)) {
            Long startTemp = DateUtils.getStartLongTime(start);
            requestMap.put("startTemp", startTemp);
        }
        if (!Check.isNull(end)) {
            Long endTemp = DateUtils.getEndLongTime(end);
            requestMap.put("endTemp", endTemp);
        }
        if (!Check.isNull(type)) {
            requestMap.put("type", type);
        }
        List<JSONObject> list = supplyChainService.indexStatistics(requestMap);
        return list;
    }


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


    @GetMapping("/bdReportList")
    @ApiOperation(value = "供应链管理员统计列表")
    public TableDataInfo bdReportList(
            @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 = supplyChainService.bdReportList(requestMap);
        return getDataTable(list);
    }


    @PostMapping("/exportBdReportList")
    @ApiOperation(value = "供应链管理员统计列表")
    @ResponseBody
    public void exportBdReportList(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject requestJson) throws IOException {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(requestJson.getString("orderStartDate"))) {
            Integer start = DateUtils.getIntegerTime(requestJson.getString("orderStartDate"));
            requestMap.put("start", start);
        }
        if (!Check.isNull(requestJson.getString("orderEndDate"))) {
            Integer end = DateUtils.getIntegerTime(requestJson.getString("orderEndDate"));
            requestMap.put("end", end);
        }
        if (!Check.isNull(requestJson.getLong("collectSampleId"))) {
            requestMap.put("collectSampleId", requestJson.getLong("collectSampleId"));
        }

        List<JSONObject> list = supplyChainService.exportBdReportList(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("/bdDetailList")
    @ApiOperation(value = "供应链管理员统计列表")
    public TableDataInfo bdDetailList(
            @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);
        }
        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 = supplyChainService.bdDetailList(requestMap);
        return getDataTable(list);
    }


    @GetMapping("/bdTotal")
    @ApiOperation(value = "供应链管理员统计列表")
    public TableDataInfo bdTotal(
            @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 = supplyChainService.bdTotal(requestMap);
        return getDataTable(list);
    }


    @PostMapping("/exportBdDetail")
    @ApiOperation(value = "认领商品列表")
    @ResponseBody
    public void exportBdDetail(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject requestJson) throws IOException {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(requestJson.getString("orderStartDate"))) {
            Integer start = DateUtils.getIntegerTime(requestJson.getString("orderStartDate"));
            requestMap.put("start", start);
        }
        if (!Check.isNull(requestJson.getString("orderEndDate"))) {
            Integer end = DateUtils.getIntegerTime(requestJson.getString("orderEndDate"));
            requestMap.put("end", end);
        }
        if (!Check.isNull(requestJson.getLong("collectSampleId"))) {
            requestMap.put("collectSampleId", (requestJson.getLong("collectSampleId")));
        }
        List<JSONObject> list = supplyChainService.exportBdDetail(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("/bdReportListV2")
    @ApiOperation(value = "供应链管理员统计列表")
    public TableDataInfo bdReportListV2(
            @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 = "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);
        }

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

        TableDataInfo dataInfo = new TableDataInfo();
        if (Check.isNullMap(requestMap)) {
            dataInfo.setCode(500);
            dataInfo.setMsg("入参不能为空");
            return dataInfo;
        }
        startPage();
        List<JSONObject> list = new ArrayList<>();
        if (!Check.isNull(orderStartDate) && !Check.isNull(orderEndDate)) {
            list = supplyChainService.bdReportListV2(requestMap);
        } else if (!Check.isNull(bindStartDate) && !Check.isNull(bindEndDate)) {
            list = supplyChainService.bdReportListV2ByBind(requestMap);
        }
        return getDataTable(list);
    }


    @PostMapping("/exportBdReportListV2")
    @ApiOperation(value = "供应链管理员统计列表")
    @ResponseBody
    public void exportBdReportListV2(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject requestJson) throws IOException {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(requestJson.getString("orderStartDate"))) {
            Integer start = DateUtils.getIntegerTime(requestJson.getString("orderStartDate"));
            requestMap.put("start", start);
        }
        if (!Check.isNull(requestJson.getString("orderEndDate"))) {
            Integer end = DateUtils.getIntegerTime(requestJson.getString("orderEndDate"));
            requestMap.put("end", end);
        }
        if (!Check.isNull(requestJson.getLong("collectSampleId"))) {
            requestMap.put("collectSampleId", requestJson.getLong("collectSampleId"));
        }
        if (!Check.isNull(requestJson.getString("bindStartDate"))) {
            Integer start = DateUtils.getIntegerTime(requestJson.getString("bindStartDate"));
            requestMap.put("bindStartDate", start);
        }
        if (!Check.isNull(requestJson.getString("bindEndDate"))) {
            Integer end = DateUtils.getIntegerTime(requestJson.getString("bindEndDate"));
            requestMap.put("bindEndDate", end);
        }
        List<JSONObject> list = new ArrayList<>();
        if (!Check.isNull(requestJson.getString("orderStartDate")) && !Check.isNull(requestJson.getString("orderEndDate"))) {
            list = supplyChainService.bdReportListV2(requestMap);
        } else if (!Check.isNull(requestJson.getString("bindStartDate")) && !Check.isNull(requestJson.getString("bindEndDate"))) {
            list = supplyChainService.bdReportListV2ByBind(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("validOrderNum"));
                export.add(date.getString("validOrderRate"));
                export.add(date.getString("orderAmount"));
                export.add(date.getString("regimentalPromotionAmount"));
                export.add(date.getString("totalRegimentalSettleAmount"));
                exportList.add(export);
            }
        }
        String[] headers = {"渠道ID", "渠道姓名", "（总）达人数", "（总）领样达人数", "（总）领样率", "（总）未领样数", "发样数", "发样达人数", "发样商品数", "订单数", "有效订单数", "有效订单率", "订单金额", "预估服务费", "结算服务费"};
        OutputStream os = response.getOutputStream();
        ExportExcelUtils eeu = new ExportExcelUtils();
        XSSFWorkbook workbook = new XSSFWorkbook();
        eeu.exportExcel(workbook, 0, "bd业绩汇总V2", headers, exportList);
        this.setResponseHeader(response, "bd业绩汇总V2.xls");
        workbook.write(os);
        os.flush();
        os.close();


    }


    @GetMapping("/bdTotalV2")
    @ApiOperation(value = "供应链管理员统计列表")
    public TableDataInfo bdTotalV2(
            @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 = supplyChainService.bdTotalV2(requestMap);
        return getDataTable(list);
    }


    @GetMapping("/bdDetailListV2")
    @ApiOperation(value = "供应链管理员统计列表")
    public TableDataInfo bdDetailListV2(
            @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);
        }
        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 = supplyChainService.bdDetailListV2(requestMap);
        return getDataTable(list);
    }

    @PostMapping("/exportBdDetailV2")
    @ApiOperation(value = "认领商品列表")
    @ResponseBody
    public void exportBdDetailV2(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject requestJson) throws IOException {
        Map<String, Object> requestMap = new HashMap<>();
        if (!Check.isNull(requestJson.getString("orderStartDate"))) {
            Integer start = DateUtils.getIntegerTime(requestJson.getString("orderStartDate"));
            requestMap.put("start", start);
        }
        if (!Check.isNull(requestJson.getString("orderEndDate"))) {
            Integer end = DateUtils.getIntegerTime(requestJson.getString("orderEndDate"));
            requestMap.put("end", end);
        }
        if (!Check.isNull(requestJson.getLong("collectSampleId"))) {
            requestMap.put("collectSampleId", requestJson.getLong("collectSampleId"));
        }
        requestMap.put("fieId", "orderNum");
        requestMap.put("sort", "desc");
        List<JSONObject> list = supplyChainService.bdDetailListV2(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();
    }


}
