EmployeeConfigInfoServiceImpl.java 16.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
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.entity.sys.Module;
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 com.bsth.service.sys.ModuleService;
import com.bsth.util.DateUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
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.jdbc.core.RowMapper;
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.findById(employeeConfigInfo.getJsy().getId()).get();
        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.findById(employeeConfigInfo.getSpy().getId()).get();
        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() + "]驾驶员列表中!");
                }

                // 检测人员是否已经配置在其他线路的售票员列表中
                param.remove("jsy.id_eq");
                param.put("spy.id_eq", employeeConfigInfo.getJsy().getId());
                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());
            param.put("isCancel_eq", false); // 未作废的人员配置

            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() + "]售票员列表中!");
                }

                // 检测人员是否已经配置在其他线路的驾驶员列表中
                param.remove("spy.id_eq");
                param.put("jsy.id_eq", employeeConfigInfo.getSpy().getId());
                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("售票员没有配置在当前线路中,不属于当前线路!");
        }
    }

    @Autowired
    private ModuleService moduleService;
    @Transactional
    @Override
    public List<String> validate_get_destory_info() {
        // 1、查找当前用户是否有$$$$$${txt-1303},没有的话不分析是否有停用人员信息
        List<Module> moduleList = this.moduleService.findByCurrentUser();
        boolean hasPlanModule = false;
        for (Module module : moduleList) {
            if ("$$$$$${txt-1303}".equals(module.getName())) {
                hasPlanModule = true;
                break;
            }
        }
        if (!hasPlanModule) {
            return null;
        }

        // 2、计算从当前时间开始的排班计划中是否有停用人员
        String sql =
                "select distinct " +
                "plan.xl_name xlName " +
                ", plan.schedule_date scheduleDate " +
                "from bsth_c_s_sp_info plan " +
                "left join bsth_c_personnel jsy on jsy.id = plan.j " +
                "left join bsth_c_personnel spy on spy.id = plan.s " +
                "where plan.schedule_date >= ? " +
                "and (jsy.destroy = 1 || spy.destroy = 1) " +
                "group by plan.xl_name, plan.schedule_date " +
                "order by plan.xl_name, plan.schedule_date ";

//        String sql =
//                "select distinct " +
//                "pinfo.xl_name xlName " +
//                ", pinfo.schedule_date scheduleDate " +
//                "from " +
//                "(" +
//                "select plan.xl_name, plan.schedule_date " +
//                "from bsth_c_s_sp_info plan " +
//                "left join bsth_c_personnel jsy on plan.j = jsy.id " +
//                "left join bsth_c_personnel spy on plan.s = spy.id " +
//                "where schedule_date >= ? " +
//                "and (jsy.destroy = 1 || spy.destroy = 1) " +
//                "order by plan.xl_name asc, plan.schedule_date asc " +
//                ") pinfo " +
//                "group by pinfo.xl_name, pinfo.schedule_date ";


        Date currentDate = new Date(DateUtils.getTimestamp());
        System.out.println(currentDate);
        String info_format = "线路[%s][%s]排班中有人员已经停用,请及时处理!";
        List<String> infoList = this.jdbcTemplate.query(sql, new Object[] {currentDate}, new RowMapper<String>() {
            @Override
            public String mapRow(ResultSet resultSet, int i) throws SQLException {
                String xlName = resultSet.getString("xlName");
                Date scheduleDate = new Date(resultSet.getDate("scheduleDate").getTime());
                return String.format(info_format, xlName, DateFormatUtils.format(scheduleDate, "yyyy年MM月dd日"));
            }
        });

        return infoList;
    }

    @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);
            }
        }
    }

}