CarConfigInfoDataToolsImpl.java 4.79 KB
package com.bsth.service.schedule.datatools;

import com.bsth.service.schedule.exception.ScheduleException;
import com.bsth.service.schedule.utils.DataToolsFile;
import com.bsth.service.schedule.utils.DataToolsProperties;
import com.bsth.service.schedule.utils.DataToolsService;
import com.bsth.util.I18n;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * Created by xu on 17/5/16.
 */
@Service(value = "carConfig_dataTool")
public class CarConfigInfoDataToolsImpl implements DataToolsService {
    /** 日志记录器 */
    private final static Logger LOGGER = LoggerFactory.getLogger(CarConfigInfoDataToolsImpl.class);

    @Autowired
    @Qualifier(value = "dataToolsServiceImpl")
    private DataToolsService dataToolsService;

    @Autowired
    private DataToolsProperties dataToolsProperties;

    @Override
    public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
        return this.dataToolsService.uploadFile(filename, filedata);
    }

    @Override
    public void importData(File file, Map<String, Object> params) throws ScheduleException {
        try {
            LOGGER.info("//---------------- 导入车辆配置信息 start... ----------------//");
            // 创建ktr转换所需参数
            Map<String, Object> ktrParms = new HashMap<>();
            String country = Locale.getDefault().getLanguage();
            File ktrFile = null;
            if ("zh".equals(country)) {
                ktrFile = new File(this.getClass().getResource(
                        dataToolsProperties.getZhVehicleConfigDataImport()).toURI());
            } else if ("en".equals(country)) {
                ktrFile = new File(this.getClass().getResource(
                        dataToolsProperties.getEnVehicleConfigDataImport()).toURI());
            } else {
                throw new Exception("not found Local[" + country + "] corresponding ktr.");
            }

            // 通用参数,转换文件路径,excel输入文件路径,错误输出文件路径
            ktrParms.put("transpath", ktrFile.getAbsolutePath());
            ktrParms.put("filepath", file.getAbsolutePath());
            ktrParms.put("erroroutputdir", dataToolsProperties.getTransErrordir());

            ktrParms.putAll(params);

            dataToolsService.importData(file, ktrParms);

            LOGGER.info("//---------------- 导入车辆配置信息 success... ----------------//");
        } catch (Exception exp) {
            LOGGER.info("//---------------- 导入车辆配置信息 failed... ----------------//");

            StringWriter sw = new StringWriter();
            exp.printStackTrace(new PrintWriter(sw));
            LOGGER.info(sw.toString());

            throw new ScheduleException(exp.getMessage());
        }
    }

    @Override
    public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
        try {
            LOGGER.info("//---------------- 导出车辆配置信息 start... ----------------//");
            // 创建ktr转换所需参数
            Map<String, Object> ktrParms = new HashMap<>();
            String country = Locale.getDefault().getLanguage();
            File ktrFile = null;
            if ("zh".equals(country)) {
                ktrFile = new File(this.getClass().getResource(
                        dataToolsProperties.getZhVehicleConfigDataExport()).toURI());
            } else if ("en".equals(country)) {
                ktrFile = new File(this.getClass().getResource(
                        dataToolsProperties.getEnVehicleConfigDataExport()).toURI());
            } else {
                throw new Exception("not found Local[" + country + "] corresponding ktr.");
            }

            // 通用参数,转换文件路径,excel输出文件名
            ktrParms.put("transpath", ktrFile.getAbsolutePath());
            ktrParms.put("filename", I18n.getInstance().getMessage("txt-1616") + "_download-");

            ktrParms.putAll(params);

            DataToolsFile file = dataToolsService.exportData(ktrParms);

            LOGGER.info("//---------------- 导出车辆配置信息 success... ----------------//");

            return file;

        } catch (Exception exp) {
            LOGGER.info("//---------------- 导出车辆配置信息 failed... ----------------//");

            StringWriter sw = new StringWriter();
            exp.printStackTrace(new PrintWriter(sw));
            LOGGER.info(sw.toString());

            throw new ScheduleException(exp.getMessage());
        }
    }
}