LoggerZnddServiceImpl.java 12.4 KB
package com.bsth.service.zndd.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsth.common.ResponseCode;
import com.bsth.controller.realcontrol.dto.DfsjChange;
import com.bsth.data.schedule.DayOfSchedule;
import com.bsth.data.schedule.ScheduleComparator;
import com.bsth.data.schedule.edit_logs.service.dto.SchEditInfoDto;
import com.bsth.entity.Line;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.entity.sys.RealControAuthority;
import com.bsth.entity.zndd.LoggerZndd;
import com.bsth.repository.realcontrol.ScheduleRealInfoRepository;
import com.bsth.repository.zndd.LoggerZnddRepository;
import com.bsth.service.LineService;
import com.bsth.service.impl.BaseServiceImpl;
import com.bsth.service.realcontrol.ScheduleRealInfoService;
import com.bsth.service.report.ReportService;
import com.bsth.service.sys.RealControAuthorityService;
import com.bsth.service.zndd.LoggerZnddService;
import com.bsth.util.ReportUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class LoggerZnddServiceImpl extends BaseServiceImpl<LoggerZndd, Integer> implements LoggerZnddService {

    @Autowired
    LoggerZnddRepository loggerZnddRepository;
    @Autowired
    DayOfSchedule dayOfSchedule;
    @Autowired
    ScheduleRealInfoService scheduleRealInfoService;
    @Autowired
    ScheduleRealInfoRepository scheduleRealInfoRepository;
    @Autowired
    LineService lineService;
    @Autowired
    ReportService reportService;

    @Autowired
    RealControAuthorityService realControAuthorityService;
    @Autowired
    JdbcTemplate jdbcTemplate;
    Logger logger = LoggerFactory.getLogger(this.getClass());
    private static SimpleDateFormat sdfMonth = new SimpleDateFormat("HH:mm");
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public List<Map<String, Object>> listAll(Map<String, Object> map){
         loggerZnddRepository.findAll((Sort) map);

         return null;
    }


    @Override
    public ScheduleRealInfo schlist(String lineCode,Integer dir){
        List<ScheduleRealInfo> list =  dayOfSchedule.findByLineAndUpDown(lineCode,dir);
        if (!list.isEmpty() ){
            for (ScheduleRealInfo sr : list){
                if(sr.getBcType().equals("normal")){
                    return sr;
                }
            }
        }
        return null;
    };

    @Override
    @Transactional //发车间隔  误点
    public Map<String, Object> znddConfig(Map<String, Object> map){
        //查出离触发时间最近的一次路单  修改部分值
        //包装好路单添加如临加班次

        List<DfsjChange> dfsjcs = new ArrayList<>();//返回的数据
        try {
            String lineCode = map.get("lineCode") != null ? map.get("lineCode").toString() : null;
            String xlDir = map.get("xlDir") != null ? map.get("xlDir").toString() : null;
            String rq = map.get("rq") != null ? map.get("rq").toString() : null;
            List<ScheduleRealInfo>  upoldSchDateList = new ArrayList<>(), //上行
                    dnoldSchDateList = new ArrayList<>(); //下行


            List<ScheduleRealInfo> listReal = new ArrayList<>();
            Boolean typeReal = false;
            //排班依次往后
            List<ScheduleRealInfo> list =  dayOfSchedule.findByLineCode(lineCode);
            //区分上下行
            for (ScheduleRealInfo t : list){
                if (t.getXlDir().equals("0")){
                    upoldSchDateList.add(t);
                }else {
                    dnoldSchDateList.add(t);
                }
            }
            if (xlDir.equals("0")){
                Collections.sort(upoldSchDateList, new ScheduleComparator.FCSJ());//排序
                list = upoldSchDateList;
            }else {
                Collections.sort(upoldSchDateList, new ScheduleComparator.FCSJ());//排序
                list = upoldSchDateList;
            }

            for (ScheduleRealInfo info : list) {
                //从相等开始
                if (info.getFcsjActual() != null && !info.getFcsjActual().equals("")) {
                    if (info.getFcsjActual().equals(rq))
                        typeReal = true;
                    continue;
                }

                if (typeReal && (info.getBcType().equals("normal")|| info.getBcType().equals("region")))
                    listReal.add(info);

                if (listReal.size() >= 5) //或者不满5条结束 listReal.size()从1开始
                    break;

            }
            //处理listReal 5条数据或5条一下
            for (int i = 0; i < listReal.size() ;i++){
                DfsjChange cl = new DfsjChange();
                Integer[] mis = {3,2,2,1,1}; //往后5个班次分别延后3 2 2 1 1
                ScheduleRealInfo info  = listReal.get(i);
                int ms = mis[i];
                //未发车时 才可改变
                if (info.getFcsjActual() == null || info.getFcsjActual().equals("")){
                    String dfsj = sdfMonth.format(sdf.parse(info.getScheduleDateStr()+ " " + info.getDfsj()).getTime() + (ms * 60 * 1000)); //延后几分钟
                    cl.setSchId(info.getId());
                    cl.setOld_dfsj(info.getDfsj());
                    cl.setNew_dfsj(dfsj);
                    dfsjcs.add(cl);
                }
            }

            return scheduleRealInfoService.multi_dftz(dfsjcs);
        }catch (Exception e){
            logger.info("--",e);
        }


        return null;
    }


    public  ScheduleRealInfo thissch(Integer ids){
        ScheduleRealInfo sch2=dayOfSchedule.get(ids);
        ScheduleRealInfo sch= null;
        try {
            sch = sch2.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        Line line = lineService.findByLineCode(sch.getXlBm());
        List<Map<String, Object>> lineList=reportService.lineList();
        Set<String> lines = new HashSet<>();
        for (Map<String, Object> map : lineList) {
            if(line.getBrancheCompany().equals(map.get("fgsbm").toString())){
                lines.add(map.get("xlbm").toString());
            }
        }
        List<String> nbbms=new ArrayList<>();
        List<String> jghs=new ArrayList<>();
        List<String> jNames=new ArrayList<>();
        for (String lineCode : lines) {
            Set<String> set = dayOfSchedule.findCarByLineCode(lineCode);
            LocalTime localTime=LocalTime.now().plusHours(1);
            DateTimeFormatter dtf=DateTimeFormatter.ofPattern("HH:mm");
            for (String nbbm : set) {
                ScheduleRealInfo s=dayOfSchedule.executeCurr(nbbm);//当前执行班次
                if(s==null){//当日班次执行完毕
                    List<ScheduleRealInfo> list=dayOfSchedule.findByNbbm(nbbm);
                    if(list.size()>0){
                        ScheduleRealInfo s2=list.get(list.size()-1);//最后一个班次
                        nbbms.add(s2.getClZbh());
                        jghs.add(s2.getjGh());
                        jNames.add(s2.getjName());
                    }
                }else {//1小时内是否有待执行班次
                    LocalTime fcsj=LocalTime.parse(s.getFcsj(),dtf);
                    if(fcsj.isAfter(localTime)){
                        nbbms.add(s.getClZbh());
                        jghs.add(s.getjGh());
                        jNames.add(s.getjName());
                    }
                }
            }
        }
        StringBuffer sb=new StringBuffer();
        String kscl="";
        if(nbbms.size()>0){
            sch.setClZbh(nbbms.get(0));
            sch.setjGh(jghs.get(0));
            sch.setjName(jNames.get(0));
            int size =nbbms.size()>10?9:nbbms.size();
            for (int i = 0; i < size; i++) {
                sb.append(nbbms.get(i)+"/");
                sb.append(jNames.get(i)+",");
            }
            kscl=sb.toString().substring(0,sb.length()-1);
        }
        sch.setKxcl(kscl);
        return sch;
    }



    public String LineUserAll(@RequestParam Integer userId){
        String xlNames ="";
        RealControAuthority rcay = realControAuthorityService.findByUserId(userId);
        String lineCodeStr = rcay.getLineCodeStr();
        String[] lineArray = lineCodeStr.split(",");
        for(String line : lineArray){
            List<ScheduleRealInfo>  schList = dayOfSchedule.findByLineCode(line);
            for(ScheduleRealInfo sc : schList){
                if (sc.getStatus() == 0 || sc.getStatus() == 1){
                    xlNames += sc.getXlName()+",";
                    break;
                }
            }
        }
        return xlNames;

    }

    public  Map<String, Object> schTZSF(String line,String date,String endDate,String changType,String type){

        Map<String, Object> rs = new HashMap();
        try {
            String cont = "";
            if (line != null && !line.equals("")){
                cont = " and line_code  ="+line;
            }
            String bcType ="";
            if (changType.equals("0")){
                bcType = " and bc_type not in ('in','out','venting')";
            }else {
                bcType =" and bc_type in ('out')";
            }

            //拼接除rq、lineCode 外的其他where 条件
            String sql = "select t1.*, t2.fcsj,t2.lp_name,t2.cl_zbh,t2.j_gh,t2.j_name,t2.xl_dir,t2.real_exec_date,t2.xl_name from (select * from logger_sch_modify where rq >=? and rq <=?  and type in('SFTZ','FCXXWT') and remarks !='自动调度实发未发-车辆掉线'"+cont+"  ) t1 INNER JOIN bsth_c_s_sp_info_real t2 on t1.sch_id=t2.id where 1=1 " +bcType;
            List<SchEditInfoDto> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper(SchEditInfoDto.class), date,endDate);
            rs.put("status", ResponseCode.SUCCESS);
            List<SchEditInfoDto> listnew = new ArrayList<>();
            for(SchEditInfoDto item : list){
                List<Map> lists = (List) JSON.parse(item.getJsonArray());
                for(Map its : lists){
                    if (item.getType().equals("FCXXWT")) {
                        if (its.get("old") != null && its.get("title").equals("调整实发时间")) {
                            String text = "调整前:" + its.get("old") + ",调整后:" + its.get("now");
                            item.setText(text);
                            listnew.add(item);
                            break;
                        }
                    }else {
                        if(its.get("old") != null){
                            String text = "调整前:" + its.get("old") + ",调整后:" + its.get("now");
                            item.setText(text);
                            break;
                        }
                    }
                };
            };

            rs.put("list", listnew);

            if(type.equals("export")){
                List<Iterator<?>> listI = new ArrayList<Iterator<?>>();
                Map<String, Object> m = new HashMap<String, Object>();
                ReportUtils ee = new ReportUtils();
                List list1 = new ArrayList();
                for(SchEditInfoDto sed : listnew){
                    Map map = JSON.parseObject(JSON.toJSONString(sed), Map.class);
                    list1.add(map);
                }

                try {
                    listI.add(list1.iterator());
                    String path = this.getClass().getResource("/").getPath() + "static/pages/forms/";
                    ee.excelReplace(listI, new Object[]{m}, path + "mould/" + "sch_tzsf.xls",
                            path + "export/"+date+"-"+endDate+"-异常调整实发.xls");
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            }

        }catch (Exception e){
            logger.error("", e);
            rs.put("status", ResponseCode.ERROR);
            rs.put("msg", e.getMessage());
        }
        return rs;
    }
}