DdexamServiceImpl.java 18.5 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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
package com.bsth.service.ddexam.impl;

import com.alibaba.dubbo.common.URL;
import com.bsth.data.BasicData;
import com.bsth.data.LineConfigData;
import com.bsth.data.schedule.DayOfSchedule;
import com.bsth.data.schedule.thread.ScheduleRefreshThread;
import com.bsth.entity.*;
import com.bsth.entity.realcontrol.LineConfig;
import com.bsth.entity.realcontrol.LineConfigSt;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.entity.realcontrol.ScheduleRealInfoSt;
import com.bsth.entity.sys.BsthCSysUserRoles;
import com.bsth.entity.sys.RealControAuthority;
import com.bsth.entity.sys.SysUser;
import com.bsth.repository.*;
import com.bsth.repository.realcontrol.LineConfigRepository;
import com.bsth.repository.realcontrol.LineConfigStRepository;
import com.bsth.repository.realcontrol.ScheduleRealInfoRepository;
import com.bsth.repository.realcontrol.ScheduleRealInfoStRepository;
import com.bsth.repository.sys.BsthCSysUserRolesRepository;
import com.bsth.repository.sys.RealControAuthorityRepository;
import com.bsth.repository.sys.SysUserRepository;
import com.bsth.service.ddexam.DdexamService;
import com.bsth.util.ConfigUtil;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class DdexamServiceImpl implements DdexamService {

    Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    SysUserRepository sysUserRepository;
    @Autowired
    RealControAuthorityRepository realControAuthorityRepository;
    @Autowired
    BsthCSysUserRolesRepository bsthCSysUserRolesRepository;
    @Autowired
    LineRepository lineRepository;
    @Autowired
    LineStRepository lineStRepository;
    @Autowired
    LineInformationStRepository LineInformationStRepository;
    @Autowired
    LineInformationRepository lineInformationRepository;
    @Autowired
    StationRouteStRepository stationRouteStRepository;
    @Autowired
    StationRouteRepository stationRouteRepository;
    @Autowired
    ScheduleRealInfoStRepository ScheduleRealInfoStRepository;
    @Autowired
    ScheduleRealInfoRepository ScheduleRealInfoRepository;
    @Autowired
    BasicData.BasicDataLoader basicDataLoader;
    @Autowired
    ScheduleRefreshThread scheduleRefreshThread;
    @Autowired
    DayOfSchedule dayOfSchedule;
    @Autowired
    LineConfigStRepository lineConfigStRepository;
    @Autowired
    LineConfigData lineConfigData;
    
    
    
    @Autowired
    LineConfigRepository lineConfigRepository;
    
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat sdfhms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public List<SysUser> userAll(){
        return  sysUserRepository.findBySusUserAll();
    }

    @Override
    public int hftime(){
        URLConnection url = null;
        try {
            java.net.URL baseUrl;
            baseUrl = com.bsth.webService.trafficManage.geotool.services.Internal.class.getResource(".");
            url = new java.net.URL(baseUrl,
                    "http://time.tianqi.com/").openConnection();

            url.connect(); //发出连接
            long ld = url.getDate(); //取得网站日期时间
            Date date = new Date(ld); //转换为标准时间对象
          /*  Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.YEAR, -1);//当前时间减去一年,即一年前的时间  
            //(时间校准网上快了一年,减去一年)*/
            String data = sdfhms.format(date.getTime());
            String[] lk = data.split(" ");
            if(lk.length == 2){
                Runtime.getRuntime().exec(ConfigUtil.get("jp.date") +" " +lk[0]);//Windows 系统
                Runtime.getRuntime().exec(ConfigUtil.get("jp.time") +" " +lk[1]);//Windows 系统
            }
        }  catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    
    
    @Override
    public int initDate(String date) {
        try {
            Runtime.getRuntime().exec(ConfigUtil.get("jp.time") +" " +date);//Windows 系统
        }catch (Exception e){
            log.error("修改系统时间失败",e);
        }
        return 1;
    }


/*    //考生人初始化 无线程
    @Override
    public Map initializationAll(Integer rs) {
        Long start = System.currentTimeMillis();
        Map m = new HashMap<>();
        try {  //用户当天分配完直接改状态  无二次分配的可能
            List<SysUser> userList = sysUserRepository.findBySusUser(); //已有的用户   不考虑用户角色表  正常情况下创建必分配
            if (userList.size() >= rs) {  //满足考试人数  默认配置都是全的
                for (int i = 0 ; i < rs ; i++ ){   //已rs人数为准
                    SysUser user = userList.get(i);
                    useryType(user);
                    sysUserRepository.changeKsStatus(user.getId()); ////状态  表示今日账户已用
                }

            } else {
                if (userList.size() == 0) { //考试人数 全部重新创建
                    if(usercreateType(rs)){
                        log.info("考试初始化创建成功");
                    }
                } else { //已有的人检查分配线路   不够的创建

                    for (SysUser user : userList) {  //已有的分配班次
                        if (realControAuthorityRepository.findByUserId(user.getId()) == null) {  //检查未分配线路
                        } else { //已分配线路的分配班次
                            System.out.println("分配班次...");
                            sysUserRepository.changeKsStatus(user.getId()); ////状态  表示今日账户已用
                            useryType(user);
                        }
                    }

                    int st = rs - userList.size(); //未创建考试人数
                    //未分配的创建线路并分配
                    if(usercreateType(st)){
                        log.info("考试初始化创建成功");
                    }



                }
            }
        }catch (Exception e){
            log.error("-----考试初始化失败------",e);
            m.put("code","创建失败");
            return m;
        }
        //调用排班初始话功能
        m.put("code","创建成功");
        System.out.println("----------创建时间--------------"+(System.currentTimeMillis() - start));
        dayOfSchedule.lineAll(); //查询所有线路
        basicDataLoader.loadAllData(); //加载所有数据

        lineConfigData.lineAll();
        scheduleRefreshThread.run();
        System.out.println("-------------本次执行时间-----------"+(System.currentTimeMillis() - start));
        return m;

    }
    */
    //考生人初始化  单线程 测试1:执行中途再次执行是否同步 测试2:
    @Override
    @Transactional //事务
    public synchronized Map initializationAll(Integer rs,Long userName) {
        Long start = System.currentTimeMillis();
        Map m = new HashMap<>();
        try {  //用户当天分配完直接改状态  无二次分配的可能
            List<SysUser> userList = sysUserRepository.findBySusUserAll(); //已有的用户   不考虑用户角色表  正常情况下创建必分配
            Map suerMap = new HashMap();
            for (SysUser s :userList){
                if(Long.parseLong(s.getUserName())  == userName ){
                    m.put("code","创建失败 账号重复");
                    return m;
                }
            }
             
            new Thread() {
                public  synchronized  void run() {
                    if(usercreateType(rs,userName)){
                        log.info("考试初始化创建成功");
                        kk();
                    }
                }
            }.start();
                     
        }catch (Exception e){
            log.error("-----考试初始化失败------",e);
            m.put("code","创建失败");
            return m;
        }
        //调用排班初始话功能
        m.put("code","创建成功");
        System.out.println("-------------本次执行时间-----------"+(System.currentTimeMillis() - start));
        return m;
        
    }


    public Map initializationAll2(Long userName){
        Long start = System.currentTimeMillis();
        Map m = new HashMap<>();
        String t = String.valueOf(userName).substring(0,7);
        
        try {  //用户当天分配完直接改状态  无二次分配的可能
            List<SysUser> userList = sysUserRepository.findBySusUser(t); //已有的用户   不考虑用户角色表  正常情况下创建必分配
            if (userList.size() > 0) {
                //已有的人检查分配线路
                new Thread() {
                    public synchronized void run() {
                        if (snt(userList)) {
                            kk();
                        }
                    }
                }.start();
            }
        }catch (Exception e){
            log.error("-----考试初始化失败------",e);
            m.put("code","创建失败");
            return m;
        }
        //调用排班初始话功能
        m.put("code","初始化成功");
        System.out.println("-------------本次执行时间-----------"+(System.currentTimeMillis() - start));
        return m;
        
    }
    /*public void aa(){
        Long start = System.currentTimeMillis();
        Map m = new HashMap<>();
        try {  //用户当天分配完直接改状态  无二次分配的可能
            List<SysUser> userList = sysUserRepository.findBySusUser(); //已有的用户   不考虑用户角色表  正常情况下创建必分配
            if (userList.size() >= rs) {  //满足考试人数  默认配置都是全的
                new Thread() {
                    public  synchronized  void run() {
                        if(snt(rs,userList,0)){
                            log.info("考试初始化创建成功");
                            kk();
                        }
                    }
                }.start();
            } else {
                if (userList.size() == 0) { //考试人数 全部重新创建
                    new Thread() {
                        public  synchronized  void run() {
                            if(usercreateType(rs)){
                                log.info("考试初始化创建成功");
                                kk();
                            }
                        }
                    }.start();

                } else { //已有的人检查分配线路   不够的创建

                    new Thread() {
                        public  synchronized  void run() {
                            if(snt(rs,userList,1)){
                                kk();
                            }
                        }
                    }.start();

                    int st = rs - userList.size(); //未创建考试人数
                    //*    //未分配的创建线路并分配
                    if(usercreateType(st)){ //暂无问题
                        log.info("考试初始化创建成功");
                        kk();
                    }//*
                    new Thread() {
                        public  synchronized  void run() {
                            if(usercreateType(st)){
                                log.info("考试初始化创建成功");
                                kk();
                            }
                        }
                    }.start();

                }
            }
        }catch (Exception e){
            log.error("-----考试初始化失败------",e);
            m.put("code","创建失败");
            return m;
        }
        //调用排班初始话功能
        m.put("code","创建成功");
        System.out.println("-------------本次执行时间-----------"+(System.currentTimeMillis() - start));
        return m;
    }
    */
    public void kk(){
        basicDataLoader.loadAllData(); //加载所有数据
        dayOfSchedule.lineAll(); //查询所有线路
        lineConfigData.lineAll();
        scheduleRefreshThread.run();
    }
    //已创建好的用户分配排班

    public Boolean snt(List<SysUser> userList){
        for (int i = 0 ; i < userList.size() ; i++ ){   //已rs人数为准
            SysUser user = userList.get(i);
            if (realControAuthorityRepository.findByUserId(user.getId()) != null) {
                useryType(user);
                System.out.println("分配班次...");
                sysUserRepository.changeKsStatus(user.getId()); ////状态  表示今日账户已用
            }
        }
        return true;
    }
    
    
    public Boolean useryType(SysUser user){
        RealControAuthority hoit = realControAuthorityRepository.findByUserId(user.getId());
        String[] st = hoit.getLineCodeStr().split(",");
        for (int k = 0 ; k < st.length ; k++){
           Line l = lineRepository.findById(Integer.parseInt(st[k])).get();
            inforealType(l);
        }
        return true;
    }
    
    //创建考试人数 并分配线路信息
    public synchronized Boolean  usercreateType(int st,Long userName){
        int usernum = sysUserRepository.findAll_distinct().size();
        SysUser user = new SysUser();//总人数
        for (int i = 0; i < st; i++) { //创建考试人数
            int username = usernum++;
            user = new SysUser();
            user.setCreateDate(new Date());
            user.setEnabled(true);
            user.setName(String.valueOf(userName + i));
            user.setPassword(new BCryptPasswordEncoder(4).encode("123456"));
            user.setUserName(String.valueOf(userName + i));
            user.setKsstatus("1"); //状态  表示今日账户已用
            sysUserRepository.save(user); //添加  -------**--
            
            BsthCSysUserRoles userRoles = new BsthCSysUserRoles();
            userRoles.setUsers(user.getId());
            userRoles.setRoles(73); //统一用73考生
            bsthCSysUserRolesRepository.save(userRoles); //添加  -------**--
            List<LineConfigSt> lineConfigList = lineConfigStRepository.findAll(); //线调 线路相关配置信息
            Integer linebumber = lineRepository.findAll().size(); //线路数量 线路code
            String [] mtline = {"A线","B线","C线","D线","E线","F线"}; //对应模板6条线 换成线路name
            List<LineInformationSt> litst = (List<LineInformationSt>) LineInformationStRepository.findAll(); //所有模板线路标准和线路
            if(litst.size() != 6){
                return false;
            }
            String lineCodeStr = "";
            for(int j = 0 ; j < litst.size() ; j++){  //6个  <6
                LineSt linest = litst.get(j).getLine(); //旧line
                LineInformationSt Limst = litst.get(j); 
                Line line = new Line();
                BeanUtils.copyProperties(linest,line); //copy
                Integer linecodeId = linebumber++ ;
                line.setId(linecodeId);
                line.setLineCode(linecodeId.toString());
                line.setName(mtline[j]+"-"+user.getName());
                line.setCoLineCode(linest.getLineCode());
                lineRepository.save(line);  //线路添加
                //----------------线路--------------------------//
                LineInformation lifat = new LineInformation();  //线路标准
                BeanUtils.copyProperties(Limst,lifat);
                lifat.setLine(line);
                lineInformationRepository.save(lifat); //线路标准信息条件
                //-------------------线路标准-----------------------//
                for(LineConfigSt cfgt : lineConfigList){
                    if(cfgt.getLine().getLineCode().equals(linest.getLineCode())){
                        LineConfig config = new LineConfig();
                        BeanUtils.copyProperties(cfgt,config); //copy
                        config.setLine(line);
                        lineConfigRepository.save(config);
                    }
                }
                //-------------------线调 线路相关配置信息-----------------------//
                
                List<StationRouteSt> srstList = stationRouteStRepository.findByLine(linest.getLineCode());
                List<StationRoute> srstListAll = new ArrayList<>();
                for(StationRouteSt route : srstList){
                    StationRoute srt = new StationRoute();
                    BeanUtils.copyProperties(route,srt); //copy
                    srt.setLine(line);
                    srt.setLineCode(linecodeId.toString());
                    srstListAll.add(srt);
                }
                stationRouteRepository.saveAll(srstListAll);//添加站点
                //-------------------站点-----------------------//
                lineCodeStr += linecodeId.toString()+",";  //线调线路
                inforealType(line);
            }
            RealControAuthority raty = new RealControAuthority();
            raty.setUserId(user.getId());
            raty.setLineCodeStr(lineCodeStr);
            raty.setPattern(1); //可用
            realControAuthorityRepository.save(raty);
            //-------------------分配本次线调权限-----------------------//
        }
        return true;
    }
 
    //添加排班
    public void inforealType(Line line){
        int t  = ScheduleRealInfoRepository.selectCountAll(sdf.format(new Date()),line.getLineCode());
        //鉴于有人员创建账号瞬间就初始化  修改人员状态慢了一步  在此多加一部判断
        if(t  >  0)
            return;
        //用旧线路code查出模板排班表
        List<ScheduleRealInfoSt>  sristList =  ScheduleRealInfoStRepository.queryUserInfo(line.getCoLineCode(),"2021-11-18");//模板固定日期
        List<ScheduleRealInfo>  sriList = new ArrayList<>();
        for(ScheduleRealInfoSt st : sristList){
            ScheduleRealInfo sri = new ScheduleRealInfo();
            BeanUtils.copyProperties(st,sri); //copy
            sri.setScheduleDate(new Date());
            sri.setScheduleDateStr(sdf.format(new Date()));
            sri.setXlBm(line.getLineCode());
            sri.setXlName(line.getName()); 
            sriList.add(sri);
        }
        ScheduleRealInfoRepository.saveAll(sriList);
    }


  

}