CarsServiceImpl.java 5.45 KB
package com.bsth.service.schedule.impl;

import com.bsth.entity.CarDevice;
import com.bsth.entity.Cars;
import com.bsth.entity.schedule.CarConfigInfo;
import com.bsth.repository.schedule.CarConfigInfoRepository;
import com.bsth.service.schedule.CarDeviceService;
import com.bsth.service.schedule.CarsService;
import com.bsth.service.schedule.exception.ScheduleException;
import com.bsth.service.schedule.utils.DataToolsFile;
import com.bsth.service.schedule.utils.DataToolsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xu on 16/12/8.
 */
@Service(value = "carsServiceImpl_sc")
public class CarsServiceImpl extends BServiceImpl<Cars, Integer> implements CarsService {
    @Autowired
    @Qualifier(value = "cars_dataTool")
    private DataToolsService dataToolsService;

    @Autowired
    @Qualifier(value = "carDeviceServiceImpl_sc")
    private CarDeviceService carDeviceService;

    @Autowired
    private CarConfigInfoRepository carConfigInfoRepository;

    @Override
    public Cars save(Cars cars) {
        if (cars.getId() != null && cars.getScrapState()) { // 更新车辆信息,报废车辆
            // 1、作废的车辆,修改报废号
            String eCode = cars.getEquipmentCode();
            cars.setEquipmentCode("BF-" + eCode);
            cars.setScrapCode("BF-" + cars.getEquipmentCode());
            // 2、添加一条相关的设备替换记录
            // 查找在哪条线路上
            List<CarConfigInfo> carConfigInfoList = carConfigInfoRepository.findByClId(cars.getId());
            for (CarConfigInfo carConfigInfo : carConfigInfoList) {
                CarDevice carDevice = new CarDevice();
                carDevice.setGsName(cars.getCompany());
                carDevice.setCompany(cars.getBusinessCode());
                carDevice.setBrancheCompany(cars.getBrancheCompanyCode());
                carDevice.setCl(cars.getId());
                carDevice.setClZbh(cars.getInsideCode());
                carDevice.setXl(carConfigInfo.getXl().getId());
                carDevice.setXlName(carConfigInfo.getXl().getName());
                carDevice.setXlBm(carConfigInfo.getXl().getLineCode());
                carDevice.setOldDeviceNo(eCode);
                carDevice.setNewDeviceNo("BF-" + eCode);
                carDevice.setIsCancel(false);

                carDevice.setCreateBy(cars.getCreateBy());
                carDevice.setUpdateBy(cars.getUpdateBy());
                carDevice.setCreateDate(new Date());
                carDevice.setUpdateDate(new Date());
                carDeviceService.save(carDevice);
            }
        }

        return super.save(cars);
    }

    @Override
    public void importData(File file, Map<String, Object> params) throws ScheduleException {
        dataToolsService.importData(file, params);
    }

    @Override
    public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
        return dataToolsService.exportData(params);
    }

    @Override
    public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
        return dataToolsService.uploadFile(filename, filedata);
    }

    @Override
    @Transactional
    public void validate_nbbh(Cars cars) throws ScheduleException {
        // 查询条件
        Map<String, Object> param = new HashMap<>();
        if (cars.getId() != null) {
            param.put("id_ne", cars.getId());
        }
        param.put("insideCode_eq", cars.getInsideCode());
        if (!CollectionUtils.isEmpty(list(param))) {
            throw new ScheduleException("车辆内部编号/自编号重复");
        }
    }

    @Override
    @Transactional
    public void validate_clbh(Cars cars) throws ScheduleException {
        // 查询条件
        Map<String, Object> param = new HashMap<>();
        if (cars.getId() != null) {
            param.put("id_ne", cars.getId());
        }
        param.put("carCode_eq", cars.getCarCode());
        if (!CollectionUtils.isEmpty(list(param))) {
            throw new ScheduleException("车辆编号重复");
        }
    }

    @Override
    @Transactional
    public void validate_cph(Cars cars) throws ScheduleException {
        // 查询条件
        Map<String, Object> param = new HashMap<>();
        if (cars.getId() != null) {
            param.put("id_ne", cars.getId());
        }
        param.put("carPlate_eq", cars.getCarPlate());
        if (!CollectionUtils.isEmpty(list(param))) {
            throw new ScheduleException("车牌号重复");
        }
    }

    @Override
    @Transactional
    public void validate_sbbh(Cars cars) throws ScheduleException {
        // 查询条件
        Map<String, Object> param = new HashMap<>();
        if (cars.getId() != null) {
            param.put("id_ne", cars.getId());
        }
        param.put("scrapState_eq", false);
        param.put("equipmentCode_eq", cars.getEquipmentCode());
        if (!CollectionUtils.isEmpty(list(param))) {
            throw new ScheduleException("设备编号重复");
        }
    }
}