package com.storlead.sales.controller.report;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.storlead.framework.common.util.ConvertUtils;
import com.storlead.framework.util.LoginUserUtil;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.dto.customer.BusinessSearchDTO;
import com.storlead.sales.pojo.vo.customer.BusinessListVO;
import com.storlead.sales.pojo.vo.customer.SalesDataAddTotalVO;
import com.storlead.sales.pojo.dto.report.DataAnalysisDTO;
import com.storlead.sales.pojo.vo.report.*;
import com.storlead.sales.service.SalesProcessAnalysisService;
import com.storlead.sales.util.report.NumberCompareUtils;
import com.storlead.sales.service.task.TaskService;
import com.storlead.system.enums.OrganizationalTypeEnum;
import com.storlead.system.service.IUserRoleService;
import com.storlead.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
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.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: sp-sales-platform
 * @description: 销售过程分析
 * @author: chenkq
 * @create: 2024-10-22 15:06
 */
@RestController
@RequestMapping("/process/analysis")
@Api(tags = "报表: 销售过程统计")
public class SalesProcessAnalysisApiController {

    @Resource
    private TaskService taskService;

    @Resource
    private IUserService userService;

    @Resource
    private IUserRoleService userRoleService;

    @Resource
    private SalesProcessAnalysisService salesProcessAnalysisService;
    /**
     * 获取近12个月数据
     * @param businessSearchDTO
     * @return
     */
    public Result<?> selectListVO(BusinessSearchDTO businessSearchDTO) {

        return Result.result(null);
    }


    /**
     * 跟进记录汇总
     * @param businessSearchDTO
     * @return
     */
    @PostMapping(value = "/get_predictive_analytics_total")
    @ApiOperation(value = "销售预测分析-预测分析")
    public Result<?> getPredictiveAnalyticsTotal(@RequestBody DataAnalysisDTO dto) {
        if (Integer.valueOf(1).equals(dto.getIsMySelf())) {
           dto.setOwnerByIds(Arrays.asList(LoginUserUtil.getCurrentUserId()));
        }
        if (!CollectionUtils.isEmpty(dto.getDeptIds()) && CollectionUtils.isEmpty(dto.getOwnerByIds()) ) {
            Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
            if (!CollectionUtils.isEmpty(userIds)) {
                dto.setOwnerByIds(new ArrayList<>(userIds));
            }
        }

        BusinessMonthTotalVO monthTotalVO = salesProcessAnalysisService.performSalesForecastAnalysis(dto);

        return Result.result(monthTotalVO);
    }

    /**
     * 跟进记录汇总
     * @param businessSearchDTO
     * @return
     */
    @PostMapping(value = "/get_predictive_analytics_report")
    @ApiOperation(value = "销售预测分析-预测报表")
    public Result<?> getPredictiveAnalyticsReport(@RequestBody DataAnalysisDTO dto) {
        if (!CollectionUtils.isEmpty(dto.getDeptIds()) && CollectionUtils.isEmpty(dto.getOwnerByIds()) ) {
            Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
            if (!CollectionUtils.isEmpty(userIds)) {
                dto.setOwnerByIds(new ArrayList<>(userIds));
            }
        }
        IPage<BusinessListVO>  monthTotalVO = salesProcessAnalysisService.listPerformSalesForecastAnalysis(dto);
        return Result.result(monthTotalVO);
    }

    /**
     * 销售漏斗分析-漏斗分析
     * @param businessSearchDTO
     * @return
     */
    @PostMapping(value = "/get_analyze_sales_funnel")
    @ApiOperation(value = "销售漏斗分析-漏斗分析")
    public Result<?> getAnalyzeSalesFunnel(@RequestBody DataAnalysisDTO dto) {
        if (Integer.valueOf(1).equals(dto.getIsMySelf())) {
            dto.setOwnerByIds(Arrays.asList(LoginUserUtil.getCurrentUserId()));
        }
        if (!CollectionUtils.isEmpty(dto.getDeptIds()) && CollectionUtils.isEmpty(dto.getOwnerByIds()) ) {
            Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
            if (!CollectionUtils.isEmpty(userIds)) {
                dto.setOwnerByIds(new ArrayList<>(userIds));
            }
        }
        if (Objects.isNull(dto.getBeginCycleTime()) ||  Objects.isNull(dto.getEndCycleTime())) {
            LocalDate currentDate = LocalDate.now();
            LocalDate date30DaysAgo = currentDate.minusDays(30);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            dto.setBeginCycleTime(currentDate.format(formatter));
            dto.setEndCycleTime(date30DaysAgo.format(formatter));
        }
        BusinessStageTotalVO monthTotalVO = salesProcessAnalysisService.analyzeSalesFunnel(dto);
        return Result.result(monthTotalVO);
    }

    /**
     * 销售漏斗分析-漏斗分析
     * @param businessSearchDTO
     * @return
     */
    @PostMapping(value = "/get_analyze_sales_funnel_report")
    @ApiOperation(value = "销售漏斗分析-漏斗报表")
    public Result<?> getAnalyzeSalesFunnelReport(@RequestBody DataAnalysisDTO dto) {
        if (Integer.valueOf(1).equals(dto.getIsMySelf())) {
            dto.setOwnerByIds(Arrays.asList(LoginUserUtil.getCurrentUserId()));
        }
        if (!CollectionUtils.isEmpty(dto.getDeptIds()) && CollectionUtils.isEmpty(dto.getOwnerByIds()) ) {
            Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
            if (!CollectionUtils.isEmpty(userIds)) {
                dto.setOwnerByIds(new ArrayList<>(userIds));
            }
        }
        IPage<BusinessListVO>  monthTotalVO = salesProcessAnalysisService.listPerformSalesForecastAnalysis(dto);
        return Result.result(monthTotalVO);
    }

    @PostMapping(value = "/get_summarize_new_business")
    @ApiOperation(value = "业务新增汇总-漏斗分析")
    public Result<?> getSummarizeNewBusiness(@RequestBody DataAnalysisDTO dto) {
        if (Integer.valueOf(1).equals(dto.getIsMySelf())) {
            dto.setOwnerByIds(Arrays.asList(LoginUserUtil.getCurrentUserId()));
        }
        dto.setDataFormat("%Y-%m-%d");
        if (Integer.valueOf(30).equals(dto.getSummaryType())) {
            dto.setDataFormat("%Y-%m");
        }
        if (Integer.valueOf(70).equals(dto.getSummaryType())) {
            Set deptScope = userRoleService.dataSelete(OrganizationalTypeEnum.DEPT_GROUP);
            if (!CollectionUtils.isEmpty(dto.getDeptIds())) {
                dto.setDeptIds(ConvertUtils.retainByIds(dto.getDeptIds(),deptScope));
            } else {
                dto.setDeptIds(List.copyOf(deptScope));
            }
            if (!CollectionUtils.isEmpty(dto.getDeptIds())) {
                Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
                if (CollectionUtils.isEmpty(userIds)) {
                    return Result.result(new SalesDataAddTotalVO());
                }
                dto.setOwnerByIds(List.copyOf(userIds));
            } else {
                return Result.result(new SalesDataAddTotalVO());
            }
        } else {
            Set dataScope = userRoleService.dataSelete(OrganizationalTypeEnum.USER_GROUP);
            if (!CollectionUtils.isEmpty(dto.getOwnerByIds())) {
                if (!CollectionUtils.isEmpty(dataScope)) {
                    dto.setOwnerByIds(ConvertUtils.retainByIds(dto.getOwnerByIds(),dataScope));
                }
            } else {
                dto.setOwnerByIds(List.copyOf(dataScope));
            }
        }

        SalesDataGrowTotalVO monthTotalVO = salesProcessAnalysisService.summarizeNewBusiness(dto);
        return Result.result(monthTotalVO);
    }



    /**
     * 跟进记录汇总
     * @param
     * @return
     */
    @PostMapping(value = "/get_follow_mode_total")
    @ApiOperation(value = "跟进记录汇总-跟进记录汇总" )
    public Result<?> getSummarizeFollowUpTotal(@RequestBody DataAnalysisDTO dto) {
        if (Integer.valueOf(1).equals(dto.getIsMySelf())) {
            dto.setOwnerByIds(Arrays.asList(LoginUserUtil.getCurrentUserId()));
        }
        if (!CollectionUtils.isEmpty(dto.getDeptIds()) && CollectionUtils.isEmpty(dto.getOwnerByIds()) ) {
            Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
            if (!CollectionUtils.isEmpty(userIds)) {
                dto.setOwnerByIds(new ArrayList<>(userIds));
            }
        }
        Set dataScope = userRoleService.dataSelete(OrganizationalTypeEnum.USER_GROUP);
        if (!CollectionUtils.isEmpty(dto.getOwnerByIds())) {
            if (!CollectionUtils.isEmpty(dataScope)) {
                dto.setOwnerByIds(ConvertUtils.retainByIds(dto.getOwnerByIds(),dataScope));
            }
        } else {
            dto.setOwnerByIds(List.copyOf(dataScope));
        }
        UserFollowUpModeCountVO vo = salesProcessAnalysisService.summarizeFollowUpModeVo(dto);
        return Result.result(vo);
    }

    /**
     * 跟进记录汇总
     * @param
     * @return
     */
    @PostMapping(value = "/get_follow_mode_report")
    @ApiOperation(value = "跟进记录汇总-跟进方式汇总" )
    public Result<?> getSummarizeFollowUpReport(@RequestBody DataAnalysisDTO dto) {
        if (Integer.valueOf(1).equals(dto.getIsMySelf())) {
            dto.setOwnerByIds(Arrays.asList(LoginUserUtil.getCurrentUserId()));
        }
        if (!CollectionUtils.isEmpty(dto.getDeptIds()) && CollectionUtils.isEmpty(dto.getOwnerByIds()) ) {
            Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
            if (!CollectionUtils.isEmpty(userIds)) {
                dto.setOwnerByIds(new ArrayList<>(userIds));
            }
        }
        Set dataScope = userRoleService.dataSelete(OrganizationalTypeEnum.USER_GROUP);
        if (!CollectionUtils.isEmpty(dto.getOwnerByIds())) {
            if (!CollectionUtils.isEmpty(dataScope)) {
                dto.setOwnerByIds(ConvertUtils.retainByIds(dto.getOwnerByIds(),dataScope));
            }
        } else {
            dto.setOwnerByIds(List.copyOf(dataScope));
        }
        IPage<UserFollowUpModeCountVO> page = salesProcessAnalysisService.summarizeFollowUpModeRecords(dto);
        return Result.result(page);
    }

    /**
     * 跟进记录汇总
     * @param
     * @return
     */
    @PostMapping(value = "/get_follow_type_report")
    @ApiOperation(value = "跟进记录汇总-跟进对象汇总" )
    public Result<?> getFollowTypeTotal(@RequestBody DataAnalysisDTO dto) {
        if (Integer.valueOf(1).equals(dto.getIsMySelf())) {
            dto.setOwnerByIds(Arrays.asList(LoginUserUtil.getCurrentUserId()));
        }
        if (!CollectionUtils.isEmpty(dto.getDeptIds()) && CollectionUtils.isEmpty(dto.getOwnerByIds()) ) {
            Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
            if (!CollectionUtils.isEmpty(userIds)) {
                dto.setOwnerByIds(new ArrayList<>(userIds));
            }
        }
        Set dataScope = userRoleService.dataSelete(OrganizationalTypeEnum.USER_GROUP);
        if (!CollectionUtils.isEmpty(dto.getOwnerByIds())) {
            if (!CollectionUtils.isEmpty(dataScope)) {
                dto.setOwnerByIds(ConvertUtils.retainByIds(dto.getOwnerByIds(),dataScope));
            }
        } else {
            dto.setOwnerByIds(List.copyOf(dataScope));
        }
        IPage<UserFollowUpCountVO> page = salesProcessAnalysisService.summarizeFollowUpTypeRecords(dto);
        return Result.result(page);
    }

    /**
     * 跟进记录汇总
     * @param
     * @return
     */
    @PostMapping(value = "/get_follow_type_total")
    @ApiOperation(value = "跟进记录汇总-跟进对象" )
    public Result<?> getFollowTypeReport(@RequestBody DataAnalysisDTO dto) {
        if (Integer.valueOf(1).equals(dto.getIsMySelf())) {
            dto.setOwnerByIds(Arrays.asList(LoginUserUtil.getCurrentUserId()));
        }
        if (!CollectionUtils.isEmpty(dto.getDeptIds()) && CollectionUtils.isEmpty(dto.getOwnerByIds()) ) {
            Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
            if (!CollectionUtils.isEmpty(userIds)) {
                dto.setOwnerByIds(new ArrayList<>(userIds));
            }
        }
        Set dataScope = userRoleService.dataSelete(OrganizationalTypeEnum.USER_GROUP);
        if (!CollectionUtils.isEmpty(dto.getOwnerByIds())) {
            if (!CollectionUtils.isEmpty(dataScope)) {
                dto.setOwnerByIds(ConvertUtils.retainByIds(dto.getOwnerByIds(),dataScope));
            }
        } else {
            dto.setOwnerByIds(List.copyOf(dataScope));
        }
        UserFollowUpCountVO vo = salesProcessAnalysisService.summarizeFollowUpTypeVo(dto);
        return Result.result(vo);
    }



    @PostMapping(value = "/generate_comprehensive_business_statistics")
    @ApiOperation(value = "业务综合统计")
    public Result<?> generateComprehensiveBusinessStatistics(@RequestBody DataAnalysisDTO dto) {
        if (Integer.valueOf(1).equals(dto.getIsMySelf())) {
            dto.setOwnerByIds(Arrays.asList(LoginUserUtil.getCurrentUserId()));
        }
        if (!CollectionUtils.isEmpty(dto.getDeptIds()) && CollectionUtils.isEmpty(dto.getOwnerByIds()) ) {
            Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
            if (!CollectionUtils.isEmpty(userIds)) {
                dto.setOwnerByIds(new ArrayList<>(userIds));
            }
        }
        Set dataScope = userRoleService.dataSelete(OrganizationalTypeEnum.USER_GROUP);
        if (!CollectionUtils.isEmpty(dto.getOwnerByIds())) {
            if (!CollectionUtils.isEmpty(dataScope)) {
                dto.setOwnerByIds(ConvertUtils.retainByIds(dto.getOwnerByIds(),dataScope));
            }
        } else {
            dto.setOwnerByIds(List.copyOf(dataScope));
        }
        List<SalesDataGrowCountVO> monthTotalVOS = salesProcessAnalysisService.generateComprehensiveBusinessStatistics(dto);
        if (!CollectionUtils.isEmpty(monthTotalVOS)) {
            monthTotalVOS = monthTotalVOS.stream().sorted(Comparator.comparing(SalesDataGrowCountVO::getGroupTypeName)).collect(Collectors.toList());
        }
        return Result.result(monthTotalVOS);
    }

    @PostMapping(value = "/get_calculate_business_growth_statistics")
    @ApiOperation(value = "业务增长统计")
    public Result<?> calculateBusinessGrowthStatistics(@RequestBody DataAnalysisDTO dto) {
        Set<Integer> queryYears = new HashSet<>(dto.getYears());
        Set<Integer> years = new HashSet<>();
        if (!CollectionUtils.isEmpty(dto.getYears())) {
            for (Integer year : dto.getYears()) {
                queryYears.add(year - 1);
                years.add(year);
            }
        }
        dto.setYears(new ArrayList<>(queryYears));
        if (Integer.valueOf(1).equals(dto.getIsMySelf())) {
            dto.setOwnerByIds(Arrays.asList(LoginUserUtil.getCurrentUserId()));
        }
        if (!CollectionUtils.isEmpty(dto.getDeptIds()) && CollectionUtils.isEmpty(dto.getOwnerByIds()) ) {
            Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
            if (!CollectionUtils.isEmpty(userIds)) {
                dto.setOwnerByIds(new ArrayList<>(userIds));
            }
        }
        Set dataScope = userRoleService.dataSelete(OrganizationalTypeEnum.USER_GROUP);
        if (!CollectionUtils.isEmpty(dto.getOwnerByIds())) {
            if (!CollectionUtils.isEmpty(dataScope)) {
                dto.setOwnerByIds(ConvertUtils.retainByIds(dto.getOwnerByIds(),dataScope));
            }
        } else {
            dto.setOwnerByIds(List.copyOf(dataScope));
        }
        List<SalesBusinessGrowCountVO> growDatas = salesProcessAnalysisService.calculateBusinessGrowthStatistics(dto);

        Map<String,SalesBusinessGrowCountVO> growMap =  growDatas.stream().collect(Collectors.toMap(SalesBusinessGrowCountVO::getGroupType, CustomerListVO -> CustomerListVO));

        List<SalesBusinessGrowTotalVO> vos = new ArrayList<>();
        for (Integer year : years) {
            List<SalesBusinessGrowCountVO> yearDatas = new ArrayList<>();
            for (int month = 1; month <= 12; month++) {
                String monthStr = (month > 9 ? month+"" : "0" + month);
                String yearMonth = year + "-" + monthStr;
                SalesBusinessGrowCountVO vo = growMap.get(yearMonth);
                if (Objects.isNull(vo)) {
                    vo = new SalesBusinessGrowCountVO();
                    vo.setGroupType(yearMonth);
                }
                if (Integer.valueOf(1).equals(dto.getComparisonType())) {
                    String lastTearMonth = (year-1) + "-" + monthStr;
                    SalesBusinessGrowCountVO vo1 = growMap.get(lastTearMonth);
                    if (Objects.isNull(vo1)) {
                        vo1 = new SalesBusinessGrowCountVO();
                    }
                    vo.setDataGrowRatio(NumberCompareUtils.computaRatio(vo.getDataCount(),vo1.getDataCount()));
                } else if (Integer.valueOf(2).equals(dto.getComparisonType())) {
                    monthStr = ((month - 1) > 9 ? (month - 1)+"" : "0" + (month - 1));
                    String lastTearMonth = ((Integer.valueOf(1).equals(month)) ? year - 1 : year) + "-" + ((Integer.valueOf(1).equals(month)) ? 12 : monthStr);
                    SalesBusinessGrowCountVO vo1 = growMap.get(lastTearMonth);
                    if (Objects.isNull(vo1)) {
                        vo1 = new SalesBusinessGrowCountVO();
                    }
                    vo.setDataGrowRatio(NumberCompareUtils.computaRatio(vo.getDataCount(),vo1.getDataCount()));
                }
                vo.setGroupTypeName(vo.getGroupType());
                yearDatas.add(vo);
            }
            SalesBusinessGrowTotalVO v = new SalesBusinessGrowTotalVO();
            v.setYearStr(year.toString());
            v.setDatas(yearDatas);
            vos.add(v);
        }
        return Result.result(vos);
    }


    /**
     * 销售排名Top10
     * @param
     * @return
     */
    @PostMapping(value = "/get_sales_deal_rank_top")
    @ApiOperation(value = "销售排名Top10" )
    public Result<?> getSalesDealRankTop(@RequestBody DataAnalysisDTO dto) {
        if (Integer.valueOf(1).equals(dto.getIsMySelf())) {
            dto.setOwnerByIds(Arrays.asList(LoginUserUtil.getCurrentUserId()));
        }
        if (!CollectionUtils.isEmpty(dto.getDeptIds()) && CollectionUtils.isEmpty(dto.getOwnerByIds()) ) {
            Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
            if (!CollectionUtils.isEmpty(userIds)) {
                dto.setOwnerByIds(new ArrayList<>(userIds));
            }
        }
        Set dataScope = userRoleService.dataSelete(OrganizationalTypeEnum.USER_GROUP);
        if (!CollectionUtils.isEmpty(dto.getOwnerByIds())) {
            if (!CollectionUtils.isEmpty(dataScope)) {
                dto.setOwnerByIds(ConvertUtils.retainByIds(dto.getOwnerByIds(),dataScope));
            }
        } else {
            dto.setOwnerByIds(List.copyOf(dataScope));
        }
        List<SalesDealRankCountlVO> vos = salesProcessAnalysisService.selectSalesDealRankTop(dto);
        // 根据成交总额降序排名，并设置 rankNum
        List<SalesDealRankCountlVO> sortedOrders = vos.stream()
                .sorted((o1, o2) -> o2.getOrderDealAmount().compareTo(o1.getOrderDealAmount())) // 按成交总额降序排序
                .collect(Collectors.toList());

        SalesDealRankTotalVO totalVo = new SalesDealRankTotalVO();
        totalVo.setDatas(sortedOrders);
        int rank = 1;
        for (SalesDealRankCountlVO vo : sortedOrders) {
            vo.setRankNum(rank++);
            totalVo.setOrderDealNum(vo.getOrderDealNum() + totalVo.getOrderDealNum());
            totalVo.setOrderDealAmount(totalVo.getOrderDealAmount().add(vo.getOrderDealAmount()));
        }
        return Result.result(totalVo);
    }

    /**
     * 跟进记录汇总
     * @param
     * @return
     */
    @PostMapping(value = "/get_sales_area_spread_summary_total")
    @ApiOperation(value = "客户区域分布" )
    public Result<?> getSalesAreaSpreadSummaryTotal(@RequestBody DataAnalysisDTO dto) {
        if (StrUtil.isBlank(dto.getBeginCycleTime())) {
            dto.setBeginCycleTime(null);
        }
        if (StrUtil.isBlank(dto.getEndCycleTime())) {
            dto.setEndCycleTime(null);
        }

        if (!CollectionUtils.isEmpty(dto.getDeptIds()) && CollectionUtils.isEmpty(dto.getOwnerByIds()) ) {
            Set<Long> userIds = userService.getUserIdsByDeptIds(dto.getDeptIds());
            if (!CollectionUtils.isEmpty(userIds)) {
                dto.setOwnerByIds(new ArrayList<>(userIds));
            }
        }
        Set dataScope = userRoleService.dataSelete(OrganizationalTypeEnum.USER_GROUP);
        if (!CollectionUtils.isEmpty(dto.getOwnerByIds())) {
            if (!CollectionUtils.isEmpty(dataScope)) {
                dto.setOwnerByIds(ConvertUtils.retainByIds(dto.getOwnerByIds(),dataScope));
            }
        } else {
            dto.setOwnerByIds(List.copyOf(dataScope));
        }

        List<SalesAreaSpreadCountVO> vos = salesProcessAnalysisService.selectSalesAreaSpreadSummary(dto);
        if (CollectionUtils.isEmpty(vos)) {
            return  Result.result(vos);
        }

        SalesAreaSpreadTotalVO totalVO = salesProcessAnalysisService.selectSalesAreaSpreadTotal(dto);
        vos.forEach(vo -> {
            vo.setCustomerRatio(NumberCompareUtils.computaPercentage(vo.getCustomerNum(),totalVO.getCustomerTotalNum()));
            vo.setOrderDealAmountRatio(NumberCompareUtils.computaPercentage(vo.getOrderDealAmount(),totalVO.getOrderTotalAmount()));
            vo.setOrderDealRatio(NumberCompareUtils.computaPercentage(vo.getOrderDealNum(),totalVO.getOrderTotalNum()));

        });
        List<SalesAreaSpreadCountVO> sorteds = vos.stream()
                .sorted((o1, o2) -> o2.getOrderDealAmount().compareTo(o1.getOrderDealAmount())) // 按成交总额降序排序
                .collect(Collectors.toList());

        int rank = 1;
        for (SalesAreaSpreadCountVO vo : sorteds) {
            vo.setRankNum(rank++);
        }
        return Result.result(sorteds);
    }

}
