package com.ningdatech.kqapi.menu;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Sets;
import com.ningdatech.kqapi.AppTests;
import com.ningdatech.kqapi.zzsfw.entity.entity.NdKqZzsfwMattersDeduplicate;
import com.ningdatech.kqapi.zzsfw.entity.entity.NdKqZzsfwMenu;
import com.ningdatech.kqapi.zzsfw.service.INdKqZzsfwMatterDeduplicateService;
import com.ningdatech.kqapi.zzsfw.service.INdKqZzsfwMenuService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Classname DuplicationTest
 * @Description
 * @Date 2023/11/17 10:42
 * @Author PoffyZhang
 */
public class DuplicationTest extends AppTests {

    @Autowired
    private INdKqZzsfwMatterDeduplicateService matterDeduplicateService;

    @Autowired
    private INdKqZzsfwMenuService menuService;

    @Test
    public void test() throws IOException {
        FileInputStream fis = new FileInputStream("C:\\Users\\PoffyZhang\\Desktop\\去重后的事项数据.xlsx");
        Workbook workbook = new XSSFWorkbook(fis);
        Assert.assertNotNull(workbook);
        Row row;
        if (workbook != null) {
            //获取最大行数
            Sheet sheet = workbook.getSheetAt(0);
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            //获取最大列数
            Integer num = 1;

            matterDeduplicateService.remove(Wrappers.lambdaQuery(NdKqZzsfwMattersDeduplicate.class));

            for (int i = 2; i < rownum; i++) {
                row = sheet.getRow(i);
                if(row.getZeroHeight()){
                    continue;
                }
                System.out.println("记数:" + (num++));
                System.out.println(row.getCell(0).getStringCellValue());
                System.out.println(row.getCell(1).getStringCellValue());
                Double numericCellValue = row.getCell(2).getNumericCellValue();

                NdKqZzsfwMattersDeduplicate menu = new NdKqZzsfwMattersDeduplicate();
                menu.setWebapplyurl(row.getCell(1).getStringCellValue());
                menu.setQlName(row.getCell(0).getStringCellValue());
                menu.setCreateOn(LocalDateTime.now());
                menu.setCountNum(numericCellValue.intValue());

                matterDeduplicateService.save(menu);
            }
        }
    }

    @Test
    public void syncUrl(){
        List<NdKqZzsfwMenu> menus = menuService.list(Wrappers.lambdaQuery(NdKqZzsfwMenu.class));

        List<NdKqZzsfwMattersDeduplicate> list = matterDeduplicateService.list();

        Set<String> qlSet = Sets.newTreeSet();
        Map<String, String> map = list.stream().filter(q -> qlSet.add(q.getQlName()))
                .filter(q -> StringUtils.isNotBlank(q.getWebapplyurl()) && StringUtils.isNotBlank(q.getQlName()))
                .collect(Collectors.toMap(q -> q.getQlName(), q -> q.getWebapplyurl()));

        for(NdKqZzsfwMenu menu : menus){
            String itemName = menu.getItemName();
            if(map.containsKey(itemName)){
                String url = map.get(itemName);
                menu.setWebapplyurl(url);
                menu.setHasUrl(1);
                menuService.updateById(menu);
            }
        }
    }
}
