package test;

import com.gyee.SpringbootStart;
import com.gyee.frame.common.feign.RemoteServiceBuilder;
import com.gyee.frame.mapper.auto.WindTurbineTestingPointAi2Mapper;
import com.gyee.frame.model.auto.WindTurbineTestingPointAi2;
import com.gyee.frame.model.auto.WindTurbineTestingPointAi2Example;
import com.gyee.frame.model.custom.export.TsPointData;
import com.gyee.frame.model.excel.Woworktickeask;
import com.gyee.frame.service.export.WorkticketService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 与两票系统关联统计风机定检之后可以正常运行时长
 */
@SpringBootTest(classes = SpringbootStart.class)
@RunWith(SpringRunner.class)
public class RegularCheckTest {

    @Resource
    private WorkticketService workticketService;
    private final String REGEX = "\\d+";

    @Test()
    public void test() {
        List<Woworktickeask> getsomething = workticketService.getsomething("2022-08-01");

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet();

        XSSFRow row0 = sheet.createRow(0);
        row0.createCell(0).setCellValue("序号");
        row0.createCell(1).setCellValue("场站");
        row0.createCell(2).setCellValue("风机");
        row0.createCell(3).setCellValue("风机定检日期");
        row0.createCell(4).setCellValue("持续时长(天)");

        int i = 1;
        for (Woworktickeask wwtt : getsomething) {
            String windturbine = getWindturbine(wwtt.getAddress());
            if(windturbine==null) continue;

            XSSFRow row = sheet.createRow(i);
            row.createCell(0).setCellValue(i-1);
            i++;
            row.createCell(1).setCellValue(wwtt.getSitenum());
            row.createCell(2).setCellValue(windturbine);
            row.createCell(3).setCellValue(DateFormatUtils.format(wwtt.getStartdate(),"yyyy-MM-dd HH:mm:ss"));
            int duration = getDuration(wwtt.getEnddate());
        }
    }

    private int getDuration(Date enddate) {

        return 0;
    }

    private String getWindturbine(String address) {
        /*Pattern pattern= Pattern.compile(REGEX);
        Matcher matcher=pattern.matcher(address);
        String group = null;
        if(matcher.find()){
            group = matcher.group(0);
            if(group.length()<2) group = "0" + group;
        }else {
            return null;
        }*/
        String s = address.replaceAll("[\\u4e00-\\u9fa5]{0,}", "");
        return s;
    }
}