package com.cyksj.web.controller.qa;

import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.CustomerConsultationRecordMapper;
import com.cyksj.model.entity.CustomerConsultationRecord;
import com.cyksj.model.views.GoodsQaShowView;
import com.cyksj.web.util.StpUserUtil;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * server/微信客服/咨询问题
 * 项目名: yhlxj2
 * 文件名: CorpKfQaController
 * 创建者: JavaZou
 * 创建时间:2025/2/7 12:02
 */
@RestController
@RequestMapping("/applet/qa")
@RequiredArgsConstructor
public class CorpKfQaController {
	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	private final CustomerConsultationRecordMapper customerConsultationRecordMapper;

	/**
	 * 咨询问题级联
	 */
	@GetMapping("/get/cascade")
	public Result<List<GoodsQaShowView>> getCascade() {
		List<GoodsQaShowView> goodsQaShowViews = beanSearcher.searchAll(GoodsQaShowView.class, MapUtils.builder().build());
		// 按type分组
		Map<Integer, List<GoodsQaShowView>> byType = goodsQaShowViews.stream()
				.collect(Collectors.groupingBy(GoodsQaShowView::getType));
		List<GoodsQaShowView> result = new ArrayList<>();
		byType.forEach((type, typeViews) -> {
			GoodsQaShowView typeGroup = new GoodsQaShowView();
			typeGroup.setLabel(type == 1 ? "售前" : "售后");
			typeGroup.setValue(type);

			// 按categoryName分组并转换为QaGoodsType
			Map<String, List<GoodsQaShowView>> byCategory = typeViews.stream()
					.collect(Collectors.groupingBy(GoodsQaShowView::getCategoryName));

			List<GoodsQaShowView.QaGoodsType> qaGoodsTypes = new ArrayList<>();
			byCategory.forEach((categoryName, categoryViews) -> {
				GoodsQaShowView.QaGoodsType qaType = new GoodsQaShowView.QaGoodsType();
				qaType.setLabel(categoryName);
				qaType.setValue(categoryName);
				// 假设需要将GoodsQaShowView转换为GoodsDon，此处需要具体逻辑
				Set<Long> dupGids = new HashSet<>();
				List<GoodsQaShowView.QaGoodsType.GoodsDonQaView> goodsDons = categoryViews.stream()
						.filter(v -> filterDupGid(v, dupGids))
						.map(view -> convertToGoodsDon(view)) // 自定义转换方法
						.collect(Collectors.toList());
				qaType.setChildren(goodsDons);
				qaGoodsTypes.add(qaType);
			});
			typeGroup.setChildren(qaGoodsTypes);
			result.add(typeGroup);
		});
		return GatewayResponse.SUCCESS.newBuilder().toResult(result);
	}


	/**
	 * 咨询问题记录
	 */
	@PostMapping("/consultation/question/record")
	public Result<String> recordConsultationQuestion(@RequestBody @Validated CustomerConsultationRecord customerConsultationRecord) {
		long userId = StpUserUtil.getLoginIdAsLong();
		customerConsultationRecord.setUserId(userId);
		customerConsultationRecordMapper.insert(customerConsultationRecord);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	private GoodsQaShowView.QaGoodsType.GoodsDonQaView convertToGoodsDon(GoodsQaShowView view) {
		GoodsQaShowView.QaGoodsType.GoodsDonQaView goodsDonQaView = new GoodsQaShowView.QaGoodsType.GoodsDonQaView();
		goodsDonQaView.setLabel(view.getTitle());
		goodsDonQaView.setValue(view.getGoodsId());
		return goodsDonQaView;
	}

	private boolean filterDupGid(GoodsQaShowView v, Set<Long> dupGids) {
		if (dupGids.contains(v.getGoodsId())) {
			return false;
		}
		dupGids.add(v.getGoodsId());
		return true;
	}
}
