CarsServiceImpl.java 5.49 KB
package com.bsth.service.schedule.impl;

import com.bsth.entity.Cars;
import com.bsth.service.schedule.CarsService;
import com.bsth.service.schedule.exception.ScheduleException;
import com.bsth.service.schedule.utils.DataToolsProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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

/**
 * Created by xu on 16/12/8.
 */
@EnableConfigurationProperties(DataToolsProperties.class)
@Service(value = "carsServiceImpl_sc")
public class CarsServiceImpl extends BServiceImpl<Cars, Integer> implements CarsService {
    /** 日志记录器 */
    private static final Logger LOGGER = LoggerFactory.getLogger(CarsServiceImpl.class);

    @Autowired
    private DataToolsProperties dataToolsProperties;

    @Override
    public void importData(File file, Map<String, Object> params) throws ScheduleException {
        try {
            LOGGER.info("//---------------- 导入车辆基础信息 start... ----------------//");
            // 创建ktr转换所需参数
            Map<String, Object> ktrParms = new HashMap<>();
            File ktrFile = new File(this.getClass().getResource(
                    dataToolsProperties.getCarsDatainputktr()).toURI());

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

            super.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 File exportData(Map<String, Object> params) throws ScheduleException {
        try {
            LOGGER.info("//---------------- 导出车辆基础信息 start... ----------------//");
            // 创建ktr转换所需参数
            Map<String, Object> ktrParms = new HashMap<>();
            File ktrFile = new File(this.getClass().getResource(
                    dataToolsProperties.getCarsDataoutputktr()).toURI());

            // 通用参数,转换文件路径,excel输出文件名
            ktrParms.put("transpath", ktrFile.getAbsolutePath());
            ktrParms.put("filename", "车辆基础信息_download-");

            File file = super.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());
        }
    }

    @Override
    @Transactional
    public void validate_nbbh(Cars cars) throws ScheduleException {
        // 查询条件
        Map<String, Object> param = new HashMap<>();
        if (cars.getId() != null) {
            param.put("id_ne", cars.getId());
        }
        param.put("insideCode_eq", cars.getInsideCode());
        if (!CollectionUtils.isEmpty(list(param))) {
            throw new ScheduleException("车辆内部编号/自编号重复");
        }
    }

    @Override
    @Transactional
    public void validate_clbh(Cars cars) throws ScheduleException {
        // 查询条件
        Map<String, Object> param = new HashMap<>();
        if (cars.getId() != null) {
            param.put("id_ne", cars.getId());
        }
        param.put("carCode_eq", cars.getCarCode());
        if (!CollectionUtils.isEmpty(list(param))) {
            throw new ScheduleException("车辆编号重复");
        }
    }

    @Override
    @Transactional
    public void validate_cph(Cars cars) throws ScheduleException {
        // 查询条件
        Map<String, Object> param = new HashMap<>();
        if (cars.getId() != null) {
            param.put("id_ne", cars.getId());
        }
        param.put("carPlate_eq", cars.getCarPlate());
        if (!CollectionUtils.isEmpty(list(param))) {
            throw new ScheduleException("车牌号重复");
        }
    }

    @Override
    @Transactional
    public void validate_sbbh(Cars cars) throws ScheduleException {
        // 查询条件
        Map<String, Object> param = new HashMap<>();
        if (cars.getId() != null) {
            param.put("id_ne", cars.getId());
        }
        param.put("equipmentCode_eq", cars.getEquipmentCode());
        if (!CollectionUtils.isEmpty(list(param))) {
            throw new ScheduleException("设备编号重复");
        }
    }
}