TTInfoServiceImpl.java 7.2 KB
package com.bsth.service.schedule.impl;

import com.bsth.entity.schedule.TTInfo;
import com.bsth.entity.schedule.TTInfoBackup;
import com.bsth.entity.schedule.TTInfoDetail;
import com.bsth.repository.schedule.TTInfoBackupRepository;
import com.bsth.repository.schedule.TTInfoDetailRepository;
import com.bsth.repository.schedule.TTInfoRepository;
import com.bsth.service.schedule.TTInfoService;
import com.bsth.service.schedule.exception.ScheduleException;
import com.bsth.service.schedule.utils.TimeTableProto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xu on 16/12/20.
 */
@Service
public class TTInfoServiceImpl extends BServiceImpl<TTInfo, Long> implements TTInfoService {
    /** 日志记录器 */
    private static final Logger LOG = LoggerFactory.getLogger(TTInfoServiceImpl.class);

    @Autowired
    private TTInfoRepository ttInfoRepository;
    @Autowired
    private TTInfoDetailRepository ttInfoDetailRepository;
    @Autowired
    private TTInfoBackupRepository ttInfoBackupRepository;

    @Override
    public void validate_name(TTInfo ttInfo) throws ScheduleException {
        // 名字重复验证
        Map<String, Object> param = new HashMap<>();
        if (ttInfo.getId() != null) {
            param.put("id_ne", ttInfo.getId());
        }
        param.put("xl.id_eq", ttInfo.getXl().getId());
        param.put("name_eq", ttInfo.getName());

        if (!CollectionUtils.isEmpty(list(param))) {
            throw new ScheduleException("名字重复");
        }
    }

    @Override
    public void validate_n_d(TTInfo ttInfo) throws ScheduleException {
        // 常规有效日重复验证
        // 找出所有未作废,已启用的时刻表,验证
        Map<String, Object> param = new HashMap<>();
        if (ttInfo.getId() != null) {
            param.put("id_ne", ttInfo.getId());
        }
        param.put("xl.id_eq", ttInfo.getXl().getId());
        param.put("isCancel_eq", false);
        param.put("isEnableDisTemplate_eq", true);
        List<TTInfo> ttInfos = list(param);
        if (StringUtils.isEmpty(ttInfo.getRule_days())) {
            throw new ScheduleException("常规有效日为空");
        } else {
            String[] nds = ttInfo.getRule_days().split(",");
            for (TTInfo t : ttInfos) {
                String[] nds_e = t.getRule_days().split(",");
                for (int i = 0; i < 7; i++) {
                    if ("0".equals(nds[i])) {
                        //
                    } else {
                        if (nds[i].equals(nds_e[i])) {
                            throw new ScheduleException("当前常规有效日期已经使用");
                        }
                    }
                }
            }
        }
    }

    @Override
    public void validate_s_d(TTInfo ttInfo) throws ScheduleException {
        // 特殊有效日重复验证
        // 找出所有未作废,已启用的时刻表,验证
        Map<String, Object> param = new HashMap<>();
        if (ttInfo.getId() != null) {
            param.put("id_ne", ttInfo.getId());
        }

        param.put("xl.id_eq", ttInfo.getXl().getId());
        param.put("isCancel_eq", false);
        param.put("isEnableDisTemplate_eq", true);
        List<TTInfo> ttInfos = list(param);
        if (StringUtils.isEmpty(ttInfo.getSpecial_days())) {
            //
        } else {
            String[] sds = ttInfo.getSpecial_days().split(",");
            for (TTInfo t : ttInfos) {
                if (StringUtils.isEmpty(t.getSpecial_days())) {
                    //
                } else {
                    for (String sd : sds) {
                        if (t.getSpecial_days().indexOf(sd) != -1) {
                            throw new ScheduleException("当前特殊日期已经使用");
                        }
                    }
                }
            }
        }
    }


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

    @Transactional
    @Override
    public void toggleCancel(Long id) throws ScheduleException {
        TTInfo ttInfo = findById(id);
        if (ttInfo.getIsCancel()) {
            ttInfo.setIsCancel(false);
        } else {
            ttInfo.setIsCancel(true);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public void backUp(Long ttInfoId) throws ScheduleException {
        LOG.info(">>>>>>开始备份时刻表<<<<<<");

        try {
            // 获取原始数据
            TTInfo ttInfo = ttInfoRepository.findOne(ttInfoId);
            List<TTInfoDetail> ttInfoDetails = ttInfoDetailRepository.findByTtinfoId(ttInfoId);

            // protobuf序列化成二进制数据
            TimeTableProto.TTInfo.Builder tb = ttInfo.toProtoBuilder();
            for (TTInfoDetail ttInfoDetail : ttInfoDetails) {
                tb.addBcInfo(ttInfoDetail.toProtoBuilder());
            }
            byte[] backupbytes = tb.build().toByteArray();
            LOG.info("......时刻表={}", ttInfo.getName());
            LOG.info("......时刻表protoBuf字节数={}", backupbytes.length);

            // 更新备份日期
            Date backupdate = new Date();
            ttInfo.setLastBackUpDate(backupdate);

            // 保存备份数据
            TTInfoBackup ttInfoBackup = new TTInfoBackup();
            ttInfoBackup.setXl(ttInfo.getXl().getId());
            ttInfoBackup.setXlName(ttInfo.getXl().getName());
            ttInfoBackup.setTtInfo(ttInfoId);
            ttInfoBackup.setTtInfoName(ttInfo.getName());
            ttInfoBackup.setBackUpDate(backupdate);
            ttInfoBackup.setBackUpInfo(backupbytes);

            ttInfoBackupRepository.save(ttInfoBackup);
//        System.out.println(backupbytes.length);
//        try {
//
//            TimeTableProto.TTInfo tt1 = TimeTableProto.TTInfo.parseFrom(backupbytes);
//            System.out.println(tt1.getName());
//            System.out.println(tt1.getBcInfoCount());
//        } catch (Exception exp) {
//            exp.printStackTrace();
//        }

            LOG.info(">>>>>>备份时刻表success<<<<<<");
        } catch (Exception exp) {
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            exp.printStackTrace(printWriter);
            LOG.info("......异常stack->{}", stringWriter.toString());
            LOG.info(">>>>>>备份时刻表failed<<<<<<");
            throw new ScheduleException(exp);
        }


    }
}