jiangping
2025-02-28 43a53e5a46ba45a6d35e3913fdfdd6f82a84a038
server/web/src/main/java/com/doumee/jtt808/web/service/Jtt808Service.java
@@ -8,12 +8,16 @@
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.constants.Constants;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.dingding.DingDingNotice;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.utils.DateUtil;
import com.doumee.core.utils.PositionUtil;
import com.doumee.core.utils.StringTools;
import com.doumee.core.wx.SendWxMessage;
import com.doumee.core.wx.WxMiniConfig;
import com.doumee.core.wx.WxMiniUtilService;
import com.doumee.dao.business.*;
import com.doumee.dao.business.join.BikesJoinMapper;
import com.doumee.dao.business.join.GoodsorderJoinMapper;
import com.doumee.dao.business.join.MemberRidesJoinMapper;
import com.doumee.dao.business.join.RefundJoinMapper;
@@ -26,6 +30,7 @@
import com.doumee.service.business.GoodsorderService;
import com.doumee.service.business.PricingRuleService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -51,9 +56,9 @@
@Service
public class Jtt808Service {
    @Value("${tencent.map.remoteHost:}")
    @Value("${tencent.map.remoteHost}")
    private String mapHost;
    @Value("${tencent.map.appKey:}")
    @Value("${tencent.map.appKey}")
    private String mapKey;
    @Autowired
    private MessageManager messageManager;
@@ -61,6 +66,8 @@
    private GoodsorderService goodsorderService;
    @Autowired
    private BikesMapper bikesMapper;
    @Autowired
    private BikesJoinMapper bikesJoinMapper;
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
@@ -73,7 +80,6 @@
    @Autowired
    private MemberRidesJoinMapper memberRidesJoinMapper;
    @Autowired
    private PricingParamMapper pricingParamMapper;
@@ -82,6 +88,9 @@
    @Autowired
    private HolidaysMapper holidaysMapper;
    @Autowired
    private SendWxMessage sendWxMessage;
    @Autowired
@@ -92,6 +101,10 @@
        return  data;
    }
    /**
     * 手动还车
     * @param param
     */
    @Transactional(rollbackFor = {BusinessException.class})
    public   void backElecBike(BackElecBikeRequest param) {
        Member member = memberMapper.selectById(param.getMemberId());
@@ -101,7 +114,24 @@
        Goodsorder gparam = new Goodsorder();
        gparam.setMemberId(param.getMemberId());
        gparam.setStatus(Constants.goodsorderStatus.pay);
        gparam.setType(Constants.ZERO);
        Goodsorder goodsorder =  goodsorderService.findOne(gparam);
        this.backBike(goodsorder);
    }
    /**
     * 自动还车
     * @param id
     */
    @Transactional
    public void forceBack(String id){
        Goodsorder goodsorder =  goodsorderService.findById(id);
        this.backBike(goodsorder);
    }
    @Transactional(rollbackFor = {BusinessException.class})
    public void backBike(Goodsorder goodsorder){
        if(Objects.isNull(goodsorder)){
            throw new BusinessException(ResponseStatus.NO_UNCLOSEED_ORDER.getCode(),"无骑行订单记录");
        }
@@ -125,7 +155,12 @@
                    backIds.add(rides.getId());
                    rides.setStatus(Constants.MEMBER_RIDES_STATUS.BACK_CYCLING.getKey());//已还车
                    rides.setBackSiteId(bike.getSiteId());//还车站点
                    rides.setBackDate(new Date());
                    Integer freeRentTime = Integer.valueOf(systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME, Constants.FREE_RENT_TIME).getCode());
                    Integer rideTime = DateUtil.betweenMin(rides.getRentDate(), rides.getBackDate());
                    //计算骑行计费时长
                    rides.setDuration( rideTime > freeRentTime  ? rideTime : 0 );
                    rides.setEditDate(rides.getBackDate());
                    memberRidesJoinMapper.updateById(rides);//更新骑行状态为已还车
                }
@@ -178,7 +213,7 @@
        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?"车辆关锁":"车辆开锁")+"失败,请稍后重试,或者联系园区管理人员!");
        }
    }
@@ -202,7 +237,7 @@
            if(array == null || array.size() ==0){
                continue;
            }
            if(PositionUtil.isInPolygon(lat,lng,array)){
            if(PositionUtil.isInPolygon(lng,lat,array)){
                //如果在停车点范围内;
                site = sites;
                break;
@@ -227,6 +262,7 @@
        Goodsorder gparam = new Goodsorder();
        gparam.setMemberId(param.getMemberId());
        gparam.setStatus(Constants.goodsorderStatus.pay);
        gparam.setType(Constants.ZERO);
        Goodsorder goodsorder =  goodsorderService.findOne(gparam);
        if(Objects.isNull(goodsorder)){
            throw new BusinessException(ResponseStatus.NO_UNCLOSEED_ORDER.getCode(),"对不起,未查询到您当前有骑行中订单记录");
@@ -279,8 +315,12 @@
        }
    }
    public void updateBikesInfo(List<T0200> list) {
    public void updateBikesInfo(List<T0200> list) throws Exception{
        Date date = new Date();
        BigDecimal lowVoltage = new BigDecimal(systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME,Constants.LOW_VOLTAGE).getCode());
        String ddToken = systemDictDataBiz.queryByCode(Constants.DINGDING,Constants.DINGDING_TOKEN).getCode();
        String ddRobotCode = systemDictDataBiz.queryByCode(Constants.DINGDING,Constants.DINGDING_ROBOTCODE).getCode();
        String ddChatToken = systemDictDataBiz.queryByCode(Constants.DINGDING,Constants.DINGDING_CHATTOKEN).getCode();
        for(T0200 m : list){
            Bikes bike = new Bikes();
            bike.setDeviceSn(m.getClientId());
@@ -290,6 +330,7 @@
            if(m.getLongitude()!=0){
                bike.setLongitude(new BigDecimal(m.getLongitude()).divide(new BigDecimal(1000000),8,BigDecimal.ROUND_HALF_UP));
            }
            updateMapLocation(bike);
            bike.setHeartDate(DateUtil.getDateFromLocalDateTime(m.getDeviceTime()));
            if(m.getAttributes()!=null ){
                Battery battery= (Battery) m.getAttributes().get(AttributeKey.Battery);
@@ -321,7 +362,27 @@
                    .set(Bikes::getSiteId,site ==null?null:site.getId())
                    .set( Bikes::getHeartDate,date)
                    .eq(Bikes::getId,bikes.getId()));
            if(bikes.getVoltage().compareTo(lowVoltage)>=Constants.ZERO
                    && bike.getVoltage().compareTo(lowVoltage)<Constants.ZERO){
                //发送钉钉通知
                DingDingNotice.lowVoltageNotice(bikes.getCode(),
                        PositionUtil.getTxMapAddrByLatAndLng(Double.valueOf(bike.getLatitude().toString()),Double.valueOf(bike.getLongitude().toString()),mapHost,mapKey)
                        ,Objects.nonNull(site)?site.getName():null,
                        ddToken,
                        ddRobotCode,
                        ddChatToken);
            }
        }
    }
    private void updateMapLocation(Bikes bike) {
        if(bike.getLatitude() == null || bike.getLongitude() == null){
            return;
        }
        PositionUtil.AMap aMap =PositionUtil.transform(bike.getLongitude().doubleValue() ,bike.getLatitude().doubleValue());
        bike.setLatitude(new BigDecimal(aMap.getLatitude()));
        bike.setLongitude(new BigDecimal(aMap.getLongitude()));
    }
    public ConcurrentMap<String,Integer> cacheOpenLock = new ConcurrentReferenceHashMap<>();
@@ -342,12 +403,13 @@
            //如果车辆类型是空
            throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), " 该车型暂时不能借车操作哦,请更换其他车型重试!");
        }
        cacheOpenLock.put(bike.getCode(), 1);
        cacheOpenLock.put(openElecBikeRequest.getCode(), 1);
        try {
            //判断当前是否已支付押金
            Goodsorder goodsorder = goodsorderService.findOneByWrapper(new QueryWrapper<Goodsorder>()
                    .eq("isdeleted", Constants.ZERO)
                    .eq("status", Constants.goodsorderStatus.pay)
                    .eq("type", Constants.ZERO)
                    .eq("member_id", member.getId())
                    .orderByDesc("create_date")
                    .last("limit 1"));
@@ -381,17 +443,19 @@
            memberRides.setOrdreId(goodsorder.getId());
            memberRides.setBikeCode(bike.getCode());
            memberRides.setRentDate(new Date());
            memberRides.setType(Constants.ZERO);
            memberRides.setType(Constants.ONE);
            memberRides.setBalance(new BigDecimal(systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME,Constants.RENT_DEPOSIT).getCode()));
            memberRides.setStatus(Constants.MEMBER_RIDES_STATUS.RIDES_RUNNING.getKey());
            memberRides.setCloseStatus(Constants.ZERO);
            memberRidesJoinMapper.insert(memberRides);
            BeanUtils.copyProperties(memberRides, memberRidesDetailResponse);
            return memberRidesDetailResponse;
        }catch (Exception e){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "开锁失败,请联系管理员");
        }catch (BusinessException biz){
            throw  biz;
        }catch (Exception biz){
            throw  new BusinessException(ResponseStatus.SERVER_ERROR);
        }finally {
            cacheOpenLock.remove(bike.getCode());
            cacheOpenLock.remove(openElecBikeRequest.getCode());
        }
    }
@@ -405,6 +469,7 @@
        Goodsorder gparam = new Goodsorder();
        gparam.setMemberId(param.getMemberId());
        gparam.setStatus(Constants.goodsorderStatus.pay);
        gparam.setType(Constants.ZERO);
        Goodsorder goodsorder =  goodsorderService.findOne(gparam);
        if(Objects.isNull(goodsorder)){
            throw new BusinessException(ResponseStatus.NO_UNCLOSEED_ORDER.getCode(),"对不起,未查询到您当前有骑行中订单记录");
@@ -496,17 +561,18 @@
    /**
     * 临时停车超时 自动还车
     */
    public void autoBackBike(){
    public void autoBackBike() throws WxErrorException {
       List<MemberRides> memberRidesList =  memberRidesJoinMapper.selectJoinList(MemberRides.class,new MPJLambdaWrapper<MemberRides>()
                .selectAll(MemberRides.class)
                .leftJoin(Goodsorder.class,Goodsorder::getId,MemberRides::getOrdreId)
                .eq(Goodsorder::getStatus,Constants.ONE)
                .eq(MemberRides::getIsdeleted,Constants.ZERO)
                .eq(MemberRides::getType,Constants.ONE)
                .eq(MemberRides::getStatus,Constants.MEMBER_RIDES_STATUS.RIDES_PAUSING)
                .apply(" NOW() >= t.create_date  + INTERVAL '2 hours' ")
                .eq(MemberRides::getStatus,Constants.MEMBER_RIDES_STATUS.RIDES_PAUSING.getKey())
                .apply(" NOW() >= (t.create_date  + INTERVAL '"+systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME,Constants.PAUSE_AUTO_CLOSE_TIME).getCode()+" min') ")
        );
        ;
        for (MemberRides timeOutRides:memberRidesList) {
            Goodsorder goodsorder = goodsorderService.findById(timeOutRides.getOrdreId());
            //查询骑行记录
@@ -525,6 +591,10 @@
                        rides.setStatus(Constants.MEMBER_RIDES_STATUS.BACK_CYCLING.getKey());//已还车
                        rides.setBackSiteId(bike.getSiteId());//还车站点
                        rides.setBackDate(new Date());
                        Integer freeRentTime = Integer.valueOf(systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME, Constants.FREE_RENT_TIME).getCode());
                        Integer rideTime = DateUtil.betweenMin(rides.getRentDate(), rides.getBackDate());
                        //计算骑行计费时长
                        rides.setDuration( rideTime > freeRentTime  ? rideTime : 0 );
                        rides.setEditDate(rides.getBackDate());
                        memberRidesJoinMapper.updateById(rides);//更新骑行状态为已还车
                    }
@@ -535,12 +605,14 @@
            goodsorder.setEditDate(date);
            goodsorder.setEditor(null);
            goodsorderService.dealCloseGoodsorderBiz( goodsorder,Constants.REFUND_TYPE.NORMAL.getKey(),memberRides);
            //发送小程序通知
            Member member = memberMapper.selectById(goodsorder.getMemberId());
            if(Objects.nonNull(member)&&StringUtils.isNotBlank(member.getOpenid())){
                sendWxMessage.bookingsCancel(member.getOpenid(),goodsorder.getId(),
                        WxMiniConfig.wxMaService.getAccessToken(),
                        timeOutRides.getBikeCode(),goodsorder.getPayDate(),goodsorder.getEditDate());
            }
        }
    }