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

import com.slodon.b2b2c.core.constant.GoodsConst;
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.TimeUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.enums.StatsTimeType;
import com.slodon.b2b2c.goods.example.GoodsBrandExample;
import com.slodon.b2b2c.goods.example.GoodsExample;
import com.slodon.b2b2c.model.goods.GoodsBrandModel;
import com.slodon.b2b2c.model.goods.GoodsModel;
import com.slodon.b2b2c.model.statistics.GoodsDayModel;
import com.slodon.b2b2c.model.statistics.PlatformDayModel;
import com.slodon.b2b2c.statistics.example.GoodsDayExample;
import com.slodon.b2b2c.statistics.example.PlatformDayExample;
import com.slodon.b2b2c.statistics.pojo.PlatformDay;
import com.slodon.b2b2c.statistics.pojo.base.BrandBase;
import com.slodon.b2b2c.statistics.pojo.base.GoodsBase;
import com.slodon.b2b2c.statistics.pojo.base.PlatformBase;
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.*;

@Api(tags = "admin-商品动销")
@RestController
@RequestMapping("v3/statistics/admin/goods/analysis")
public class AdminGoodsAnalysisController extends BaseController {

    @Resource
    private PlatformDayModel platformDayModel;
    @Resource
    private GoodsDayModel goodsDayModel;
    @Resource
    private StatsModelUtil statsModelUtil;
    @Resource
    private GoodsModel goodsModel;
    @Resource
    private GoodsBrandModel goodsBrandModel;

    @ApiOperation("商品总览(商品总数和品牌总数)")
    @GetMapping("goodsBrandNum")
    public JsonResult<GoodsBrandNumVO> goodsBrandNum(HttpServletRequest request,String webSite) {
        GoodsBrandNumVO vo = new GoodsBrandNumVO();
        GoodsExample example = new GoodsExample();
        example.setStateNotEquals(GoodsConst.GOODS_STATE_DELETE);
        example.setWebSite(webSite);
        vo.setGoodsTotalNum(goodsModel.getGoodsCount(example));
        GoodsBrandExample brandExample = new GoodsBrandExample();
        brandExample.setStateNotEquals(GoodsConst.BRAND_STATE_4);
        brandExample.setWebSite(webSite);
        vo.setBrandTotalNum(goodsBrandModel.getBrandCount(brandExample));
        return SldResponse.success(vo);
    }

    @ApiOperation("商品总览")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webSite", value = "站点"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true)
    })
    @GetMapping("goodsOverview")
    public JsonResult<GoodsOverviewVO> goodsOverview(HttpServletRequest request, String webSite,Date startTime, Date endTime) {
        StatsTimeType timeType = StatsTimeType.checkTimeType(startTime, endTime, false);
        List<? extends PlatformBase> list = statsModelUtil.getPlatformList(timeType, webSite, startTime, endTime);
        GoodsOverviewVO vo = new GoodsOverviewVO();
        if (!CollectionUtils.isEmpty(list)) {
            PlatformBase platformBase = list.get(0);
            //上期数据
            PlatformBase previousData = null;
            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);
            }
            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 GoodsOverviewVO(platformBase, previousData);
            }
        }
        return SldResponse.success(vo);
    }

    @ApiOperation("商品动销趋势")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "webSite", value = "站点")
    })
    @GetMapping("goodsSalesTrend")
    public JsonResult<GoodsSalesTrendVO> goodsSalesTrend(HttpServletRequest request, Date startTime, Date endTime,@RequestParam(value = "webSite",required = false,defaultValue = "1") String webSite) {
        StatsTimeType timeType = StatsTimeType.checkTimeType(startTime, endTime, true);
        List<MovableGoodsVO> list = statsModelUtil.getPlatformTrendList(timeType, startTime, endTime, webSite);
        GoodsSalesTrendVO vo = new GoodsSalesTrendVO();
        //新增商品数列表
        List<MovableGoodsVO> newGoodsList = new ArrayList<>();
        //动销商品数列表
        List<MovableGoodsVO> movableGoodsList = new ArrayList<>();
        //下单数列表
        List<MovableGoodsVO> orderNumList = new ArrayList<>();
        //支付订单数列表
        List<MovableGoodsVO> orderPayNumList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(trendVO -> {
                //新增商品数
                MovableGoodsVO newGoods = new MovableGoodsVO();
                newGoods.setStatsTime(trendVO.getStatsTime());
                newGoods.setNewGoodsNum(trendVO.getNewGoodsNum());
                newGoodsList.add(newGoods);
                //动销商品数
                MovableGoodsVO movableGoods = new MovableGoodsVO();
                movableGoods.setStatsTime(trendVO.getStatsTime());
                movableGoods.setMovableGoodsNum(trendVO.getMovableGoodsNum());
                movableGoodsList.add(movableGoods);
                //下单数
                MovableGoodsVO orderNum = new MovableGoodsVO();
                orderNum.setStatsTime(trendVO.getStatsTime());
                orderNum.setOrderSubmitNum(trendVO.getOrderSubmitNum());
                orderNumList.add(orderNum);
                //支付订单数
                MovableGoodsVO orderPayNum = new MovableGoodsVO();
                orderPayNum.setStatsTime(trendVO.getStatsTime());
                orderPayNum.setOrderPayNum(trendVO.getOrderPayNum());
                orderPayNumList.add(orderPayNum);
            });
            vo.setNewGoodsList(newGoodsList);
            vo.setMovableGoodsList(movableGoodsList);
            vo.setOrderNumList(orderNumList);
            vo.setOrderPayNumList(orderPayNumList);
        }
        return SldResponse.success(vo);
    }

    @ApiOperation("品牌销售排行-TOP10")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webSite", value = "站点"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "sort", value = "排序:1-销售额；2-销量 默认不传按销售额降序")
    })
    @GetMapping("brandSalesRank")
    public JsonResult<List<BrandRankVO>> brandSalesRank(HttpServletRequest request,String webSite, Date startTime, Date endTime, Integer sort) {
        StatsTimeType timeType = StatsTimeType.checkTimeType(startTime, endTime, false);
        //默认查询前十条数据
        PagerInfo pager = new PagerInfo(10, 1);
        List<? extends BrandBase> list = statsModelUtil.getBrandList(timeType, webSite, startTime, endTime, sort, pager);
        List<BrandRankVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(brandBase -> {
                //销量和销售额二者必须有一个满足条件
                if (brandBase.getSaleNum() > 0 || brandBase.getSaleAmount().compareTo(BigDecimal.ZERO) > 0) {
                    vos.add(new BrandRankVO(brandBase));
                }
            });
        }
        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<GoodsChangeTrendVO>> goodsSalesRank(HttpServletRequest request, Date startTime, Date endTime,@RequestParam(name = "webSite", required = false,defaultValue = "1") String webSite, Integer sort) {
        StatsTimeType timeType = StatsTimeType.checkTimeType(startTime, endTime, true);
        //如果查询24小时，先按天查询前十条数据
        StatsTimeType timeType2 = timeType;
        if (timeType == StatsTimeType.HOUR) {
            timeType2 = StatsTimeType.DAY;
        }
        List<? extends GoodsBase> list = statsModelUtil.getGoodsList(timeType2, startTime, endTime,webSite, sort, "sale", null);
        List<GoodsChangeTrendVO> vos = getGoodsSalesRank(list, timeType, startTime, endTime);
        return SldResponse.success(vos);
    }

    @ApiOperation("商品收藏数排行-TOP10")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "webSite", value = "站点")
    })
    @GetMapping("goodsCollectionRank")
    public JsonResult<List<GoodsCollectionRankVO>> goodsCollectionRank(HttpServletRequest request, Date startTime, Date endTime,@RequestParam(name = "webSite", required = false,defaultValue = "1") String webSite) {
        StatsTimeType timeType = StatsTimeType.checkTimeType(startTime, endTime, false);
        List<? extends GoodsBase> list = statsModelUtil.getGoodsList(timeType, startTime, endTime, webSite,1, "collection", null);
        List<GoodsCollectionRankVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(goodsBase -> {
                if (goodsBase.getCollectionNum() > 0) {
                    vos.add(new GoodsCollectionRankVO(goodsBase));
                }
            });
        }
        return SldResponse.success(vos);
    }

    @ApiOperation("商品报表(按天)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webSite", value = "站点"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "type", value = "排序类型；desc-降序;asc-升序;默认类型为降序"),
            @ApiImplicitParam(name = "sort", value = "排序:stats_time-时间;new_goods_num-新增商品数;saling_goods_num-动销商品数;" +
                    "order_submit_num-下单数;order_pay_num-支付订单数;默认按时间降序排序"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("dayReport")
    public JsonResult<PageVO<GoodsReportVO>> dayReport(HttpServletRequest request,
                                                       String webSite,
                                                       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);
        List<GoodsReportVO> vos = new ArrayList<>();
        PlatformDayExample example = new PlatformDayExample();
        example.setStatsTimeAfter(startTime);
        example.setStatsTimeBefore(endTime);
        example.setWebSite(webSite);
        example.setOrderBy(sort + " " + type);
        List<PlatformDay> list = platformDayModel.getPlatformDayList(example, pager);
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(platformDay -> {
                vos.add(new GoodsReportVO(platformDay));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("商品报表(按商品)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "goodsName", value = "商品名称"),
            @ApiImplicitParam(name = "type", value = "排序类型；desc-降序;asc-升序;默认类型为降序"),
            @ApiImplicitParam(name = "sort", value = "排序:visitorNum-访客数;goodsPrice-价格;saleNum-销量;saleAmount-支付金额;" +
                    "orderSubmitAmount-下单金额;orderPayNum-支付订单数;orderSubmitNum-下单数;viewNum-浏览量;collectionNum-收藏人数;" +
                    "addCartMemberNum-加购人数;returnNum-退单数;returnAmount-退单金额;默认按访客数降序排序"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("goodsReport")
    public JsonResult<PageVO<AdminGoodsReportVO>> goodsReport(HttpServletRequest request, Date startTime, Date endTime, String goodsName,
                                                              @RequestParam(value = "type", required = false, defaultValue = "desc") String type,
                                                              @RequestParam(value = "sort", required = false, defaultValue = "visitorNum") String sort) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        GoodsDayExample example = new GoodsDayExample();
        example.setStatsTimeAfter(startTime);
        example.setStatsTimeBefore(endTime);
        example.setGoodsNameLike(goodsName);
        example.setOrderBy(sort + " " + type);
        List<GoodsBase> list = goodsDayModel.getGoodsList(example, pager);
        List<AdminGoodsReportVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(goodsBase -> {
                vos.add(new AdminGoodsReportVO(goodsBase));
            });
        }
        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-时间;new_goods_num-新增商品数;saling_goods_num-动销商品数;" +
                    "order_submit_num-下单数;order_pay_num-支付订单数;默认按时间降序排序")
    })
    @GetMapping("dayExport")
    public JsonResult dayExport(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.goodsReportExport(request, response, example);
        return null;
    }

    @ApiOperation("导出报表(按商品)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "goodsName", value = "商品名称"),
            @ApiImplicitParam(name = "type", value = "排序类型；desc-降序;asc-升序;默认类型为降序"),
            @ApiImplicitParam(name = "sort", value = "排序:visitorNum-访客数;goodsPrice-价格;saleNum-销量;saleAmount-支付金额;" +
                    "orderSubmitAmount-下单金额;orderPayNum-支付订单数;orderSubmitNum-下单数;viewNum-浏览量;collectionNum-收藏人数;" +
                    "addCartMemberNum-加购人数;returnNum-退单数;returnAmount-退单金额;默认按访客数降序排序")
    })
    @GetMapping("goodsExport")
    public JsonResult goodsExport(HttpServletRequest request, HttpServletResponse response, Date startTime, Date endTime, String goodsName,
                                  @RequestParam(value = "type", required = false, defaultValue = "desc") String type,
                                  @RequestParam(value = "sort", required = false, defaultValue = "visitorNum") String sort) {
        GoodsDayExample example = new GoodsDayExample();
        example.setStatsTimeAfter(startTime);
        example.setStatsTimeBefore(endTime);
        example.setGoodsNameLike(goodsName);
        example.setOrderBy(sort + " " + type);
        goodsDayModel.goodsReportExport(request, response, example);
        return null;
    }

    /**
     * 变化趋势
     *
     * @param list
     * @return
     */
    public List<GoodsChangeTrendVO> getGoodsSalesRank(List<? extends GoodsBase> list, StatsTimeType timeType, Date startTime, Date endTime) {
        List<GoodsChangeTrendVO> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            //按照统计时间分组
            Map<String, List<GoodsChangeTrendVO.GoodsTrendVO>> timeMap = new HashMap<>();
            //商品id集合
            StringBuilder goodsIds = new StringBuilder();
            for (GoodsBase goodsBase : list) {
                goodsIds.append(",").append(goodsBase.getGoodsId());
            }
            //查询销售信息
            List<SalesTrendVO> trendList = statsModelUtil.getGoodsTrendList(timeType, startTime, endTime, goodsIds.toString().substring(1));
            if (!CollectionUtils.isEmpty(trendList)) {
                switch (timeType) {
                    case HOUR:
                        timeMap = getGoodsHourList(trendList);
                        break;
                    case DAY:
                    case WEEK:
                    case MONTH:
                        timeMap = getGoodsDayList(trendList, startTime, endTime);
                        break;
                    case YEAR:
                        timeMap = getGoodsMonthList(trendList);
                        break;
                }
            }
            timeMap.forEach((statsTime, goodsList) -> {
                GoodsChangeTrendVO vo = new GoodsChangeTrendVO();
                vo.setStatsTime(statsTime);
                vo.setGoodsList(goodsList);
                result.add(vo);
            });
            Collections.sort(result);
            if (!CollectionUtils.isEmpty(result)) {
                //销售额map
                Map<Long/*商品id*/, BigDecimal> saleAmountMap = new HashMap<>();
                //销量map
                Map<Long/*商品id*/, Integer> saleNumMap = new HashMap<>();
                for (int i = 0; i < result.size(); i++) {
                    GoodsChangeTrendVO vo = result.get(i);
                    vo.getGoodsList().forEach(goodsTrendVO -> {
                        if (saleAmountMap.containsKey(goodsTrendVO.getGoodsId())) {
                            goodsTrendVO.setSaleAmount(goodsTrendVO.getSaleAmount().add(saleAmountMap.get(goodsTrendVO.getGoodsId())));
                            goodsTrendVO.setSaleNum(goodsTrendVO.getSaleNum() + saleNumMap.get(goodsTrendVO.getGoodsId()));

                            saleAmountMap.put(goodsTrendVO.getGoodsId(), goodsTrendVO.getSaleAmount());
                            saleNumMap.put(goodsTrendVO.getGoodsId(), goodsTrendVO.getSaleNum());
                        } else {
                            //进入这里说明是第一个元素，默认第一个元素值
                            saleAmountMap.put(goodsTrendVO.getGoodsId(), goodsTrendVO.getSaleAmount());
                            saleNumMap.put(goodsTrendVO.getGoodsId(), goodsTrendVO.getSaleNum());
                        }
                    });
                }
            }
        }
        return result;
    }

    /**
     * 获取24小时数据
     *
     * @param list
     * @return
     */
    public Map<String, List<GoodsChangeTrendVO.GoodsTrendVO>> getGoodsHourList(List<SalesTrendVO> list) {
        Map<String, List<GoodsChangeTrendVO.GoodsTrendVO>> result = new HashMap<>();
        if (CollectionUtils.isEmpty(list)) {
            return result;
        }
        //构造现有商品数据
        Map<Long, SalesTrendVO> goodsMap = new HashMap<>();
        List<Long> goodsIdList = new ArrayList<>();
        for (SalesTrendVO trendVO : list) {
            goodsMap.put(trendVO.getGoodsId(), trendVO);
            goodsIdList.add(trendVO.getGoodsId());
        }
        //构造现有时间段数据
        Map<String, SalesTrendVO> map = new HashMap<>();
        list.forEach(salesTrendVO -> {
            map.put(salesTrendVO.getStatsTime() + "," + salesTrendVO.getGoodsId(), salesTrendVO);
        });
        //构造全部时间段数据
        List<String> timeGoodsIdsList = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            String timeStr = i > 9 ? i + ":00" : "0" + i + ":00";
            goodsIdList.forEach(goodsId -> {
                timeGoodsIdsList.add(timeStr + "," + goodsId);
            });
        }
        //补充缺少的时间段数据
        timeGoodsIdsList.forEach(timeGoodsId -> {
            if (!map.containsKey(timeGoodsId)) {
                SalesTrendVO trendVO = goodsMap.get(Long.parseLong(timeGoodsId.split(",")[1]));
                SalesTrendVO salesTrendVO = new SalesTrendVO(trendVO.getGoodsId(), trendVO.getGoodsName());
                salesTrendVO.setOrderPayAmount(new BigDecimal("0.00"));
                salesTrendVO.setOrderPayNum(0);
                map.put(timeGoodsId, salesTrendVO);
            }
        });
        //遍历全部时间段数据
        map.forEach((timeGoodsId, vo) -> {
            GoodsChangeTrendVO.GoodsTrendVO goodsTrendVO = new GoodsChangeTrendVO.GoodsTrendVO();
            goodsTrendVO.setGoodsId(vo.getGoodsId());
            goodsTrendVO.setGoodsName(vo.getGoodsName());
            goodsTrendVO.setSaleAmount(vo.getOrderPayAmount());
            goodsTrendVO.setSaleNum(vo.getOrderPayNum());

            String timeStr = timeGoodsId.split(",")[0];
            //按照统计时间分组
            if (result.containsKey(timeStr)) {
                result.get(timeStr).add(goodsTrendVO);
            } else {
                //商品列表
                List<GoodsChangeTrendVO.GoodsTrendVO> goodsList = new ArrayList<>();
                goodsList.add(goodsTrendVO);

                result.put(timeStr, goodsList);
            }
        });
        return result;
    }

    /**
     * 获取n天数据
     *
     * @param list
     * @param startTime
     * @param endTime
     * @return
     */
    public Map<String, List<GoodsChangeTrendVO.GoodsTrendVO>> getGoodsDayList(List<SalesTrendVO> list, Date startTime, Date endTime) {
        Map<String, List<GoodsChangeTrendVO.GoodsTrendVO>> result = new HashMap<>();
        if (CollectionUtils.isEmpty(list)) {
            return result;
        }
        //构造现有商品数据
        Map<Long, SalesTrendVO> goodsMap = new HashMap<>();
        List<Long> goodsIdList = new ArrayList<>();
        for (SalesTrendVO trendVO : list) {
            goodsMap.put(trendVO.getGoodsId(), trendVO);
            goodsIdList.add(trendVO.getGoodsId());
        }
        //构造现有时间段数据
        Map<String, SalesTrendVO> map = new HashMap<>();
        list.forEach(salesTrendVO -> {
            map.put(salesTrendVO.getStatsTime() + "," + salesTrendVO.getGoodsId(), salesTrendVO);
        });
        //构造全部时间段数据
        List<String> timeGoodsIdsList = new ArrayList<>();
        for (Date i = startTime; i.before(endTime); i = TimeUtil.getDayAgoDate(i, 1)) {
            String timeStr = TimeUtil.dealMonthAndDay(i);
            goodsIdList.forEach(goodsId -> {
                timeGoodsIdsList.add(timeStr + "," + goodsId);
            });
        }
        //补充缺少的时间段数据
        timeGoodsIdsList.forEach(timeGoodsId -> {
            if (!map.containsKey(timeGoodsId)) {
                SalesTrendVO trendVO = goodsMap.get(Long.parseLong(timeGoodsId.split(",")[1]));
                SalesTrendVO salesTrendVO = new SalesTrendVO(trendVO.getGoodsId(), trendVO.getGoodsName());
                salesTrendVO.setOrderPayAmount(new BigDecimal("0.00"));
                salesTrendVO.setOrderPayNum(0);
                map.put(timeGoodsId, salesTrendVO);
            }
        });
        //遍历全部时间段数据
        map.forEach((timeGoodsId, vo) -> {
            GoodsChangeTrendVO.GoodsTrendVO goodsTrendVO = new GoodsChangeTrendVO.GoodsTrendVO();
            goodsTrendVO.setGoodsId(vo.getGoodsId());
            goodsTrendVO.setGoodsName(vo.getGoodsName());
            goodsTrendVO.setSaleAmount(vo.getOrderPayAmount());
            goodsTrendVO.setSaleNum(vo.getOrderPayNum());

            String timeStr = timeGoodsId.split(",")[0];
            //按照统计时间分组
            if (result.containsKey(timeStr)) {
                result.get(timeStr).add(goodsTrendVO);
            } else {
                //商品列表
                List<GoodsChangeTrendVO.GoodsTrendVO> goodsList = new ArrayList<>();
                goodsList.add(goodsTrendVO);

                result.put(timeStr, goodsList);
            }
        });
        return result;
    }

    /**
     * 获取12个月数据
     *
     * @param list
     * @return
     */
    public Map<String, List<GoodsChangeTrendVO.GoodsTrendVO>> getGoodsMonthList(List<SalesTrendVO> list) {
        Map<String, List<GoodsChangeTrendVO.GoodsTrendVO>> result = new HashMap<>();
        if (CollectionUtils.isEmpty(list)) {
            return result;
        }
        //构造现有商品数据
        Map<Long, SalesTrendVO> goodsMap = new HashMap<>();
        List<Long> goodsIdList = new ArrayList<>();
        for (SalesTrendVO trendVO : list) {
            goodsMap.put(trendVO.getGoodsId(), trendVO);
            goodsIdList.add(trendVO.getGoodsId());
        }
        //构造现有时间段数据
        Map<String, SalesTrendVO> map = new HashMap<>();
        list.forEach(salesTrendVO -> {
            map.put(salesTrendVO.getStatsTime() + "," + salesTrendVO.getGoodsId(), salesTrendVO);
        });
        //构造全部时间段数据
        List<String> timeGoodsIdsList = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            String timeStr = i > 9 ? i + "" : "0" + i;
            goodsIdList.forEach(goodsId -> {
                timeGoodsIdsList.add(timeStr + "," + goodsId);
            });
        }
        //补充缺少的时间段数据
        timeGoodsIdsList.forEach(timeGoodsId -> {
            if (!map.containsKey(timeGoodsId)) {
                SalesTrendVO trendVO = goodsMap.get(Long.parseLong(timeGoodsId.split(",")[1]));
                SalesTrendVO salesTrendVO = new SalesTrendVO(trendVO.getGoodsId(), trendVO.getGoodsName());
                salesTrendVO.setOrderPayAmount(new BigDecimal("0.00"));
                salesTrendVO.setOrderPayNum(0);
                map.put(timeGoodsId, salesTrendVO);
            }
        });
        //遍历全部时间段数据
        map.forEach((timeGoodsId, vo) -> {
            GoodsChangeTrendVO.GoodsTrendVO goodsTrendVO = new GoodsChangeTrendVO.GoodsTrendVO();
            goodsTrendVO.setGoodsId(vo.getGoodsId());
            goodsTrendVO.setGoodsName(vo.getGoodsName());
            goodsTrendVO.setSaleAmount(vo.getOrderPayAmount());
            goodsTrendVO.setSaleNum(vo.getOrderPayNum());

            String timeStr = timeGoodsId.split(",")[0];
            //按照统计时间分组
            if (result.containsKey(timeStr)) {
                result.get(timeStr).add(goodsTrendVO);
            } else {
                //商品列表
                List<GoodsChangeTrendVO.GoodsTrendVO> goodsList = new ArrayList<>();
                goodsList.add(goodsTrendVO);

                result.put(timeStr, goodsList);
            }
        });
        return result;
    }
}
