forked from kidgrow-microservices-platform

zhaoxiaohao
2021-04-22 d6c989a5d9122e5cf969dd640a92b8e12caf8aa6
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package com.kidgrow.oprationcenter.service.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kidgrow.advisory.model.AdvisoryDoctorSummary;
import com.kidgrow.advisory.model.AdvisoryManager;
import com.kidgrow.advisory.model.AdvisorySummary;
import com.kidgrow.advisory.model.feign.AdvisoryService;
import com.kidgrow.common.model.PageResult;
import com.kidgrow.common.model.ResultBody;
import com.kidgrow.common.service.impl.SuperServiceImpl;
import com.kidgrow.common.utils.StringUtils;
import com.kidgrow.oprationcenter.mapper.AdvisoryDoctorInfoMapper;
import com.kidgrow.oprationcenter.model.AdvisoryDoctorInfo;
import com.kidgrow.oprationcenter.model.AppointmentTime;
import com.kidgrow.oprationcenter.model.HospitalScreening;
import com.kidgrow.oprationcenter.service.IAdvisoryDoctorInfoService;
import com.kidgrow.oprationcenter.service.IAppointmentTimeService;
import com.kidgrow.redis.util.RedisUtils;
import com.kidgrow.user.feign.DoctorAnswerUserRelationService;
import com.kidgrow.user.feign.UserInfoService;
import com.kidgrow.user.model.UserInfo;
import com.kidgrow.usercenter.feign.SysDoctorService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
 
import java.text.DecimalFormat;
import java.util.*;
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 *
 * @Description: <br>
 * @Project: <br>
 * @CreateDate: Created in 2021/3/1 14:34 <br>
 * @Author: <a href="78125310@kidgrow.com">dougang</a>
 */
@Slf4j
@Service
 
public class AdvisoryDoctorInfoServiceImpl extends SuperServiceImpl<AdvisoryDoctorInfoMapper, AdvisoryDoctorInfo> implements IAdvisoryDoctorInfoService {
 
    @Autowired
    private SysDoctorService doctorService;
 
    @Autowired
    private IAppointmentTimeService appointmentTimeService;
 
    @Autowired
    private AdvisoryService advisoryService;
 
    @Autowired
    private UserInfoService userInfoService;
 
    @Autowired
    private RedisUtils redisUtils;
 
    @Autowired
    private DoctorAnswerUserRelationService doctorAnswerUserRelationService;
 
    /**
     * 医生列表
     *
     * @param params
     * @return
     */
    @Override
    public PageResult<AdvisoryDoctorInfo> findList(Map<String, Object> params) {
        Page<AdvisoryDoctorInfo> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
        List<AdvisoryDoctorInfo> list = baseMapper.findList(page, params);
        return PageResult.<AdvisoryDoctorInfo>builder().data(list).code(0).count(page.getTotal()).build();
    }
 
    /**
     * 通过ID删除
     *
     * @param id
     * @return
     */
    @Override
    public int removeById(Long id) {
        ResultBody resultBody = doctorAnswerUserRelationService.deletByDoctorId(String.valueOf(id));
        log.info("删除医生关系返回值=>" + resultBody);
 
        appointmentTimeService.delByAdvisoryId(id);
        baseMapper.removeById(id);
        return 1;
    }
 
    /**
     * 根据条件查询
     *
     * @param params
     * @return
     */
    @Override
    public List<AdvisoryDoctorInfo> findByObject(Map<String, Object> params) {
        return baseMapper.findByObject(params);
    }
 
    /**
     * 更新医生信息
     *
     * @param map
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultBody updateDoctorInfo(Map<String, Object> map) {
        //医生ID
        String id = MapUtils.getString(map, "doctorId");
        //价格
        double price = MapUtils.getDouble(map, "price") * 100;
        //最大人数
        int maxPeople = MapUtils.getIntValue(map, "maxPeople");
        //预约时间
        List<AppointmentTime> appointmments = JSONArray.parseArray(JSONArray.toJSONString(map.get("appointments")), AppointmentTime.class);
 
        AdvisoryDoctorInfo advisoryDoctorInfo = baseMapper.selectById(Long.parseLong(id));
        if (advisoryDoctorInfo == null) {
            return ResultBody.failed().msg("查询医生信息失败");
        }
 
        advisoryDoctorInfo.setPrice(price);
        advisoryDoctorInfo.setMaxPeople(maxPeople);
 
        int b = baseMapper.updateById(advisoryDoctorInfo);
        if (b == 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultBody.failed().msg("操作失败");
        }
 
        if (appointmments != null && appointmments.size() > 0) {
            appointmentTimeService.delByAdvisoryId(Long.parseLong(id));
            for (AppointmentTime time : appointmments) {
                time.setAdvisoryId(Long.parseLong(id));
                appointmentTimeService.saveOrUpdate(time);
            }
        }
 
        return ResultBody.ok().msg("操作成功");
    }
 
    /**
     * 查询咨询医生详情
     *
     * @param map
     * @return
     */
    @Override
    public ResultBody getDoctorById(Map<String, Object> map) {
        //医生ID
        String id = MapUtils.getString(map, "doctorId");
        //是否需要查询预约时间
        int apppointmentFlag = MapUtils.getIntValue(map, "apppointmentFlag");
 
        AdvisoryDoctorInfo byId = baseMapper.selectById(Long.parseLong(id));
        if (byId == null) {
            return ResultBody.failed().msg("未找到医生信息");
        }
 
        ResultBody byId1 = doctorService.findById(byId.getDoctorId());
        if (byId1.getCode() != 0 || byId1.getData() == null) {
            return ResultBody.failed().msg("查询医生信息失败");
        }
 
        JSONObject doctor = JSONObject.parseObject(JSONObject.toJSONString(byId1.getData()));
 
        JSONObject json = new JSONObject();
        json.put("doctorName", doctor.getString("doctorName"));
        json.put("departmentName", doctor.getString("departmentName"));
        json.put("hospitalName", doctor.getString("hospitalName"));
        json.put("head", doctor.getString("doctorLogo"));
        json.put("phone", doctor.getString("doctorTel"));
        json.put("otherLink", doctor.getString("doctorOtherLink"));
        json.put("about", doctor.getString("doctorAbout"));
        json.put("price", byId.getPrice() / 100);
        json.put("specialty", byId.getSpecialty());
        json.put("service", byId.getService());
        json.put("maxPeople", byId.getMaxPeople());
        json.put("rank", doctor.getString("doctorRank"));
 
        if (apppointmentFlag == 1) {
            List<AppointmentTime> byAdvisoryId = appointmentTimeService.findByAdvisoryId(Long.parseLong(id));
            if (byAdvisoryId.size() > 0) {
                JSONArray array = new JSONArray();
                JSONObject obj = null;
                for (AppointmentTime app : byAdvisoryId) {
                    boolean flag = true;
                    for (int i = 0; i < array.size(); i++) {
                        JSONObject jsonObject = array.getJSONObject(i);
                        if (jsonObject.getString("name").equals(app.getName())) {
                            if (app.getAfternoon() == 1) {
                                jsonObject.put("afternoon", 1);
                            } else if (app.getEvening() == 1) {
                                jsonObject.put("evening", 1);
                            } else if (app.getMorning() == 1) {
                                jsonObject.put("morning", 1);
                            }
                            flag = false;
                            array.add(jsonObject);
                            break;
                        }
                    }
 
                    if (flag) {
                        obj = new JSONObject();
                        obj.put("name", app.getName());
                        obj.put("afternoon", app.getAfternoon());
                        obj.put("evening", app.getEvening());
                        obj.put("morning", app.getMorning());
                        array.add(obj);
                    }
                }
                json.put("appointments", array);
            }
        }
 
        return ResultBody.ok().data(json);
    }
 
    /**
     * 获取筛查咨询医生列表
     *
     * @param hospitalId
     * @return
     */
    @Override
    public ResultBody getSreeningAdvisoryDoctorList(Long hospitalId) {
        Map<String, Object> map = new HashMap<>(16);
        map.put("hospitalId", hospitalId);
 
        JSONArray array = new JSONArray();
        JSONObject json = null;
        List<AdvisoryDoctorInfo> byObject = baseMapper.findByObject(map);
        if (byObject.size() > 0) {
            for (AdvisoryDoctorInfo doc : byObject) {
                json = new JSONObject();
                json.put("doctorName", doc.getDoctorName());
                json.put("departmentName", doc.getDeptName());
                json.put("hospitalName", doc.getHospitalName());
                json.put("head", doc.getHead());
                json.put("about", doc.getIntroduction());
                json.put("price", doc.getPrice() / 100);
                json.put("specialty", doc.getSpecialty());
                json.put("service", doc.getService());
                json.put("id", String.valueOf(doc.getId()));
 
                ResultBody byId1 = doctorService.findById(doc.getDoctorId());
                if (byId1.getCode() == 0 || byId1.getData() != null) {
                    JSONObject doctor = JSONObject.parseObject(JSONObject.toJSONString(byId1.getData()));
                    json.put("rank", doctor.getString("doctorRank"));
                }
 
                array.add(json);
            }
        }
        return ResultBody.ok().data(array).msg("操作成功");
    }
 
    /**
     * 获取咨询医生列表
     *
     * @param map
     * @return
     */
    @Override
    public ResultBody getAdvisoryDoctorList(Map<String, Object> map) {
        double lat = MapUtils.getDouble(map, "lat");
        double lon = MapUtils.getDouble(map, "lon");
        String excludeHospitalId = MapUtils.getString(map, "excludeHospitalId");
 
        List<Map<String, Object>> array = new ArrayList<>();
        Map<String, Object> json = null;
 
        //根据筛查医院ID换取医院ID
        if (StringUtils.isNotBlank(excludeHospitalId)) {
            HospitalScreening hget = (HospitalScreening) redisUtils.hget("CUSOTMER_HOSPITAL", excludeHospitalId);
            if (hget != null) {
                map.put("excludeHospitalId", hget.getHospitalId());
            }
        }
 
        List<AdvisoryDoctorInfo> byObject = baseMapper.findByObject(map);
        if (byObject.size() > 0) {
            for (AdvisoryDoctorInfo adi : byObject) {
                json = new HashMap<>(16);
                json.put("doctorName", adi.getDoctorName());
                json.put("departmentName", adi.getDeptName());
                json.put("hospitalName", adi.getHospitalName());
                json.put("head", adi.getHead());
                json.put("about", adi.getIntroduction());
                json.put("price", adi.getPrice() / 100);
                json.put("specialty", adi.getSpecialty());
                json.put("service", adi.getService());
                json.put("id", String.valueOf(adi.getId()));
                double lat1 = StringUtils.isNotBlank(adi.getLatitude()) ? Double.parseDouble(adi.getLatitude()) : 0d;
                double lon1 = StringUtils.isNotBlank(adi.getLongitude()) ? Double.parseDouble(adi.getLongitude()) : 0d;
                String distance = getDistance(lat1, lon1, lat, lon);
                json.put("distance", distance);
 
                ResultBody byId1 = doctorService.findById(adi.getDoctorId());
                if (byId1.getCode() == 0 || byId1.getData() != null) {
                    JSONObject doctor = JSONObject.parseObject(JSONObject.toJSONString(byId1.getData()));
                    json.put("rank", doctor.getString("doctorRank"));
                }
 
                array.add(json);
            }
 
            //按照距离排序(由远到近)
            Collections.sort(array, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    String distance1 = o1.get("distance").toString();
                    String distance2 = o2.get("distance").toString();
                    return distance2.compareTo(distance1);
                }
            });
 
            //倒叙list排序
            Collections.reverse(byObject);
        }
 
        return ResultBody.ok().data(array).msg("操作成功");
    }
 
    @Override
    public PageResult<AdvisoryManager> findAll(Map<String, Object> params) {
        return advisoryService.findAll(params);
    }
 
    /**
     * 后台更新咨询状态
     *
     * @param id
     * @return
     */
    @Override
    public ResultBody updateStatusByService(String id) {
        return advisoryService.updateStatusByService(id);
    }
 
    /**
     * 查询注册用户信息
     *
     * @param phone
     * @return
     */
    @Override
    public UserInfo getUserByPhone(String phone) {
        UserInfo user = new UserInfo();
        user.setPhoneNum(phone);
        ResultBody byObject = userInfoService.findByObject(user);
 
        if (byObject.getCode() == 0 || byObject.getData() != null) {
            UserInfo userInfo = JSONObject.parseObject(JSONObject.toJSONString(byObject.getData()), UserInfo.class);
            if (userInfo != null) {
                return userInfo;
            }
        }
        return null;
    }
 
    /**
     * 微信退款
     *
     * @param map
     * @return
     */
    @Override
    public ResultBody refund(Map<String, Object> map) {
        return advisoryService.refund(map);
    }
 
    /**
     * 按照月/年查询咨询统计数据
     *
     * @param map type:1月,2年
     *            riqi: 时间
     * @return
     */
    @Override
    public PageResult<AdvisorySummary> summaryBate(Map<String, Object> map) {
        PageResult<AdvisorySummary> resultBody = null;
        int type = MapUtils.getIntValue(map, "type");
        if (type == 1) {
            resultBody = advisoryService.summaryByMonth(map);
        }
        if (type == 2) {
            resultBody = advisoryService.summaryByYear(map);
        }
        return resultBody;
    }
 
    /**
     * 咨询医生统计数据查询
     *
     * @param map startTime/EndTime
     * @return
     */
    @Override
    public PageResult<AdvisoryDoctorSummary> summaryDoctorByBate(Map<String, Object> map) {
        return advisoryService.summaryByDoctor(map);
    }
 
    /**
     * @param lat1 经度1
     * @param lng1 纬度1
     * @param lat2 经度2
     * @param lng2 纬度2
     * @return 距离
     */
    private String getDistance(double lat1, double lng1, double lat2, double lng2) {
 
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
 
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
 
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * 6378.137;
        s = Math.round(s * 10000d) / 10000d;
        //s = s * 1000;如果需要返回单位为米
 
        DecimalFormat df = new DecimalFormat("0.00");//格式化,区小数后两位
        String distance = df.format(s);
 
        return distance;
    }
 
    private double rad(double d) {
        return d * Math.PI / 180.0;
    }
 
}