CarConfigInfoServiceImpl.java 7.47 KB
package com.bsth.service.schedule.impl;

import com.bsth.entity.Cars;
import com.bsth.entity.schedule.CarConfigInfo;
import com.bsth.entity.schedule.rule.ScheduleRule1Flat;
import com.bsth.entity.sys.CompanyAuthority;
import com.bsth.service.schedule.CarConfigInfoService;
import com.bsth.service.schedule.CarsService;
import com.bsth.service.schedule.ScheduleRule1FlatService;
import com.bsth.service.schedule.exception.ScheduleException;
import com.bsth.service.schedule.utils.DataToolsFile;
import com.bsth.service.schedule.utils.DataToolsService;
import com.bsth.util.I18n;
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.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    @Qualifier(value = "carConfig_dataTool")
    private DataToolsService dataToolsService;

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

    @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 void validate_cars_gs(CarConfigInfo carConfigInfo, List<CompanyAuthority> companyAuthorityList) throws ScheduleException {
        if (CollectionUtils.isEmpty(companyAuthorityList)) {
            throw new ScheduleException(I18n.getInstance().getMessage("carConfigInfoServiceImpl-line57"));
        }

        boolean isFind = false;
        Cars cars = carsService.findById(carConfigInfo.getCl().getId());
        for (CompanyAuthority companyAuthority : companyAuthorityList) {
            if (companyAuthority.getCompanyCode().equals(cars.getBusinessCode())) {
                isFind = true;
                break;
            }
        }
        if (!isFind) {
            throw new ScheduleException(I18n.getInstance().getMessage("carConfigInfoServiceImpl-line69"));
        }
    }

    @Override
    public void validate_cars_fgs(CarConfigInfo carConfigInfo, List<CompanyAuthority> companyAuthorityList) throws ScheduleException {
        if (CollectionUtils.isEmpty(companyAuthorityList)) {
            throw new ScheduleException(I18n.getInstance().getMessage("carConfigInfoServiceImpl-line76"));
        }

        boolean isFind = false;
        Cars cars = carsService.findById(carConfigInfo.getCl().getId());
        for (CompanyAuthority companyAuthority : companyAuthorityList) {
            if (companyAuthority.getCompanyCode().equals(cars.getBusinessCode())) {
                isFind = true;
                break;
            }
        }
        if (!isFind) {
            // 如果没有公司权限,验证通过
            return;
        }

        isFind = false;
        for (CompanyAuthority companyAuthority : companyAuthorityList) {
            if (companyAuthority.getCompanyCode().equals(cars.getBusinessCode()) &&
                    companyAuthority.getSubCompanyCode().equals(cars.getBrancheCompanyCode())) {
                isFind = true;
                break;
            }
        }
        if (!isFind) {
            throw new ScheduleException(I18n.getInstance().getMessage("carConfigInfoServiceImpl-line101"));
        }
    }

    @Transactional
    public void validate_cars(CarConfigInfo carConfigInfo) throws ScheduleException {
        // 相同车辆不能同时配置
        Map<String, Object> param = new HashMap<>();
        if (carConfigInfo.getId() != null) {
            param.put("id_ne", carConfigInfo.getId());
        }

        if (carConfigInfo.getXl() == null ||
                carConfigInfo.getXl().getId() == null ||
                carConfigInfo.getXl().getName() == null) {
            throw new ScheduleException(I18n.getInstance().getMessage("carConfigInfoServiceImpl-line116"));
        } else {
//            param.put("xl.id_eq", carConfigInfo.getXl().getId());
            param.put("isCancel_eq", false);
            if (carConfigInfo.getCl() == null || carConfigInfo.getCl().getId() == null) {
                throw new ScheduleException(I18n.getInstance().getMessage("carConfigInfoServiceImpl-line121"));
            } else {
                param.put("cl.id_eq", carConfigInfo.getCl().getId());
                List<CarConfigInfo> carConfigInfos = list(param);
                if (!CollectionUtils.isEmpty(carConfigInfos)) {
                    throw new ScheduleException(I18n.getInstance().getMessage("carConfigInfoServiceImpl-line126",
                            carConfigInfos.get(0).getXl().getName()));
                }
            }
        }

    }

    @Override
    public void validate_cars(Integer xlId, Integer clId) throws ScheduleException {
        Map<String, Object> param = new HashMap<>();
        param.put("cl.id_eq", clId);
        param.put("isCancel_eq", false);
        List<CarConfigInfo> carConfigInfos = list(param);
        for (CarConfigInfo carConfigInfo : carConfigInfos) {
            if (!carConfigInfo.getXl().getId().equals(xlId)) {
                throw new ScheduleException(I18n.getInstance().getMessage("carConfigInfoServiceImpl-line142",
                        carConfigInfo.getXl().getName()));
            }
        }
    }

    @Override
    public void validate_cars_config(CarConfigInfo carConfigInfo) throws ScheduleException {
        Map<String, Object> param = new HashMap<>();
        param.put("xl.id_eq", carConfigInfo.getXl().getId());
        param.put("cl.id_eq", carConfigInfo.getCl().getId());
        param.put("isCancel_eq", false);
        List<CarConfigInfo> carConfigInfos = list(param);
        if (CollectionUtils.isEmpty(carConfigInfos)) {
            throw new ScheduleException(I18n.getInstance().getMessage("carConfigInfoServiceImpl-line156"));
        }
    }

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

    @Transactional
    public void toggleCancel(Long id) throws ScheduleException {
        CarConfigInfo carConfigInfo = findById(id);
        Map<String, Object> param = new HashMap<>();
        if (carConfigInfo.getIsCancel()) {
            validate_cars(carConfigInfo);
            carConfigInfo.setIsCancel(false);
        } else {
            param.clear();
            param.put("carConfigInfo.id_eq", carConfigInfo.getId());
            List<ScheduleRule1Flat> scheduleRule1Flats = (List<ScheduleRule1Flat>) scheduleRule1FlatService.list(param);
            if (CollectionUtils.isEmpty(scheduleRule1Flats)) {
                carConfigInfo.setIsCancel(true);
            } else {
                throw new ScheduleException(I18n.getInstance().getMessage("carConfigInfoServiceImpl-line180"));
            }
        }
    }

}