EmployeeConfigInfoServiceImpl.java 7.81 KB
package com.bsth.service.schedule.impl;

import com.bsth.entity.schedule.EmployeeConfigInfo;
import com.bsth.entity.schedule.rule.ScheduleRule1Flat;
import com.bsth.service.schedule.EmployeeConfigInfoService;
import com.bsth.service.schedule.exception.ScheduleException;
import com.bsth.service.schedule.ScheduleRule1FlatService;
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.*;

/**
 * Created by xu on 16/5/10.
 */
@EnableConfigurationProperties(DataToolsProperties.class)
@Service
public class EmployeeConfigInfoServiceImpl extends BServiceImpl<EmployeeConfigInfo, Long> implements EmployeeConfigInfoService {
    /** 日志记录器 */
    private static Logger LOGGER = LoggerFactory.getLogger(EmployeeConfigInfoServiceImpl.class);

    @Autowired
    private DataToolsProperties dataToolsProperties;

    @Autowired
    private ScheduleRule1FlatService scheduleRule1FlatService;

    @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.getEmployeesconfigDatainputktr()).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.getEmployeesconfigDataoutputktr()).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());
        }
    }

    @Transactional
    @Override
    public void validate_jsy(EmployeeConfigInfo employeeConfigInfo) throws ScheduleException {
        // 驾驶员不能重复配置
        Map<String, Object> param = new HashMap<>();
        if (employeeConfigInfo.getId() != null) {
            param.put("id_ne", employeeConfigInfo.getId());
        }

        if (employeeConfigInfo.getXl() == null ||
                employeeConfigInfo.getXl().getId() == null ||
                employeeConfigInfo.getXl().getName() == null) {
            throw new ScheduleException("线路未选择");
        } else {
//            param.put("xl.id_eq", employeeConfigInfo.getXl().getId());
            param.put("isCancel_eq", false);
            if (employeeConfigInfo.getJsy() == null || employeeConfigInfo.getJsy().getId() == null) {
                throw new ScheduleException("驾驶员未选择");
            } else {
                param.put("jsy.id_eq", employeeConfigInfo.getJsy().getId());
                List<EmployeeConfigInfo> employeeConfigInfos = list(param);
                if (!CollectionUtils.isEmpty(list(param))) {
                    throw new ScheduleException("驾驶员已经配置在" + employeeConfigInfos.get(0).getXl().getName() + "线路中!");
                }
            }
        }
    }

    @Transactional
    @Override
    public void validate_spy(EmployeeConfigInfo employeeConfigInfo) throws ScheduleException {
        // 售票员不能重复配置
        Map<String, Object> param = new HashMap<>();
        if (employeeConfigInfo.getId() != null) {
            param.put("id_ne", employeeConfigInfo.getId());
        }

        if (employeeConfigInfo.getXl() == null ||
                employeeConfigInfo.getXl().getId() == null ||
                employeeConfigInfo.getXl().getName() == null) {
            throw new ScheduleException("线路未选择");
        } else {
//            param.put("xl.id_eq", employeeConfigInfo.getXl().getId());
            if (employeeConfigInfo.getSpy() == null || employeeConfigInfo.getSpy().getId() == null) {
                throw new ScheduleException("售票员未选择");
            } else {
                param.put("spy.id_eq", employeeConfigInfo.getSpy().getId());
                List<EmployeeConfigInfo> employeeConfigInfos = list(param);
                if (!CollectionUtils.isEmpty(list(param))) {
                    throw new ScheduleException("售票员已经配置在" + employeeConfigInfos.get(0).getXl().getName() + "线路中!");
                }
            }
        }
    }

    @Transactional
    @Override
    public void delete(Long aLong) throws ScheduleException {
        toggleCancel(aLong);
    }

    @Transactional
    @Override
    public void toggleCancel(Long id) throws ScheduleException {
        EmployeeConfigInfo employeeConfigInfo = findById(id);
        Map<String, Object> param = new HashMap<>();
        if (employeeConfigInfo.getIsCancel()) {
            validate_jsy(employeeConfigInfo);
            if (employeeConfigInfo.getSpy() != null) {
                validate_spy(employeeConfigInfo);
            }
            employeeConfigInfo.setIsCancel(false);
        } else {
            param.clear();
            param.put("xl.id_eq", employeeConfigInfo.getXl().getId());
            List<ScheduleRule1Flat> scheduleRule1Flats = (List<ScheduleRule1Flat>) scheduleRule1FlatService.list(param);
            List<String> ryConfigIds = new ArrayList<>();
            for (ScheduleRule1Flat scheduleRule1Flat : scheduleRule1Flats) {
                ryConfigIds.addAll(Arrays.asList(scheduleRule1Flat.getRyConfigIds().split(",")));
            }

            if (ryConfigIds.contains(String.valueOf(id))) {
                throw new ScheduleException("人员配置已被规则使用,不能作废,请先修改规则!");
            } else {
                employeeConfigInfo.setIsCancel(true);
            }
        }
    }

}