CarConfigInfoServiceImpl.java 1.97 KB
package com.bsth.service.schedule;

import com.bsth.common.ResponseCode;
import com.bsth.entity.schedule.CarConfigInfo;
import com.bsth.entity.schedule.rule.ScheduleRule1Flat;
import com.bsth.repository.schedule.CarConfigInfoRepository;
import com.bsth.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.*;

/**
 * Created by xu on 16/5/9.
 */
@Service
public class CarConfigInfoServiceImpl extends BaseServiceImpl<CarConfigInfo, Long> implements CarConfigInfoService {
    @Autowired
    private CarConfigInfoRepository carConfigInfoRepository;
    @Autowired
    private ScheduleRule1FlatService scheduleRule1FlatService;

    @Override
    @Transactional
    public Map<String, Object> delete(Long aLong) {
        // 获取待作废的车辆配置
        CarConfigInfo carConfigInfo = carConfigInfoRepository.findOne(aLong);
        // 查询有无规则使用过此车辆配置
        Map<String, Object> param = new HashMap<>();
        param.put("carConfigInfo.id_eq", carConfigInfo.getId());
        Iterator<ScheduleRule1Flat> scheduleRule1FlatIterator =
                scheduleRule1FlatService.list(param).iterator();
        boolean isExist = false;
        while (scheduleRule1FlatIterator.hasNext()) {
            ScheduleRule1Flat scheduleRule1Flat = scheduleRule1FlatIterator.next();
            if (scheduleRule1Flat.getCarConfigInfo().getId().equals(carConfigInfo.getId())) {
                isExist = true;
                break;
            }
        }

        Map<String, Object> map = new HashMap<>();

        if (isExist) {
            throw new RuntimeException("车辆配置已被规则使用,不能删除,请先修改规则!");
        } else {
            carConfigInfo.setIsCancel(true);
            map.put("status", ResponseCode.SUCCESS);
        }

        return map;

    }
}