package com.fjhx.sale.service.sale.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.account.controller.utils.DateUtils;
import com.fjhx.area.utils.CustomizeAreaUtil;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.customer.entity.customer.dto.CustomerDto;
import com.fjhx.customer.entity.customer.dto.CustomerSelectDto;
import com.fjhx.customer.entity.customer.po.Customer;
import com.fjhx.customer.entity.customer.vo.CustomerVo;
import com.fjhx.customer.service.customer.CustomerService;
import com.fjhx.sale.entity.contract.vo.ContractVo;
import com.fjhx.sale.entity.quotation.po.QuotationPay;
import com.fjhx.sale.entity.quotation.po.QuotationProduct;
import com.fjhx.sale.entity.sale.dto.SaleQuotationDto;
import com.fjhx.sale.entity.sale.dto.SaleQuotationSelectDto;
import com.fjhx.sale.entity.sale.po.SaleQuotation;
import com.fjhx.sale.entity.sale.vo.SaleQuotationVo;
import com.fjhx.sale.mapper.sale.SaleQuotationMapper;
import com.fjhx.sale.service.contract.ContractService;
import com.fjhx.sale.service.quotation.QuotationPayService;
import com.fjhx.sale.service.quotation.QuotationProductService;
import com.fjhx.sale.service.sale.SaleQuotationService;
import com.fjhx.sale.util.code.CodeEnum;
import com.fjhx.tenant.entity.dict.dto.DictTenantDataSelectDto;
import com.fjhx.tenant.entity.dict.vo.DictTenantDataVo;
import com.fjhx.tenant.service.dict.DictTenantDataService;
import com.obs.services.internal.ServiceException;
import com.ruoyi.common.core.domain.BasePo;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


/**
 * <p>
 * 报价表 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-14
 */
@Service
public class SaleQuotationServiceImpl extends ServiceImpl<SaleQuotationMapper, SaleQuotation> implements SaleQuotationService {
    @Autowired
    private QuotationProductService quotationProductService;

    @Autowired
    private QuotationPayService quotationPayService;

    @Autowired
    private ContractService contractService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private DictTenantDataService dictTenantDataService;


    /**
     * 报价表分页
     */
    @Override
    public Page<SaleQuotationVo> getPage(SaleQuotationSelectDto dto) {
        LambdaQueryWrapper<SaleQuotation> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getSellCorporationId()),
                SaleQuotation::getSellCorporationId, dto.getSellCorporationId());
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getStatus()), SaleQuotation::getStatus, dto.getStatus());
        wrapper.orderByDesc(SaleQuotation::getCreateTime);
        Page<SaleQuotationVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<SaleQuotationVo> saleQuotationVoList = page.getRecords();

        //赋值用户名称
        UserUtil.assignmentNickName(saleQuotationVoList, BasePo::getCreateUser, SaleQuotationVo::setUserName);
        return page;
    }

    /**
     * 报价表明细
     */
    @Override
    public SaleQuotationVo detail(Long id) {
        //查询报价表详情
        SaleQuotation SaleQuotation = this.getById(id);
        SaleQuotationVo result = BeanUtil.toBean(SaleQuotation, SaleQuotationVo.class);
        if (ObjectUtil.isNotEmpty(result)) {
            //赋值买方公司地址
            setArea(result);
            //查询报价-商品表的详情
            List<QuotationProduct> quotationProductList = quotationProductService.list(Wrappers.<QuotationProduct>lambdaQuery()
                    .eq(QuotationProduct::getSaleQuotationId, result.getId()));
            result.setQuotationProductList(quotationProductList);

            //查询报价-收费项目表的详情
            List<QuotationPay> quotationPayList = quotationPayService.list(Wrappers.<QuotationPay>lambdaQuery()
                    .eq(QuotationPay::getSaleQuotationId, result.getId()));
            result.setQuotationPayList(quotationPayList);
        }

        return result;
    }

    @Override
    public void add(SaleQuotationDto saleQuotationDto) {
        this.save(saleQuotationDto);
    }

    @Override
    public void edit(SaleQuotationDto saleQuotationDto) {
        this.updateById(saleQuotationDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

    /**
     * 报价单的复制功能（报价单，报价-商品表的 ，报价-收费项目表，根据报价表的ID相关数据复制一份）
     */
    @Override
    public void copy(SaleQuotationDto saleQuotationDto) {
        if (ObjectUtil.isEmpty(saleQuotationDto.getId())) {
            throw new ServiceException("参数缺失：报价单ID不能为null");
        }
        //查询商品-报价表的数据
        SaleQuotation saleQuotation = this.getById(saleQuotationDto.getId());
        if (ObjectUtil.isNotEmpty(saleQuotation)) {
            throw new ServiceException("报价表的数据不存在");
        }

        //查询报价-商品表的数据
        List<QuotationProduct> quotationProductList = quotationProductService.list(Wrappers.<QuotationProduct>lambdaQuery()
                .eq(QuotationProduct::getSaleQuotationId, saleQuotationDto.getId()));

        //查询报价-收费项目表
        List<QuotationPay> quotationPays = quotationPayService.list(Wrappers.<QuotationPay>lambdaQuery()
                .eq(QuotationPay::getSaleQuotationId, saleQuotationDto.getId()));

        //添加商品-报价表
        quotationProductList.forEach(quotationProduct -> quotationProduct.setId(null));
        quotationProductService.saveBatch(quotationProductList);

        //添加报价-收费项目表
        quotationPays.forEach(quotationPay -> quotationPay.setId(null));
        quotationPayService.saveBatch(quotationPays);


        //添加报价表的信息
        saleQuotation.setId(null);
        saleQuotation.setCode(CodeEnum.SALE_QUOTATION.getCode());
        baseMapper.insert(saleQuotation);

    }

    /**
     * 查询这个客户的报价次数
     *
     * @param id （客户ID）
     */
    @Override
    public Integer getCount(Long id) {
        return baseMapper.getCount(id);
    }

    /**
     * 查询指定客户的每月报价总额
     *
     * @param dto
     * @return
     */
    @Override
    public List<SaleQuotationVo> getAmount(CustomerDto dto) {
        return baseMapper.getAmount(dto);
    }

    /**
     * 最新跟进（客户画像）
     */
    @Override
    public Page<SaleQuotationVo> latestFollowUp(CustomerSelectDto dto) {
        Page<SaleQuotationVo> saleQuotationVoPage = baseMapper.latestFollowUp(dto.getPage(), Wrappers.query().eq("buy_corporation_id", dto.getId())
                .orderByDesc("create_time"));

        return saleQuotationVoPage;
    }

    /**
     * 销售统计（客户画像）
     */
    @Override
    public Map<String, Object> salesStatistics(CustomerDto dto) {
        Map<String, Object> map = new HashMap<>();
        //切换数据源
        DynamicDataSourceContextHolder.push(SourceConstant.SALE);
        //查询这个客户的销售额（合同总金额）及成交合同数量
        ContractVo contractVo = contractService.getSalesTotal(dto.getId());
        map.put("contractVo", contractVo);

        //查询这个客户的报价次数
        Integer count = this.getCount(dto.getId());
        DynamicDataSourceContextHolder.poll();
        map.put("count", count);
        return map;
    }

    /**
     * 报价销售走势（客户画像）
     */
    @Override
    public Map<String, Object> salesTrend(CustomerDto dto) {
        Map<String, Object> map = new HashMap<>();

        List<String> monthBetweenDate = DateUtils.getMonthBetweenDate(DateUtil.offsetMonth(new Date(), -12), new Date());

        //存放报价的数据；
        List<Map<String, Object>> saleQuotationList = new ArrayList<>();
        //存放报价的数据；
        List<Map<String, Object>> contractList = new ArrayList<>();

        dto.setBeginTime(DateUtil.format(DateUtil.offsetMonth(new Date(), -12), "yyyy-MM"));
        dto.setEndTime(DateUtil.format(new Date(), "yyyy-MM"));
        //查询指定客户的每月报价总额
        List<SaleQuotationVo> saleQuotationVoList = this.getAmount(dto);

        Map<String, List<SaleQuotationVo>> saleQuotationVoMap = saleQuotationVoList.stream()
                .collect(Collectors.groupingBy(SaleQuotationVo::getTime));

        //查询指定客户的每月合同总额
        List<ContractVo> contractVoList = contractService.getAmount(dto);
        Map<String, List<ContractVo>> contractVoMap = contractVoList.stream()
                .collect(Collectors.groupingBy(ContractVo::getTime));

        //赋值每个月的值
        for (String month : monthBetweenDate) {
            //存放每月的报价总额
            Map<String, Object> saleQuotationMap = new HashMap<>();
            //每月的金额初始化的值为0
            saleQuotationMap.put("month", month);
            saleQuotationMap.put("saleAmount", new BigDecimal(0));
            List<SaleQuotationVo> saleQuotationVos = saleQuotationVoMap.get(month);
            //给每月的报价金额赋值
            if (ObjectUtil.isNotEmpty(saleQuotationVos)) {
                saleQuotationMap.put("saleAmount", saleQuotationVos.get(0).getAmount());
            }
            saleQuotationList.add(saleQuotationMap);

            //存放每月的合同总额
            Map<String, Object> contractMap = new HashMap<>();
            //每月的金额初始化的值为0
            contractMap.put("month", month);
            contractMap.put("contractAmount", new BigDecimal(0));
            List<ContractVo> contractVoList1 = contractVoMap.get(month);
            if (ObjectUtil.isNotEmpty(contractVoList1)) {
                contractMap.put("contractAmount", contractVoList1.get(0).getAmount());
            }
            contractList.add(contractMap);
        }
        map.put("contractList", contractList);
        map.put("saleQuotationList", saleQuotationList);
        return map;
    }

    /**
     * 销售明细（客户画像）
     */
    @Override
    public Page<SaleQuotationVo> saleDetail(CustomerSelectDto dto) {
        Page<SaleQuotationVo> saleQuotationVoPage = baseMapper.saleDetail(dto.getPage(), dto);
        List<SaleQuotationVo> records = saleQuotationVoPage.getRecords();
        // 赋值用户名称
        UserUtil.assignmentNickName(records, SaleQuotationVo::getCreateUser, SaleQuotationVo::setUserName);

        return saleQuotationVoPage;
    }

    /**
     * 来源统计（客户分析）
     */
    @Override
    public Map<String, Object> sourceStatistics(CustomerDto dto) {
        //切换数据源
        DynamicDataSourceContextHolder.push(SourceConstant.CUSTOMER);
        //存放来源统计的数据
        Map<String, Object> map = new HashMap<>();

        //存放来源存量与增量的数据列表
        List<Map<String,Object>> stockList = new ArrayList<>();


        //来源存量（在指定时间外的数量）
        QueryWrapper<Object> query = Wrappers.query();
        query.eq(ObjectUtil.isNotEmpty(dto.getCountryId()), "c.country_id", dto.getCountryId());
        query.eq(ObjectUtil.isNotEmpty(dto.getProvinceId()), "c.province_id", dto.getProvinceId());
        query.eq(ObjectUtil.isNotEmpty(dto.getCityId()), "c.city_id", dto.getCityId());
        query.groupBy("c.source");
        query.le("DATE_FORMAT(c.create_time,'%Y-%m')", dto.getBeginTime());
        query.ge("DATE_FORMAT(c.create_time,'%Y-%m')", dto.getEndTime());
        List<CustomerVo> sourceStockList = customerService.sourceStock(query);

        //来源增量（在指定时间内的数量）
        query.clear();
        query.eq(ObjectUtil.isNotEmpty(dto.getCountryId()), "c.country_id", dto.getCountryId());
        query.eq(ObjectUtil.isNotEmpty(dto.getProvinceId()), "c.province_id", dto.getProvinceId());
        query.eq(ObjectUtil.isNotEmpty(dto.getCityId()), "c.city_id", dto.getCityId());
        query.groupBy("c.source");
        query.ge("DATE_FORMAT(c.create_time,'%Y-%m')", dto.getBeginTime());
        query.le("DATE_FORMAT(c.create_time,'%Y-%m')", dto.getEndTime());
        List<CustomerVo> sourceIncrementList = customerService.sourceIncrement(query);

        DynamicDataSourceContextHolder.poll();

        //切换数据源
        DynamicDataSourceContextHolder.push(SourceConstant.BASE);
        //获取来源的字典数据
        List<DictTenantDataVo> dictTenantDataVos = getDict("customer_source");
        DynamicDataSourceContextHolder.poll();
        //获取来源增量总计
        Integer incrementAmount = sourceIncrementList.stream().map(customerVo -> customerVo.getCount())
                .collect(Collectors.toList()).stream().reduce(Integer::sum).orElse(0);
        map.put("incrementAmount", incrementAmount);
        //获取来源存量总计
        Integer stockAmount = sourceStockList.stream().map(customerVo -> customerVo.getCount())
                .collect(Collectors.toList()).stream().reduce(Integer::sum).orElse(0);
        map.put("stockAmount", stockAmount);

        Map<String, List<CustomerVo>> sourceIncrementMap = sourceIncrementList.stream()
                .collect(Collectors.groupingBy(CustomerVo::getSource));

        Map<String, List<CustomerVo>> sourceStockMap = sourceStockList.stream().collect(Collectors.groupingBy(Customer::getSource));

        if (dictTenantDataVos.size()==0){
            throw new ServiceException("数据有误：该租户没有配客户来源字典");
        }

        //赋值 增量 存量数据
        for (DictTenantDataVo dictTenantDataVo : dictTenantDataVos) {
            //存放来源存量与增量的数据
            Map<String,Object>  stockMap = new HashMap<>();
            //赋值初使值
            stockMap.put("source",dictTenantDataVo.getDictValue());
            stockMap.put("stockAmount",0);
            stockMap.put("incrementAmount",0);

            //赋值增量数据
            List<CustomerVo> customers = sourceIncrementMap.get(dictTenantDataVo.getDictKey());
            if (ObjectUtil.isNotEmpty(customers)){
                stockMap.put("incrementAmount",customers.get(0).getCount());
            }
            //赋值存量数据
            List<CustomerVo> customerVos = sourceStockMap.get(dictTenantDataVo.getDictKey());
            if (ObjectUtil.isNotEmpty(customerVos)){
                stockMap.put("stockAmount",customerVos.get(0).getCount());
            }
            stockList.add(stockMap);
        }
        map.put("stockList", stockList);

        return map;
    }

    /**
     * 类型统计（客户分析）
     */
    @Override
    public Map<String, Object> typeStatistics(CustomerDto dto) {
        if (ObjectUtil.isEmpty(dto.getBeginTime()) && ObjectUtil.isNotEmpty(dto.getEndTime())) {
            throw new ServiceException("参数缺失：开始时间，结束时间不能为null");
        }
        //存放类型统计数据
        Map<String, Object> map = new HashMap<>();

        //存放询盘数据 成交单的列表
        List<Map<String,Object>> saleList = new ArrayList<>();

        QueryWrapper<Object> query = Wrappers.query();
        query.ge("DATE_FORMAT(sq.create_time,'%Y-%m')", dto.getBeginTime());
        query.le("DATE_FORMAT(sq.create_time,'%Y-%m')", dto.getEndTime());
        query.eq(ObjectUtil.isNotEmpty(dto.getCountryId()), "c.country_id", dto.getCountryId());
        query.eq(ObjectUtil.isNotEmpty(dto.getProvinceId()), "c.province_id", dto.getProvinceId());
        query.eq(ObjectUtil.isNotEmpty(dto.getCityId()), "c.city_id", dto.getCityId());
        query.eq("sq.status", 30);
        //类型询盘统计（报价）
        List<SaleQuotationVo> saleStatisticsList = baseMapper.saleStatistics(query);
        Map<String, List<SaleQuotationVo>> saleStatisticsMap = saleStatisticsList.stream().collect(Collectors.groupingBy(SaleQuotationVo::getStatus));

        //查询成交单统计（合同）
        List<SaleQuotationVo> contractStatisticsList = contractService.contractStatistics(query);
        Map<String, List<SaleQuotationVo>> contractStatisticsMap = contractStatisticsList.stream()
                .collect(Collectors.groupingBy(SaleQuotationVo::getStatus));

        //获取询盘总计
        Integer saleAmount = saleStatisticsList.stream().map(saleQuotationVo -> saleQuotationVo.getCount())
                .reduce(Integer::sum).orElse(0);

        //获取成交单总计
        Integer contractAmount = contractStatisticsList.stream().map(saleQuotationVo -> saleQuotationVo.getCount())
                .reduce(Integer::sum).orElse(0);

        map.put("saleAmount", saleAmount);
        map.put("contractAmount", contractAmount);

        //获取类型的字典数据
        DynamicDataSourceContextHolder.push(SourceConstant.BASE);
        List<DictTenantDataVo> dictTenantDataVoList = getDict("customer_status");

        if (dictTenantDataVoList.size()==0){
            throw new ServiceException("数据有误：该租户没有配客户类型字典");
        }

        //赋值询盘 成交单数据
        for (DictTenantDataVo dictTenantDataVo : dictTenantDataVoList) {
            //存放询盘数据 成交单的数据
            Map<String, Object> saleMap = new HashMap<>();
            //赋值初使值
            saleMap.put("status",dictTenantDataVo.getDictValue());
            saleMap.put("contractAmount",0);
            saleMap.put("saleAmount",0);

            //赋值询盘数据
            List<SaleQuotationVo> saleQuotationVoList = saleStatisticsMap.get(dictTenantDataVo.getDictKey());
            if (ObjectUtil.isNotEmpty(saleQuotationVoList)){
                saleMap.put("saleAmount",saleQuotationVoList.get(0).getCount());
            }

            //赋值成交单数据
            List<SaleQuotationVo> saleQuotationList = contractStatisticsMap.get(dictTenantDataVo.getDictKey());
            if (ObjectUtil.isNotEmpty(saleQuotationList)){
                saleMap.put("contractAmount",saleQuotationList.get(0).getCount());
            }
            saleList.add(saleMap);
        }
        map.put("saleList", saleList);
        return map;
    }

    /**
     * 趋势分析（客户分析）
     */
    @Override
    public Map<String, Object> trendAnalysis(CustomerDto dto) {
        Map<String, Object> map = new HashMap<>();
        //切换数据源
        DynamicDataSourceContextHolder.push(SourceConstant.CUSTOMER);

        Date now = new Date();
        //获取当前月数往前12月的月份数
        List<String> monthBetweenDate = DateUtils.getMonthBetweenDate(DateUtil.offsetMonth(now, -12), now);
        dto.setBeginTime(DateUtil.format(DateUtil.offsetMonth(now, -12), "yyyy-MM"));
        dto.setEndTime(DateUtil.format(now, "yyyy-MM"));
        //来源存量（在指定时间外的数量）
        QueryWrapper<Object> query = Wrappers.query();
        query.eq(ObjectUtil.isNotEmpty(dto.getCountryId()), "c.country_id", dto.getCountryId());
        query.eq(ObjectUtil.isNotEmpty(dto.getProvinceId()), "c.province_id", dto.getProvinceId());
        query.eq(ObjectUtil.isNotEmpty(dto.getCityId()), "c.city_id", dto.getCityId());
        query.groupBy("DATE_FORMAT(c.create_time,'%Y-%m')");
        query.le("DATE_FORMAT(c.create_time,'%Y-%m')", dto.getBeginTime());
        query.ge("DATE_FORMAT(c.create_time,'%Y-%m')", dto.getEndTime());
//        query.select("count(*) count, DATE_FORMAT(c.create_time,'%Y-%m') time");
        List<CustomerVo> sourceStockList = customerService.sourceStock(query);
        Map<String, List<CustomerVo>> sourceStockMap = sourceStockList.stream().collect(Collectors.groupingBy(CustomerVo::getTime));

        //来源增量（在指定时间内的数量）
        query.clear();
        query.eq(ObjectUtil.isNotEmpty(dto.getCountryId()), "c.country_id", dto.getCountryId());
        query.eq(ObjectUtil.isNotEmpty(dto.getProvinceId()), "c.province_id", dto.getProvinceId());
        query.eq(ObjectUtil.isNotEmpty(dto.getCityId()), "c.city_id", dto.getCityId());
        query.groupBy("DATE_FORMAT(c.create_time,'%Y-%m')");
        query.ge("DATE_FORMAT(c.create_time,'%Y-%m')", dto.getBeginTime());
        query.le("DATE_FORMAT(c.create_time,'%Y-%m')", dto.getEndTime());
//        query.select("count(*) count, DATE_FORMAT(c.create_time,'%Y-%m') time");
        List<CustomerVo> sourceIncrementList = customerService.sourceIncrement(query);
        Map<String, List<CustomerVo>> sourceIncrementMap = sourceIncrementList.stream().collect(Collectors.groupingBy(CustomerVo::getTime));
        DynamicDataSourceContextHolder.poll();

        query.clear();
        query.ge("DATE_FORMAT(sq.create_time,'%Y-%m')", dto.getBeginTime());
        query.le("DATE_FORMAT(sq.create_time,'%Y-%m')", dto.getBeginTime());
        query.eq(ObjectUtil.isNotEmpty(dto.getCountryId()), "c.country_id", dto.getCountryId());
        query.eq(ObjectUtil.isNotEmpty(dto.getProvinceId()), "c.province_id", dto.getProvinceId());
        query.eq(ObjectUtil.isNotEmpty(dto.getCityId()), "c.city_id", dto.getCityId());
        query.eq("sq.status", 30);
        query.groupBy("DATE_FORMAT(sq.create_time,'%Y-%m')");
//        query.select("count(*), DATE_FORMAT(sq.create_time,'%Y-%m') time");
        //类型询盘统计（报价）
        List<SaleQuotationVo> saleStatisticsList = baseMapper.saleStatistics(query);
        Map<String, List<SaleQuotationVo>> saleStatisticsMap = saleStatisticsList.stream()
                .collect(Collectors.groupingBy(SaleQuotationVo::getTime));

        //查询成交单统计（合同）
        List<SaleQuotationVo> contractStatisticsList = contractService.contractStatistics(query);
        Map<String, List<SaleQuotationVo>> contractStatisticsMap = contractStatisticsList.stream()
                .collect(Collectors.groupingBy(SaleQuotationVo::getTime));

        //存放来源存量
        List<Map<String, Object>> sourceStockList1 = new ArrayList<>();
        //存放来源增量
        List<Map<String, Object>> sourceIncrementList1 = new ArrayList<>();
        //存放类型询盘
        List<Map<String, Object>> saleStatisticList1 = new ArrayList<>();
        //类型成交单
        List<Map<String, Object>> contractStatisticsList1 = new ArrayList<>();

        //赋值
        for (String month : monthBetweenDate) {
            //存放来源存量
            Map<String, Object> stockMap = new HashMap<>();
            //设置默认值
            stockMap.put("month", month);
            stockMap.put("count", 0);
            List<CustomerVo> sourceStocks = sourceStockMap.get(month);
            if (ObjectUtil.isNotEmpty(sourceStocks)) {
                stockMap.put("count", sourceStocks.get(0).getCount());
            }
            sourceStockList1.add(stockMap);

            //存放来源增量
            Map<String, Object> incrementMap = new HashMap<>();
            //设置默认值
            incrementMap.put("month", month);
            incrementMap.put("count", 0);
            List<CustomerVo> sourceIncrements = sourceIncrementMap.get(month);
            if (ObjectUtil.isNotEmpty(sourceIncrements)) {
                incrementMap.put("count", sourceIncrements.get(0).getCount());
            }
            sourceIncrementList1.add(incrementMap);

            //类型询盘
            Map<String, Object> saleMap = new HashMap<>();
            //设置默认值
            saleMap.put("month", month);
            saleMap.put("count", 0);
            List<SaleQuotationVo> saleList = saleStatisticsMap.get(month);
            if (ObjectUtil.isNotEmpty(saleList)) {
                saleMap.put("count", saleList.get(0).getCount());
            }
            saleStatisticList1.add(saleMap);

            //类型成交单
            Map<String, Object> contractMap = new HashMap<>();
            //设置默认值
            contractMap.put("month", month);
            contractMap.put("count", 0);
            List<SaleQuotationVo> contractList = contractStatisticsMap.get(month);
            if (ObjectUtil.isNotEmpty(contractList)) {
                contractMap.put("count", contractList.get(0).getCount());
            }
            contractStatisticsList1.add(contractMap);
        }
        map.put("sourceStockList", sourceStockList1);
        map.put("sourceIncrementList", sourceIncrementList1);
        map.put("saleStatisticList", saleStatisticList1);
        map.put("contractStatisticsList", contractStatisticsList1);
        return map;
    }

    /**
     * 赋值买卖公司地址
     */
    private void setArea(SaleQuotationVo saleQuotationVo) {

        List<Long> areaIdList = Arrays.asList(
                saleQuotationVo.getBuyCountryId(),
                saleQuotationVo.getBuyProvinceId(),
                saleQuotationVo.getBuyCityId()
        );
        Map<Long, String> areaMap = CustomizeAreaUtil.getAreaMapByIds(areaIdList);
        saleQuotationVo.setBuyCountryName(areaMap.get(saleQuotationVo.getBuyCountryId()));
        saleQuotationVo.setBuyProvinceName(areaMap.get(saleQuotationVo.getBuyProvinceId()));
        saleQuotationVo.setBuyCityName(areaMap.get(saleQuotationVo.getBuyCityId()));
        saleQuotationVo.setBuyDetailedAddress(saleQuotationVo.getBuyAddress());
    }


    //根据字典编码获取字典的数据
    private List<DictTenantDataVo> getDict(String code){
        DictTenantDataSelectDto dto = new DictTenantDataSelectDto();
        dto.setDictCode(code);
        return dictTenantDataService.getList(dto);
    }


}
