package com.ningdatech.kqapi.menu;

import cn.hutool.core.date.DateUtil;
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.entity.entity.NdKqZzsfwPolicy;
import com.ningdatech.kqapi.zzsfw.service.IDscSxAdsShareItemQltQlsxCommonIDVKqService;
import com.ningdatech.kqapi.zzsfw.service.INdKqZzsfwMenuService;
import com.ningdatech.kqapi.zzsfw.service.INdKqZzsfwPolicyService;
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.Objects;

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

    @Autowired
    private INdKqZzsfwPolicyService policyService;

    @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;

        policyService.remove(Wrappers.lambdaQuery(NdKqZzsfwPolicy.class));

        if (workbook != null) {
            //获取最大行数
            Sheet sheet = workbook.getSheetAt(0);
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            //获取最大列数
            Integer sort = 1;
            for (int i = 2; i < rownum; i++) {
                row = sheet.getRow(i);
                if(row.getZeroHeight()){
                    continue;
                }

                String header = Objects.nonNull(row.getCell(0)) ? row.getCell(0).getStringCellValue() : null;
                String title = Objects.nonNull(row.getCell(1)) ? row.getCell(1).getStringCellValue() : null;
                String secondTitle = Objects.nonNull(row.getCell(2)) ? row.getCell(2).getStringCellValue() : null;
                String regionName = Objects.nonNull(row.getCell(3)) ? row.getCell(3).getStringCellValue() : null;
                String department = Objects.nonNull(row.getCell(4)) ? row.getCell(4).getStringCellValue() : null;
                String issueDate = Objects.nonNull(row.getCell(5)) ? DateUtil.format(row.getCell(5).getDateCellValue(),"yyyy/MM/dd") : null;
                String status = Objects.nonNull(row.getCell(7)) ? row.getCell(7).getStringCellValue() : null;
                String applyTime = Objects.nonNull(row.getCell(8)) ? row.getCell(8).getStringCellValue() : null;
                String url = Objects.nonNull(row.getCell(9)) ? row.getCell(9).getStringCellValue() : null;

                NdKqZzsfwPolicy policy = new NdKqZzsfwPolicy();
                policy.setHeader(header);
                policy.setTitle(title);
                policy.setSecondTitle(secondTitle);
                policy.setRegionName(regionName);
                policy.setDepartment(department);
                policy.setIssueDate(issueDate);
                policy.setStatus(status);
                policy.setApplyTime(applyTime);
                policy.setOnlineConsultationUrl(url);
                policy.setCreateOn(LocalDateTime.now());
                policy.setSort(sort);

                policyService.save(policy);

                System.out.println("记数:" + (sort++));
            }
        }
    }

}
