package cn.estsh.i3plus.core.apiservice.serviceimpl.busi;

import cn.estsh.i3plus.pojo.platform.bean.*;
import cn.estsh.i3plus.pojo.platform.repository.SysConfigRepository;
import cn.estsh.i3plus.pojo.platform.repository.SysDictionaryRepository;
import cn.estsh.i3plus.pojo.platform.repository.SysLocaleLanguageRepository;
import cn.estsh.i3plus.pojo.platform.repository.SysLocaleResourceRepository;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description :
 * @Reference :
 * @Author : yunhao
 * @CreateDate : 2018-11-22 11:01
 * @Modify:
 **/
public class TestExcelSysConfig extends cn.estsh.i3plus.core.apiservice.serviceimpl.busi.TestBase {

    public static final Logger LOGGER = LoggerFactory.getLogger(cn.estsh.i3plus.core.apiservice.serviceimpl.busi.TestExcelPermission.class);

    // 文件路径
    public static final String PATH_NAME = "init/sys-config.xlsx";
    // 权限Sheet 名称
    public static final String SHEET_SYS_CONFIG = "sys-config";
    public static final String SHEET_DICTIONARY = "dictionary";
    public static final String SHEET_LANGUAGE = "language";
    public static final String SHEET_RESOURCE = "resource";

    @Autowired
    private SysConfigRepository sysConfigRDao;
    @Autowired
    private SysDictionaryRepository sysDictionaryRDao;
    @Autowired
    private SysLocaleLanguageRepository sysLocaleLanguageRDao;
    @Autowired
    private SysLocaleResourceRepository sysLocaleResourceRDao;

    @Test
    public void testInit() throws Exception {
        XSSFWorkbook workbook = getWorkbook(PATH_NAME);
        if(workbook != null){
            XSSFSheet sheetSysConfig = workbook.getSheet(SHEET_SYS_CONFIG);
            XSSFSheet sheetDictionary = workbook.getSheet(SHEET_DICTIONARY);
            XSSFSheet sheetLanguage = workbook.getSheet(SHEET_LANGUAGE);
            XSSFSheet sheetResource = workbook.getSheet(SHEET_RESOURCE);

            List<SysConfig> sysConfigList = getSysConfig(sheetSysConfig);
            List<SysDictionary> sysDictionaryList = getSysDictionary(sheetDictionary);
            List<SysLocaleLanguage> sysLocaleLanguageList = getSysLocaleLanguage(sheetLanguage);
            List<SysLocaleResource> sysLocaleResourceList= getSysLocaleResource(sheetResource);

            LOGGER.info("System Init SysConfig Size:{}",sysConfigList.size());
            LOGGER.info("System Init SysDictionary Size:{}",sysDictionaryList.size());
            LOGGER.info("System Init SysLocaleLanguage Size:{}",sysLocaleLanguageList.size());
            LOGGER.info("System Init SysLocaleResource Size:{}",sysLocaleResourceList.size());

            sysConfigRDao.saveAll(sysConfigList);
            sysDictionaryRDao.saveAll(sysDictionaryList);
            sysLocaleLanguageRDao.saveAll(sysLocaleLanguageList);
            sysLocaleResourceRDao.saveAll(sysLocaleResourceList);
        }
    }

    public List<SysConfig> getSysConfig(XSSFSheet sheet){
        List<SysConfig> result = new ArrayList<>();
        if(sheet != null){
            if(sheet.getLastRowNum() >= 1){
                SysConfig obj = null;
                for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                    try {
                        Row row = sheet.getRow(i);//获取索引为i的行，以0开始
                        if(row.getCell(0).toString().trim().length() > 0){
                            obj = new SysConfig();
                            obj.setId(Long.parseLong(row.getCell(0).getStringCellValue()));
                            obj.setName(row.getCell(1).getStringCellValue());
                            obj.setConfigType(Integer.valueOf(row.getCell(2).getStringCellValue()));
                            obj.setConfigCode(row.getCell(3).getStringCellValue());
                            obj.setConfigValue(row.getCell(4).getStringCellValue());
                            obj.setConfigDescription(row.getCell(5).getStringCellValue());
                            obj.setIsValid(1);

                            result.add(obj);
                        }
                    }catch (Exception e){
                        LOGGER.error("Excel Sheet Name :{} Index:{} DataType Error", sheet.getSheetName(), i);
                    }
                }
            }
        }
        return result;
    }

    public List<SysDictionary> getSysDictionary(XSSFSheet sheet){
        List<SysDictionary> result = new ArrayList<>();
        if(sheet != null){
            if(sheet.getLastRowNum() >= 1){
                SysDictionary obj = null;
                for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                    try {
                        Row row = sheet.getRow(i);//获取索引为i的行，以0开始
                        if(row.getCell(0).toString().trim().length() > 0){
                            obj = new SysDictionary();
                            obj.setId(Long.parseLong(row.getCell(0).getStringCellValue()));
                            obj.setName(row.getCell(1).getStringCellValue());
                            obj.setDictionaryCode(row.getCell(2).getStringCellValue());
                            obj.setParentId(Long.valueOf(row.getCell(3).getStringCellValue()));
                            obj.setParentNameRdd(row.getCell(4).getStringCellValue());
                            obj.setParentCodeRdd(row.getCell(5).getStringCellValue());
                            obj.setDictionaryValue(row.getCell(6).getStringCellValue());
                            obj.setDictionarySort(Integer.valueOf(row.getCell(7).getStringCellValue()));
                            obj.setDictionaryDescription(row.getCell(8).getStringCellValue());
                            obj.setIsValid(1);

                            result.add(obj);
                        }
                    }catch (Exception e){
                        LOGGER.error("Excel Sheet Name :{} Index:{} DataType Error", sheet.getSheetName(), i);
                    }
                }
            }
        }
        return result;
    }

    public List<SysLocaleLanguage> getSysLocaleLanguage(XSSFSheet sheet){
        List<SysLocaleLanguage> result = new ArrayList<>();
        if(sheet != null){
            if(sheet.getLastRowNum() >= 1){
                SysLocaleLanguage obj = null;
                for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                    try {
                        Row row = sheet.getRow(i);//获取索引为i的行，以0开始
                        if(row.getCell(0).toString().trim().length() > 0){
                            obj = new SysLocaleLanguage();
                            obj.setId(Long.parseLong(row.getCell(0).getStringCellValue()));
                            obj.setLanguageName(row.getCell(1).getStringCellValue());
                            obj.setLanguageCode(row.getCell(2).getStringCellValue());
                            obj.setLanguageSort(Integer.valueOf(row.getCell(3).getStringCellValue()));
                            obj.setIsDefault(Integer.valueOf(row.getCell(4).getStringCellValue()));
                            obj.setLanguageStatus(Integer.valueOf(row.getCell(5).getStringCellValue()));
                            obj.setIsValid(1);

                            result.add(obj);
                        }
                    }catch (Exception e){
                        LOGGER.error("Excel Sheet Name :{} Index:{} DataType Error", sheet.getSheetName(), i);
                    }
                }
            }
        }
        return result;
    }

    public List<SysLocaleResource> getSysLocaleResource(XSSFSheet sheet){
        List<SysLocaleResource> result = new ArrayList<>();
        if(sheet != null){
            if(sheet.getLastRowNum() >= 1){
                SysLocaleResource obj = null;
                for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                    try {
                        Row row = sheet.getRow(i);//获取索引为i的行，以0开始
                        if(row.getCell(0).toString().trim().length() > 0){
                            obj = new SysLocaleResource();
                            obj.setId(Long.parseLong(row.getCell(0).getStringCellValue()));
                            obj.setResourceType(Integer.valueOf(row.getCell(1).getStringCellValue()));
                            obj.setLanguageCode(row.getCell(2).getStringCellValue());
                            obj.setLanguageNameRdd(row.getCell(3).getStringCellValue());
                            obj.setResourceKey(row.getCell(4).getStringCellValue());
                            obj.setResourceValue(row.getCell(5).getStringCellValue());
                            obj.setIsSystem(Integer.valueOf(row.getCell(6).getStringCellValue()));
                            obj.setIsValid(1);

                            result.add(obj);
                        }
                    }catch (Exception e){
                        LOGGER.error("Excel Sheet Name :{} Index:{} DataType Error", sheet.getSheetName(), i);
                    }
                }
            }
        }
        return result;
    }


    public XSSFWorkbook getWorkbook(String pathName){
        try {
            InputStream in = cn.estsh.i3plus.core.apiservice.serviceimpl.busi.TestExcelPermission.class.getClassLoader().getResourceAsStream(pathName);
            XSSFWorkbook workbook = new XSSFWorkbook(in);
            return workbook;
        }catch (Exception e){
            LOGGER.error(" System Init Sys Data Excel Error file path {} error message :{}",pathName,e.getMessage());
            e.getMessage();
            return null;
        }
    }

}
