package com.ningdatech.kqapi.menu;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.kqapi.AppTests;
import com.ningdatech.kqapi.zzsfw.entity.entity.DscSxAdsShareItemQltQlsxCommonIDVKq;
import com.ningdatech.kqapi.zzsfw.entity.entity.NdKqZzsfwMenu;
import com.ningdatech.kqapi.zzsfw.service.IDscSxAdsShareItemQltQlsxCommonIDVKqService;
import com.ningdatech.kqapi.zzsfw.service.INdKqZzsfwMenuService;
import org.apache.poi.ss.usermodel.*;
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.Objects;

/**
 * @Classname MenuTest
 * @Description
 * @Date 2023/10/27 9:51
 * @Author PoffyZhang
 */
public class MenuTest extends AppTests {

    @Autowired
    private INdKqZzsfwMenuService menuService;

    @Autowired
    private IDscSxAdsShareItemQltQlsxCommonIDVKqService itemQltQlsxCommonIDVKqService;

    @Test
    public void test() throws IOException {
        FileInputStream fis = new FileInputStream("C:\\Users\\PoffyZhang\\Desktop\\8.8柯桥区企业综合服务中心政务服务事项清单2.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;
            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());
                System.out.println(row.getCell(2).getStringCellValue());
                Double numericCellValue = row.getCell(3).getNumericCellValue();
                String itemName = row.getCell(4).getStringCellValue();
                System.out.println(itemName);

                NdKqZzsfwMenu menu = new NdKqZzsfwMenu();
                menu.setItemName(itemName);
                menu.setZoneName(row.getCell(0).getStringCellValue());
                menu.setWindow(row.getCell(1).getStringCellValue());
                menu.setCreateOn(LocalDateTime.now());
                menu.setDepartment(row.getCell(2).getStringCellValue());
                menu.setSort(numericCellValue.intValue());

                //去查询 url
                DscSxAdsShareItemQltQlsxCommonIDVKq item = itemQltQlsxCommonIDVKqService.getOne(Wrappers.lambdaQuery(DscSxAdsShareItemQltQlsxCommonIDVKq.class)
                        .select(DscSxAdsShareItemQltQlsxCommonIDVKq::getRowguid, DscSxAdsShareItemQltQlsxCommonIDVKq::getWebapplyurl, DscSxAdsShareItemQltQlsxCommonIDVKq::getQlName)
                        .eq(DscSxAdsShareItemQltQlsxCommonIDVKq::getQlName, itemName)
                        .isNotNull(DscSxAdsShareItemQltQlsxCommonIDVKq::getWebapplyurl)
                        .last("limit 1"));
                if(Objects.nonNull(item)){
                    menu.setHasUrl(1);
                    menu.setWebapplyurl(item.getWebapplyurl());
                    menu.setItemRowid(item.getRowguid());
                }

                menuService.save(menu);
            }
        }
    }

}
