package com.sckw.example.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.HttpResult;
import com.sckw.example.model.vo.ProjectPageVo;
import com.sckw.example.model.vo.SysEnterpriseVo;
import com.sckw.example.model.vo.SysUserVo;
import com.sckw.excel.common.FileType;
import com.sckw.excel.easyexcel.ExcelImportListener;
import com.sckw.excel.easyexcel.SheetExcelData;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ExcelExportService {
    public List<List> exportList() {
        SysUserVo sysUserVo = new SysUserVo();
        sysUserVo.setId("1");
        sysUserVo.setSystemType(1);
        sysUserVo.setAccount("张三");
        sysUserVo.setPassword("zhangsan");
        sysUserVo.setPhone("zhangsantel");
        SysUserVo sysUserVo1 = new SysUserVo();
        sysUserVo1.setId("2");
        sysUserVo1.setSystemType(2);
        sysUserVo1.setAccount("李四");
        sysUserVo1.setPassword("lisi");
        sysUserVo1.setPhone("lisitel");
        List<SysUserVo> list = new ArrayList<>();
        list.add(sysUserVo1);
        list.add(sysUserVo);
        List<SysEnterpriseVo> enterpriseList = new ArrayList<>();
        SysEnterpriseVo sysEnterpriseVo = new SysEnterpriseVo();
        sysEnterpriseVo.setEnterpriseType(1);
        sysEnterpriseVo.setEnterpriseName("建筑");
        sysEnterpriseVo.setEnterpriseAccount("建筑account");
        SysEnterpriseVo sysEnterpriseVo1 = new SysEnterpriseVo();
        sysEnterpriseVo1.setEnterpriseType(2);
        sysEnterpriseVo1.setEnterpriseName("IT");
        sysEnterpriseVo1.setEnterpriseAccount("ItAccount");
        enterpriseList.add(sysEnterpriseVo);
        enterpriseList.add(sysEnterpriseVo1);
        List<List> dataList = new ArrayList<>();
        dataList.add(list);
        dataList.add(enterpriseList);
        return dataList;
    }

    public void downloadTemplates(HttpServletRequest request, HttpServletResponse response, Integer type) throws UnsupportedEncodingException {
        // 配置文件下载
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream");
        // 下载文件能正常显示中文
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(FileType.getValue(type), "UTF-8"));
        // 实现文件下载
        byte[] buffer = new byte[1024];
        InputStream fis = null;
        BufferedInputStream bis = null;
        try {
            //地址可变动
            fis = this.getClass().getResourceAsStream("/excel/" + FileType.getValue(type));
            bis = new BufferedInputStream(fis);
            OutputStream os = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        } catch (Exception e) {
            log.error("下载异常");
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public List<SysUserVo> export() {
        SysUserVo sysUserVo = new SysUserVo();
        sysUserVo.setId("1");
        sysUserVo.setSystemType(1);
        sysUserVo.setAccount("张三");
        sysUserVo.setPassword("zhangsan");
        sysUserVo.setPhone("zhangsantel");
        SysUserVo sysUserVo1 = new SysUserVo();
        sysUserVo1.setId("2");
        sysUserVo1.setSystemType(2);
        sysUserVo1.setAccount("李四");
        sysUserVo1.setPassword("lisi");
        sysUserVo1.setPhone("lisitel");
        SysUserVo sysUserVo2 = new SysUserVo();
        sysUserVo2.setId("3");
        sysUserVo2.setSystemType(3);
        sysUserVo2.setAccount("王五");
        sysUserVo2.setPassword("wangwu");
        sysUserVo2.setPhone("wangwutel");
        SysUserVo sysUserVo3 = new SysUserVo();
        sysUserVo3.setId("4");
        sysUserVo3.setSystemType(4);
        sysUserVo3.setAccount("陈六");
        sysUserVo3.setPassword("chenliu");
        sysUserVo3.setPhone("chenliutel");
        List<SysUserVo> list = new ArrayList<>();
        list.add(sysUserVo);
        list.add(sysUserVo1);
        list.add(sysUserVo2);
        list.add(sysUserVo3);
        return list;
    }

    public List<SheetExcelData> exportList1() {
        SysUserVo sysUserVo = new SysUserVo();
        sysUserVo.setId("1");
        sysUserVo.setSystemType(1);
        sysUserVo.setAccount("张三");
        sysUserVo.setPassword("zhangsan");
        sysUserVo.setPhone("zhangsantel");
        SysUserVo sysUserVo1 = new SysUserVo();
        sysUserVo1.setId("2");
        sysUserVo1.setSystemType(2);
        sysUserVo1.setAccount("李四");
        sysUserVo1.setPassword("lisi");
        sysUserVo1.setPhone("lisitel");
        List<SysUserVo> list = new ArrayList<>();
        list.add(sysUserVo1);
        list.add(sysUserVo);
        List<SysEnterpriseVo> enterpriseList = new ArrayList<>();
        SysEnterpriseVo sysEnterpriseVo = new SysEnterpriseVo();
        sysEnterpriseVo.setEnterpriseType(1);
        sysEnterpriseVo.setEnterpriseName("建筑");
        sysEnterpriseVo.setEnterpriseAccount("建筑account");
        SysEnterpriseVo sysEnterpriseVo1 = new SysEnterpriseVo();
        sysEnterpriseVo1.setEnterpriseType(2);
        sysEnterpriseVo1.setEnterpriseName("IT");
        sysEnterpriseVo1.setEnterpriseAccount("ItAccount");
        enterpriseList.add(sysEnterpriseVo);
        enterpriseList.add(sysEnterpriseVo1);
        List<List> dataList = new ArrayList<>();
        dataList.add(list);
        dataList.add(enterpriseList);

        List<SheetExcelData> sheetExcelDataArrayList = new ArrayList<>();
        SheetExcelData sheetExcelData = new SheetExcelData();
        sheetExcelData.setDataList(list);
        sheetExcelData.setSheetName("A");
        sheetExcelData.setTClass(List.class);
        SheetExcelData sheetExcelData1 = new SheetExcelData();
        sheetExcelData1.setDataList(enterpriseList);
        sheetExcelData1.setSheetName("B");
        sheetExcelData1.setTClass(List.class);
        sheetExcelDataArrayList.add(sheetExcelData);
        sheetExcelDataArrayList.add(sheetExcelData1);
        return sheetExcelDataArrayList;
    }

    public HttpResult importExcel(MultipartFile file, ExcelImportListener importListener) {
        ExcelReader excelReader = null;
        List<String> failureMsgList = new ArrayList<>();
        try {
            InputStream inputStream = file.getInputStream();
            excelReader = EasyExcel.read(inputStream, importListener).build();
            ReadSheet readSheet = EasyExcel.readSheet(0).head(SysUserVo.class).build();
            excelReader.read(readSheet);
            if (importListener.getErrorList().size() > 0) {
                return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, "导入失败", importListener.getErrorList());
            }
            if (importListener.getDataList().size() == 0) {
                return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, "模板为空");
            }
            importListener.getDataList().clear();
            importListener.getErrorList().clear();
            ReadSheet readSheetSku = EasyExcel.readSheet(1).head(SysEnterpriseVo.class).build();
            excelReader.read(readSheetSku);
            if (importListener.getErrorList().size() > 0) {
                return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, "导入失败", importListener.getErrorList());
            }
            if (importListener.getDataList().size() == 0) {
                return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, "模板为空");
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (excelReader != null) {
                excelReader.finish();
            }
        }
        return HttpResult.ok(HttpStatus.SUCCESS_CODE);
    }

    public List<ProjectPageVo> easyExcelDemo() {
        ProjectPageVo projectPageVo = new ProjectPageVo();
        projectPageVo.setStatus("0");
        projectPageVo.setRemark("0");
        ProjectPageVo projectPageVo1 = new ProjectPageVo();
        projectPageVo1.setStatus("1");
        projectPageVo1.setRemark("0");
        List<ProjectPageVo> list = new ArrayList<>();
        list.add(projectPageVo);
        list.add(projectPageVo1);
        return list;
    }
}