From 07ca844b3441a2774a1ca02a952e27e3fe986819 Mon Sep 17 00:00:00 2001
From: jiangping <jp@doumee.com>
Date: 星期三, 19 二月 2025 17:50:44 +0800
Subject: [PATCH] jtt808初始化

---
 server/web/src/main/java/com/doumee/jtt808/web/service/Jtt808Service.java |  306 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 306 insertions(+), 0 deletions(-)

diff --git a/server/web/src/main/java/com/doumee/jtt808/web/service/Jtt808Service.java b/server/web/src/main/java/com/doumee/jtt808/web/service/Jtt808Service.java
index 1ae9388..b22e0eb 100644
--- a/server/web/src/main/java/com/doumee/jtt808/web/service/Jtt808Service.java
+++ b/server/web/src/main/java/com/doumee/jtt808/web/service/Jtt808Service.java
@@ -1,23 +1,329 @@
 package com.doumee.jtt808.web.service;
 
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.TypeReference;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.doumee.biz.system.SystemDictDataBiz;
+import com.doumee.core.constants.Constants;
+import com.doumee.core.constants.ResponseStatus;
+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.WxMiniUtilService;
+import com.doumee.dao.business.*;
+import com.doumee.dao.business.join.GoodsorderJoinMapper;
+import com.doumee.dao.business.join.MemberRidesJoinMapper;
+import com.doumee.dao.business.join.RefundJoinMapper;
+import com.doumee.dao.business.model.*;
+import com.doumee.dao.business.web.request.BackElecBikeRequest;
+import com.doumee.dao.business.web.request.LocaltionDTO;
 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 org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.yzh.commons.model.APIResult;
+import org.yzh.protocol.commons.transform.AttributeKey;
+import org.yzh.protocol.commons.transform.attribute.Battery;
+import org.yzh.protocol.t808.T0200;
 import org.yzh.protocol.t808.T0201_0500;
 import org.yzh.protocol.t808.T8500;
 import reactor.core.publisher.Mono;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Objects;
 
 @Service
 public class Jtt808Service {
     @Autowired
     private MessageManager messageManager;
+    @Autowired
+    private GoodsorderService goodsorderService;
+    @Autowired
+    private BikesMapper bikesMapper;
 
+    @Autowired
+    private DiscountMemberMapper discountMemberMapper;
+
+    @Autowired
+    private DiscountLogMapper discountLogMapper;
+
+    @Autowired
+    GoodsorderJoinMapper goodsorderJoinMapper;
+
+    @Autowired
+    private SystemDictDataBiz systemDictDataBiz;
+
+    @Autowired
+    private MemberMapper memberMapper;
+    @Autowired
+    private BaseParamMapper baseParamMapper;
+    @Autowired
+    private RefundMapper refundMapper;
+
+    @Autowired
+    private RefundJoinMapper refundJoinMapper;
+    @Autowired
+    private SitesMapper sitesMapper;
+
+    @Autowired
+    private MemberRidesJoinMapper memberRidesJoinMapper;
+
+    @Autowired
+    private AdMapper adMapper;
+
+    @Autowired
+    private TransactionsMapper transactionsMapper;
+
+    @Autowired
+    private WxMiniUtilService wxMiniUtilService;
+
+    @Autowired
+    PricingRuleService pricingRuleService;
     public  APIResult<T0201_0500>  bikeControl(@RequestBody T8500 request) {
         Mono<APIResult<T0201_0500>>  result = messageManager.requestR(request, T0201_0500.class);
         APIResult<T0201_0500> data = result.block();
         return  data;
     }
 
+    @Transactional(rollbackFor = {BusinessException.class})
+    public   void backElecBike(BackElecBikeRequest param) {
+        Member member = memberMapper.selectById(param.getMemberId());
+        if (member == null) {
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝鎮ㄧ殑璐﹀彿淇℃伅鏌ヨ澶辫触锛岃杩斿洖閲嶆柊杩涘叆灏忕▼搴忓啀璇曪紒");
+        }
+        Goodsorder gparam = new Goodsorder();
+        gparam.setMemberId(param.getMemberId());
+        gparam.setStatus(Constants.goodsorderStatus.pay);
+        Goodsorder goodsorder =  goodsorderService.findOne(gparam);
+        if(Objects.isNull(goodsorder)){
+            throw new BusinessException(ResponseStatus.NO_UNCLOSEED_ORDER.getCode(),"鏃犻獞琛岃鍗曡褰�");
+        }
+        if(Constants.formatIntegerNum(goodsorder.getStatus()) != Constants.GOODSORDER_STATUS.HAVING_PAY.getKey() ){
+            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"瀵逛笉璧凤紝璇ヨ鍗曞綋鍓嶇姸鎬佷笉鏀寔缁撶畻鎿嶄綔锛岃灏濊瘯鍒锋柊鍒楄〃閲嶈瘯锛�" );
+        }
+        //鏌ヨ楠戣璁板綍
+        List<MemberRides>  memberRides = goodsorderService.getMemberRidesForClose(goodsorder.getId());
+        List<String> backIds = new ArrayList<>();
+        if(memberRides!=null && memberRides.size()>0){
+            for (MemberRides  rides : memberRides){
+                if(Constants.equalsInteger(rides.getStatus(),Constants.MEMBER_RIDES_STATUS.RIDES_RUNNING.getKey())
+                        &&Constants.equalsInteger(rides.getType(),Constants.ONE)){
+                    //濡傛灉鏄數杞﹀苟涓旀槸楠戣涓� 杩涜鍏抽攣澶勭悊
+                    Bikes  bike = getElecBikeByCode(rides.getBikeCode());
+                    //鏌ヨ鍋滆溅绔欑偣淇℃伅
+                    if(bike.getSiteId() ==null){
+                        throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "瀵逛笉璧凤紝璇锋寜瑕佹眰鍓嶅線鍋滆溅鐐瑰仠杞︼紒");
+                    }
+                    lockBikes(bike.getDeviceSn(),Constants.ZERO);//鍙戣捣鍏抽攣鎸囦护璇锋眰
+                    backIds.add(rides.getId());
+                    rides.setStatus(Constants.MEMBER_RIDES_STATUS.BACK_CYCLING.getKey());//宸茶繕杞�
+                    rides.setBackSiteId(bike.getSiteId());//杩樿溅绔欑偣
+                    rides.setBackDate(new Date());
+                    rides.setEditDate(rides.getBackDate());
+                    memberRidesJoinMapper.updateById(rides);//鏇存柊楠戣鐘舵�佷负宸茶繕杞�
+                }
+            }
+        }
+        //璁㈠崟缁撶畻
+        Date date = new Date();
+        goodsorder.setEditDate(date);
+        goodsorder.setEditor(null);
+        goodsorderService.dealCloseGoodsorderBiz( goodsorder,Constants.REFUND_TYPE.NORMAL.getKey(),memberRides);
+    }
+
+    private Bikes getElecBikeByCode(String bikeCode) {
+        Bikes  bike = bikesMapper.selectOne(new QueryWrapper<Bikes>().lambda()
+                .eq(Bikes::getType,Constants.ONE)
+                .eq(Bikes::getIsdeleted,Constants.ZERO)
+                .eq(Bikes::getCode,bikeCode)
+        );
+        if(bike==null){
+            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"杞﹁締淇℃伅鏌ヨ澶辫触锛岃绋嶅悗閲嶈瘯鎴栬仈绯诲洯鍖虹鐞嗗憳澶勭悊锛�" );
+        }
+        if(StringUtils.isBlank(bike.getDeviceSn()) || bike.getLongitude()==null ||bike.getLatitude() ==null){
+            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"杞﹁締浣嶇疆淇℃伅鏌ヨ澶辫触锛岃绋嶅悗閲嶈瘯鎴栬仈绯诲洯鍖虹鐞嗗憳澶勭悊锛�" );
+        }
+        if(StringUtils.isBlank(bike.getDeviceSn()) ){
+            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"杞﹁締鎺у埗鍣ㄦ晠闅滐紝璇风◢鍚庨噸璇曟垨鑱旂郴鍥尯绠$悊鍛樺鐞嗭紒" );
+        }
+        int expireTime = 10*1000;//榛樿10绉�
+        try {
+            expireTime = Integer.parseInt(systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME,Constants.HEART_TIME_EXPIRE_TIME).getCode())*1000;
+        }catch (Exception e){
+        }
+        if(bike.getHeartDate() == null || System.currentTimeMillis() - bike.getHeartDate().getTime() >expireTime){
+            //濡傛灉杞﹁締涓婃姤鏃堕棿瓒呰繃30绉掞紝鍒欒〃绀鸿溅杈嗘帶鍒跺櫒澶辨晥
+            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"杞﹁締鎺у埗鍣ㄦ晠闅滐紝鑾峰彇瀹氫綅淇℃伅澶辫触锛岃绋嶅悗閲嶈瘯鎴栬仈绯诲洯鍖虹鐞嗗憳澶勭悊锛�" );
+        }
+        return  bike;
+    }
+
+    /**
+     * 鍙戣捣鍏抽攣銆佸紑閿佹寚浠よ姹�
+     * @param clientId
+     * @param type 鎺у埗鏍囧織锛�0.杞﹂棬瑙i攣 1.杞﹂棬鍔犻攣
+     */
+    private void lockBikes(String clientId,int type) {
+        T8500 request = new T8500();
+        request.setType(type);//鍏抽攣
+        request.setClientId(StringTools.leftTrip(clientId,'0'));
+        Mono<APIResult<T0201_0500>>  result = messageManager.requestR(request, T0201_0500.class);
+        APIResult<T0201_0500> data = result.block();
+        if(!data.isSuccess()){
+            //濡傛灉寮�閿佸け璐ワ紝鍒欒繑鍥炲紓甯�
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝"+(type ==0?"杞﹁締寮�閿�":"杞﹁締鍏抽攣")+"澶辫触锛岃绋嶅悗閲嶈瘯锛屾垨鑰呰仈绯诲洯鍖虹鐞嗕汉鍛橈紒");
+        }
+    }
+
+    /**
+     * 鏌ヨ鍋滆溅绔欑偣淇℃伅
+     * @return
+     */
+    private Sites getBackSiteByPostion(double lat,double lng) {
+        Sites site = null;//鏌ヨ鍋滆溅绔欑偣淇℃伅
+        List<Sites> sitesList = sitesMapper.selectList(new QueryWrapper<Sites>().lambda()
+                .eq(Sites::getStatus,Constants.ZERO)
+                .eq(Sites::getIsdeleted,Constants.ZERO)
+                .eq(Sites::getType,Constants.ONE));
+        if(sitesList ==null ||sitesList.size() ==0){
+//            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "瀵逛笉璧凤紝璇锋寜鐓ц姹傚墠寰�鍋滆溅鐐瑰仠杞︼紒");
+            return  null;
+        }
+        for(Sites sites :sitesList){
+            TypeReference typeReference =  new TypeReference<List<LocaltionDTO>>(){};
+            List<LocaltionDTO> array = JSONObject.parseObject(sites.getElectronicFence(), typeReference.getType());
+            if(array == null || array.size() ==0){
+                continue;
+            }
+            if(PositionUtil.isInPolygon(lat,lng,array)){
+                //濡傛灉鍦ㄥ仠杞︾偣鑼冨洿鍐咃紱
+                site = sites;
+                break;
+            }
+           /* if(site ==null){
+                throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "瀵逛笉璧凤紝璇锋寜鐓ц姹傚墠寰�鍋滆溅鐐瑰仠杞︼紒");
+            }*/
+        }
+        return site;
+    }
+
+    /**
+     * 涓存椂鍋滆溅
+     * @param param
+     */
+    @Transactional(rollbackFor = {BusinessException.class})
+    public void pauseElecBike(BackElecBikeRequest param) {
+        Member member = memberMapper.selectById(param.getMemberId());
+        if (member == null) {
+            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "瀵逛笉璧凤紝鎮ㄧ殑璐﹀彿淇℃伅鏌ヨ澶辫触锛岃杩斿洖閲嶆柊杩涘叆灏忕▼搴忓啀璇曪紒");
+        }
+        Goodsorder gparam = new Goodsorder();
+        gparam.setMemberId(param.getMemberId());
+        gparam.setStatus(Constants.goodsorderStatus.pay);
+        Goodsorder goodsorder =  goodsorderService.findOne(gparam);
+        if(Objects.isNull(goodsorder)){
+            throw new BusinessException(ResponseStatus.NO_UNCLOSEED_ORDER.getCode(),"瀵逛笉璧凤紝鏈煡璇㈠埌鎮ㄥ綋鍓嶆湁楠戣涓鍗曡褰�");
+        }
+        //鏌ヨ鎵�鏈夐獞琛岃褰�
+        MemberRides memberRides = memberRidesJoinMapper.selectJoinOne(MemberRides.class,new MPJLambdaWrapper<MemberRides>()
+                .eq(MemberRides::getOrdreId,goodsorder.getId())
+                .eq(MemberRides::getStatus,Constants.MEMBER_RIDES_STATUS.RIDES_RUNNING.getKey())
+                .selectAll(MemberRides.class)
+                .last("limit 1"));
+        if(memberRides ==null || memberRides.getBikeCode() == null){
+            throw new BusinessException(ResponseStatus.NO_UNCLOSEED_ORDER.getCode(),"瀵逛笉璧凤紝鏈煡璇㈠埌鎮ㄥ綋鍓嶆湁楠戣涓褰�");
+        }
+        Bikes bike = getElecBikeByCode(memberRides.getBikeCode());
+        //鏌ヨ鍋滆溅浣嶇疆鏄惁绗﹀悎鍋滆溅浣嶇疆瑙勮寖
+        checkPausePostionBiz(bike);
+        memberRides.setPauseDate(new Date());
+        memberRides.setEditDate(memberRides.getBackDate());
+        memberRides.setStatus(Constants.MEMBER_RIDES_STATUS.RIDES_PAUSING.getKey());
+        memberRidesJoinMapper.updateById(memberRides);//鏇存柊楠戣鐘舵�佷负涓村仠涓�
+    }
+
+    private void checkPausePostionBiz(Bikes bike) {
+        int ruleType = 0;
+        try {
+            ruleType =Integer.parseInt( systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME,Constants.PAUSE_BIKE_TYPE).getCode());
+        }catch (Exception e){
+        }
+        if(ruleType == 0){
+            //濡傛灉浠呴檺浜庡洯鍖轰复鏃跺仠杞�
+            List<LocaltionDTO> array = new ArrayList<>();
+            try {
+                TypeReference typeReference =  new TypeReference<List<LocaltionDTO>>(){};
+                array = JSONObject.parseObject(systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME, Constants.PARK_LAT_LNG_LIST).getCode(), typeReference.getType());
+            }catch (Exception e){
+            }
+            if(array == null || array.size() ==0){
+                throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "瀵逛笉璧凤紝鍥尯杩愯惀鍖哄煙閰嶇疆鏈夎锛岃鑱旂郴鍥尯绠$悊鍛樺鐞嗭紒");
+            }
+            if( !PositionUtil.isInPolygon(bike.getLatitude().doubleValue(),bike.getLongitude().doubleValue(),array)){
+                //濡傛灉鍦ㄥ仠杞︾偣鑼冨洿鍐咃紱
+                throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "瀵逛笉璧凤紝璇锋寜瑕佹眰鍦ㄥ洯鍖鸿繍钀ュ尯鍩熻寖鍥村唴鍋滆溅锛�");
+            }
+        }else{
+            //妫�鏌ユ槸鍚︽弧瓒冲仠杞︾偣鍋滆溅
+            if(bike.getSiteId() ==null){
+                throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "瀵逛笉璧凤紝璇锋寜瑕佹眰鍓嶅線鍋滆溅鐐瑰仠杞︼紒");
+            }
+        }
+    }
+
+    public void updateBikesInfo(List<T0200> list) {
+        Date date = new Date();
+        for(T0200 m : list){
+            Bikes bike = new Bikes();
+            bike.setDeviceSn(m.getClientId());
+            if(m.getLatitude()!=0){
+                bike.setLatitude(new BigDecimal(m.getLatitude()).divide(new BigDecimal(1000000),8,BigDecimal.ROUND_HALF_UP));
+            }
+            if(m.getLongitude()!=0){
+                bike.setLongitude(new BigDecimal(m.getLongitude()).divide(new BigDecimal(1000000),8,BigDecimal.ROUND_HALF_UP));
+            }
+            bike.setHeartDate(DateUtil.getDateFromLocalDateTime(m.getDeviceTime()));
+            if(m.getAttributes()!=null ){
+                Battery battery= (Battery) m.getAttributes().get(AttributeKey.Battery);
+                if(battery !=null && battery.getVoltage()!=null){
+                    bike.setVoltage(new BigDecimal(battery.getVoltage()));
+                }
+            }
+            if(StringUtils.isBlank(bike.getDeviceSn() )){
+                return;
+            }
+            String tSn = StringTools.leftPad(bike.getDeviceSn(),12,'0') ;
+            Bikes bikes = bikesMapper.selectOne(new QueryWrapper<Bikes>().lambda()
+                    .eq(Bikes::getDeviceSn,tSn)
+                    .eq(Bikes::getIsdeleted,Constants.ZERO)
+                    .eq(Bikes::getType,Constants.ONE)
+                    .last("limit 1"));
+            if(bikes == null){
+                return;
+            }
+            Sites site = null;
+            if(bikes.getLatitude()!=null && bikes.getLongitude()!=null){
+                //鏍规嵁瀹氫綅淇℃伅鏌ヨ鎵�鍦ㄧ珯鐐�
+                site =  getBackSiteByPostion(bikes.getLatitude().doubleValue(),bikes.getLongitude().doubleValue());
+            }
+            bikesMapper.update(null,new UpdateWrapper<Bikes>().lambda()
+                    .set(bike.getLatitude()!=null,Bikes::getLatitude,bike.getLatitude())
+                    .set(bike.getVoltage()!=null,Bikes::getVoltage,bike.getVoltage())
+                    .set(bike.getLongitude()!=null,Bikes::getLongitude,bike.getLongitude())
+                    .set(Bikes::getSiteId,site ==null?null:site.getId())
+                    .set( Bikes::getHeartDate,date)
+                    .eq(Bikes::getId,bikes.getId()));
+        }
+    }
 }

--
Gitblit v1.9.3