package com.cyksj.service.goods.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.util.Jsons;
import com.cyksj.mapper.GoodsDonSpecMapper;
import com.cyksj.model.dto.RealGoodsConfigDto;
import com.cyksj.model.entity.GoodsDonSpec;
import com.cyksj.model.entity.HomeManagementConfig;
import com.cyksj.model.manage.request.ReqGoodsSkuSpec;
import com.cyksj.model.views.GoodsDonDetailFrontView;
import com.cyksj.model.views.HomeManagementFrontView;
import com.cyksj.service.goods.GoodsDonFrontService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.param.Operator;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目名: yhlxj
 * 文件名: GoodsDonFrontServiceImpl
 * 创建者: JavaZou
 * 创建时间:2023/10/19 17:53
 */
@Service
@RequiredArgsConstructor
public class GoodsDonFrontServiceImpl implements GoodsDonFrontService {

	private final BeanSearcher beanSearcher;

	private final GoodsDonSpecMapper goodsDonSpecMapper;

	@Override
	public List<HomeManagementFrontView> getRealGoodsConfig() throws Exception {
		List<HomeManagementFrontView> realGoodsConfigList = beanSearcher.searchAll(HomeManagementFrontView.class, MapUtils.builder().field(HomeManagementFrontView::getType, HomeManagementConfig.Type.rg.name()).build());
		Map<String, List<RealGoodsConfigDto>> map = new HashMap<>();
		List<ReqGoodsSkuSpec> reqGoodsSkuSpecList = null;
		for (HomeManagementFrontView realGoodsConfig : realGoodsConfigList) {
			if (StrUtil.isEmpty(realGoodsConfig.getTitle())) {
				continue;
			}
			boolean isCtRoute = realGoodsConfig.getTitle().contains("路由器");
			boolean isCtApple = realGoodsConfig.getTitle().contains("Apple");
			if (isCtRoute || isCtApple) {
				List<RealGoodsConfigDto> subInfoList = new ArrayList<>();
				String type = null;
				if (CollUtil.isEmpty(reqGoodsSkuSpecList)) {
					GoodsDonSpec goodsDonSpec = goodsDonSpecMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSpec.class)
							.eq(GoodsDonSpec::getGoodsId, 15).last("limit 1")
							.select(GoodsDonSpec::getSpecsJson));
					reqGoodsSkuSpecList = Jsons.parseList(goodsDonSpec.getSpecsJson(), ReqGoodsSkuSpec.class);
				}
				if (CollUtil.isEmpty(realGoodsConfigList)) {
					continue;
				}
				for (ReqGoodsSkuSpec reqGoodsSkuSpec : reqGoodsSkuSpecList) {
					if (StrUtil.isEmpty(reqGoodsSkuSpec.getKey())) {
						continue;
					}
					boolean subIsCtRoute = reqGoodsSkuSpec.getKey().contains("路由器");
					boolean subIsCtApple = reqGoodsSkuSpec.getKey().contains("机顶盒");
					if ((isCtRoute && subIsCtRoute) || (isCtApple && subIsCtApple)) {
						List<ReqGoodsSkuSpec.Node> values = reqGoodsSkuSpec.getValues();
						for (ReqGoodsSkuSpec.Node value : values) {
							RealGoodsConfigDto subInfo = new RealGoodsConfigDto();
							subInfo.setTitle(value.getValue());
							subInfo.setLogo(value.getLogo());
							subInfo.setPrice(value.getPrice());
							subInfoList.add(subInfo);
						}
					}
				}
				realGoodsConfig.setSubInfoList(subInfoList);
			}
			if (realGoodsConfig.getTitle().contains("潮玩")) {
				List<GoodsDonDetailFrontView> fashionGoodsList = beanSearcher.searchAll(GoodsDonDetailFrontView.class, MapUtils.builder().field(GoodsDonDetailFrontView::getType, 3)
						.field(GoodsDonDetailFrontView::getId, 27).op(Operator.NotEqual)
						.onlySelect(GoodsDonDetailFrontView::getTitle, GoodsDonDetailFrontView::getLogo, GoodsDonDetailFrontView::getMinPrice, GoodsDonDetailFrontView::getId, GoodsDonDetailFrontView::getTags)
						.build());
				if (CollUtil.isEmpty(fashionGoodsList)) {
					continue;
				}
				List<RealGoodsConfigDto> subInfoList = new ArrayList<>();
				for (GoodsDonDetailFrontView fashionGoods : fashionGoodsList) {
					RealGoodsConfigDto subInfo = new RealGoodsConfigDto();
					subInfo.setLogo(fashionGoods.getLogo());
					subInfo.setTitle(fashionGoods.getTitle());
					subInfo.setPrice(fashionGoods.getMinPrice());
					subInfo.setTags(fashionGoods.getTags());
					subInfoList.add(subInfo);
				}
				realGoodsConfig.setSubInfoList(subInfoList);
			}
		}
		return realGoodsConfigList;
	}
}
