CarDeviceServiceImpl.java 2.68 KB
package com.bsth.service.schedule.impl;

import com.bsth.entity.CarDevice;
import com.bsth.entity.Cars;
import com.bsth.service.CarsService;
import com.bsth.service.schedule.CarDeviceService;
import com.bsth.service.schedule.exception.ScheduleException;
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.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xu on 16/12/15.
 */
@Service(value = "carDeviceServiceImpl_sc")
public class CarDeviceServiceImpl extends BServiceImpl<CarDevice, Long> implements CarDeviceService {
    @Autowired
    private CarsService carsService;

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    @Override
    public CarDevice save(CarDevice carDevice) {
        // 查找对应的车辆基础信息,更新设备编号数据
        Cars cars = carsService.findById(carDevice.getCl());
        cars.setEquipmentCode(carDevice.getNewDeviceNo());
        // 设备启用日期使用后台日期
        carDevice.setQyrq(new Date());
        return super.save(carDevice);
    }

    @Transactional
    @Override
    public void validate_qyrq(CarDevice carDevice) throws ScheduleException {
        if (carDevice.getXl() == null) {
            throw new ScheduleException("线路未选择");
        }
        if (carDevice.getCl() == null) {
            throw new ScheduleException("车辆未选择");
        }
        Map<String, Object> param = new HashMap<>();
        if (carDevice.getId() != null) {
            param.put("id_ne", carDevice.getId());
        }
        param.put("xl_eq", carDevice.getXl());
        param.put("cl_eq", carDevice.getCl());
        param.put("isCancel_eq", false); // 未标记作废删除的
        param.put("qyrq_ge", carDevice.getQyrq());
        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 {
        CarDevice carDevice = findById(id);
        if (carDevice.getIsCancel()) {
            carDevice.setIsCancel(false);
        } else {
            carDevice.setIsCancel(true);
        }
    }
}