GuideboardInfoServiceImpl.java 6.68 KB
package com.bsth.service.schedule.impl;

import com.bsth.entity.schedule.GuideboardInfo;
import com.bsth.entity.schedule.TTInfoDetail;
import com.bsth.service.schedule.GuideboardInfoService;
import com.bsth.service.schedule.TTInfoDetailService;
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.util.CollectionUtils;

import javax.transaction.Transactional;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 路牌信息服务。
 */
@EnableConfigurationProperties(DataToolsProperties.class)
@Service
public class GuideboardInfoServiceImpl extends BServiceImpl<GuideboardInfo, Long> implements GuideboardInfoService {
    /** 日志记录器 */
    private static final Logger LOGGER = LoggerFactory.getLogger(GuideboardInfoServiceImpl.class);

    @Autowired
    private DataToolsProperties dataToolsProperties;

    @Autowired
    private TTInfoDetailService ttInfoDetailService;

    @Override
    public void validate_lpno(GuideboardInfo guideboardInfo) throws ScheduleException {
        // 查询条件
        Map<String, Object> param = new HashMap<>();
        if (guideboardInfo.getId() != null) {
            param.put("id_ne", guideboardInfo.getId());
        }

        if (guideboardInfo.getXl() == null || guideboardInfo.getXl().getId() == null) {
            throw new ScheduleException("线路未选择");
        } else {
            param.put("isCancel_eq", false); // 作废的也算入判定区
            param.put("xl.id_eq", guideboardInfo.getXl().getId());
            param.put("lpNo_eq", guideboardInfo.getLpNo());
            if (!CollectionUtils.isEmpty(list(param))) {
                throw new ScheduleException("路牌编号重复");
            }
        }
    }

    @Override
    public void validate_lpname(GuideboardInfo guideboardInfo) throws ScheduleException {
        // 查询条件
        Map<String, Object> param = new HashMap<>();
        if (guideboardInfo.getId() != null) {
            param.put("id_ne", guideboardInfo.getId());
        }

        if (guideboardInfo.getXl() == null || guideboardInfo.getXl().getId() == null) {
            throw new ScheduleException("线路未选择");
        } else {
            param.put("isCancel_eq", false); // 作废的也算入判定区
            param.put("xl.id_eq", guideboardInfo.getXl().getId());
            param.put("lpName_eq", guideboardInfo.getLpName());
            if (!CollectionUtils.isEmpty(list(param))) {
                throw new ScheduleException("路牌名字重复");
            }
        }
    }

    @Transactional
    @Override
    public void delete(Long aLong) throws ScheduleException {
        // 删除操作就是作废操作
        toggleCancel(aLong);
    }

    // 作废方法
    @Transactional
    public void toggleCancel(Long id) throws ScheduleException {
        GuideboardInfo guideboardInfo = findById(id);
        Map<String, Object> param = new HashMap<>();
        if (guideboardInfo.getIsCancel()) {
            validate_lpno(guideboardInfo);
            validate_lpname(guideboardInfo);
            guideboardInfo.setIsCancel(false);
        } else {
            param.clear();
            param.put("xl.id_eq", guideboardInfo.getXl().getId());
            param.put("ttinfo.isCancel_eq", false);
            param.put("lp.id_eq", guideboardInfo.getId());
            List<TTInfoDetail> ttInfoDetailList = (List<TTInfoDetail>) ttInfoDetailService.list(param);
            if (CollectionUtils.isEmpty(ttInfoDetailList)) {
                guideboardInfo.setIsCancel(true);
            } else {
                throw new ScheduleException("在时刻表" +
                        ttInfoDetailList.get(0).getTtinfo().getName() +
                        "已使用,无法作废!");
            }
        }
    }

    @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.getGuideboardsDatainputktr()).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.getGuideboardsDataoutputktr()).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());
        }
    }

}