jiangping
2025-02-18 9d67b4462bf494a0b25c1b40be8198f71666131e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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.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.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.t808.T0201_0500;
import org.yzh.protocol.t808.T8500;
import reactor.core.publisher.Mono;
 
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 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) {
        if (param.getLatitude() == null || param.getLongitude() == null || StringUtils.isBlank(param.getMemberId())) {
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,定位信息有误,请确认定位权限已开启!");
        }
        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());
        if(memberRides!=null && memberRides.size()>0){
            for (MemberRides  rides : memberRides){
                if(Constants.equalsInteger(rides.getStatus(),Constants.MEMBER_RIDES_STATUS.RIDES_RUNNING.getKey())){
                    if(StringUtils.isNotBlank(rides.getDeviceSn()) &&Constants.equalsInteger(rides.getType(),Constants.ONE) ){
                        Sites site = getBackSiteByPostion(param);//查询停车站点信息
                        //如果是电车并且是骑行中 进行关锁处理
                        lockBikes(rides.getDeviceSn(),Constants.ZERO);//发起关锁指令请求
                    }
                }
            }
        }
        //订单结算
        Date date = new Date();
        goodsorder.setEditDate(date);
        goodsorder.setEditor(null);
        goodsorderService.dealCloseGoodsorderBiz( goodsorder,Constants.REFUND_TYPE.NORMAL.getKey(),memberRides);
 
    }
 
    /**
     * 发起关锁、开锁指令请求
     * @param clientId
     */
    private void lockBikes(String clientId,int type) {
        T8500 request = new T8500();
        request.setType(type);//关锁
        request.setClientId(StringTools.leftPad(clientId,12,'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(), "对不起,还车关锁失败,请稍后重试,或者联系园区管理人员!");
        }
    }
 
    /**
     * 查询停车站点信息
     * @param param
     * @return
     */
    private Sites getBackSiteByPostion(BackElecBikeRequest param) {
        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(), "对不起,请按照要求前往停车点停车!");
        }
        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(param.getLongitude(),param.getLatitude(),array)){
                //如果在停车点范围内;
                site = sites;
                break;
            }
            if(site ==null){
                throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "对不起,请按照要求前往停车点停车!");
            }
        }
        return site;
    }
}