package com.czg.service.product.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.czg.account.entity.PlatformDict;
import com.czg.account.entity.SysDict;
import com.czg.product.service.HomePageService;
import com.czg.account.service.PlatformDictService;
import com.czg.product.service.SysDictService;
import com.czg.account.vo.home.BannerInfoVo;
import com.czg.account.vo.home.HomePageVO;
import com.czg.account.vo.home.HotRankingVO;
import com.czg.enums.LogoEnum;
import com.czg.product.entity.Product;
import com.czg.product.service.ProdSkuService;
import com.czg.product.service.ProductService;
import com.czg.product.vo.RecommendProVO;
import com.czg.service.product.mapper.ProductMapper;
import com.czg.utils.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

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

/**
 * @author Administrator
 */
@Service
public class HomePageServiceImpl implements HomePageService {
    private static final Character[] CHINESE_CHARS_ARRAY = {
            '你', '我', '他', '她', '它', '们', '这', '里', '那', '里', '多', '少', '是', '否',
            '好', '坏', '快', '慢', '上', '下', '左', '右', '前', '后', '高', '低', '大', '小',
            '长', '短', '方', '圆', '胖', '瘦', '黑', '白', '红', '绿', '蓝', '黄', '紫', '粉',
            '红', '桔', '红', '橙', '黄', '棕', '灰', '褐'
    };


    @DubboReference
    private PlatformDictService platformDictService;

    @Resource
    private SysDictService sysDictService;
    @Resource
    private ProductService productService;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProdSkuService prodSkuService;

    /**
     * 小条幅随机数据
     * @return
     */
    private List<BannerInfoVo> bannerVoRandom(){
        List<BannerInfoVo> bannerInfoList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            String nickName = RandomUtil.randomEle(CHINESE_CHARS_ARRAY) + "***";
            BannerInfoVo bannerInfoVo = new BannerInfoVo();
            bannerInfoVo.setName(nickName);
            bannerInfoVo.setLogo(LogoEnum.getValueByKey(RandomUtil.randomInt()));
            // 生成 0 到 1000 之间的随机金额，保留两位小数
            double amount = RandomUtil.randomDouble(0, 1000);
            // 格式化为两位小数
            bannerInfoVo.setMoney(String.format("%.2f", amount));
            bannerInfoList.add(bannerInfoVo);
        }
        return bannerInfoList;
    }

    @Override
    public HomePageVO getHomeInfo() {
        HomePageVO homeUpVO = new HomePageVO();
        //轮播图

        List<PlatformDict> platformDictList =  platformDictService.list(new QueryWrapper().in(PlatformDict::getType, "carousel", "homeDistrict"));;
        Map<String, List<PlatformDict>> groupedByType = platformDictList.stream()
                .collect(Collectors.groupingBy(PlatformDict::getType));
        //轮播图
        homeUpVO.setBannerList(groupedByType.get("carousel"));
        //金刚区
        homeUpVO.setDistrict(groupedByType.get("homeDistrict"));
        //小条幅
        homeUpVO.setFreeBannerList(bannerVoRandom());

        // 销量榜
        List<RecommendProVO> productList = productMapper.selectCouponProBySaleNum();
        HotRankingVO hotRankingVO = new HotRankingVO();
        hotRankingVO.setHotList(productList);
        homeUpVO.setHotRanking(hotRankingVO);

        List<SysDict> sysDictList = sysDictService.list(new QueryWrapper().eq(SysDict::getType, "home").eq(SysDict::getStatus, 1));
        for (SysDict sysDict : sysDictList) {
            if (sysDict.getIsChild().equals(1)) {
                sysDict.setDetail(sysDictService.list(new QueryWrapper().eq(SysDict::getReleId, sysDict.getId()).eq(SysDict::getStatus, 1)));
            }
            sysDict.setIsChild((sysDict.getIsChild() == null || sysDict.getIsChild().equals(0)) ? 0 : 1);
        }
        homeUpVO.setMenuList(sysDictList);

        HotRankingVO tody = new HotRankingVO();
        tody.setHotList(productMapper.getTodayProInfo());
        tody.setName("今日上线");
        tody.setDate("十点更新");
        homeUpVO.setTodayProInfo(hotRankingVO);

        return homeUpVO;
    }

    @Override
    public Page<RecommendProVO> getProd(String lng, String lat, String address, Integer classify, Integer orderType, Integer distanceType, String name) {
        PageHelper.startPage(PageUtil.buildPageHelp());
        return PageUtil.convert(new PageInfo<>(productMapper.selectRecommendProductList(lng, lat, address, classify, orderType, distanceType, name)));
    }
}
