package com.unis.biz.modular.test.service.impl;

import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson2.JSON;
import com.unis.biz.modular.test.entity.TestEntity;
import com.unis.biz.modular.test.service.TestService;
import com.unis.sys.modular.user.param.SysUserImportParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author ybw
 * @version V1.0
 * @className TestServiceImpl
 * @date 2024/4/25
 **/
@Service
@Slf4j
public class TestServiceImpl implements TestService {


    @Override
    public String importExcel(MultipartFile file) {
        try {
            // 创建临时文件
            File tempFile = FileUtil.writeBytes(file.getBytes(), FileUtil.file(FileUtil.getTmpDir() + FileUtil.FILE_SEPARATOR + "temp.xlsx"));
            /**
             * 读取excel
             * headRowNumber(1)方法指定Excel的头部行数为1行。
             * doReadSync()方法同步读取Excel内容
             */
            List<TestEntity> testEntityList = EasyExcel.read(tempFile).head(TestEntity.class).sheet().headRowNumber(1).doReadSync();
            testEntityList.forEach(testEntity -> {
                log.info("testEntity:{}", JSON.toJSONString(testEntity));
            });
        } catch (IOException e) {
            log.error("TestServiceImpl importUser error:", e);
        }
        return "OK";
    }
}
