package com.ningdatech.kqapi.zzsfw.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.StopWatch;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.kqapi.common.enumeration.CommonEnum;
import com.ningdatech.kqapi.common.helper.MatterCacheHelper;
import com.ningdatech.kqapi.zzsfw.constants.ZzsfwMenuConstant;
import com.ningdatech.kqapi.zzsfw.entity.dto.NdKqZzsfwMattersDeduplicateDTO;
import com.ningdatech.kqapi.zzsfw.entity.dto.NdKqZzsfwMenuDTO;
import com.ningdatech.kqapi.zzsfw.entity.entity.NdKqZzsfwMattersDeduplicate;
import com.ningdatech.kqapi.zzsfw.entity.entity.NdKqZzsfwMenu;
import com.ningdatech.kqapi.zzsfw.entity.vo.MatterTopVO;
import com.ningdatech.kqapi.zzsfw.entity.vo.TreeVO;
import com.ningdatech.kqapi.zzsfw.service.INdKqZzsfwMatterDeduplicateService;
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.*;
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;

    private final INdKqZzsfwMatterDeduplicateService matterDeduplicateService;

    public List<TreeVO> getMatters() {
        List<NdKqZzsfwMenu> matters = menuService.list(Wrappers.lambdaQuery(NdKqZzsfwMenu.class)
                .isNotNull(NdKqZzsfwMenu::getWebapplyurl)
                .orderByAsc(NdKqZzsfwMenu::getSort));
        if(CollUtil.isEmpty(matters)){
            return Collections.emptyList();
        }

        Set<String> zoneSet = new HashSet<>();
        List<TreeVO> res = matters.stream().filter(m -> zoneSet.add(m.getZoneName())).map(m -> {
            TreeVO zone = new TreeVO();
            zone.setName(m.getZoneName());
            zone.setChilren(generateWindows(m, matters));
            zone.setType(ZzsfwMenuConstant.MENU_TYPE_ZONE);
            return zone;
        }).collect(Collectors.toList());
        return res;
    }

    private List<TreeVO> generateWindows(NdKqZzsfwMenu menu, List<NdKqZzsfwMenu> matters) {
        Set<String> windowSet = new HashSet<>();
        return matters.stream().filter(m -> StringUtils.isNotBlank(m.getZoneName()) && m.getZoneName().equals(menu.getZoneName())
                && windowSet.add(m.getWindow()))
                .map(m -> {
                    TreeVO window = new TreeVO();
                    window.setName(m.getWindow());
                    window.setChilren(generateDepartment(m,matters));
                    window.setType(ZzsfwMenuConstant.MENU_TYPE_WINDOW);
                    return window;
                }).collect(Collectors.toList());
    }

    private List<TreeVO> generateDepartment(NdKqZzsfwMenu menu, List<NdKqZzsfwMenu> matters) {
        Set<String> departmentSet = new HashSet<>();
        return matters.stream().filter( m -> StringUtils.isNotBlank(m.getWindow()) && m.getWindow().equals(menu.getWindow()) &&
                        m.getZoneName().equals(menu.getZoneName()) && departmentSet.add(m.getDepartment()))
                .map(m -> {
                    TreeVO department = new TreeVO();
                    department.setName(m.getDepartment());
                    department.setChilren(generateMatters(m,matters));
                    department.setType(ZzsfwMenuConstant.MENU_TYPE_DEPARTMENT);
                    return department;
                }).collect(Collectors.toList());
    }

    private List<TreeVO> generateMatters(NdKqZzsfwMenu menu, List<NdKqZzsfwMenu> matters) {
        return matters.stream().filter(m -> Objects.nonNull(m.getHasUrl()) && m.getHasUrl() == 1 &&
                        StringUtils.isNotBlank(m.getDepartment()) &&
                        m.getDepartment().equals(menu.getDepartment()) && m.getWindow().equals(menu.getWindow()) &&
                        m.getZoneName().equals(menu.getZoneName()))
                .map(m -> {
                    TreeVO matter = new TreeVO();
                    matter.setName(m.getItemName());
                    matter.setUrl(m.getWebapplyurl());
                    matter.setId(m.getItemRowid());
                    matter.setSort(m.getSort());
                    matter.setType(ZzsfwMenuConstant.MENU_TYPE_MATTER);
                    return matter;
                }).collect(Collectors.toList());
    }

    public List<MatterTopVO> topTen() {
        List<NdKqZzsfwMattersDeduplicate> topTen = matterDeduplicateService.list(Wrappers.lambdaQuery(NdKqZzsfwMattersDeduplicate.class)
                .orderByDesc(NdKqZzsfwMattersDeduplicate::getCountNum)
                .last("limit 10"));
        return topTen.stream().map(m -> {
            MatterTopVO vo = new MatterTopVO();
            vo.setMatterName(m.getQlName());
            vo.setWebapplyurl(m.getWebapplyurl());
            vo.setCount(m.getCountNum());
            return vo;
        }).collect(Collectors.toList());
    }

    public String save(NdKqZzsfwMenuDTO dto) {
        if(Objects.isNull(dto)){
            return "保存失败 传入为空";
        }

        NdKqZzsfwMenu entity = BeanUtil.copyProperties(dto,NdKqZzsfwMenu.class);
        if(menuService.save(entity)){
            return "保存成功 :" + entity;
        }else{
            return "保存失败";
        }
    }

    public String saveDup(NdKqZzsfwMattersDeduplicateDTO dto) {
        if(Objects.isNull(dto)){
            return "保存失败 传入为空";
        }

        NdKqZzsfwMattersDeduplicate entity = BeanUtil.copyProperties(dto,NdKqZzsfwMattersDeduplicate.class);
        if(matterDeduplicateService.save(entity)){
            return "保存成功 :" + entity;
        }else{
            return "保存失败";
        }
    }

    public String removeAll() {
        menuService.remove(Wrappers.lambdaQuery(NdKqZzsfwMenu.class));
        return "删除成功";
    }

    public String removeAllDup() {
        matterDeduplicateService.remove(Wrappers.lambdaQuery(NdKqZzsfwMattersDeduplicate.class));
        return "删除成功";
    }
}
