doum
22 小时以前 275aa50b1de31ace0cb17fdf0b6f678a63267b3c
server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/JkSketchServiceImpl.java
@@ -33,6 +33,7 @@
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.*;
import java.util.Date;
@@ -75,7 +76,6 @@
        jkSketchMapper.insert(jkSketch);
        return jkSketch.getId();
    }
    @Override
    public void deleteById(Integer id) {
        jkSketchMapper.deleteById(id);
@@ -85,6 +85,21 @@
    public void delete(JkSketch jkSketch) {
        UpdateWrapper<JkSketch> deleteWrapper = new UpdateWrapper<>(jkSketch);
        jkSketchMapper.delete(deleteWrapper);
    }
    @Override
    @PostConstruct
    public void initAyncsJob() {
        //重置交通规划和线路规划的异步任务状态
        jkSketchMapper.update(null,new UpdateWrapper<JkSketch>().lambda()
                .set(JkSketch::getStatus,Constants.THREE)
                .eq(JkSketch::getIsdeleted,Constants.ZERO)
                .eq(JkSketch::getStatus,Constants.ONE));
        categoryMapper.update(null,new UpdateWrapper<Category>().lambda()
                .set(Category::getStatus,Constants.ZERO)
                .eq(Category::getType,Constants.FOUR)
                .eq(Category::getIsdeleted,Constants.ZERO)
                .eq(Category::getStatus,Constants.TWO));
    }
    @Override
@@ -142,6 +157,78 @@
        jkSketchMapper.updateById(model);
        return model;
    }
    @Override
    public  JkSketch updateSketchLine(JkSketch jkSketch) {
        JkSketch model = jkSketchMapper.selectById(jkSketch.getId());
        if(model == null ||Constants.equalsInteger(model.getIsdeleted(),Constants.ONE)){
            throw  new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(  jkSketch.getSketchLineList() ==null || jkSketch.getSketchLineList().size()==0 ){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"请按要求调整优化方案!");
        }
        if(Constants.equalsInteger(model.getStatus(),Constants.ONE)){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前线路正存在优化任务,请耐心等待优化完成再进行该调整操作!");
        }
        //当前所有线路(符合条件的线路)
        List<JkSketchLine> lineList =  jkSketchLineMapper.selectJoinList(JkSketchLine.class,new MPJLambdaWrapper<JkSketchLine>()
                .selectAll(JkSketchLine.class )
                .selectAs(JkLine::getName,JkSketchLine::getLineName)
                .selectAs(JkLine::getMaxOrder,JkSketchLine::getMaxOrder)
                .selectAs(JkLine::getMaxCustomer,JkSketchLine::getMaxCustomer)
                .leftJoin(JkLine.class,JkLine::getId,JkOrders::getLineId )
                .eq(JkSketchLine::getSketchId,jkSketch.getId())
                .eq(JkSketchLine::getType, Constants.formatIntegerNum(model.getOptStatus()))
                .eq(JkSketchLine::getIsdeleted,Constants.ZERO));
        if(lineList ==null ||lineList.size() ==0){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前线路不满足优化方案调整条件!");
        }
        for(JkSketchLine line :lineList){
            JkSketchLine lineParam =getLineParamById(line.getId(),jkSketch.getSketchLineList());
            if(lineParam == null){
                throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"请基于现有线路进行方案调整,线路【"+line.getLineName()+"】线路配置不正确!");
            }
            if(lineParam.getCustomerList() == null || lineParam.getCustomerList().size() == 0 ){
                throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"请基于现有线路进行方案调整,线路【"+line.getLineName()+"】客户信息不能为空!");
            }
            Integer totalCus = 0;
            BigDecimal totalNum =new BigDecimal(0);
            for(JkSketchCustomer customer :lineParam.getCustomerList()){
                 totalCus += 1;//总客户量
                 totalNum = totalNum.add(Constants.formatBigdecimal(customer.getTotalNum()));//总送货量
                 customer.setSketchLineId(line.getId());
            }
            if( totalCus > Constants.formatIntegerNum(line.getMaxCustomer()) ){
                throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"线路【"+line.getLineName()+"】线路订单客户数量超过了线路总客户量限制,无法进行调整!");
            }
            if( totalNum.doubleValue() > Constants.formatIntegerNum(line.getMaxOrder())){
                throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"线路【"+line.getLineName()+"】订单送货量超过了线路总送货量限制,无法进行调整!");
            }
            lineParam.setTotalNum(totalNum);
            lineParam.setOrderNum(totalCus);
        }
        model.setSketchLineList(jkSketch.getSketchLineList());//线路信息
        model.setStatus(Constants.ONE);
        model.setEditDate(new Date());
        model.setEditor(jkSketch.getLoginUserInfo().getId());
        model.setPlanLineDate(model.getEditDate());
        model.setPlanLineUserId(jkSketch.getLoginUserInfo().getId());
        model.setJobId(UUID.randomUUID().toString());
        redisTemplate.opsForValue().set(Constants.RedisKeys.JKLINE_JOB+model.getJobId(),true );//做异步处理
        jkSketchMapper.updateById(model);
        return model;
    }
    private JkSketchLine getLineParamById(Integer id, List<JkSketchLine> sketchLineList) {
        for(JkSketchLine line: sketchLineList){
            if(Constants.equalsInteger(line.getId(),id)){
                return line;
            }
        }
        return  null;
    }
    @Override
    public JkSketch initOriginDistance(JkSketch jkSketch ) {
        JkSketch model = jkSketchMapper.selectById(jkSketch.getId());
@@ -344,7 +431,7 @@
        }
        String errorMsg ="";
        for(JkCustomer c1 : customerList){
            if(c1.getLatitude()==null || c1.getLongitude() ==null){
            if(c1.getLatitude()==null || c1.getLongitude() ==null || Constants.equalsInteger(c1.getDistanceStatus(),Constants.TWO)){
                errorMsg += c.getName()+"-"+c.getName()+" | ";
            }
        }
@@ -356,6 +443,11 @@
        c.setCustomerList(customerList);
        return  c;
    }
    /**
     * 开始异步执行线路优化任务
     * @param model
     */
    @Override
    @Async
    public void startUpdateLineAsync(JkSketch model) {
@@ -367,13 +459,17 @@
            long[] vehicleCapacities1=new long[lineList.size()];//每辆车的最大订单量限制
            long[] demands1 = new long[customerList.size()+1]; //各个点的订单量
            long[][] distanceMatrix1 = new long[customerList.size()+1][customerList.size()+1];
            distanceMatrix1[0][0] = 0;
            demands1[0] =0;//原点
            for (int i = 0; i < customerList.size(); i++) {
                List<DistanceMapParam>  disList =  customerList.get(i).getDistanceMapParamList();
                distanceMatrix1[0][i] =  disList.get(0).getDistance();
                distanceMatrix1[i][0] = disList.get(disList.size() -1).getDistance();
                distanceMatrix1[0][i+1] =  disList.get(0).getDistance();
                distanceMatrix1[i+1][0] = disList.get(disList.size() -1).getDistance();
                demands1[i+1] = Constants.formatBigdecimal( customerList.get(i).getTotalNum()).longValue();  //各个点的订单量
                for (int j = 0; j< disList.size()-2; j++) {
                for (int j = 0; j < customerList.size(); j++) {
                    distanceMatrix1[i+1][j+1] =disList.get(j+1).getDistance() ;
                }
               /* for (int j = 0; j< disList.size()-2; j++) {
                    if(j+1 >=10){
                        break;
                    }
@@ -382,7 +478,7 @@
                    }else{
                        distanceMatrix1[i+1][j+1]  = 1l;
                    }
                }
                }*/
            }
            for (int i = 0; i < lineList.size(); i++) {
                vehicleCapacities1[i] = lineList.get(i).getMaxOrder();//每辆车的最大订单量限制
@@ -400,8 +496,156 @@
                    .set(JkSketch::getStatus,Constants.THREE)
                    .set(JkSketch::getPlanLineEndDate,new Date()));
        }
    }
    @Override
    @Async
    public  void startInitOriginDistanceBatch(List<JkSketch> list) {
        if(list!=null){
            for(JkSketch model :list){
                initOriginDistance(model);
            }
        }
    }
    @Override
    @Async
    public  void startEditSketchLineAsync(JkSketch model) {
        boolean success = true;
        int totalDistance = 0;
        List<JkSketchLine> lineList = model.getSketchLineList();
        try {
            MPJLambdaWrapper<JkSketchCustomer> queryWrapper = new MPJLambdaWrapper<>();
            queryWrapper.selectAll(JkSketchCustomer.class )
                    .selectAs(JkCustomer::getName,JkSketchCustomer::getName)
                    .selectAs(JkCustomer::getCode,JkSketchCustomer::getCode)
                    .selectAs(JkCustomer::getDistance,JkSketchCustomer::getDistanceJson)
                    .selectAs(JkCustomer::getLongitude,JkSketchCustomer::getLongitude)
                    .selectAs(JkCustomer::getLatitude,JkSketchCustomer::getLatitude)
                    .selectAs(JkCustomer::getStartDistance,JkSketchCustomer::getStartDistance)
                    .selectAs(JkCustomer::getEndDistance,JkSketchCustomer::getEndDistance)
                    .leftJoin(JkCustomer.class,JkCustomer::getId,JkSketchCustomer::getCustomerId )
                    .eq(JkSketchCustomer::getType, Constants.equalsInteger(model.getOptStatus(),Constants.ONE)?1:0)
                    .eq(JkSketchCustomer::getSketchId, model.getId())
                    .eq(JkSketchCustomer::getIsdeleted,Constants.ZERO)
                    .orderByAsc(JkSketchCustomer::getSortnum);
            List<JkSketchCustomer> customerList = jkSketchCustomerMapper.selectJoinList(JkSketchCustomer.class,queryWrapper);
            if(customerList == null ||customerList.size() ==0){
                throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"该线路客户信息为空,不满足优化条件!");
            }
            int totalNum = 0;
            for(JkSketchLine line : lineList){
                //完善线路客户优化参数
                List<JkSketchCustomer> customerListParam =  line.getCustomerList() ;
                if(customerListParam ==null || customerListParam.size()==0){
                    throw  new BusinessException(ResponseStatus.BAD_REQUEST);
                }
                initSketchCustomerListParam(line.getCustomerList(),customerList) ;
                totalNum += customerListParam.size();
            }
            if(totalNum != Constants.formatIntegerNum(model.getOrderNum())){
                throw  new BusinessException(ResponseStatus.BAD_REQUEST);
            }
            for(JkSketchLine line : lineList){
                //逐个路线优化
                List<JkSketchCustomer> customerListParam =  line.getCustomerList() ;
                TspSolver.DataModel dataModel = new TspSolver.DataModel();
                int vehicleNumber1 = 1;//线路数量
                long[] vehicleCapacities1=new long[]{line.getMaxOrder()};//每辆车的最大订单量限制
                long[] demands1 = new long[customerListParam.size()+1]; //各个点的订单量
                long[][] distanceMatrix1 = new long[customerListParam.size()+1][customerListParam.size()+1];
                distanceMatrix1[0][0] = 0;
                demands1[0] =0;//原点
                for (int i = 0; i < customerListParam.size(); i++) {
                    List<DistanceMapParam>  disList =  customerListParam.get(i).getDistanceMapParamList();
                    distanceMatrix1[0][i+1] =  disList.get(0).getDistance();
                    distanceMatrix1[i+1][0] = disList.get(disList.size() -1).getDistance();
                    demands1[i+1] = Constants.formatBigdecimal( customerListParam.get(i).getTotalNum()).longValue();  //各个点的订单量
                    for (int j = 0; j < customerListParam.size(); j++) {
                        distanceMatrix1[i+1][j+1] =disList.get(j+1).getDistance() ;
                    }
                }
                //构造优化数据模型
                dataModel.initDataInfo(vehicleNumber1,demands1,vehicleCapacities1,distanceMatrix1);
                TspSolver.startSearch(dataModel);
                if(dataModel.getSolutions()==null || dataModel.getSolutions().size()==0){
                    throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"线路【"+line.getLineName()+"】调整失败 ,未获得最优交通规划方案!");
                }
                TspSolverSolutions so = dataModel.getSolutions().get(0);
                List<Integer> routes = so.getRouteIndex();
                totalDistance += so.getDistance();
                if(routes.size() <=2) {
                    throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"线路【"+line.getLineName()+"】调整失败 ,未获得最优交通规划方案!");
                }
                //有效路径
                for (Integer cIndex : routes){
                    if(cIndex ==0){
                        continue; //起始点不处理
                    }
                    JkSketchCustomer customer = customerListParam.get(cIndex-1);
                    customer.setSortnum(cIndex-1);
                }
                line.setDistance(so.getDistance());
            }
        }catch (Exception e){
            e.printStackTrace();
            success =false;
        }finally {
            if(success){
                List<JkSketchCustomer> allList = new ArrayList<>();
                for(JkSketchLine line : lineList){
                    allList.addAll(line.getCustomerList());
                }
                jkSketchLineMapper.updateById(lineList);
                jkSketchCustomerMapper.updateById(allList);
            }
            jkSketchMapper.update(null,new UpdateWrapper<JkSketch>().lambda()
                    .eq(JkSketch::getId,model.getId() )
                    .eq(JkSketch::getJobId,model.getJobId() )
                    .set(success,JkSketch::getDistance,totalDistance)
                    .set(JkSketch::getPlanLineInfo,success?"最近一次线路调整成功":"最近一次线路调整失败!")
                    .set(JkSketch::getStatus,success?Constants.TWO:Constants.THREE)
                    .set(JkSketch::getPlanLineEndDate,new Date()));
        }
    }
    private List<JkSketchCustomer> initSketchCustomerListParam(List<JkSketchCustomer> customerList, List<JkSketchCustomer> customerList1) {
        for(JkSketchCustomer param : customerList){
            for(JkSketchCustomer model : customerList1){
              if(Constants.equalsInteger(model.getId(),param.getId())){
                  param.setLatitude(model.getLatitude());
                  param.setLongitude(model.getLongitude());
                  param.setStartDistance(model.getStartDistance());
                  param.setEndDistance(model.getEndDistance());
                  param.setDistanceJson(model.getDistanceJson());
              }
            }
        }
        for(JkSketchCustomer c : customerList){
            List<DistanceMapParam> tmpList = new ArrayList<>();
            List<DistanceMapParam> distanceMapParamList  = getListFromJsonStr(c.getDistanceJson());
            DistanceMapParam t0 = new DistanceMapParam();
            t0.setId(-2);//表示返回园区
            t0.setDistance(Constants.formatLongNum(c.getStartDistance()) );
            tmpList.add(t0);
            for(JkSketchCustomer cm : customerList){
                //客户和客户之间的距离信息
                DistanceMapParam t = new DistanceMapParam();
                t.setId(cm.getCustomerId());
                t.setDistance(0);
                DistanceMapParam param = getParamByCustomerIds( cm.getCustomerId(),distanceMapParamList);
                if(param!=null){//如果之前已经获取过
                    t = param;
                }
                tmpList.add(t);
            }
            DistanceMapParam tt = new DistanceMapParam();
            tt.setId(-2);//表示返回园区
            tt.setDistance(Constants.formatLongNum(c.getEndDistance()));
            tmpList.add(tt);
            c.setDistanceMapParamList(tmpList);
        }
        return  customerList;
    }
    private void initCustomerDistance( List<JkSketchLine> lineList,JkSketch model,boolean updateLineDistance) {
        List<JkSketchCustomer> customerList = model.getCustomerList();
@@ -443,13 +687,22 @@
            for (JkSketchLine line :lineList){
                long lineDistance = 0;
                List<JkSketchCustomer> customers = getCustomerListByLineId(line.getId(),customerList);
                int index =0;
                for(JkSketchCustomer c : customers){
                    lineDistance+= Constants.formatLongNum(c.getStartDistance());
                    lineDistance+= Constants.formatLongNum(c.getEndDistance());
                    for(JkSketchCustomer cm : customers){
                    if(index ==0){
                        lineDistance+= Constants.formatLongNum(c.getStartDistance());
                    }
                    if(index == customers.size()-1){
                        lineDistance+= Constants.formatLongNum(c.getEndDistance());
                        break;
                    }
                    DistanceMapParam param = getParamByCustomerIds( customers.get(index+1).getCustomerId(),getListFromJsonStr(c.getDistanceJson()));
                    lineDistance += param.getDistance();
                    index++;
                  /*  for(JkSketchCustomer cm : customers){
                        DistanceMapParam param = getParamByCustomerIds( cm.getCustomerId(),getListFromJsonStr(c.getDistanceJson()));
                        lineDistance += param.getDistance();
                    }
                    }*/
                }
                if(updateLineDistance && Constants.equalsInteger(model.getStatus(),Constants.ZERO) ){
                    line.setDistance(lineDistance);
@@ -503,10 +756,11 @@
        if(solutions!=null && solutions.size()>0){
            for(TspSolverSolutions so : solutions){
                List<Integer> routes = so.getRouteIndex();
                totalDistance+= so.getDistance();
                if(routes.size() <=2) {
                    continue;//无客户的非有效路线
                }
                totalDistance+= so.getDistance();
//                totalDistance+= so.getDistance();
                JkLine  line =model.getLineList().get(so.getLineIndex());
                JkSketchLine tModel =  new JkSketchLine();
                tModel.setSketchId(model.getId());
@@ -516,6 +770,7 @@
                tModel.setTotalNum(new BigDecimal(0));
                tModel.setOrderNum(routes.size()-2);
                tModel.setDateInfo(model.getDateInfo());
                tModel.setType(Constants.ONE);
                tModel.setSortnum(sketchLineList.size()+1);
                tModel.setEditDate(tModel.getCreateDate());
                tModel.setEditor(tModel.getCreator());
@@ -534,6 +789,7 @@
                    cModel.setCreator(tModel.getCreator());
                    cModel.setOrderId(customer.getOrderId());
                    cModel.setCreateDate(tModel.getCreateDate());
                    cModel.setType(Constants.ONE);
                    cModel.setTotalNum(customer.getTotalNum());
                    cModel.setDateInfo(model.getDateInfo());
                    cModel.setSortnum(tModel.getCustomerList().size()+1);
@@ -551,10 +807,12 @@
            jkSketchLineMapper.update(null,new UpdateWrapper<JkSketchLine>().lambda()
                    .set(JkSketchLine::getIsdeleted,Constants.ONE)
                    .eq(JkSketchLine::getIsdeleted,Constants.ZERO)
                    .eq(JkSketchLine::getType,Constants.ONE)
                    .eq(JkSketchLine::getDateInfo,model.getDateInfo()));
            jkSketchCustomerMapper.update(null,new UpdateWrapper<JkSketchCustomer>().lambda()
                    .set(JkSketchCustomer::getIsdeleted,Constants.ONE)
                    .eq(JkSketchCustomer::getIsdeleted,Constants.ZERO)
                    .eq(JkSketchCustomer::getType,Constants.ONE)
                    .eq(JkSketchCustomer::getDateInfo,model.getDateInfo()));
            if(sketchLineList.size()>0){
                jkSketchLineMapper.insert(sketchLineList);
@@ -574,6 +832,7 @@
                    .eq(JkSketch::getId,model.getId() )
                    .eq(JkSketch::getJobId,model.getJobId() )
                    .set(JkSketch::getLineNum,sketchLineList.size() )
                    .set(JkSketch::getOptStatus,Constants.ONE)//已生成优化线路
                    .set(JkSketch::getDistance,totalDistance)
                    .set(JkSketch::getPlanLineInfo,"最近一次线路优化成功,优化后总距离:"+(totalDistance/1000)+"公里!")
                    .set(JkSketch::getStatus,Constants.TWO)
@@ -600,6 +859,7 @@
                .selectAs(JkCustomer::getStartDistance,JkSketchCustomer::getStartDistance)
                .selectAs(JkCustomer::getEndDistance,JkSketchCustomer::getEndDistance)
                .leftJoin(JkCustomer.class,JkCustomer::getId,JkSketchCustomer::getCustomerId )
                .eq(JkSketchCustomer::getType, Constants.formatIntegerNum(model.getOptStatus()))
                .eq(JkSketchCustomer::getSketchId, model.getId())
                .eq(JkSketchCustomer::getIsdeleted,Constants.ZERO)
                .orderByAsc(JkSketchCustomer::getSortnum);
@@ -628,6 +888,7 @@
        queryWrapper1.selectAll(JkSketchLine.class )
                .eq(JkSketchLine::getSketchId, model.getId())
                .eq(JkSketchLine::getIsdeleted,Constants.ZERO)
                .eq(JkSketchLine::getType, Constants.formatIntegerNum(model.getOptStatus()))
                .orderByAsc(JkSketchLine::getSortnum);
        List<JkSketchLine> lineList = jkSketchLineMapper.selectJoinList(JkSketchLine.class,queryWrapper1);
        initCustomerDistance(lineList,model,updateLineDistance);
@@ -841,6 +1102,7 @@
                tModel.setTotalNum(orders.getNum());
                tModel.setDateInfo(orders.getDateInfo());
                tModel.setSortnum(list.size()+1);
                tModel.setType(Constants.ZERO);
                tModel.setEditDate(tModel.getCreateDate());
                tModel.setEditor(tModel.getCreator());
                tModel.setIsdeleted(Constants.ZERO);
@@ -863,6 +1125,7 @@
            initOrderAndTotalNum(tModel,line.getId(),newOrderList);
            tModel.setDateInfo(newList.get(0).getDateInfo());
            tModel.setSortnum(list.size()+1);
            tModel.setType(Constants.ZERO);
            tModel.setEditDate(tModel.getCreateDate());
            tModel.setEditor(tModel.getCreator());
            tModel.setIsdeleted(Constants.ZERO);
@@ -918,6 +1181,7 @@
                tModel.setCreateDate(orders.getCreateDate());
                tModel.setCategoryId(orders.getCategoryId());
                tModel.setTotalNum(orders.getNum());
                tModel.setOptStatus(Constants.ZERO);
                tModel.setOrderNum(1);
                tModel.setDateInfo(orders.getDateInfo());
                tModel.setSortnum(list.size()+1);