MrShi
2025-09-15 856f526f823f5dad88c28657d82f971ff66afb1e
server/web/src/main/java/com/doumee/jtt808/web/service/Jtt808Service.java
@@ -3,6 +3,7 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.doumee.biz.system.SystemDictDataBiz;
@@ -26,10 +27,12 @@
import com.doumee.dao.business.web.request.LocaltionDTO;
import com.doumee.dao.business.web.request.OpenElecBikeRequest;
import com.doumee.dao.business.web.response.MemberRidesDetailResponse;
import com.doumee.dao.business.web.response.UserResponse;
import com.doumee.jtt808.web.endpoint.MessageManager;
import com.doumee.service.business.GoodsorderService;
import com.doumee.service.business.PricingRuleService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
@@ -55,6 +58,7 @@
import java.util.concurrent.ConcurrentMap;
@Service
@Slf4j
public class Jtt808Service {
    @Value("${tencent.map.remoteHost}")
    private String mapHost;
@@ -151,7 +155,7 @@
                    if(bike.getSiteId() ==null){
                        throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "对不起,请按要求前往停车点停车!");
                    }
                    lockBikes(bike.getDeviceSn(),Constants.ZERO);//发起关锁指令请求
                    lockBikes(bike.getDeviceSn(),Constants.ONE);//发起关锁指令请求
                    backIds.add(rides.getId());
                    rides.setStatus(Constants.MEMBER_RIDES_STATUS.BACK_CYCLING.getKey());//已还车
                    rides.setBackSiteId(bike.getSiteId());//还车站点
@@ -171,6 +175,10 @@
        goodsorder.setEditDate(date);
        goodsorder.setEditor(null);
        goodsorderService.dealCloseGoodsorderBiz( goodsorder,Constants.REFUND_TYPE.NORMAL.getKey(),memberRides);
        //更新电动车车辆状态
        bikesMapper.update(null,new UpdateWrapper<Bikes>().lambda().set(Bikes::getStatus,Constants.ZERO).eq(Bikes::getType,Constants.ONE)
                .apply(" code in ( select m.bike_code from member_rides m where m.ordre_id = '"+goodsorder.getId()+"' ) ")
        );
    }
    private Bikes getElecBikeByCode(String bikeCode) {
@@ -213,9 +221,12 @@
        APIResult<T0201_0500> data = result.block();
        if(!data.isSuccess()){
            //如果开锁失败,则返回异常
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,"+(type ==0?"车辆关锁":"车辆开锁")+"失败,请稍后重试,或者联系园区管理人员!");
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,"+(type ==0?"车辆开锁":"车辆关锁")+"失败,请稍后重试,或者联系园区管理人员!");
        }
    }
    /**
     * 查询停车站点信息
@@ -279,6 +290,7 @@
        Bikes bike = getElecBikeByCode(memberRides.getBikeCode());
        //查询停车位置是否符合停车位置规范
        checkPausePostionBiz(bike);
        lockBikes(bike.getDeviceSn(),Constants.ONE);//发起关锁指令请求
        memberRides.setPauseDate(new Date());
        memberRides.setEditDate(memberRides.getBackDate());
        memberRides.setStatus(Constants.MEMBER_RIDES_STATUS.RIDES_PAUSING.getKey());
@@ -292,7 +304,7 @@
            ruleType =Integer.parseInt( systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME,Constants.PAUSE_BIKE_TYPE).getCode());
        }catch (Exception e){
        }
        if(ruleType == 0){
        if(ruleType == 1){
            //如果仅限于园区临时停车
            List<LocaltionDTO> array = new ArrayList<>();
            try {
@@ -303,7 +315,7 @@
            if(array == null || array.size() ==0){
                throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "对不起,园区运营区域配置有误,请联系园区管理员处理!");
            }
            if( !PositionUtil.isInPolygon(bike.getLatitude().doubleValue(),bike.getLongitude().doubleValue(),array)){
            if( !PositionUtil.isInPolygon(bike.getLongitude().doubleValue(),bike.getLatitude().doubleValue(),array)){
                //如果在停车点范围内;
                throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "对不起,请按要求在园区运营区域范围内停车!");
            }
@@ -399,9 +411,16 @@
        }
        Bikes bike = bikesMapper.selectOne(new QueryWrapper<Bikes>().lambda().eq(Bikes::getCode,openElecBikeRequest.getCode()).eq(Bikes::getIsdeleted,Constants.ZERO)
                .eq(Bikes::getType,Constants.ONE).last(" limit 1 "));
        if(bike == null && StringUtils.isBlank(bike.getParamId())){
            //如果车辆类型是空
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), " 该车型暂时不能借车操作哦,请更换其他车型重试!");
        }
        if(!Constants.equalsInteger(bike.getBikeStatus(),Constants.ONE)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"该车辆状态异常,不支持出借,请尝试更换其他车辆!");
        }
        if(!Constants.equalsInteger(bike.getStatus(),Constants.ZERO)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,该车辆已被锁定借用,请尝试更换其他车辆!");
        }
        cacheOpenLock.put(openElecBikeRequest.getCode(), 1);
        try {
@@ -434,7 +453,7 @@
            //根据车型查询计价方案
            isValidePricingType(bike,memberRides) ;
            MemberRidesDetailResponse memberRidesDetailResponse = new MemberRidesDetailResponse();
            this.lockBikes(bike.getDeviceSn(),1);
            this.lockBikes(bike.getDeviceSn(),0);
            //存储骑行记录
            memberRides.setId(Constants.getUUID());
            memberRides.setIsdeleted(Constants.ZERO);
@@ -492,7 +511,7 @@
            //如果车辆类型是空
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "车型信息异常,请联系管理员");
        }
        this.lockBikes(bike.getDeviceSn(),1);
        this.lockBikes(bike.getDeviceSn(),0);
        //更新骑行状态为骑行中
        memberRides.setEditDate(memberRides.getBackDate());
        memberRides.setStatus(Constants.MEMBER_RIDES_STATUS.RIDES_RUNNING.getKey());
@@ -587,7 +606,7 @@
                                .eq(Bikes::getIsdeleted,Constants.ZERO)
                                .eq(Bikes::getCode,rides.getBikeCode())
                        );
                        lockBikes(bike.getDeviceSn(),Constants.ZERO);//发起关锁指令请求
                        lockBikes(bike.getDeviceSn(),Constants.ONE);//发起关锁指令请求
                        rides.setStatus(Constants.MEMBER_RIDES_STATUS.BACK_CYCLING.getKey());//已还车
                        rides.setBackSiteId(bike.getSiteId());//还车站点
                        rides.setBackDate(new Date());
@@ -605,6 +624,12 @@
            goodsorder.setEditDate(date);
            goodsorder.setEditor(null);
            goodsorderService.dealCloseGoodsorderBiz( goodsorder,Constants.REFUND_TYPE.NORMAL.getKey(),memberRides);
            //更新电动车车辆状态
            bikesMapper.update(null,new UpdateWrapper<Bikes>().lambda().set(Bikes::getStatus,Constants.ZERO).eq(Bikes::getType,Constants.ONE)
                    .apply(" code in ( select m.bike_code from member_rides m where m.ordre_id = '"+goodsorder.getId()+"' ) ")
            );
            //发送小程序通知
            Member member = memberMapper.selectById(goodsorder.getMemberId());
            if(Objects.nonNull(member)&&StringUtils.isNotBlank(member.getOpenid())){
@@ -615,6 +640,52 @@
        }
    }
    public String updateLockStatusBatch(UserResponse user, List<String> idList, Integer lockStatus) {
        if(idList ==null && idList.size()==0){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        String message = "";
        int sNum =0,eNum=0,ingNum=0;
        lockStatus = lockStatus==0?0:1;
        List<Bikes> bikesList = bikesMapper.selectList(new LambdaQueryWrapper<Bikes>()
                .eq(Bikes::getType,Constants.ONE)
                .in(Bikes::getIsdeleted,Constants.ZERO)
                .in(Bikes::getId,idList)
        );
        for(Bikes bike: bikesList){
            if(Constants.equalsInteger(bike.getStatus(),Constants.ONE)){
                //出借中
                ingNum++;
                continue;
            }
            boolean r = true;
            try {
                this.lockBikes(bike.getDeviceSn(),lockStatus==0?1:0);
            }catch (Exception e){
                log.error(e.getMessage());
                r =false;
                eNum++;
            }
            if(r){
                bikesMapper.update(null,new UpdateWrapper<Bikes>().lambda()
                        .set(Bikes::getEditDate,new Date())
                        .set(Bikes::getEditor,user.getId())
                        .set(Bikes::getLockStatus,lockStatus)
                        .set(Bikes::getIsdeleted,Constants.ZERO)
                        .eq(Bikes::getType,Constants.ONE )
                        .eq(Bikes::getId,bike.getId()));
                sNum++;
            }
        }
       message="成功操作["+sNum+"]辆电车";
        if(ingNum>0){
            message = message+",["+ingNum+"]辆电车正出借中";
        }
        if(eNum>0){
            message = message+",["+eNum+"]辆电车远程操作失败";
        }
        return message;
    }