package com.ningdatech.kqapi.zzsfw.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.kqapi.zzsfw.entity.entity.DscSxAdsShareItemQltQlsxCommonIDVKq;
import com.ningdatech.kqapi.zzsfw.entity.entity.NdKqZzsfwMenu;
import com.ningdatech.kqapi.zzsfw.entity.vo.MattersVO;
import com.ningdatech.kqapi.zzsfw.entity.vo.WindowVO;
import com.ningdatech.kqapi.zzsfw.entity.vo.ZoneVO;
import com.ningdatech.kqapi.zzsfw.service.IDscSxAdsShareItemQltQlsxCommonIDVKqService;
import com.ningdatech.kqapi.zzsfw.service.INdKqZzsfwMenuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Classname MatterManage
 * @Description
 * @Date 2023/10/25 14:55
 * @Author PoffyZhang
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class MatterManage {

    private final INdKqZzsfwMenuService menuService;

    public List<ZoneVO> getMatters() {
        List<NdKqZzsfwMenu> matters = menuService.list(Wrappers.lambdaQuery(NdKqZzsfwMenu.class)
                .eq(NdKqZzsfwMenu::getHasUrl,1)
                .orderByAsc(NdKqZzsfwMenu::getSort));

        if(CollUtil.isEmpty(matters)){
            return Collections.emptyList();
        }

        Set<String> zoneSet = new HashSet<>();
        return matters.stream().filter(m -> zoneSet.add(m.getZoneName())).map(m -> {
            ZoneVO zone = new ZoneVO();
            zone.setZoneName(m.getZoneName());
            zone.setWindows(generateWindows(m,matters));
            return zone;
        }).collect(Collectors.toList());
    }

    private List<WindowVO> generateWindows(NdKqZzsfwMenu menu, List<NdKqZzsfwMenu> matters) {
        return matters.stream().filter(m -> StringUtils.isNotBlank(m.getZoneName()) && m.getZoneName().equals(menu.getZoneName()))
                .map(m -> {
                    WindowVO window = new WindowVO();
                    window.setWindowName(m.getWindow());
                    window.setMatters(generateMatters(m,matters));
                    return window;
                }).collect(Collectors.toList());
    }

    private List<MattersVO> generateMatters(NdKqZzsfwMenu menu, List<NdKqZzsfwMenu> matters) {
        return matters.stream().filter(m -> StringUtils.isNotBlank(m.getWindow()) && m.getWindow().equals(menu.getWindow()))
                .map(m -> {
                    MattersVO matter = new MattersVO();
                    matter.setMatterName(m.getItemName());
                    matter.setWebapplyurl(m.getWebapplyurl());
                    matter.setRowguid(m.getItemRowid());
                    matter.setSort(m.getSort());
                    return matter;
                }).collect(Collectors.toList());
    }
}
