package com.slodon.b2b2c.controller.statistics.admin;

import cn.hutool.core.date.DateUtil;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.enums.StatsConstant;
import com.slodon.b2b2c.enums.StatsTimeType;
import com.slodon.b2b2c.model.statistics.PlatformDayModel;
import com.slodon.b2b2c.model.statistics.ProvinceDayModel;
import com.slodon.b2b2c.model.statistics.TerminalDayModel;
import com.slodon.b2b2c.statistics.example.PlatformDayExample;
import com.slodon.b2b2c.statistics.example.ProvinceDayExample;
import com.slodon.b2b2c.statistics.example.TerminalDayExample;
import com.slodon.b2b2c.statistics.pojo.PlatformDay;
import com.slodon.b2b2c.statistics.pojo.base.*;
import com.slodon.b2b2c.util.StatsModelUtil;
import com.slodon.b2b2c.vo.statistics.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
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.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

import static com.slodon.b2b2c.vo.statistics.FlowOverviewVO.dealPvPayRate;

@Api(tags = "admin-交易分析")
@RestController
@RequestMapping("v3/statistics/admin/trade/analysis")
public class AdminTradeAnalysisController extends BaseController {

    @Resource
    private PlatformDayModel platformDayModel;
    @Resource
    private TerminalDayModel terminalDayModel;
    @Resource
    private ProvinceDayModel provinceDayModel;
    @Resource
    private StatsModelUtil statsModelUtil;

    @ApiOperation("交易总览")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webSite", value = "站点"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "terminalType", value = "终端类型：Android,IOS,pc,H5,xcx 默认全部不传")
    })
    @GetMapping("tradeOverview")
    public JsonResult<TradeOverviewVO> tradeOverview(HttpServletRequest request,String webSite, Date startTime, Date endTime, String terminalType) {
        StatsTimeType timeType = StatsTimeType.checkTimeType(startTime, endTime, false);
        //上期开始时间和结束时间
        Date preStartTime;
        Date preEndTime;
        int differentDay = StatsTimeType.getDifferentDay(startTime, endTime);
        if (timeType == StatsTimeType.DAY && differentDay != 1) {
            preStartTime = StatsTimeType.getPreStartTime(startTime, differentDay);
            preEndTime = StatsTimeType.getPreEndTime(endTime, differentDay);
        } else {
            preStartTime = StatsTimeType.getStartTime(startTime, timeType);
            preEndTime = StatsTimeType.getEndTime(endTime, timeType);
        }
        TradeOverviewVO vo = new TradeOverviewVO();
        if (!StringUtil.isEmpty(terminalType)) {
            //终端类型不为空，查询终端数据
            List<? extends TerminalBase> list = statsModelUtil.getTerminalList(timeType, startTime, endTime, terminalType);
            if (!CollectionUtils.isEmpty(list)) {
                TerminalBase terminalBase = list.get(0);
                //上期数据
                TerminalBase previousData = null;
                if (preStartTime != null) {
                    List<? extends TerminalBase> previousList = statsModelUtil.getTerminalList(timeType, preStartTime, preEndTime, terminalType);
                    if (!CollectionUtils.isEmpty(previousList)) {
                        previousData = previousList.get(0);
                    }
                }
                if (terminalBase != null) {
                    vo = new TradeOverviewVO(terminalBase, previousData);
                }
            }
        } else {
            //终端类型为空，查询平台数据
            List<? extends PlatformBase> list = statsModelUtil.getPlatformList(timeType, webSite, startTime, endTime);
            if (!CollectionUtils.isEmpty(list)) {
                PlatformBase platformBase = list.get(0);
                //上期数据
                PlatformBase previousData = null;
                if (preStartTime != null) {
                    List<? extends PlatformBase> previousList = statsModelUtil.getPlatformList(timeType, webSite, preStartTime, preEndTime);
                    if (!CollectionUtils.isEmpty(previousList)) {
                        previousData = previousList.get(0);
                    }
                }
                if (platformBase != null) {
                    vo = new TradeOverviewVO(platformBase, previousData);
                }
            }
        }
        return SldResponse.success(vo);
    }

    @ApiOperation("近30天变化趋势(与流量分析共用)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webSite", value = "站点"),
            @ApiImplicitParam(name = "terminalTypes", value = "终端类型：android,ios,pc,h5,xcx 默认全部")
    })
    @GetMapping("changeTrend")
    public JsonResult<List<ChangeTrendVO>> changeTrend(HttpServletRequest request, String webSite, String terminalTypes) {
        //获取当前时间30天之前的时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -30);
        Date startTime = DateUtil.beginOfDay(calendar.getTime());
        List<ChangeTrendVO> vos = new ArrayList<>();
        //如果终端类型为空或者终端类型全选，查平台数据，否则查终端数据
        if (StringUtil.isEmpty(terminalTypes) || checkDifferent(terminalTypes)) {
            PlatformDayExample example = new PlatformDayExample();
            example.setStatsTimeAfter(startTime);
            example.setOrderBy("stats_time ASC");
            example.setWebSite(webSite);
            List<PlatformDay> list = platformDayModel.getPlatformDayList(example, null);
            if (!CollectionUtils.isEmpty(list)) {
                list.forEach(platformDay -> {
                    vos.add(new ChangeTrendVO(platformDay));
                });
            }
        } else {
            TerminalDayExample example = new TerminalDayExample();
            example.setStatsTimeAfter(startTime);
            example.setTerminalCodeIn("'" + terminalTypes.replace(",", "','") + "'");
            example.setOrderBy("statsTime ASC");
            List<ChangeTrendVO> list = terminalDayModel.getChangeTrendList(example);
            if (!CollectionUtils.isEmpty(list)) {
                list.forEach(trendVO -> {
                    trendVO.setPvPayRate(dealPvPayRate(trendVO.getOrderPayMemberNum(), trendVO.getVisitorNum()));
                });
            }
            vos.addAll(list);
        }
        return SldResponse.success(vos);
    }

    @ApiOperation("店铺销售排行-TOP10")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "webSite", value = "站点"),
            @ApiImplicitParam(name = "sort", value = "排序:1-销售额；2-订单量 默认不传按销售额降序")
    })
    @GetMapping("storeSalesRank")
    public JsonResult<List<StoreRankVO>> storeSalesRank(HttpServletRequest request, Date startTime, Date endTime, @RequestParam(name = "webSite", required = false,defaultValue = "1") String webSite,Integer sort) {
        StatsTimeType timeType = StatsTimeType.checkTimeType(startTime, endTime, false);
        //默认查询前十条数据
        PagerInfo pager = new PagerInfo(10, 1);
        List<? extends StoreBase> list = statsModelUtil.getStoreList(timeType, startTime, endTime, webSite, sort, "sale", null, pager);
        List<StoreRankVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(storeBase -> {
                //销量和销售额二者必须有一个满足条件
                if (storeBase.getOrderPayNum() > 0 || storeBase.getOrderPayAmount().compareTo(BigDecimal.ZERO) > 0) {
                    vos.add(new StoreRankVO(storeBase.getStoreId(), storeBase.getStoreName(), storeBase.getOrderPayNum(), storeBase.getOrderPayAmount()));
                }
            });
        }
        return SldResponse.success(vos);
    }

    @ApiOperation("商品销售排行-TOP10")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "webSite", value = "站点"),
            @ApiImplicitParam(name = "sort", value = "排序:1-销售额；2-销量 默认不传按销售额降序")
    })
    @GetMapping("goodsSalesRank")
    public JsonResult<List<GoodsRankVO>> goodsSalesRank(HttpServletRequest request, Date startTime, Date endTime,@RequestParam(name = "webSite", required = false,defaultValue = "1") String webSite, Integer sort) {
        StatsTimeType timeType = StatsTimeType.checkTimeType(startTime, endTime, false);
        List<? extends GoodsBase> list = statsModelUtil.getGoodsList(timeType, startTime, endTime, webSite,sort, "sale", null);
        List<GoodsRankVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(goodsBase -> {
                //销量和销售额二者必须有一个满足条件
                if (goodsBase.getSaleNum() > 0 || goodsBase.getSaleAmount().compareTo(BigDecimal.ZERO) > 0) {
                    vos.add(new GoodsRankVO(goodsBase.getGoodsId(), goodsBase.getGoodsName(), goodsBase.getGoodsImage(), goodsBase.getSaleAmount(), goodsBase.getSaleNum()));
                }
            });
        }
        return SldResponse.success(vos);
    }

    @ApiOperation("各省份销售变化趋势")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "provinceCode", value = "省份编码 默认按北京编码查询")
    })
    @GetMapping("provinceSalesTrend")
    public JsonResult<ProvinceSalesVO> provinceSalesTrend(HttpServletRequest request, Date startTime, Date endTime, String provinceCode) {
        StatsTimeType timeType = StatsTimeType.checkTimeType(startTime, endTime, true);
        List<SalesTrendVO> list = statsModelUtil.getProvinceTrendList(timeType, startTime, endTime, provinceCode);
        ProvinceSalesVO vo = new ProvinceSalesVO();
        //订单量列表
        List<SalesTrendVO> orderNumList = new ArrayList<>();
        //销售额列表
        List<SalesTrendVO> orderAmountList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(trendVO -> {
                orderNumList.add(new SalesTrendVO(trendVO.getStatsTime(), trendVO.getOrderPayNum()));
                orderAmountList.add(new SalesTrendVO(trendVO.getStatsTime(), trendVO.getOrderPayAmount()));
            });
            vo.setOrderNumList(orderNumList);
            vo.setOrderAmountList(orderAmountList);
        }
        return SldResponse.success(vo);
    }

    @ApiOperation("各省份销售占比")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "sort", value = "排序:1-销售额；2-订单量 默认不传按销售额降序")
    })
    @GetMapping("provinceSalesPercent")
    public JsonResult<SalesPercentVO> provinceSalesPercent(HttpServletRequest request, Date startTime, Date endTime, Integer sort) {
        StatsTimeType timeType = StatsTimeType.checkTimeType(startTime, endTime, false);
        List<? extends ProvinceBase> list;
        if (timeType.equals(StatsTimeType.DAY)) {
            ProvinceDayExample example = new ProvinceDayExample();
            example.setStatsTimeAfter(startTime);
            example.setStatsTimeBefore(endTime);
            if (!StringUtil.isNullOrZero(sort) && sort == 2) {
                example.setOrderBy("orderPayNum DESC");
            } else {
                example.setOrderBy("orderPayAmount DESC");
            }
            list = provinceDayModel.getProvincePercentList(example);
        } else {
            list = statsModelUtil.getProvinceList(timeType, startTime, endTime, sort);
        }
        SalesPercentVO vo = getProvinceSalesPercent(list);
        return SldResponse.success(vo);
    }

    @ApiOperation("各终端销售变化趋势")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true)
    })
    @GetMapping("terminalSalesTrend")
    public JsonResult<List<TerminalSalesVO>> terminalSalesTrend(HttpServletRequest request, Date startTime, Date endTime) {
        StatsTimeType timeType = StatsTimeType.checkTimeType(startTime, endTime, true);
        List<SalesTrendVO> list = statsModelUtil.getTerminalTrendList(timeType, startTime, endTime);
        List<TerminalSalesVO> vos = getTerminalSalesTrend(list);
        return SldResponse.success(vos);
    }

    @ApiOperation("各终端销售占比")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true)
    })
    @GetMapping("terminalSalesPercent")
    public JsonResult<SalesPercentVO> terminalSalesPercent(HttpServletRequest request, Date startTime, Date endTime) {
        StatsTimeType timeType = StatsTimeType.checkTimeType(startTime, endTime, false);
        List<? extends TerminalBase> list;
        if (timeType.equals(StatsTimeType.DAY)) {
            TerminalDayExample example = new TerminalDayExample();
            example.setStatsTimeAfter(startTime);
            example.setStatsTimeBefore(endTime);
            list = terminalDayModel.getTerminalPercentList(example);
        } else {
            list = statsModelUtil.getTerminalList(timeType, startTime, endTime, null);
        }
        SalesPercentVO vo = getTerminalSalesPercent(list);
        return SldResponse.success(vo);
    }

    @ApiOperation("交易报表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "type", value = "排序类型；desc-降序;asc-升序;默认类型为降序"),
            @ApiImplicitParam(name = "sort", value = "排序:stats_time-时间;order_submit_num-下单数;order_submit_member_num-下单人数;" +
                    "order_submit_amount-下单金额;order_pay_num-支付订单数;order_pay_member_num-支付人数;order_pay_amount-支付金额;" +
                    "submit_pay_rate-支付转化率;默认按时间降序排序"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("tradeReport")
    public JsonResult<PageVO<TradeReportVO>> tradeReport(HttpServletRequest request, Date startTime, Date endTime,
                                                         @RequestParam(value = "type", required = false, defaultValue = "desc") String type,
                                                         @RequestParam(value = "sort", required = false, defaultValue = "stats_time") String sort) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        PlatformDayExample example = new PlatformDayExample();
        example.setStatsTimeAfter(startTime);
        example.setStatsTimeBefore(endTime);
        example.setOrderBy(sort + " " + type);
        List<PlatformDay> list = platformDayModel.getPlatformDayList(example, pager);
        List<TradeReportVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(platformDay -> {
                vos.add(new TradeReportVO(platformDay));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("导出报表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "type", value = "排序类型；desc-降序;asc-升序;默认类型为降序"),
            @ApiImplicitParam(name = "sort", value = "排序:stats_time-时间;order_submit_num-下单数;order_submit_member_num-下单人数;" +
                    "order_submit_amount-下单金额;order_pay_num-支付订单数;order_pay_member_num-支付人数;order_pay_amount-支付金额;" +
                    "submit_pay_rate-支付转化率;默认按时间降序排序")
    })
    @GetMapping("export")
    public JsonResult export(HttpServletRequest request, HttpServletResponse response, Date startTime, Date endTime,
                             @RequestParam(value = "type", required = false, defaultValue = "desc") String type,
                             @RequestParam(value = "sort", required = false, defaultValue = "stats_time") String sort) {
        PlatformDayExample example = new PlatformDayExample();
        example.setStatsTimeAfter(startTime);
        example.setStatsTimeBefore(endTime);
        example.setOrderBy(sort + " " + type);
        platformDayModel.reportExport(request, response, example);
        return null;
    }

    /**
     * 各终端销售占比
     *
     * @param list
     * @return
     */
    public SalesPercentVO getTerminalSalesPercent(List<? extends TerminalBase> list) {
        SalesPercentVO result = new SalesPercentVO();
        //销售额列表
        List<SalesPercentVO.OrderPayAmountVO> orderAmountList = new ArrayList<>();
        //订单量列表
        List<SalesPercentVO.OrderNumVO> orderNumList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            //总订单量，用于计算各终端占比
            Integer totalNum = 0;
            //总销售额，用于计算各终端占比
            BigDecimal totalAmount = BigDecimal.ZERO;
            for (TerminalBase terminalBase : list) {
                totalNum += terminalBase.getOrderPayNum();
                totalAmount = totalAmount.add(terminalBase.getOrderPayAmount());
            }
            //如果总的都为0直接返回空数组
            if (totalNum == 0 && totalAmount.compareTo(BigDecimal.ZERO) == 0) {
                return result;
            }

            for (TerminalBase terminalBase : list) {
                if (totalNum != 0 && terminalBase.getOrderPayNum() != 0) {
                    SalesPercentVO.OrderNumVO vo = new SalesPercentVO.OrderNumVO();
                    vo.setTerminalName(terminalBase.getTerminalName());
                    vo.setOrderPayNum(terminalBase.getOrderPayNum());
                    BigDecimal numPer = new BigDecimal(terminalBase.getOrderPayNum()).multiply(new BigDecimal(100)).divide(new BigDecimal(totalNum), 2, BigDecimal.ROUND_HALF_UP);
                    vo.setOrderNumPercent(numPer.toString() + "%");
                    orderNumList.add(vo);
                }
                if (totalAmount.compareTo(BigDecimal.ZERO) != 0 && terminalBase.getOrderPayAmount().compareTo(BigDecimal.ZERO) != 0) {
                    SalesPercentVO.OrderPayAmountVO vo = new SalesPercentVO.OrderPayAmountVO();
                    vo.setTerminalName(terminalBase.getTerminalName());
                    vo.setOrderPayAmount(terminalBase.getOrderPayAmount());
                    BigDecimal per = terminalBase.getOrderPayAmount().multiply(new BigDecimal(100)).divide(totalAmount, 2, BigDecimal.ROUND_HALF_UP);
                    vo.setOrderAmountPercent(per.toString() + "%");
                    orderAmountList.add(vo);
                }
            }
            result.setOrderAmountList(orderAmountList);
            result.setOrderNumList(orderNumList);
        }
        return result;
    }

    /**
     * 各终端销售变化趋势
     *
     * @param list
     * @return
     */
    public List<TerminalSalesVO> getTerminalSalesTrend(List<SalesTrendVO> list) {
        List<TerminalSalesVO> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            //按照统计时间分组
            Map<String, List<TerminalSalesVO.TerminalVO>> timeMap = new HashMap<>();

            for (SalesTrendVO salesTrendVO : list) {
                TerminalSalesVO.TerminalVO terminalVO = new TerminalSalesVO.TerminalVO();
                terminalVO.setTerminalName(salesTrendVO.getTerminalName());
                terminalVO.setOrderPayAmount(salesTrendVO.getOrderPayAmount());
                terminalVO.setOrderPayNum(salesTrendVO.getOrderPayNum());
                //按照统计时间分组
                if (timeMap.containsKey(salesTrendVO.getStatsTime())) {
                    timeMap.get(salesTrendVO.getStatsTime()).add(terminalVO);
                } else {
                    //终端列表
                    List<TerminalSalesVO.TerminalVO> terminalList = new ArrayList<>();
                    terminalList.add(terminalVO);

                    timeMap.put(salesTrendVO.getStatsTime(), terminalList);
                }
            }
            timeMap.forEach((statsTime, terminalList) -> {
                TerminalSalesVO vo = new TerminalSalesVO();
                vo.setStatsTime(statsTime);
                vo.setTerminalList(terminalList);
                result.add(vo);
            });
            Collections.sort(result);
        }
        return result;
    }

    /**
     * 各省份销售占比
     *
     * @param list
     * @return
     */
    public SalesPercentVO getProvinceSalesPercent(List<? extends ProvinceBase> list) {
        SalesPercentVO result = new SalesPercentVO();
        //销售额列表
        List<SalesPercentVO.OrderPayAmountVO> orderAmountList = new ArrayList<>();
        //订单量列表
        List<SalesPercentVO.OrderNumVO> orderNumList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            //总订单量，用于计算各省份占比
            Integer totalNum = 0;
            //总销售额，用于计算各省份占比
            BigDecimal totalAmount = BigDecimal.ZERO;
            for (ProvinceBase provinceBase : list) {
                totalNum += provinceBase.getOrderPayNum();
                totalAmount = totalAmount.add(provinceBase.getOrderPayAmount());
            }
            //如果总的都为0直接返回空数组
            if (totalNum == 0 && totalAmount.compareTo(BigDecimal.ZERO) == 0) {
                return result;
            }

            //循环列表,构造返回数据
            if (list.size() <= 10) {
                //省份少于等于10个，直接记录各省份的销售占比
                for (ProvinceBase provinceBase : list) {
                    if (totalNum != 0 && provinceBase.getOrderPayNum() != 0) {
                        SalesPercentVO.OrderNumVO vo = new SalesPercentVO.OrderNumVO();
                        vo.setProvinceName(provinceBase.getProvinceName());
                        vo.setOrderPayNum(provinceBase.getOrderPayNum());
                        BigDecimal numPer = new BigDecimal(provinceBase.getOrderPayNum()).multiply(new BigDecimal(100)).divide(new BigDecimal(totalNum), 2, BigDecimal.ROUND_HALF_UP);
                        vo.setOrderNumPercent(numPer.toString() + "%");
                        orderNumList.add(vo);
                    }
                    if (totalAmount.compareTo(BigDecimal.ZERO) != 0 && provinceBase.getOrderPayAmount().compareTo(BigDecimal.ZERO) != 0) {
                        SalesPercentVO.OrderPayAmountVO vo = new SalesPercentVO.OrderPayAmountVO();
                        vo.setProvinceName(provinceBase.getProvinceName());
                        vo.setOrderPayAmount(provinceBase.getOrderPayAmount());
                        BigDecimal per = provinceBase.getOrderPayAmount().multiply(new BigDecimal(100)).divide(totalAmount, 2, BigDecimal.ROUND_HALF_UP);
                        vo.setOrderAmountPercent(per.toString() + "%");
                        orderAmountList.add(vo);
                    }
                }
            } else {
                //省份大于10，记录前10个，剩余记为其他
                //其他省份金额
                Integer otherNum = totalNum;
                BigDecimal otherAmount = totalAmount;
                //其他省份占比
                BigDecimal otherNumPer = new BigDecimal(100);
                BigDecimal otherPer = new BigDecimal(100);
                for (int i = 0; i < list.size() && i < 10; i++) {
                    ProvinceBase provinceBase = list.get(i);

                    Integer orderPayNum = provinceBase.getOrderPayNum();
                    BigDecimal orderPayAmount = provinceBase.getOrderPayAmount();

                    if (totalNum != 0 && orderPayNum != 0) {
                        SalesPercentVO.OrderNumVO vo = new SalesPercentVO.OrderNumVO();
                        vo.setProvinceName(provinceBase.getProvinceName());
                        vo.setOrderPayNum(orderPayNum);
                        BigDecimal numPer = new BigDecimal(orderPayNum).multiply(new BigDecimal(100)).divide(new BigDecimal(totalNum), 2, BigDecimal.ROUND_HALF_UP);
                        vo.setOrderNumPercent(numPer.toString() + "%");
                        orderNumList.add(vo);

                        //数据计算
                        otherNum = otherNum - orderPayNum;
                        otherNumPer = otherNumPer.subtract(numPer);
                    }
                    if (totalAmount.compareTo(BigDecimal.ZERO) != 0 && orderPayAmount.compareTo(BigDecimal.ZERO) != 0) {
                        SalesPercentVO.OrderPayAmountVO vo = new SalesPercentVO.OrderPayAmountVO();
                        vo.setProvinceName(provinceBase.getProvinceName());
                        vo.setOrderPayAmount(orderPayAmount);
                        BigDecimal per = orderPayAmount.multiply(new BigDecimal(100)).divide(totalAmount, 2, BigDecimal.ROUND_HALF_UP);
                        vo.setOrderAmountPercent(per.toString() + "%");
                        orderAmountList.add(vo);

                        //数据计算
                        otherAmount = otherAmount.subtract(orderPayAmount);
                        otherPer = otherPer.subtract(per);
                    }
                }
                //其他
                if (otherNum > 0) {
                    SalesPercentVO.OrderNumVO orderNumVO = new SalesPercentVO.OrderNumVO();
                    orderNumVO.setProvinceName("其他");
                    orderNumVO.setOrderPayNum(otherNum);
                    orderNumVO.setOrderNumPercent(otherNumPer.toString() + "%");
                    orderNumList.add(orderNumVO);
                }

                if (otherAmount.compareTo(BigDecimal.ZERO) > 0) {
                    SalesPercentVO.OrderPayAmountVO payAmountVO = new SalesPercentVO.OrderPayAmountVO();
                    payAmountVO.setProvinceName("其他");
                    payAmountVO.setOrderPayAmount(otherAmount);
                    payAmountVO.setOrderAmountPercent(otherPer.toString() + "%");
                    orderAmountList.add(payAmountVO);
                }
            }
            result.setOrderAmountList(orderAmountList);
            result.setOrderNumList(orderNumList);
        }
        return result;
    }

    /**
     * 使用list自带的sort方法先进性排序，然后转成toString去判断两个集合是否相等
     */
    public boolean checkDifferent(String terminalTypes) {
        //默认终端类型
        String defaultType = StatsConstant.TERMINAL_ANDROID + "," + StatsConstant.TERMINAL_H5 + ","
                + StatsConstant.TERMINAL_IOS + "," + StatsConstant.TERMINAL_PC + "," + StatsConstant.TERMINAL_WX_XCX;
        List<String> defaultList = Arrays.asList(defaultType.split(","));

        List<String> list = Arrays.asList(terminalTypes.split(","));

        defaultList.sort(Comparator.comparing(String::hashCode));
        list.sort(Comparator.comparing(String::hashCode));
        return defaultList.toString().equals(list.toString());
    }

}
