EmployeeConfigInfoServiceImpl.java 15 KB
package com.bsth.service.schedule.impl;

import com.bsth.entity.Personnel;
import com.bsth.entity.schedule.EmployeeConfigInfo;
import com.bsth.entity.schedule.rule.ScheduleRule1Flat;
import com.bsth.entity.sys.CompanyAuthority;
import com.bsth.repository.PersonnelRepository;
import com.bsth.service.schedule.EmployeeConfigInfoService;
import com.bsth.service.schedule.EmployeeService;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * Created by xu on 16/5/10.
 */
@Service
public class EmployeeConfigInfoServiceImpl extends BServiceImpl<EmployeeConfigInfo, Long> implements EmployeeConfigInfoService {
    @Autowired
    private ScheduleRule1FlatService scheduleRule1FlatService;
    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private PersonnelRepository personnelRepository;

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

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @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 Long getMaxDbbm(Integer xlId) {
        String sql = "select max(dbbm + 0) as maxdbbm from bsth_c_s_ecinfo where xl = ?";

        Long maxBM = jdbcTemplate.query(sql, new ResultSetExtractor<Long>() {
            @Override
            public Long extractData(ResultSet rs) throws SQLException, DataAccessException {
                if (rs.next()) {
                    return rs.getLong("maxdbbm");
                } else {
                    return 0L;
                }

            }
        }, xlId);

        return maxBM + 1;
    }

    @Transactional
    @Override
    public void validate_jsy_destroy(EmployeeConfigInfo employeeConfigInfo) throws ScheduleException {
        Personnel jsy = this.personnelRepository.findOne(employeeConfigInfo.getJsy().getId());
        if (jsy.getDestroy() != null && jsy.getDestroy() == 1) {
            throw new ScheduleException("当前驾驶员已经停用!");
        }
    }
    @Transactional
    @Override
    public void validate_spy_destroy(EmployeeConfigInfo employeeConfigInfo) throws ScheduleException {
        Personnel spy = this.personnelRepository.findOne(employeeConfigInfo.getSpy().getId());
        if (spy.getDestroy() != null && spy.getDestroy() == 1) {
            throw new ScheduleException("当前售票员已经停用!");
        }
    }

    @Transactional
    @Override
    public void validate_jsy(EmployeeConfigInfo employeeConfigInfo) throws ScheduleException {
        // 驾驶员不能重复配置
        Map<String, Object> param = new HashMap<>();
        if (employeeConfigInfo.getId() != null) {
            param.put("id_ne", employeeConfigInfo.getId());
        }

        if (employeeConfigInfo.getXl() == null ||
                employeeConfigInfo.getXl().getId() == null ||
                employeeConfigInfo.getXl().getName() == null) {
            throw new ScheduleException("线路未选择");
        } else {
//            param.put("xl.id_eq", employeeConfigInfo.getXl().getId());
            param.put("isCancel_eq", false);
            if (employeeConfigInfo.getJsy() == null || employeeConfigInfo.getJsy().getId() == null) {
                throw new ScheduleException("驾驶员未选择");
            } else {
                param.put("jsy.id_eq", employeeConfigInfo.getJsy().getId());
                List<EmployeeConfigInfo> employeeConfigInfos = list(param);
                if (!CollectionUtils.isEmpty(list(param))) {
                    throw new ScheduleException("驾驶员已经配置在" + employeeConfigInfos.get(0).getXl().getName() + "线路中!");
                }
            }
        }
    }

    @Transactional
    @Override
    public void validate_jsy_config(EmployeeConfigInfo employeeConfigInfo) throws ScheduleException {
        Map<String, Object> param = new HashMap<>();
        param.put("xl.id_eq", employeeConfigInfo.getXl().getId());
        param.put("jsy.id_eq", employeeConfigInfo.getJsy().getId());
        List<EmployeeConfigInfo> employeeConfigInfos = list(param);
        if (CollectionUtils.isEmpty(employeeConfigInfos)) {
            throw new ScheduleException("驾驶员没有配置在当前线路中,不属于当前线路!");
        }
    }

    @Override
    public void validate_jsy_gs(EmployeeConfigInfo employeeConfigInfo, List<CompanyAuthority> companyAuthorityList) throws ScheduleException {
        if (CollectionUtils.isEmpty(companyAuthorityList)) {
            throw new ScheduleException("当前用户没有公司权限!");
        }

        boolean isFind = false;
        Personnel personnel = employeeService.findById(employeeConfigInfo.getJsy().getId());
        for (CompanyAuthority companyAuthority : companyAuthorityList) {
            if (companyAuthority.getCompanyCode().equals(personnel.getCompanyCode())) {
                isFind = true;
                break;
            }
        }
        if (!isFind) {
            throw new ScheduleException("当前驾驶员不在用户所属公司中!");
        }
    }

    @Override
    public void validate_jsy_fgs(EmployeeConfigInfo employeeConfigInfo, List<CompanyAuthority> companyAuthorityList) throws ScheduleException {
        if (CollectionUtils.isEmpty(companyAuthorityList)) {
            throw new ScheduleException("当前用户没有分公司权限!");
        }

        boolean isFind = false;
        Personnel personnel = employeeService.findById(employeeConfigInfo.getJsy().getId());
        for (CompanyAuthority companyAuthority : companyAuthorityList) {
            if (companyAuthority.getCompanyCode().equals(personnel.getCompanyCode())) {
                isFind = true;
                break;
            }
        }
        if (!isFind) {
            // 如果没有公司权限,验证通过
            return;
        }

        isFind = false;
        for (CompanyAuthority companyAuthority : companyAuthorityList) {
            if (companyAuthority.getCompanyCode().equals(personnel.getCompanyCode()) &&
                    companyAuthority.getSubCompanyCode().equals(personnel.getBrancheCompanyCode())) {
                isFind = true;
                break;
            }
        }
        if (!isFind) {
            throw new ScheduleException("当前驾驶员不在用户所属分公司中!");
        }
    }

    @Transactional
    @Override
    public void validate_spy(EmployeeConfigInfo employeeConfigInfo) throws ScheduleException {
        // 售票员不能重复配置
        Map<String, Object> param = new HashMap<>();
        if (employeeConfigInfo.getId() != null) {
            param.put("id_ne", employeeConfigInfo.getId());
        }

        if (employeeConfigInfo.getXl() == null ||
                employeeConfigInfo.getXl().getId() == null ||
                employeeConfigInfo.getXl().getName() == null) {
            throw new ScheduleException("线路未选择");
        } else {
//            param.put("xl.id_eq", employeeConfigInfo.getXl().getId());
            if (employeeConfigInfo.getSpy() == null || employeeConfigInfo.getSpy().getId() == null) {
                throw new ScheduleException("售票员未选择");
            } else {
                param.put("spy.id_eq", employeeConfigInfo.getSpy().getId());
                List<EmployeeConfigInfo> employeeConfigInfos = list(param);
                if (!CollectionUtils.isEmpty(list(param))) {
                    throw new ScheduleException("售票员已经配置在" + employeeConfigInfos.get(0).getXl().getName() + "线路中!");
                }
            }
        }
    }

    @Override
    public void validate_spy_gs(EmployeeConfigInfo employeeConfigInfo, List<CompanyAuthority> companyAuthorityList) throws ScheduleException {
        if (CollectionUtils.isEmpty(companyAuthorityList)) {
            throw new ScheduleException("当前用户没有公司权限!");
        }

        boolean isFind = false;
        Personnel personnel = employeeService.findById(employeeConfigInfo.getSpy().getId());
        for (CompanyAuthority companyAuthority : companyAuthorityList) {
            if (companyAuthority.getCompanyCode().equals(personnel.getCompanyCode())) {
                isFind = true;
                break;
            }
        }
        if (!isFind) {
            throw new ScheduleException("当前售票员不在用户所属公司中!");
        }
    }

    @Override
    public void validate_spy_fgs(EmployeeConfigInfo employeeConfigInfo, List<CompanyAuthority> companyAuthorityList) throws ScheduleException {
        if (CollectionUtils.isEmpty(companyAuthorityList)) {
            throw new ScheduleException("当前用户没有分公司权限!");
        }

        boolean isFind = false;
        Personnel personnel = employeeService.findById(employeeConfigInfo.getSpy().getId());
        for (CompanyAuthority companyAuthority : companyAuthorityList) {
            if (companyAuthority.getCompanyCode().equals(personnel.getCompanyCode())) {
                isFind = true;
                break;
            }
        }
        if (!isFind) {
            // 如果没有公司权限,验证通过
            return;
        }

        isFind = false;
        for (CompanyAuthority companyAuthority : companyAuthorityList) {
            if (companyAuthority.getCompanyCode().equals(personnel.getCompanyCode()) &&
                    companyAuthority.getSubCompanyCode().equals(personnel.getBrancheCompanyCode())) {
                isFind = true;
                break;
            }
        }
        if (!isFind) {
            throw new ScheduleException("当前售票员不在用户所属分公司中!");
        }
    }

    @Transactional
    @Override
    public void validate_spy_config(EmployeeConfigInfo employeeConfigInfo) throws ScheduleException {
        Map<String, Object> param = new HashMap<>();
        param.put("xl.id_eq", employeeConfigInfo.getXl().getId());
        param.put("spy.id_eq", employeeConfigInfo.getSpy().getId());
        List<EmployeeConfigInfo> employeeConfigInfos = list(param);
        if (CollectionUtils.isEmpty(employeeConfigInfos)) {
            throw new ScheduleException("售票员没有配置在当前线路中,不属于当前线路!");
        }
    }

    @Transactional
    @Override
    public List<String> validate_get_destroy_info(List<CompanyAuthority> companyAuthorityList) {
        // 获取公司分公司权限代码
        List<String> gs_fgs_dms = new ArrayList<>();
        for (CompanyAuthority companyAuthority : companyAuthorityList) {
            gs_fgs_dms.add(companyAuthority.getCompanyCode() + "_" + companyAuthority.getSubCompanyCode());
        }

        // 查询所有人员配置,不用in操作符,n+1查询慢,程序过滤
        Map<String, Object> params = new HashMap<>();
        List<EmployeeConfigInfo> employeeConfigInfoList_all = (List<EmployeeConfigInfo>) this.list(params);
        List<EmployeeConfigInfo> employeeConfigInfoList = new ArrayList<>();
        for (EmployeeConfigInfo employeeConfigInfo : employeeConfigInfoList_all) {
            if (gs_fgs_dms.contains(employeeConfigInfo.getXl().getCgsbm())) {
                employeeConfigInfoList.add(employeeConfigInfo);
            }
        }

        // 停用信息
        List<String> destroy_infos = new ArrayList<>();
        String info_format = "线路[%s]中人员配置有停用人员,请处理!";
        String info = "";
        Integer xlId = null;
        for (EmployeeConfigInfo employeeConfigInfo : employeeConfigInfoList) {
            if (employeeConfigInfo.getJsy() != null &&
                employeeConfigInfo.getJsy().getDestroy() != null &&
                employeeConfigInfo.getJsy().getDestroy() == 1) { // 驾驶员判定是否停用
                info = String.format(info_format, employeeConfigInfo.getXl().getName());
                if (!destroy_infos.contains(info)) {
                    destroy_infos.add(info);
                }
            }

            if (employeeConfigInfo.getSpy() != null &&
                employeeConfigInfo.getSpy().getDestroy() != null &&
                employeeConfigInfo.getSpy().getDestroy() == 1) {
                info = String.format(info_format, employeeConfigInfo.getXl().getName());
                if (!destroy_infos.contains(info)) {
                    destroy_infos.add(info);
                }
            }
        }

        return destroy_infos;
    }

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

    @Transactional
    @Override
    public void toggleCancel(Long id) throws ScheduleException {
        EmployeeConfigInfo employeeConfigInfo = findById(id);
        Map<String, Object> param = new HashMap<>();
        if (employeeConfigInfo.getIsCancel()) {
            validate_jsy(employeeConfigInfo);
            if (employeeConfigInfo.getSpy() != null) {
                validate_spy(employeeConfigInfo);
            }
            employeeConfigInfo.setIsCancel(false);
        } else {
            param.clear();
            param.put("xl.id_eq", employeeConfigInfo.getXl().getId());
            List<ScheduleRule1Flat> scheduleRule1Flats = (List<ScheduleRule1Flat>) scheduleRule1FlatService.list(param);
            List<String> ryConfigIds = new ArrayList<>();
            for (ScheduleRule1Flat scheduleRule1Flat : scheduleRule1Flats) {
                ryConfigIds.addAll(Arrays.asList(scheduleRule1Flat.getRyConfigIds().split(",")));
            }

            if (ryConfigIds.contains(String.valueOf(id))) {
                throw new ScheduleException("人员配置已被规则使用,不能作废,请先修改规则!");
            } else {
                employeeConfigInfo.setIsCancel(true);
            }
        }
    }

}