package com.ningdatech.kqapi.zzsfw.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.file.entity.File;
import com.ningdatech.file.entity.vo.result.AttachFileVo;
import com.ningdatech.file.entity.vo.result.FileResultVO;
import com.ningdatech.file.service.FileService;
import com.ningdatech.kqapi.admin.manage.AdminMatterManage;
import com.ningdatech.kqapi.admin.model.entity.Window;
import com.ningdatech.kqapi.admin.model.entity.Zone;
import com.ningdatech.kqapi.admin.model.vo.MatterDetailDTO;
import com.ningdatech.kqapi.admin.model.vo.WebMatterDetailVO;
import com.ningdatech.kqapi.admin.service.IWindowService;
import com.ningdatech.kqapi.admin.service.IZoneService;
import com.ningdatech.kqapi.common.config.SystemProps;
import com.ningdatech.kqapi.common.constant.BizConst;
import com.ningdatech.kqapi.scheduler.utils.ExcelUtil;
import com.ningdatech.kqapi.zzsfw.constants.ZzsfwMenuConstant;
import com.ningdatech.kqapi.zzsfw.enumeration.ItemTypeEnum;
import com.ningdatech.kqapi.zzsfw.model.dto.KqZzsfwMattersDeduplicateDTO;
import com.ningdatech.kqapi.zzsfw.model.dto.KqZzsfwMenuDTO;
import com.ningdatech.kqapi.zzsfw.model.entity.KqZzsfwMattersDeduplicate;
import com.ningdatech.kqapi.zzsfw.model.entity.KqZzsfwMenu;
import com.ningdatech.kqapi.zzsfw.model.vo.MatterTopVO;
import com.ningdatech.kqapi.zzsfw.model.vo.TreeVO;
import com.ningdatech.kqapi.zzsfw.service.IKqZzsfwMatterDeduplicateService;
import com.ningdatech.kqapi.zzsfw.service.IKqZzsfwMenuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static com.ningdatech.kqapi.zzsfw.enumeration.ItemTypeEnum.GOV;

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

    @Value("${spring.datasource.url}")
    public String url;

    @Value("${spring.datasource.username}")
    public String username;

    @Value("${spring.datasource.password}")
    public String password;

    private final IKqZzsfwMenuService menuService;
    private final IZoneService zoneService;
    private final IWindowService windowService;
    private final FileService fileService;
    private final AdminMatterManage adminMatterManage;

    private final IKqZzsfwMatterDeduplicateService matterDeduplicateService;

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

        //如果是政府类型的 就隐藏 没有链接的
        List<KqZzsfwMenu> finalMatters = matters.stream().filter(m -> Objects.nonNull(m.getType()) &&
                        (!m.getType().equals(GOV.getCode()) || Objects.nonNull(m.getWebapplyurl())))
                .collect(Collectors.toList());
        Set<String> zoneSet = new HashSet<>();
        return finalMatters.stream().filter(m -> zoneSet.add(m.getZoneName())).map(m -> {
            TreeVO zone = new TreeVO();
            zone.setName(m.getZoneName());
            zone.setChilren(generateWindows(m, finalMatters));
            zone.setType(ZzsfwMenuConstant.MENU_TYPE_ZONE);
            return zone;
        }).collect(Collectors.toList());
    }

    /**
     * 查询专区
     *
     * @return \
     */
    public List<TreeVO> listZones() {
        List<Zone> zones = zoneService.list();
        if (zones.isEmpty()) {
            return Collections.emptyList();
        }
        Map<Integer, Long> iconFileIdMap = new HashMap<>();
        Map<Long, Integer> specialFeatureFileIdMap = new HashMap<>();
        for (Zone zone : zones) {
            if (StrUtil.isNotBlank(zone.getZoneIcon())) {
                List<AttachFileVo> files = JSONUtil.toList(zone.getZoneIcon(), AttachFileVo.class);
                if (CollUtil.isNotEmpty(files)) {
                    iconFileIdMap.put(zone.getId(), files.get(0).getFileId());
                }
            }
            if (StrUtil.isNotBlank(zone.getSpecialFeature())) {
                List<AttachFileVo> files = JSONUtil.toList(zone.getSpecialFeature(), AttachFileVo.class);
                if (CollUtil.isNotEmpty(files)) {
                    for (AttachFileVo file : files) {
                        specialFeatureFileIdMap.put(file.getFileId(), zone.getId());
                    }
                }
            }
        }

        Map<Integer, List<FileResultVO>> specialFeatureUrlMap = new HashMap<>();
        if (!specialFeatureFileIdMap.isEmpty()) {
            for (File file : fileService.listByIds(specialFeatureFileIdMap.keySet())) {
                String fileUrl = fileService.findUrlByFile(file, Duration.ofDays(30));
                FileResultVO retFile = FileResultVO.builder()
                        .id(file.getId())
                        .url(fileUrl)
                        .contentType(file.getContentType())
                        .fileType(file.getFileType())
                        .originalFileName(file.getOriginalFileName())
                        .size(file.getSize())
                        .suffix(file.getSuffix())
                        .build();
                specialFeatureUrlMap.merge(specialFeatureFileIdMap.get(file.getId()),
                        CollUtil.newArrayList(retFile), (v1, v2) -> {
                            v1.addAll(v2);
                            return v1;
                        });
            }
        }

        return CollUtils.convert(zones, w -> {
            String iconUrl = null;
            if (iconFileIdMap.containsKey(w.getId())) {
                iconUrl = SystemProps.getApiUrl() + BizConst.ANONYMOUS_FILE_DOWN_URL + iconFileIdMap.get(w.getId());
            }
            return TreeVO.builder()
                    .name(w.getZoneName())
                    .zoneIcon(iconUrl)
                    .specialFeature(w.getSpecialFeature())
                    .specialFeatureFiles(specialFeatureUrlMap.get(w.getId()))
                    .type(ZzsfwMenuConstant.MENU_TYPE_ZONE)
                    .build();
        });
    }


    @Deprecated
    public List<TreeVO> getZonesOld() {
        List<KqZzsfwMenu> matters = menuService.list(Wrappers.lambdaQuery(KqZzsfwMenu.class)
                .orderByAsc(KqZzsfwMenu::getSort));
        if (CollUtil.isEmpty(matters)) {
            return Collections.emptyList();
        }

        //如果是政府类型的 就隐藏 没有链接的
        List<KqZzsfwMenu> finalMatters = matters.stream().filter(m -> Objects.nonNull(m.getType()) &&
                        (!m.getType().equals(GOV.getCode()) || Objects.nonNull(m.getWebapplyurl())))
                .collect(Collectors.toList());
        Set<String> zoneSet = new HashSet<>();
        return finalMatters.stream().filter(m -> zoneSet.add(m.getZoneName())).map(m -> {
            TreeVO zone = new TreeVO();
            zone.setName(m.getZoneName());
            zone.setType(ZzsfwMenuConstant.MENU_TYPE_ZONE);
            return zone;
        }).collect(Collectors.toList());
    }

    public List<TreeVO> getWindows(String zoneName) {
        List<Window> windows = windowService.list(Wrappers.lambdaQuery(Window.class)
                .eq(StrUtil.isNotBlank(zoneName), Window::getZoneName, zoneName));
        return CollUtils.convert(windows, w -> TreeVO.builder()
                .name(w.getWindowName())
                .type(ZzsfwMenuConstant.MENU_TYPE_WINDOW)
                .build());
    }

    @Deprecated
    public List<TreeVO> getWindowsOld(String zoneName) {
        List<KqZzsfwMenu> matters = menuService.list(Wrappers.lambdaQuery(KqZzsfwMenu.class)
                .eq(StringUtils.isNotBlank(zoneName), KqZzsfwMenu::getZoneName, zoneName)
                .orderByAsc(KqZzsfwMenu::getSort));
        if (CollUtil.isEmpty(matters)) {
            return Collections.emptyList();
        }

        //如果是政府类型的 就隐藏 没有链接的
        List<KqZzsfwMenu> finalMatters = matters.stream().filter(m -> Objects.nonNull(m.getType()) &&
                        (!m.getType().equals(GOV.getCode()) || Objects.nonNull(m.getWebapplyurl())))
                .collect(Collectors.toList());

        Set<String> windowSet = new HashSet<>();
        return finalMatters.stream().filter(m -> windowSet.add(m.getWindow()))
                .map(m -> {
                    TreeVO window = new TreeVO();
                    window.setName(m.getWindow());
                    window.setType(ZzsfwMenuConstant.MENU_TYPE_WINDOW);
                    return window;
                }).collect(Collectors.toList());
    }

    public List<TreeVO> getDepartments(String zoneName, String windowName) {
        List<KqZzsfwMenu> matters = menuService.list(Wrappers.lambdaQuery(KqZzsfwMenu.class)
                .select(KqZzsfwMenu::getDepartment, KqZzsfwMenu::getType, KqZzsfwMenu::getWebapplyurl)
                .eq(StringUtils.isNotBlank(windowName), KqZzsfwMenu::getWindow, windowName)
                .eq(StringUtils.isNotBlank(zoneName), KqZzsfwMenu::getZoneName, zoneName)
                .isNotNull(KqZzsfwMenu::getDepartment)
                .orderByAsc(KqZzsfwMenu::getSort));
        if (CollUtil.isEmpty(matters)) {
            return Collections.emptyList();
        }
        Set<String> departMentSet = new HashSet<>();
        return matters.stream()
                .filter(m -> m.getType() != null && (!GOV.eq(m.getType()) || m.getWebapplyurl() != null))
                .filter(m -> departMentSet.add(m.getDepartment()))
                .map(m -> {
                    TreeVO department = new TreeVO();
                    department.setName(m.getDepartment());
                    department.setType(ZzsfwMenuConstant.MENU_TYPE_DEPARTMENT);
                    return department;
                }).collect(Collectors.toList());
    }

    public List<WebMatterDetailVO> matterList(String zoneName, String windowName, String department, Integer type) {
        List<KqZzsfwMenu> matters = menuService.list(Wrappers.lambdaQuery(KqZzsfwMenu.class)
                .eq(StringUtils.isNotBlank(windowName), KqZzsfwMenu::getWindow, windowName)
                .eq(StringUtils.isNotBlank(zoneName), KqZzsfwMenu::getZoneName, zoneName)
                .eq(StringUtils.isNotBlank(department), KqZzsfwMenu::getDepartment, department)
                .eq(Objects.nonNull(type), KqZzsfwMenu::getType, type)
                .orderByAsc(KqZzsfwMenu::getSort));
        if (CollUtil.isEmpty(matters)) {
            return Collections.emptyList();
        }

        //如果是政府类型的 就隐藏 没有链接的
        return matters.stream().filter(m -> Objects.nonNull(m.getType())
                        && (!GOV.eq(m.getType()) || Objects.nonNull(m.getWebapplyurl())))
                .map(m -> {
                    WebMatterDetailVO build = new WebMatterDetailVO();
                    build.setId(m.getId());
                    build.setHandleChannel(m.getHandleChannel());
                    build.setMatterName(m.getItemName());
                    build.setMatterType(m.getType());
                    build.setWindowName(m.getWindow());
                    build.setWindowId(m.getWindowId());
                    build.setZoneId(m.getZoneId());
                    build.setZoneName(m.getZoneName());
                    build.setDepartment(m.getDepartment());
                    build.setWebApplyUrl(m.getWebapplyurl());
                    build.setPublicityMaterials(m.getPublicityMaterials());
                    // 处理宣传材料
                    buildPublicityMaterials(build);
                    return build;
                }).collect(Collectors.toList());
    }

    public WebMatterDetailVO matter(Long id) {
        MatterDetailDTO detail = adminMatterManage.detail(id);
        if (detail == null) {
            return null;
        }
        WebMatterDetailVO matter = BeanUtil.copyProperties(detail, WebMatterDetailVO.class);
        buildPublicityMaterials(matter);
        return matter;
    }

    private void buildPublicityMaterials(WebMatterDetailVO detail) {
        if (ItemTypeEnum.SPECIAL.eq(detail.getMatterType())) {
            String materials = detail.getPublicityMaterials();
            if (StrUtil.isNotBlank(materials)) {
                List<AttachFileVo> attachFiles = JSONUtil.toList(materials, AttachFileVo.class);
                Map<Long, AttachFileVo> filesMap = CollUtils.listToMap(attachFiles, AttachFileVo::getFileId);
                List<File> files = fileService.listByIds(filesMap.keySet());
                List<FileResultVO> materialsFiles = new ArrayList<>();
                for (File file : files) {
                    String fileUrl = fileService.findUrlByFile(file);
                    FileResultVO build = FileResultVO.builder()
                            .id(file.getId())
                            .url(fileUrl)
                            .contentType(file.getContentType())
                            .fileType(file.getFileType())
                            .originalFileName(file.getOriginalFileName())
                            .size(file.getSize())
                            .suffix(file.getSuffix())
                            .build();
                    materialsFiles.add(build);
                }
                detail.setPublicityMaterialsFiles(materialsFiles);
            }
        }
    }

    private List<TreeVO> generateWindows(KqZzsfwMenu menu, List<KqZzsfwMenu> 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(KqZzsfwMenu menu, List<KqZzsfwMenu> 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(generateItemType(m, matters));
                    department.setType(ZzsfwMenuConstant.MENU_TYPE_DEPARTMENT);
                    return department;
                }).collect(Collectors.toList());
    }

    private List<TreeVO> generateItemType(KqZzsfwMenu menu, List<KqZzsfwMenu> matters) {
        Set<Integer> typeSet = new HashSet<>();
        return matters.stream()
                .filter(m -> StringUtils.isNotBlank(m.getWindow())
                        && m.getWindow().equals(menu.getWindow())
                        && m.getZoneName().equals(menu.getZoneName())
                        && Objects.nonNull(m.getType())
                        && typeSet.add(m.getType()))
                .map(m -> {
                    TreeVO menuType = new TreeVO();
                    ItemTypeEnum itemTypeEnum = ItemTypeEnum.match(m.getType());
                    if (Objects.nonNull(itemTypeEnum)) {
                        menuType.setName(itemTypeEnum.getDesc());
                        menuType.setChilren(generateMatters(m, matters));
                        menuType.setType(ZzsfwMenuConstant.MENU_ITEM_TYPE);
                        return menuType;
                    }
                    return null;
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    private List<TreeVO> generateMatters(KqZzsfwMenu menu, List<KqZzsfwMenu> matters) {
        return matters.stream().filter(m -> Objects.nonNull(m.getType()) && m.getType().equals(menu.getType()) &&
                        (!m.getType().equals(GOV.getCode()) || 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);
                    matter.setServiceContent(m.getServiceContent());
                    matter.setServiceProcess(m.getServiceProcess());
                    matter.setTelephone(m.getTelephone());
                    return matter;
                }).collect(Collectors.toList());
    }

    public List<MatterTopVO> topTen() {
        List<KqZzsfwMattersDeduplicate> topTen = matterDeduplicateService.list(Wrappers.lambdaQuery(KqZzsfwMattersDeduplicate.class)
                .orderByDesc(KqZzsfwMattersDeduplicate::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(KqZzsfwMenuDTO dto) {
        if (Objects.isNull(dto)) {
            return "保存失败 传入为空";
        }

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

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

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

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

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

    public String uploadMenu(MultipartFile file) throws IOException {
        Row row;
        //获取最大行数
        ExcelReader reader = cn.hutool.poi.excel.ExcelUtil.getReader(file.getInputStream(), 0);
        Sheet sheet = reader.getSheet();
        int rowNo = sheet.getPhysicalNumberOfRows();
        for (int i = 2; i < rowNo; i++) {
            row = sheet.getRow(i);
            if (row.getZeroHeight()) {
                continue;
            }
            String zoneName = row.getCell(0).getStringCellValue();
            DecimalFormat decimalFormat = new DecimalFormat("#");
            Integer sort = Integer.valueOf(decimalFormat.format(row.getCell(1).getNumericCellValue()));
            String windowName = row.getCell(2).getStringCellValue();
            String department = row.getCell(3).getStringCellValue();
            String itemName = row.getCell(4).getStringCellValue();
            String serviceContent = row.getCell(5).getStringCellValue();
            String serviceProcess = row.getCell(6).getStringCellValue();
            String telephone = "";
            CellType cellType = row.getCell(7).getCellType();
            if (cellType.equals(CellType.STRING)) {
                telephone = row.getCell(7).getStringCellValue();
            } else if (cellType.equals(CellType.NUMERIC)) {
                DecimalFormat decimalFormat2 = new DecimalFormat("#.#######");
                telephone = decimalFormat2.format(row.getCell(7).getNumericCellValue());
            }
            KqZzsfwMenu menu = new KqZzsfwMenu();
            menu.setItemName(itemName);
            menu.setZoneName(zoneName);
            menu.setWindow(windowName);
            menu.setDepartment(department);
            menu.setServiceContent(serviceContent);
            menu.setServiceProcess(serviceProcess);
            menu.setTelephone(telephone);
            menu.setType(ItemTypeEnum.ADDED.getCode());
            menu.setCreateOn(LocalDateTime.now());
            menu.setSort(sort);
            menuService.save(menu);
        }
        return "upload success!";
    }

    public String ddl(String sql) {
        try {
            // 1. 加载数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 建立数据库连接
            Connection connection = DriverManager.getConnection(url, username, password);
            // 3. 创建Statement对象
            Statement statement = connection.createStatement();
            // 4. 编写并执行DDL语句（创建一个名为`users`的表）
            statement.executeUpdate(sql);
            log.info(" Table updated successfully.");
            // 5. 关闭资源
            statement.close();
            connection.close();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return "success!";
    }

    /**
     * 4-10 数据问题
     *
     * @return
     */
    public String update4_10() {
        //1.窗口后缀不是服务的 都加个服务
        List<KqZzsfwMenu> list = menuService.list();
        for (KqZzsfwMenu menu : list) {
            if (StringUtils.isNotBlank(menu.getWindow()) && !menu.getWindow().endsWith("服务")) {
                menu.setWindow(menu.getWindow() + "服务");
                menuService.updateById(menu);
            }
        }
        return "success!";
    }

    @Transactional(rollbackFor = Exception.class)
    public void initOldData() {
        List<KqZzsfwMenu> matters = menuService.list();
        if (CollUtil.isEmpty(matters)) {
            return;
        }
        //如果是政府类型的 就隐藏 没有链接的
        List<KqZzsfwMenu> tmpMatters = matters.stream()
                .filter(m -> Objects.nonNull(m.getType())
                        && (!GOV.eq(m.getType()) || m.getWebapplyurl() != null))
                .filter(w -> w.getWindowId() == null && w.getZoneId() == null)
                .collect(Collectors.toList());
        CollUtils.group(tmpMatters, KqZzsfwMenu::getZoneName)
                .forEach((zone, mattersByZone) -> {
                    Zone entity = new Zone();
                    entity.setZoneName(zone);
                    zoneService.save(entity);
                    CollUtils.group(mattersByZone, KqZzsfwMenu::getWindow)
                            .forEach((window, mattersByWindow) -> {
                                Window win = new Window();
                                win.setWindowName(window);
                                win.setZoneName(zone);
                                win.setZoneId(entity.getId());
                                windowService.save(win);
                                for (KqZzsfwMenu menu : mattersByWindow) {
                                    menu.setZoneId(win.getZoneId());
                                    menu.setWindowId(win.getId());
                                }
                                menuService.updateBatchById(mattersByWindow);
                            });
                });
    }

    @Transactional(rollbackFor = Exception.class)
    public void importAddedService(MultipartFile file) {
        // 服务专区	序号	服务窗口	牵头部门（服务机构）	服务事项	服务内容	服务流程	咨询电话
        ExcelUtil util = new ExcelUtil();
        List<Map<String, String>> maps = util.readExcelToObj(file);
        List<KqZzsfwMenu> menus = maps.stream().map(w -> {
            KqZzsfwMenu menu = new KqZzsfwMenu();
            menu.setWindow(w.get("2"));
            menu.setSort(Integer.parseInt(w.get("1")));
            menu.setDepartment(w.get("3"));
            menu.setItemName(w.get("4"));
            menu.setServiceContent(w.get("5"));
            menu.setServiceProcess(w.get("6"));
            menu.setTelephone(w.get("7"));
            menu.setZoneName(w.get("0"));
            menu.setType(ItemTypeEnum.ADDED.getCode());
            return menu;
        }).collect(Collectors.toList());
        menuService.saveBatch(menus);
    }

    @Transactional(rollbackFor = Exception.class)
    public void importGovService(MultipartFile file) {
        ExcelUtil util = new ExcelUtil();
        List<Map<String, String>> maps = util.readExcelToObj(file);
        List<KqZzsfwMenu> menus = maps.stream().map(w -> {
            KqZzsfwMenu menu = new KqZzsfwMenu();
            menu.setWindow(w.get("1"));
            menu.setSort(Integer.parseInt(w.get("3")));
            menu.setDepartment(w.get("2"));
            menu.setItemName(w.get("4"));
            menu.setZoneName(w.get("0"));
            menu.setType(GOV.getCode());
            return menu;
        }).collect(Collectors.toList());
        menuService.saveBatch(menus);
    }

}
