package com.yinlihupo.enlish.service.mapper;

import com.yinlihupo.enlish.service.domain.dataobject.GradeUnitDO;
import com.yinlihupo.enlish.service.domain.dataobject.UnitDO;
import com.yinlihupo.enlish.service.domain.dataobject.VocabularyBankDO;
import com.yinlihupo.enlish.service.domain.mapper.GradeUnitDOMapper;
import com.yinlihupo.enlish.service.domain.mapper.UnitDOMapper;
import com.yinlihupo.enlish.service.domain.mapper.VocabularyBankDOMapper;
import com.yinlihupo.enlish.service.domain.mapper.WordMasteryLogDOMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
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.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;

@SpringBootTest
@Slf4j
public class TestVocabularyBankInsert {

    @Resource
    private VocabularyBankDOMapper vocabularyBankMapper;
    @Resource
    private UnitDOMapper unitDOMapper;
    @Resource
    private WordMasteryLogDOMapper wordMasteryLogDOMapper;
    @Resource
    private GradeUnitDOMapper gradeUnitDOMapper;
    @Test
    void test() {
//        String file = "C:\\project\\java\\enlish_edu\\enlish\\enlish-service\\src\\test\\java\\com\\yinlihupo\\enlish\\service\\mapper\\八下.xlsx";
//        HashMap<String, Integer> map = new HashMap<>();
//        int gradeId = 8;
//        try (FileInputStream fis = new FileInputStream(file); Workbook workbook = new XSSFWorkbook(fis)) {
//
//            Sheet sheet = workbook.getSheetAt(0);
//
//            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
//                Row row = sheet.getRow(i);
//                if (row == null) {
//                    continue;
//                }
//
//
//                String word = row.getCell(0).getStringCellValue();
//                String pronunciation = row.getCell(1) != null ? row.getCell(1).getStringCellValue() : "";
//                String pos = row.getCell(2) != null ? row.getCell(2).getStringCellValue() : "";
//                String meaning = row.getCell(3) != null ? row.getCell(3).getStringCellValue() : "";
//                String gradeUnit = row.getCell(4) != null ? row.getCell(4).getStringCellValue() : "";
//
//                String word = row.getCell(0).getStringCellValue();
//                String meaning = row.getCell(1) != null ? row.getCell(1).getStringCellValue() : "";
//                String gradeUnit = row.getCell(2) != null ? row.getCell(2).getStringCellValue() : "";
//                String pronunciation = "";
//                String pos = "";
//                if (word.contains("Unit")) {
//                    continue;
//                }
//
//                int gradeUnitId;
//                if (map.containsKey(gradeUnit)) {
//                    gradeUnitId = map.get(gradeUnit);
//                } else {
//                    UnitDO unitDO = unitDOMapper.selectByTitle(gradeUnit);
//                    if (unitDO == null) {
//                        unitDO = UnitDO.builder()
//                                .title(gradeUnit)
//                                .version("人教版")
//                                .createAt(LocalDateTime.now())
//                                .build();
//                        unitDOMapper.insert(unitDO);
//                        gradeUnitDOMapper.insert(GradeUnitDO.builder().unitId(unitDO.getId()).gradeId(gradeId).build());
//                        gradeUnitId = unitDO.getId();
//                    } else {
//                        gradeUnitId = unitDO.getId();
//                    }
//
//                    map.put(gradeUnit, gradeUnitId);
//                }
//                VocabularyBankDO vocabularyBankDO = VocabularyBankDO.builder()
//                        .word(word)
//                        .definition(meaning)
//                        .pronunciation(pronunciation)
//                        .pos(pos)
//                        .unitId(gradeUnitId)
//                        .build();
//                vocabularyBankMapper.insertSelective(vocabularyBankDO);
//                log.info("插入数据 {} ", vocabularyBankDO);
//            }
//
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
    }
}
