puzhibing
2023-08-16 c18ec3846c8483975de2224c1ecac9470e9b2804
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package com.supersavedriving.user.modular.system.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.supersavedriving.user.core.util.ToolUtil;
import com.supersavedriving.user.modular.system.dao.DriverMapper;
import com.supersavedriving.user.modular.system.model.*;
import com.supersavedriving.user.modular.system.service.*;
import com.supersavedriving.user.modular.system.util.GeodesyUtil;
import com.supersavedriving.user.modular.system.util.RedisUtil;
import com.supersavedriving.user.modular.system.util.ResultUtil;
import com.supersavedriving.user.modular.system.util.UUIDUtil;
import com.supersavedriving.user.modular.system.util.huawei.OBSUtil;
import com.supersavedriving.user.modular.system.util.mongodb.model.Location;
import com.supersavedriving.user.modular.system.util.weChat.WeChatUtil;
import com.supersavedriving.user.modular.system.warpper.DriverRegisterWarpper;
import com.supersavedriving.user.modular.system.warpper.NearbyDriverWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
 
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
* 司机
* @author pzb
* @Date 2023/2/8 18:52
*/
@Service
public class DriverServiceImpl extends ServiceImpl<DriverMapper, Driver> implements IDriverService {
 
    @Autowired
    private MongoTemplate mongoTemplate;
 
    @Autowired
    private IDriverWorkService driverWorkService;
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private IAppUserService appUserService;
 
    @Autowired
    private WeChatUtil weChatUtil;
 
    @Autowired
    private IBranchOfficeService branchOfficeService;
 
    @Autowired
    private ISystemConfigService systemConfigService;
 
    @Autowired
    private IAccountChangeDetailService accountChangeDetailService;
 
 
 
 
 
 
    /**
     * 获取5公里范围内的司机
     * @param lon
     * @param lat
     * @return
     * @throws Exception
     */
    @Override
    public List<NearbyDriverWarpper> queryDriverPosition(String lon, String lat, Double scope) throws Exception {
        List<NearbyDriverWarpper> list = new ArrayList<>();
        //找到中心点
        GeoJsonPoint geoJsonPoint = new GeoJsonPoint(Double.valueOf(lon), Double.valueOf(lat));
        //构造半径
        Distance distanceR = new Distance(scope, Metrics.KILOMETERS);
        //画圆
        Circle circle = new Circle(geoJsonPoint, distanceR);
        // 构造query对象
        Query query = Query.query(Criteria.where("location").withinSphere(circle));
        List<Location> locations = mongoTemplate.find(query, Location.class);
        List<Integer> collect = locations.stream().map(Location::getDriverId).collect(Collectors.toList());
        if(collect.size() == 0){
            return list;
        }
        List<Driver> drivers = this.selectList(new EntityWrapper<Driver>().in("id", collect).eq("approvalStatus", 2).eq("serverStatus", 1).eq("status", 1));
        collect = drivers.stream().map(Driver::getId).collect(Collectors.toList());
        if(collect.size() == 0){
            return list;
        }
        List<DriverWork> driverWorks = driverWorkService.selectList(new EntityWrapper<DriverWork>().in("driverId", collect).eq("status", 1));
        for (DriverWork driverWork : driverWorks) {
            String value = redisUtil.getValue("DRIVER" + driverWork.getDriverId());
            if(ToolUtil.isEmpty(value)){
                continue;
            }
            NearbyDriverWarpper nearbyDriverWarpper = new NearbyDriverWarpper();
            nearbyDriverWarpper.setLonLat(value);
            Map<String, Double> distance = GeodesyUtil.getDistance(value, lon + "," + lat);
            nearbyDriverWarpper.setDistance(distance.get("WGS84") / 1000);
            nearbyDriverWarpper.setDriverId(driverWork.getDriverId());
            list.add(nearbyDriverWarpper);
        }
        return list;
    }
 
 
    /**
     * 注册司机
     * @param uid
     * @param driverRegisterWarpper
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil driverRegister(Integer uid, DriverRegisterWarpper driverRegisterWarpper) throws Exception {
        Driver driver = this.selectOne(new EntityWrapper<Driver>().eq("phone", driverRegisterWarpper.getPhone()).ne("status", 3));
        if(null != driver && driver.getStatus() == 2){
            return ResultUtil.error("该手机账号已被冻结,请联系管理员。");
        }
        if(null != driver && driver.getApprovalStatus() == 1){
            return ResultUtil.error("该手机账号正在审核中。");
        }
        if(null != driver && driver.getApprovalStatus() == 2){
            return ResultUtil.error("该手机账号已审核通过,请直接登录。");
        }
 
        //账号审核拒绝后的处理
        if(null != driver && driver.getApprovalStatus() == 3){
            try {
                driver = setDriverParamete(driver, driverRegisterWarpper);
            }catch (Exception e){
                return ResultUtil.error(e.getMessage());
            }
            this.updateAllColumnById(driver);
        }
 
        AppUser appUser = appUserService.selectById(uid);
        //新账号
        if(null == driver){
            driver = new Driver();
            driver.setCode(System.currentTimeMillis() + UUIDUtil.getNumberRandom(5));
            try {
                driver = setDriverParamete(driver, driverRegisterWarpper);
            }catch (Exception e){
                return ResultUtil.error(e.getMessage());
            }
 
            driver.setCreateTime(new Date());
            driver.setInviterId(appUser.getInviterId());
            driver.setInviterType(appUser.getInviterType());
            this.insert(driver);
            String s = wechatMiniProgramORCode(driver.getId());
            driver.setReferralCode(s);
            this.updateById(driver);
 
 
            //司机邀请注册奖励
            if(null != driver.getInviterId()){
                Driver driver1 = this.selectById(driver.getInviterId());
                SystemConfig systemConfig = systemConfigService.selectOne(new EntityWrapper<SystemConfig>().eq("type", 4));
                if(null != systemConfig){
                    Integer num7 = JSON.parseObject(systemConfig.getContent()).getInteger("num7");
                    if(num7 > 0){
                        AccountChangeDetail accountChangeDetail = new AccountChangeDetail();
                        accountChangeDetail.setCode(System.currentTimeMillis() + UUIDUtil.getNumberRandom(5));
                        accountChangeDetail.setUserType(2);
                        accountChangeDetail.setUserId(driver1.getId());
                        accountChangeDetail.setType(2);
                        accountChangeDetail.setChangeType(5);
                        accountChangeDetail.setOldData(driver1.getIntegral().doubleValue());
                        driver1.setIntegral(driver1.getIntegral() + num7);
                        accountChangeDetail.setNewData(driver1.getIntegral().doubleValue());
                        accountChangeDetail.setExplain("邀请司机注册奖励");
                        accountChangeDetailService.saveData(accountChangeDetail);
                        this.updateById(driver1);
                    }
                }
            }
        }
        return ResultUtil.success();
    }
 
 
 
    /**
     * 组装个人信息
     * @param driver
     * @param driverRegisterWarpper
     * @return
     */
    public Driver setDriverParamete(Driver driver, DriverRegisterWarpper driverRegisterWarpper) throws Exception{
        driver.setAvatar(driverRegisterWarpper.getAvatar());
        driver.setName(driverRegisterWarpper.getName());
        driver.setPhone(driverRegisterWarpper.getPhone());
        driver.setEmergencyContact(driverRegisterWarpper.getEmergencyContact());
        driver.setEmergencyPhone(driverRegisterWarpper.getEmergencyPhone());
        driver.setIdcardBack(driverRegisterWarpper.getIdcardBack());
        driver.setIdcardFront(driverRegisterWarpper.getIdcardFront());
        driver.setDriverLicense(driverRegisterWarpper.getDriverLicense());
        if(ToolUtil.isNotEmpty(driverRegisterWarpper.getFirstCertificateTime())){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            driver.setFirstCertificateTime(sdf.parse(driverRegisterWarpper.getFirstCertificateTime()));
        }
        //注册地
        String code = driverRegisterWarpper.getCode();
        BranchOffice branchOffice = branchOfficeService.selectOne(new EntityWrapper<BranchOffice>().eq("districtCode", code).eq("status", 1));
        if(null == branchOffice){
            branchOffice = branchOfficeService.selectOne(new EntityWrapper<BranchOffice>().eq("cityCode", code).eq("status", 1));
            if(null == branchOffice){
                throw new Exception("该区域无服务商");
            }
        }
        driver.setBranchOfficeId(branchOffice.getId());
        driver.setAgentId(branchOffice.getAgentId());
        driver.setProvinceCode(branchOffice.getProvinceCode());
        driver.setProvinceName(branchOffice.getProvinceName());
        driver.setCityName(branchOffice.getCityName());
        driver.setCityCode(branchOffice.getCityCode());
        driver.setAreaCode(branchOffice.getDistrictCode());
        driver.setAreaName(branchOffice.getDistrictName());
        if(null != driverRegisterWarpper.getInviterId()){
            driver.setInviterType(driverRegisterWarpper.getInviterType());
            driver.setInviterId(driverRegisterWarpper.getInviterId());
        }
        driver.setApprovalStatus(1);
        driver.setApprovalNotes("");
        driver.setApprovalTime(null);
        driver.setApprovalUserId(null);
        driver.setStatus(1);
        driver.setSource(driverRegisterWarpper.getSource());
        return driver;
    }
 
 
 
    //生成小程序二维码
    public String wechatMiniProgramORCode(Integer driverId) throws Exception{
        InputStream release = weChatUtil.getwxacodeunlimit("pages/index/index", "driverId=" + driverId, "release");
        String s = OBSUtil.putObjectToBucket(release, "driver_" + driverId);
        return s;
    }
}