doum
11 小时以前 05ee96b3f690ad0ce4f47ad161e14f5f0d862707
server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/JkSketchServiceImpl.java
@@ -1,37 +1,41 @@
package com.doumee.service.business.impl;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.annotation.excel.ExcelImporter;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.dao.admin.request.JkCustomerImport;
import com.doumee.core.tsp.*;
import com.doumee.core.utils.*;
import com.doumee.core.wms.model.response.WmsBaseDataResponse;
import com.doumee.core.wms.model.response.WmsBaseResponse;
import com.doumee.dao.admin.request.JkOrdersImport;
import com.doumee.dao.business.*;
import com.doumee.dao.business.model.*;
import com.doumee.service.business.third.model.LoginUserInfo;
import com.doumee.service.business.third.model.PageData;
import com.doumee.service.business.third.model.PageWrap;
import com.doumee.core.utils.Utils;
import com.doumee.service.business.JkSketchService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import netscape.javascript.JSObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.*;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
@@ -40,16 +44,21 @@
 * @date 2025/09/28 09:01
 */
@Service
@Slf4j
public class JkSketchServiceImpl implements JkSketchService {
    @Autowired
    private JkSketchMapper jkSketchMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private JkSketchLineMapper jkSketchLineMapper;
    @Autowired
    private JkSketchCustomerMapper jkSketchCustomerMapper;
    @Autowired
    private JkCustomerMapper jkCustomerMapper;
    @Autowired
    private JkCustomerNavigationMapper jkCustomerNavigationMapper;
    @Autowired
    private JkLineMapper jkLineMapper;
@@ -85,10 +94,544 @@
        }
        jkSketchMapper.deleteBatchIds(ids);
    }
    //线路优化
    @Override
    public JkSketch updateById(JkSketch jkSketch ) {
        JkSketch model = jkSketchMapper.selectById(jkSketch.getId());
        if(model == null ||Constants.equalsInteger(model.getIsdeleted(),Constants.ONE)){
            throw  new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if( model.getDateInfo() ==  null){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"该线路日期信息不正确,不支持优化!");
        }
        if(Constants.equalsInteger(Constants.ZERO,jkSketch.getForceUpdate()) && Constants.equalsInteger(model.getStatus(),Constants.ONE)){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"该线路存在正在优化中,请耐心等待优化完成或者选择强制优化操作!");
        }
        if(  jkSketch.getLineIdList() ==null || jkSketch.getLineIdList().size()==0 ){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"请选择有效合理的线路进行优化操作!");
        }
        //当前所有线路(符合条件的线路)
        List<JkLine> lineList =  jkLineMapper.selectJoinList(JkLine.class,new MPJLambdaWrapper<JkLine>()
                .selectAll(JkLine.class)
                .eq(JkLine::getWeeks,DateUtil.getWeekZhouOfDate(model.getDateInfo()))//只能选择当前的线路
                .eq(JkLine::getIsdeleted,Constants.ZERO)
                .in(JkLine::getId,jkSketch.getLineIdList())
        );
        Integer totalCus = 0,totalNum =0;
        for(JkLine line :lineList){
            totalCus += Constants.formatIntegerNum(line.getMaxCustomer());//总客户量
            totalNum += Constants.formatIntegerNum(line.getMaxOrder());//总送货量
        }
        if( totalCus < Constants.formatIntegerNum(model.getOrderNum())  ){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"该线路订单客户数量超过了线路总客户量限制,无法进行优化!");
        }
        if( totalNum < Constants.formatBigdecimal(model.getTotalNum()).doubleValue()){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"该线路订单送货量超过了线路总送货量限制,无法进行优化!");
        }
        model.setLineList(lineList);//线路集合
        model.setCustomerList(checkJketchCustomerLocation(model,false));//订单集合
        model.setStatus(Constants.ONE);
        model.setPlanLineNum(lineList.size());
        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;
    }
    @Override
    public JkSketch initOriginDistance(JkSketch jkSketch ) {
        JkSketch model = jkSketchMapper.selectById(jkSketch.getId());
        if(model == null ||Constants.equalsInteger(model.getIsdeleted(),Constants.ONE)){
            throw  new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!Constants.equalsInteger(model.getStatus(),Constants.ZERO) ){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"该线路已被优化,已无法计算原始路线路程数");
        }
        checkJketchCustomerLocation(model,true);
        model.setEditDate(new Date());
        model.setEditor(jkSketch.getLoginUserInfo().getId());
        model.setDistance(model.getOriginDistance());
        jkSketchMapper.updateById(model);
        return model;
    }
    @Override
    public void updateById(JkSketch jkSketch) {
        jkSketchMapper.updateById(jkSketch);
    @Async
    public  void distanceCustomer( Category cate) {
        try {
            List<JkCustomer> customerList = cate.getCustomerList();
            String url = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.GAODE_DISTANCE_GEOAPI_URL).getCode();
            Date date = new Date();
            BigDecimal cLatitude =new BigDecimal(0);
            BigDecimal cLongitude =new BigDecimal(0);
            String location = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.COMPANY_LOCATION).getCode();
            try {
                String[] ss = location.split(",");
                cLongitude = new BigDecimal(ss[0]);
                cLatitude =  new BigDecimal(ss[1]);
            }catch (Exception e){
            }
            int index =0;
            for(JkCustomer c : customerList){
                try {
                    log.info("交通规划========"+c.getName()+"==========="+index++);
                    dealDistancePerCustomer(c,url,date,cLatitude,cLongitude,customerList);
                }catch (Exception e){
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            cate.setStatus(Constants.ZERO);
            categoryMapper.updateById(cate);//更新任务执行状态
        }
    }
    private void dealDistancePerCustomer(JkCustomer c, String url, Date date, BigDecimal cLatitude, BigDecimal cLongitude, List<JkCustomer> customerList) {
        List<JkCustomer> updateCustomerList = new ArrayList<>();
        List<JkCustomerNavigation> navigationList = new ArrayList<>();
        List<DistanceMapParam> tmpList = new ArrayList<>();
        List<DistanceMapParam> distanceMapParamList  = getListFromJsonStr(c.getDistance());
        boolean isNew = false;
        JkCustomer u =new JkCustomer();
        DistanceMapParam t0 = new DistanceMapParam();
        t0.setId(-2);//表示返回园区
        t0.setDistance(Constants.formatLongNum(c.getStartDistance()) );
        if(Constants.formatLongNum(c.getStartDistance()) <= 0){
            //园区前往该客户的距离,如果之前未获取过
            isNew = true;
            JkCustomer start = new JkCustomer();
            start.setId(-1);
            start.setLongitude(cLongitude);
            start.setLatitude(cLatitude);
            DistanceCustomerModel dm = DistanceCalculator.calculateDistanceGaode(url,start,c);
            c.setStartDistance(dm.getDistance() );
            t0.setDistance(dm.getDistance());
            u.setStartDistance(dm.getDistance());
            if(dm.getLocations().size()>0){
                //如果有路径信息
                u.setStartSteps(dm.getPolyline());
            }
        }
        tmpList.add(t0);
        for(JkCustomer cm : customerList){
            //客户和客户之间的距离信息
            DistanceMapParam t = new DistanceMapParam();
            t.setId(cm.getId());
            DistanceMapParam param = getParamByCustomerIds( cm.getId(),distanceMapParamList);
            if(param!=null){//如果之前已经获取过
                t = param;
            }else{
                JkCustomerNavigation navigation = new JkCustomerNavigation();
                navigation.setStartId(c.getId());
                navigation.setEndId(cm.getId());
                navigation.setIsdeleted(Constants.ZERO);
                navigation.setCreateDate(date);
                navigation.setEditDate(date);
                navigation.setEndLatitude(cm.getLatitude());
                navigation.setEndLogitude(cm.getLongitude());
                navigation.setStartLatitude(c.getLatitude());
                navigation.setStartLogitude(c.getLongitude());
                isNew = true;
                if(Constants.equalsInteger(c.getId(),cm.getId())){
                    t.setDistance(0l);
                }else{
                    DistanceCustomerModel dm = DistanceCalculator.calculateDistanceGaode(url,c,cm);
                    t.setDistance(dm.getDistance() );
                    if(dm.getLocations().size()>0){
                        //如果有路径信息
                        navigation.setSteps(dm.getPolyline());
                    }
                }
                navigation.setIdIndex(c.getId()+"-"+cm.getId());
                navigation.setDistance(t.getDistance());
                navigationList.add(navigation);
            }
            tmpList.add(t);
        }
        DistanceMapParam tt = new DistanceMapParam();
        tt.setId(-2);//表示返回园区
        tt.setDistance(Constants.formatLongNum(c.getEndDistance()));
        if(Constants.formatLongNum(c.getEndDistance()) <= 0){
            //该客户返回园区的距离 ,如果之前未获取过
            isNew = true;
            JkCustomer start = new JkCustomer();
            start.setId(-1);
            start.setLongitude(cLongitude);
            start.setLatitude(cLatitude);
            DistanceCustomerModel dm = DistanceCalculator.calculateDistanceGaode(url,c,start);
            c.setEndDistance(dm.getDistance() );
            tt.setDistance(dm.getDistance());
            u.setEndDistance(dm.getDistance());
            if(dm.getLocations().size()>0){
                //如果有路径信息
                u.setEndSteps(dm.getPolyline());
            }
        }
        tmpList.add(tt);
        if(isNew){//
            u.setDistanceStatus(Constants.ONE);
            u.setId(c.getId());
            u.setDistance(JSONObject.toJSONString(tmpList));
            updateCustomerList.add(u);
        }
        if(updateCustomerList.size()>0){
            for(JkCustomer c1 : updateCustomerList){
                jkCustomerMapper.updateById(c1);//更新客户与其他点之间的距离
            }
        }
        if(navigationList.size()>0){
            jkCustomerNavigationMapper.delete(new UpdateWrapper<JkCustomerNavigation>().lambda()
                    .eq(JkCustomerNavigation::getStartId,c.getId()));//删除历史数据
            jkCustomerNavigationMapper.insert(navigationList);//插入最新数据
        }
    }
    private  List<JkCustomer>  checkNeedDistanceDo( List<JkCustomer> customerList) {
        if(customerList==null || customerList.size()==0){
            return null;
        }
        boolean need = false;
        List<JkCustomer> list = new ArrayList<>();
        for(JkCustomer jk : customerList){
            if(Constants.equalsInteger(jk.getDistanceStatus(),Constants.ZERO)){
                need = true;
                list.add(jk);
            }
        }
        if(!need){//如果无需进行处理
            return null;
        }
        return  list;
    }
    @Override
    public  Category checkDataValid(JkSketch model) {
        if(model.getCategoryId() == null){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        MPJLambdaWrapper<Category> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(Category.class )
                .eq(Category::getIsdeleted,Constants.ZERO)
                .eq(Category::getType,Constants.FOUR)
                .eq(Category::getId,model.getCategoryId() );
        Category c = categoryMapper.selectJoinOne(Category.class,queryWrapper);
        if(c==null){
            throw  new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(Constants.equalsInteger(c.getStatus(),Constants.TWO)){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"该主线正在进行客户距离计算任务,请耐心等待任务完成!");
        }
        MPJLambdaWrapper<JkCustomer> queryWrapper1 = new MPJLambdaWrapper<>();
        queryWrapper1.selectAll(JkCustomer.class )
                .leftJoin(JkLine.class,JkLine::getId,JkCustomer::getLineId)
                .eq(JkCustomer::getIsdeleted,Constants.ZERO)
                .eq(JkLine::getCategoryId,model.getCategoryId() );
        List<JkCustomer> customerList = jkCustomerMapper.selectJoinList(JkCustomer.class,queryWrapper1);
        List<JkCustomer> needList =checkNeedDistanceDo(customerList);
        if(needList == null || needList.size() == 0){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"该主线下无需要进行距离计算的客户信息!");
        }
        String errorMsg ="";
        for(JkCustomer c1 : customerList){
            if(c1.getLatitude()==null || c1.getLongitude() ==null){
                errorMsg += c.getName()+"-"+c.getName()+" | ";
            }
        }
        if(StringUtils.isNotBlank(errorMsg)){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"该线路客户:【"+errorMsg+"】定位信息不完整,不满足距离计算条件!");
        }
        c.setStatus(Constants.TWO);
        categoryMapper.updateById(c);//更新任务执行状态
        c.setCustomerList(customerList);
        return  c;
    }
    @Override
    @Async
    public void startUpdateLineAsync(JkSketch model) {
        try {
            List<JkSketchCustomer> customerList = model.getCustomerList();
            List<JkLine> lineList = model.getLineList();
            TspSolver.DataModel dataModel = new TspSolver.DataModel();
            int vehicleNumber1 = lineList.size();//线路数量
            long[] vehicleCapacities1=new long[lineList.size()];//每辆车的最大订单量限制
            long[] demands1 = new long[customerList.size()+1]; //各个点的订单量
            long[][] distanceMatrix1 = new long[customerList.size()+1][customerList.size()+1];
            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();
                demands1[i+1] = Constants.formatBigdecimal( customerList.get(i).getTotalNum()).longValue();  //各个点的订单量
                for (int j = 0; j< disList.size()-2; j++) {
                    if(j+1 >=10){
                        break;
                    }
                    if(disList.size()>j+1){
                        distanceMatrix1[i+1][j+1]  = disList.get(j+1).getDistance()/1000;//构造距离矩阵
                    }else{
                        distanceMatrix1[i+1][j+1]  = 1l;
                    }
                }
            }
            for (int i = 0; i < lineList.size(); i++) {
                vehicleCapacities1[i] = lineList.get(i).getMaxOrder();//每辆车的最大订单量限制
            }
            //构造优化数据模型
            dataModel.initDataInfo(vehicleNumber1,demands1,vehicleCapacities1,distanceMatrix1);
            TspSolver.startSearch(dataModel);
            dealSearchSolution(model,dataModel);
        }catch (Exception e){
            e.printStackTrace();
            jkSketchMapper.update(null,new UpdateWrapper<JkSketch>().lambda()
                    .eq(JkSketch::getId,model.getId() )
                    .eq(JkSketch::getJobId,model.getJobId() )
                    .set(JkSketch::getPlanLineInfo,"最近一次线路优化失败!")
                    .set(JkSketch::getStatus,Constants.THREE)
                    .set(JkSketch::getPlanLineEndDate,new Date()));
        }
    }
    private void initCustomerDistance( List<JkSketchLine> lineList,JkSketch model,boolean updateLineDistance) {
        List<JkSketchCustomer> customerList = model.getCustomerList();
        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()) );
            if(Constants.formatLongNum(c.getStartDistance()) <= 0){
                //园区前往该客户的距离,如果之前未获取过
                throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"该线路客户:"+c.getName()+"交通规划尚未完成,不满足优化条件!");
            }
            tmpList.add(t0);
            for(JkSketchCustomer cm : customerList){
                //客户和客户之间的距离信息
                DistanceMapParam t = new DistanceMapParam();
                t.setId(cm.getCustomerId());
                DistanceMapParam param = getParamByCustomerIds( cm.getCustomerId(),distanceMapParamList);
                if(param!=null){//如果之前已经获取过
                    t = param;
                }else{
                    throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"该线路客户:"+c.getName()+"交通规划尚未完成,不满足路程计算条件!");
                }
                tmpList.add(t);
            }
            DistanceMapParam tt = new DistanceMapParam();
            tt.setId(-2);//表示返回园区
            tt.setDistance(Constants.formatLongNum(c.getEndDistance()));
            if(Constants.formatLongNum(c.getEndDistance()) <= 0){
                //该客户返回园区的距离 ,如果之前未获取过
                throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"该线路客户:"+c.getName()+"交通规划尚未完成,不满足路程计算条件!");
            }
            tmpList.add(tt);
            c.setDistanceMapParamList(tmpList);
        }
        if(lineList!=null){
            long totalDistance = 0;
            for (JkSketchLine line :lineList){
                long lineDistance = 0;
                List<JkSketchCustomer> customers = getCustomerListByLineId(line.getId(),customerList);
                for(JkSketchCustomer c : customers){
                    lineDistance+= Constants.formatLongNum(c.getStartDistance());
                    lineDistance+= Constants.formatLongNum(c.getEndDistance());
                    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);
                    jkSketchLineMapper.updateById(line);
                }
                totalDistance += lineDistance;
            }
            if(Constants.equalsInteger(model.getStatus(),Constants.ZERO) ||Constants.formatLongNum(model.getOriginDistance()) <= 0l ){
                //如果首次优化或者之前没有计算过,设置优化前原始路径
                model.setOriginDistance(totalDistance);
            }
        }
    }
    private List<JkSketchCustomer> getCustomerListByLineId(Integer id, List<JkSketchCustomer> customerList) {
        List<JkSketchCustomer> list = new ArrayList<>();
        for (JkSketchCustomer c :customerList){
            if(Constants.equalsInteger(c.getSketchLineId(),id)){
                list.add(c);
            }
        }
        return list;
    }
    private DistanceMapParam getParamByCustomerIds(  Integer id1, List<DistanceMapParam> distanceMapParamList) {
        if(distanceMapParamList!=null){
            for(DistanceMapParam p :distanceMapParamList){
                if(Constants.equalsInteger(p.getId(),id1)){
                    return p;
                }
            }
        }
        return null;
    }
    private List<DistanceMapParam> getListFromJsonStr(String distanceJson) {
        try {
            return  JSONObject.parseObject(distanceJson, new TypeReference<List<DistanceMapParam>>(){}.getType());
        }catch (Exception e){
        }
        return new ArrayList<>();
    }
    private void dealSearchSolution(JkSketch model, TspSolver.DataModel dataModel) {
        Date date = new Date();
        List<TspSolverSolutions> solutions = dataModel.getSolutions();
        List<JkSketchLine> sketchLineList = new ArrayList<>();
        List<JkSketchCustomer> sketchCustomerList  = new ArrayList<>();
        long totalDistance = 0l;
        if(solutions!=null && solutions.size()>0){
            for(TspSolverSolutions so : solutions){
                List<Integer> routes = so.getRouteIndex();
                if(routes.size() <=2) {
                    continue;//无客户的非有效路线
                }
                totalDistance+= so.getDistance();
                JkLine  line =model.getLineList().get(so.getLineIndex());
                JkSketchLine tModel =  new JkSketchLine();
                tModel.setSketchId(model.getId());
                tModel.setCreator(model.getEditor());
                tModel.setCreateDate(date);
                tModel.setLineId(line.getId());
                tModel.setTotalNum(new BigDecimal(0));
                tModel.setOrderNum(routes.size()-2);
                tModel.setDateInfo(model.getDateInfo());
                tModel.setSortnum(sketchLineList.size()+1);
                tModel.setEditDate(tModel.getCreateDate());
                tModel.setEditor(tModel.getCreator());
                tModel.setIsdeleted(Constants.ZERO);
                tModel.setDistance(so.getDistance());//
                sketchLineList.add(tModel);
                tModel.setCustomerList( new ArrayList<>());
                //有效路径
                for (Integer cIndex : routes){
                    if(cIndex ==0){
                        continue; //起始点不处理
                    }
                    JkSketchCustomer customer = model.getCustomerList().get(cIndex-1);
                    tModel.setTotalNum(tModel.getTotalNum().add(Constants.formatBigdecimal(customer.getTotalNum())));//送货量
                    JkSketchCustomer cModel =  new JkSketchCustomer();
                    cModel.setCreator(tModel.getCreator());
                    cModel.setOrderId(customer.getOrderId());
                    cModel.setCreateDate(tModel.getCreateDate());
                    cModel.setTotalNum(customer.getTotalNum());
                    cModel.setDateInfo(model.getDateInfo());
                    cModel.setSortnum(tModel.getCustomerList().size()+1);
                    cModel.setEditDate(tModel.getCreateDate());
                    cModel.setEditor(tModel.getCreator());
                    cModel.setIsdeleted(Constants.ZERO);
                    cModel.setSketchId(model.getId());
                    cModel.setCustomerId(customer.getCustomerId());
                    tModel.getCustomerList().add(cModel);
                }
            }
        }
        if(sketchLineList!=null && sketchLineList.size()>0){
            jkSketchLineMapper.update(null,new UpdateWrapper<JkSketchLine>().lambda()
                    .set(JkSketchLine::getIsdeleted,Constants.ONE)
                    .eq(JkSketchLine::getIsdeleted,Constants.ZERO)
                    .eq(JkSketchLine::getDateInfo,model.getDateInfo()));
            jkSketchCustomerMapper.update(null,new UpdateWrapper<JkSketchCustomer>().lambda()
                    .set(JkSketchCustomer::getIsdeleted,Constants.ONE)
                    .eq(JkSketchCustomer::getIsdeleted,Constants.ZERO)
                    .eq(JkSketchCustomer::getDateInfo,model.getDateInfo()));
            if(sketchLineList.size()>0){
                jkSketchLineMapper.insert(sketchLineList);
            }
            for(JkSketchLine l : sketchLineList){
                if(l.getCustomerList()!=null ){
                    for(JkSketchCustomer c :l.getCustomerList()){
                        c.setSketchLineId(l.getId());
                    }
                    sketchCustomerList.addAll(l.getCustomerList());
                }
            }
            if(sketchCustomerList.size()>0){
                jkSketchCustomerMapper.insert(sketchCustomerList);
            }
            jkSketchMapper.update(null,new UpdateWrapper<JkSketch>().lambda()
                    .eq(JkSketch::getId,model.getId() )
                    .eq(JkSketch::getJobId,model.getJobId() )
                    .set(JkSketch::getLineNum,sketchLineList.size() )
                    .set(JkSketch::getDistance,totalDistance)
                    .set(JkSketch::getPlanLineInfo,"最近一次线路优化成功,优化后总距离:"+(totalDistance/1000)+"公里!")
                    .set(JkSketch::getStatus,Constants.TWO)
                    .set(JkSketch::getPlanLineEndDate,date));
        }else{
            jkSketchMapper.update(null,new UpdateWrapper<JkSketch>().lambda()
                    .eq(JkSketch::getId,model.getId() )
                    .eq(JkSketch::getJobId,model.getJobId() )
                    .set(JkSketch::getPlanLineInfo,"最近一次线路优化失败,未找到最优路线!")
                    .set(JkSketch::getStatus,Constants.THREE)
                    .set(JkSketch::getPlanLineEndDate,date));
        }
    }
    private   List<JkSketchCustomer>  checkJketchCustomerLocation(JkSketch model,boolean updateLineDistance) {
        MPJLambdaWrapper<JkSketchCustomer> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(JkSketchCustomer.class )
                .selectAs(JkCustomer::getName,JkSketchCustomer::getName)
                .selectAs(JkCustomer::getCode,JkSketchCustomer::getCode)
                .selectAs(JkCustomer::getDistanceStatus,JkSketchCustomer::getDistanceStatus)
                .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::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(),"该线路客户信息为空,不满足优化条件!");
        }
        String errorMsg ="";
        String errorMsg1 ="";
        for(JkSketchCustomer c : customerList){
            if(c.getLatitude()==null || c.getLongitude() ==null){
                errorMsg += c.getName()+"-"+c.getName()+" | ";
            }
            if(!Constants.equalsInteger(c.getDistanceStatus(),Constants.ONE)){
                errorMsg1 += c.getName()+"-"+c.getName()+" | ";
            }
        }
        if(StringUtils.isNotBlank(errorMsg)){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"该线路客户:【"+errorMsg+"】定位信息不完整,不满足优化条件!");
        }
        if(StringUtils.isNotBlank(errorMsg1)){
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"该线路客户:【"+errorMsg1+"】交通规划尚未完成,不满足优化条件!");
        }
        model.setCustomerList(customerList);
        MPJLambdaWrapper<JkSketchLine> queryWrapper1 = new MPJLambdaWrapper<>();
        queryWrapper1.selectAll(JkSketchLine.class )
                .eq(JkSketchLine::getSketchId, model.getId())
                .eq(JkSketchLine::getIsdeleted,Constants.ZERO)
                .orderByAsc(JkSketchLine::getSortnum);
        List<JkSketchLine> lineList = jkSketchLineMapper.selectJoinList(JkSketchLine.class,queryWrapper1);
        initCustomerDistance(lineList,model,updateLineDistance);
        return customerList;
    }
    @Override
@@ -184,7 +727,6 @@
        QueryWrapper<JkSketch> wrapper = new QueryWrapper<>(jkSketch);
        return jkSketchMapper.selectCount(wrapper);
    }
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public List<JkSketch> importBatch(MultipartFile file, String dateInfoStr, LoginUserInfo loginUser){
@@ -216,9 +758,11 @@
                    .selectAll(JkCustomer.class)
                    .eq(JkCustomer::getIsdeleted,Constants.ZERO)
            );
            String week =  DateUtil.getWeekZhouOfDate(dateInfo);
            //当前所有线路
            List<JkLine> lineList =  jkLineMapper.selectJoinList(JkLine.class,new MPJLambdaWrapper<JkLine>()
                    .selectAll(JkLine.class)
                    .eq(JkLine::getWeeks,week)//只能选择当前的线路
                    .eq(JkLine::getIsdeleted,Constants.ZERO)
            );
            List<JkOrders> newOrderList = new ArrayList<>();
@@ -255,8 +799,7 @@
            if(newOrderList.size()>0){
                jkOrdersMapper.insert(newOrderList);
            }
            List<JkSketchLine> sketchLineList = initNewSketchLineList(newList,newOrderList);
            List<JkSketchLine> sketchLineList = initNewSketchLineList(newList,newOrderList,lineList);
            jkSketchLineMapper.update(null,new UpdateWrapper<JkSketchLine>().lambda()
                    .set(JkSketchLine::getIsdeleted,Constants.ONE)
                    .eq(JkSketchLine::getIsdeleted,Constants.ZERO)
@@ -264,7 +807,6 @@
            if(sketchLineList.size()>0){
                jkSketchLineMapper.insert(sketchLineList);
            }
            List<JkSketchCustomer> sketchCuustomerList = initNewSketchCustomerList(sketchLineList,newOrderList);//按照主线路分组
            jkSketchCustomerMapper.update(null,new UpdateWrapper<JkSketchCustomer>().lambda()
                    .set(JkSketchCustomer::getIsdeleted,Constants.ONE)
@@ -284,6 +826,8 @@
        }
    }
    private List<JkSketchCustomer> initNewSketchCustomerList(List<JkSketchLine> sketchLineList, List<JkOrders> newOrderList) {
        List<JkSketchCustomer> list =new ArrayList<>();
@@ -308,32 +852,35 @@
        }
        return list;
    }
    private List<JkSketchLine> initNewSketchLineList(List<JkSketch> newList, List<JkOrders> newOrderList) {
    private List<JkSketchLine> initNewSketchLineList(List<JkSketch> newList, List<JkOrders> newOrderList,List<JkLine> lineList) {
        List<JkSketchLine> list =new ArrayList<>();
        for(JkOrders orders : newOrderList){
            JkSketchLine tModel = findModelFromListByLineId(orders.getLineId(),list);
            if(tModel!=null){
                tModel.setTotalNum(  tModel.getTotalNum().add(orders.getNum()));//订单量累计
                tModel.setOrderNum(tModel.getOrderNum()+1);//客户数加1
            }else{
                tModel = new JkSketchLine();
                tModel.setSketchId(getSKetchIdByyCategoryId(orders.getCategoryId(),newList));
                tModel.setCreator(orders.getCreator());
                tModel.setCreateDate(orders.getCreateDate());
                tModel.setLineId(orders.getLineId());
                tModel.setTotalNum(orders.getNum());
                tModel.setOrderNum(1);
                tModel.setDateInfo(orders.getDateInfo());
                tModel.setSortnum(list.size()+1);
                tModel.setEditDate(tModel.getCreateDate());
                tModel.setEditor(tModel.getCreator());
                tModel.setIsdeleted(Constants.ZERO);
                if(tModel.getSketchId()!=null){
                    list.add(tModel);
                }
        for (JkLine line : lineList){
            JkSketchLine tModel =  new JkSketchLine();
            tModel.setSketchId(getSKetchIdByyCategoryId(line.getCategoryId(),newList));
            tModel.setCreator(newList.get(0).getCreator());
            tModel.setCreateDate(newList.get(0).getCreateDate());
            tModel.setLineId(line.getId());
            initOrderAndTotalNum(tModel,line.getId(),newOrderList);
            tModel.setDateInfo(newList.get(0).getDateInfo());
            tModel.setSortnum(list.size()+1);
            tModel.setEditDate(tModel.getCreateDate());
            tModel.setEditor(tModel.getCreator());
            tModel.setIsdeleted(Constants.ZERO);
            if(tModel.getSketchId()!=null && Constants.formatIntegerNum(tModel.getOrderNum()) >0){
                list.add(tModel);
            }
        }
        return list;
    }
    private void initOrderAndTotalNum(JkSketchLine tModel, Integer id, List<JkOrders> newOrderList) {
        for(JkOrders orders : newOrderList){
            if(Constants.equalsInteger(orders.getLineId(),id)){
                tModel.setOrderNum(Constants.formatIntegerNum(tModel.getOrderNum())+1);
                tModel.setTotalNum(Constants.formatBigdecimal(tModel.getTotalNum()).add(Constants.formatBigdecimal(orders.getNum())));
            }
        }
    }
    private Integer getSKetchIdByyCategoryId(Integer categoryId, List<JkSketch> newList) {
@@ -366,6 +913,7 @@
            }else{
                tModel = new JkSketch();
                tModel.setLineNum(getLineNumByOrders(orders.getCategoryId(),newOrderList));
                tModel.setPlanLineNum(tModel.getLineNum());
                tModel.setCreator(orders.getCreator());
                tModel.setCreateDate(orders.getCreateDate());
                tModel.setCategoryId(orders.getCategoryId());
@@ -429,7 +977,7 @@
        }
        JkLine line = findLineFromListByName(model.getLineName(),lineList);
        if(line == null){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + (index + 3) + "行线路【" + model.getLineName() + "】不存在,请检查表格内容!");
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + (index + 3) + "行线路【" + model.getLineName() + "】不存在或者配送周期不符合,请检查表格内容!");
        }
        JkOrders tModel =   new JkOrders();
@@ -440,6 +988,8 @@
        tModel.setNum(getDecimalByVal(model.getNum()));
        tModel.setCustomerId(customer.getId());
        tModel.setDateInfo(dateInfo);
        tModel.setLatitude(customer.getLatitude());
        tModel.setLongitude(customer.getLongitude());
        tModel.setSortnum(model.getSortnum());
        tModel.setLineId(line.getId());
        tModel.setCategoryId(line.getCategoryId());