zhaowenxuan
2025-02-15 d9006551fff47c172ea6a0672af7dd21df34f28f
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
445
446
447
448
449
450
451
452
453
454
package com.ltkj.web.controller.app;
 
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.base.Joiner;
import com.ltkj.common.core.controller.BaseController;
import com.ltkj.common.core.domain.AjaxResult;
import com.ltkj.common.task.TaskService;
import com.ltkj.common.utils.StringUtils;
import com.ltkj.framework.config.MatchUtils;
import com.ltkj.framework.config.UserHoder;
import com.ltkj.hosp.domain.*;
import com.ltkj.hosp.service.*;
import com.ltkj.mall.domain.MallTimeConfig;
import com.ltkj.mall.service.IMallTimeConfigService;
import com.ltkj.system.service.ISysConfigService;
import com.ltkj.web.config.task.TjReservationTask;
import com.ltkj.web.wxUtils.WxUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * @Author: 西安路泰科技有限公司
 * @Author: 作者: zjh
 * @Date: 2023/1/30 10:28
 */
@RestController
@RequestMapping("/cus/reservation")
@Api(tags = "小程序-体检预约管理接口")
public class ReservationController extends BaseController {
    @Resource
    private ITjReservationService reservationService;
    @Resource
    private ITjPackageService packageService;
    @Resource
    private ITjProjectService projectService;
    @Resource
    private ITjPackageProjectService ppservice;
    @Resource
    private ITbTransitionService transitionService;
    @Resource
    private TjAsyncService asyncService;
    @Resource
    private ITjCustomerService customerService;
    @Autowired
    private IMallTimeConfigService mallTimeConfigService;
    @Resource
    private ISysConfigService configService;
    //    @Resource
//    private OrderDelayService delayService;
    @Autowired
    private TaskService taskService;
 
    @GetMapping("/isStartYy")
    @ApiOperation(value = "查询预约功能是否开启")
    public AjaxResult isStartYy() {
        String isStartYy = configService.selectConfigByKey("is_start_yy");
        return AjaxResult.success(isStartYy);
    }
 
 
    @PostMapping
    @ApiOperation(value = "个人预约")
    @Transactional
    public AjaxResult cusReservation(@RequestBody @ApiParam(value = "体检预约对象") TjReservation tjReservation) {
        Wxuser wxuser = UserHoder.getWxuser();
        if (null == tjReservation) return AjaxResult.error();
        if (null == tjReservation.getName()) return AjaxResult.error("姓名不能为空");
        if (null == tjReservation.getReservationTime()) return AjaxResult.error("请选择预约时间");
        //判断身份证号格式是否正确
        if (!(MatchUtils.isMobileNO(tjReservation.getPhoe())))
            return AjaxResult.error("手机号错误");
 
        if (tjReservation.getIdCard().length() == 18) {
            if (!MatchUtils.isIdCard(tjReservation.getIdCard())) {
                return AjaxResult.error("身份证号码错误");
            }
        }
        if (tjReservation.getIdCard().length() == 9) {
            if (!MatchUtils.cardValidates(tjReservation.getIdCard())) {
                return AjaxResult.error("身份证号码错误");
            }
        }
        LambdaQueryWrapper<TjReservation> wq = new LambdaQueryWrapper<>();
        wq.eq(TjReservation::getIdCard, tjReservation.getIdCard());
        wq.eq(TjReservation::getIsExpire, 2);
        TjReservation reservation = reservationService.getOne(wq);
        if (null != reservation) return AjaxResult.success("您已成功预约,无需再约");
        try {
            tjReservation.setBirthday(DateUtil.parse(MatchUtils.getBirthDayByIdCard(tjReservation.getIdCard()), "yyyy-MM-dd"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        tjReservation.setSex(MatchUtils.getSexByIdCard(tjReservation.getIdCard()));
        tjReservation.setAge(String.valueOf(MatchUtils.getAgeByIdCard(tjReservation.getIdCard())));
        tjReservation.setIdType("1");
        tjReservation.setPayType(0L);
        tjReservation.setIsExpire(2);
        tjReservation.setTjType(String.valueOf(2));
        tjReservation.setCreateTime(new Date());
        tjReservation.setUpdateTime(new Date());
        tjReservation.setWechat(String.valueOf(wxuser.getOpenid()));
        tjReservation.setConnect(String.valueOf(wxuser.getId()));
        if (null != tjReservation.getProIdList() && tjReservation.getProIdList().size() > 0) {
            String join = Joiner.on(",").join(tjReservation.getProIdList());
            tjReservation.setProIds(join);
        }
        if (reservationService.save(tjReservation)) {
 
            taskService.addTask(new TjReservationTask(tjReservation.getId(),
                    DateUtil.between(new Date(), DateUtil.endOfDay(tjReservation.getReservationTime()), DateUnit.MS)));
 
            TjCustomer one = customerService.getOne(new LambdaQueryWrapper<TjCustomer>().eq(TjCustomer::getCusIdcard, tjReservation.getIdCard()));
            if (null != one) {
                one.setWechat(String.valueOf(wxuser.getId()));
                one.setConnect(wxuser.getOpenid());
                customerService.updateById(one);
            } else {
                TjCustomer customer = new TjCustomer();
                customer.setCusName(tjReservation.getName());
                customer.setCusSex(Long.valueOf(tjReservation.getSex()));
                customer.setCusBrithday(tjReservation.getBirthday());
                customer.setCusAddr(tjReservation.getCusaddr());
                customer.setAddr(tjReservation.getAddress());
                customer.setCusPhone(tjReservation.getPhoe());
                customer.setCusPassword(tjReservation.getIdCard().substring(tjReservation.getIdCard().length() - 6));
                customer.setCusEmail(tjReservation.getEmail());
                customer.setCusNational(String.valueOf(tjReservation.getNation()));
                customer.setCusMarryStatus(String.valueOf(tjReservation.getMarriage()));
                customer.setCusIdcard(tjReservation.getIdCard());
                customer.setWechat(String.valueOf(wxuser.getId()));
                customer.setConnect(wxuser.getOpenid());
                customer.setCreateTime(new Date());
                customerService.save(customer);
            }
            if ((null == tjReservation.getProIdList() || tjReservation.getProIdList().size() == 0) && (null == tjReservation.getPacId())) {
                transitionService.deletedTbTransitionByCusId(tjReservation.getIdCard());
            }
            if (null != tjReservation.getPacId()) {
                asyncService.addRedisTransitionPac(tjReservation.getIdCard(), Long.valueOf(tjReservation.getPacId()), ppservice.getTjPackageProjectListByPacId(tjReservation.getPacId()));
            }
 
            if (null != tjReservation.getProIdList() && tjReservation.getProIdList().size() > 0) {
                asyncService.addRedisTransitionPro(tjReservation.getIdCard(), tjReservation.getProIdList());
            }
 
            String isPays = configService.selectConfigByKey("isPay");
            if (null != isPays && isPays.equals("true")) {
                //对应预约时间数量减1
                final TjReservation byId = reservationService.getById(tjReservation.getId());
                final Date reservationTime = byId.getReservationTime();
                LambdaQueryWrapper<MallTimeConfig> wq111 = new LambdaQueryWrapper<>();
                wq111.eq(MallTimeConfig::getTime, reservationTime);
                final MallTimeConfig config = mallTimeConfigService.getOne(wq111);
                config.setNowNum(config.getNowNum() - 1);
                mallTimeConfigService.updateById(config);
            }
 
            //发订单信息给wx订阅服务消息————————————————start————————————————————————————————————————
            Map<String, Object> res = new HashMap<>();
            res.put("touser", wxuser.getOpenid());
            res.put("template_id", "M4K_BNVG7g4hOAcrIDO18ux8KOTneHeNide5GdPW3kc");
            res.put("page", "pages/mine/index");
            res.put("miniprogram_state", "trial");
            //订阅消息参数值
            JSONObject data = new JSONObject();
 
            JSONObject name1 = new JSONObject();
            name1.put("value", tjReservation.getName());
            data.put("thing1", name1);
 
            JSONObject amount3 = new JSONObject();
            amount3.put("value", "体检中心");
            data.put("thing2", amount3);
 
            JSONObject time4 = new JSONObject();
            String dateFormat = DateUtil.format(tjReservation.getReservationTime(), "yyyy-MM-dd");
            time4.put("value", dateFormat);
            data.put("time4", time4);
 
            final TjPackage byId = packageService.getById(tjReservation.getPacId());
            if (byId != null) {
                JSONObject thing5 = new JSONObject();
                thing5.put("value", byId.getPacName());
                data.put("thing5", thing5);
            } else {
                JSONObject thing5 = new JSONObject();
                thing5.put("value", "未知");
                data.put("thing5", thing5);
            }
 
 
            JSONObject thing6 = new JSONObject();
            thing6.put("value", "体检前一天请您进清淡饮食,勿饮酒!");
            data.put("thing6", thing6);
            WxUtil.sendCommonSubscribeMessage(res, data);
            //发订单信息给wx订阅服务消息————————————————end————————————————————————————————————————
 
            return AjaxResult.success("预约成功!!!", tjReservation.getId());
        }
        return AjaxResult.error("预约失败");
    }
 
    @DeleteMapping
    @ApiOperation(value = "撤销个人预约")
    @Transactional
    public AjaxResult cusDeletedReservation(@RequestBody TjReservation reservation) {
        Wxuser wxuser = UserHoder.getWxuser();
//        LambdaQueryWrapper<TjReservation> wq = new LambdaQueryWrapper<>();
//        wq.eq(TjReservation::getIdCard, reservation.getIdCard());
//        wq.eq(TjReservation::getIsExpire, 2);
//        TjReservation one = reservationService.getOne(wq);
        if (reservation.getId() == null) {
            return AjaxResult.success("出错了,请联系工作人员!");
        }
        taskService.removeTask(new TjReservationTask(reservation.getId()));
        TjReservation one = reservationService.getById(reservation.getId());
        if (null != one) {
            String isPays = configService.selectConfigByKey("isPay");
            if (null != isPays && isPays.equals("true")) {
                //对应预约时间数量+1
                final TjReservation byId = reservationService.getById(one.getId());
                final Date reservationTime = byId.getReservationTime();
                LambdaQueryWrapper<MallTimeConfig> wq111 = new LambdaQueryWrapper<>();
                wq111.eq(MallTimeConfig::getTime, reservationTime);
                final MallTimeConfig config = mallTimeConfigService.getOne(wq111);
                config.setNowNum(config.getNowNum() + 1);
                mallTimeConfigService.updateById(config);
            }
 
            transitionService.deletedTbTransitionByCusId(reservation.getIdCard());
            reservationService.removeById(one);
 
 
            //发订单信息给wx订阅服务消息————————————————start————————————————————————————————————————
            Map<String, Object> res = new HashMap<>();
            res.put("touser", wxuser.getOpenid());
            res.put("template_id", "X467Xtd8HnFDNk-IoT0ChcdoBnClLZJQQwP7DMztYQ8");
            res.put("page", "pages/mine/index");
            res.put("miniprogram_state", "trial");
            //订阅消息参数值
            JSONObject data = new JSONObject();
 
            JSONObject amount3 = new JSONObject();
            amount3.put("value", "体检中心");
            data.put("thing3", amount3);
 
            JSONObject name1 = new JSONObject();
            name1.put("value", reservation.getName());
            data.put("name1", name1);
 
            final TjPackage byId = packageService.getById(reservation.getPacId());
            if (byId != null) {
                JSONObject thing5 = new JSONObject();
                thing5.put("value", byId.getPacName());
                data.put("thing4", thing5);
            } else {
                JSONObject thing5 = new JSONObject();
                thing5.put("value", "未知");
                data.put("thing4", thing5);
            }
 
            JSONObject time4 = new JSONObject();
            String dateFormat = DateUtil.format(reservation.getReservationTime(), "yyyy-MM-dd");
            time4.put("value", dateFormat);
            data.put("date2", time4);
 
            JSONObject thing6 = new JSONObject();
            thing6.put("value", "您已成功取消本次预约,欢迎再次使用!");
            data.put("thing5", thing6);
            WxUtil.sendCommonSubscribeMessage(res, data);
            //发订单信息给wx订阅服务消息————————————————end————————————————————————————————————————
            return AjaxResult.success("取消成功");
        }
        return AjaxResult.success("暂无预约信息");
    }
 
    @GetMapping
    @ApiOperation(value = "查看个人预约")
    public AjaxResult cusGetReservation() {
        Wxuser wxuser = UserHoder.getWxuser();
        LambdaQueryWrapper<TjReservation> wq = new LambdaQueryWrapper<>();
        wq.eq(TjReservation::getConnect, wxuser.getId());
//        wq.eq(TjReservation::getIsExpire, 2);
        wq.eq(TjReservation::getPhoe, wxuser.getPhone());
        List<TjReservation> reservationList = reservationService.list(wq);
        if (null != reservationList && reservationList.size() > 0) {
            for (TjReservation one : reservationList) {
                TjPackage tjPackage = packageService.getById(one.getPacId());
                if (null != tjPackage) {
                    LambdaQueryWrapper<TjPackageProject> wq1 = new LambdaQueryWrapper<>();
                    wq1.eq(TjPackageProject::getPacId, one.getPacId());
                    List<TjPackageProject> projectList = ppservice.list(wq1);
                    if (null != projectList && projectList.size() > 0) {
                        List<TjProject> tjProjectList = new ArrayList<>();
                        for (TjPackageProject packageProject : projectList) {
                            tjProjectList.add(projectService.getById(packageProject.getProId()));
                        }
                        tjPackage.setTjProjectList(tjProjectList);
                    }
                    one.setTjPackage(tjPackage);
                }
                if (null != one.getProIds()) {
                    String[] proIds = StringUtils.split(one.getProIds(), ",");
                    if (null != proIds && proIds.length > 0) {
                        List<TjProject> projectList = new ArrayList<>();
                        for (String proId : proIds) {
                            projectList.add(projectService.getById(proId));
                        }
                        one.setProjectList(projectList);
                    }
                }
            }
            return AjaxResult.success(reservationList);
        }
        return AjaxResult.success("暂无预约信息");
    }
 
 
    @PostMapping("/updateReservationTime")
    @ApiOperation(value = "修改个人预约时间接口")
    @Transactional
    public AjaxResult updateReservationTime(@RequestBody @ApiParam(value = "体检预约对象") TjReservation tjReservation) {
        TjReservation one = reservationService.getById(tjReservation.getId());
        if (null != one && one.getIsExpire().equals(2)) {
            //原时间段数加1
            LambdaQueryWrapper<MallTimeConfig> wq111 = new LambdaQueryWrapper<>();
            wq111.eq(MallTimeConfig::getTime, one.getReservationTime());
            final MallTimeConfig config = mallTimeConfigService.getOne(wq111);
            config.setNowNum(config.getNowNum() + 1);
            mallTimeConfigService.updateById(config);
 
            //修改后时间段数-1
            LambdaQueryWrapper<MallTimeConfig> wq = new LambdaQueryWrapper<>();
            wq.eq(MallTimeConfig::getTime, tjReservation.getReservationTime());
            final MallTimeConfig config1 = mallTimeConfigService.getOne(wq);
            config1.setNowNum(config1.getNowNum() - 1);
            mallTimeConfigService.updateById(config1);
 
            one.setReservationTime(tjReservation.getReservationTime());
            if (reservationService.updateById(one)) {
                taskService.removeTask(new TjReservationTask(tjReservation.getId()));
                taskService.addTask(new TjReservationTask(tjReservation.getId(),
                        DateUtil.between(new Date(), DateUtil.endOfDay(tjReservation.getReservationTime()), DateUnit.MS)));
            }
 
            return AjaxResult.success("操作成功");
        }
        return AjaxResult.error("预约不存在或已超时 不可修改!");
    }
 
 
    @GetMapping("/cusGetHistryReservation")
    @ApiOperation(value = "查看个人历史预约")
    public AjaxResult cusGetHistryReservation() {
        Wxuser wxuser = UserHoder.getWxuser();
        LambdaQueryWrapper<TjReservation> wq = new LambdaQueryWrapper<>();
        wq.eq(TjReservation::getConnect, wxuser.getId());
        wq.eq(TjReservation::getPhoe, wxuser.getPhone());
        wq.orderByDesc(TjReservation::getCreateTime);
        List<TjReservation> reservationList = reservationService.list(wq);
        if (null != reservationList && reservationList.size() > 0) {
            for (TjReservation one : reservationList) {
                TjPackage tjPackage = packageService.getById(one.getPacId());
                if (null != tjPackage) {
                    LambdaQueryWrapper<TjPackageProject> wq1 = new LambdaQueryWrapper<>();
                    wq1.eq(TjPackageProject::getPacId, one.getPacId());
                    List<TjPackageProject> projectList = ppservice.list(wq1);
                    if (null != projectList && projectList.size() > 0) {
                        List<TjProject> tjProjectList = new ArrayList<>();
                        for (TjPackageProject packageProject : projectList) {
                            tjProjectList.add(projectService.getById(packageProject.getProId()));
                        }
                        tjPackage.setTjProjectList(tjProjectList);
                    }
                    one.setTjPackage(tjPackage);
                }
                if (null != one.getProIds()) {
                    String[] proIds = StringUtils.split(one.getProIds(), ",");
                    if (null != proIds && proIds.length > 0) {
                        List<TjProject> projectList = new ArrayList<>();
                        for (String proId : proIds) {
                            projectList.add(projectService.getById(proId));
                        }
                        one.setProjectList(projectList);
                    }
                }
                //预约时间
                Date reservationTime = one.getReservationTime();
                //计算 截止时间 往后推24小时
                Calendar calendar = Calendar.getInstance(); //得到日历
                calendar.setTime(reservationTime);//把预约时间赋给日历
                calendar.add(Calendar.DAY_OF_MONTH, 1);  //设置为后一天
                Date tomorrow = calendar.getTime();   //得到后一天的时间
 
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
                String format = sdf.format(tomorrow);
 
            }
            return AjaxResult.success(reservationList);
        }
        return AjaxResult.success("暂无预约信息");
    }
 
    @GetMapping("/getReservationByIdCard")
    @ApiOperation(value = "根据身份证号查看预约信息")
    public AjaxResult getReservationByIdCard(@RequestParam @ApiParam(value = "身份证号") String idCard) {
        LambdaQueryWrapper<TjReservation> wq = new LambdaQueryWrapper<>();
        wq.eq(TjReservation::getIdCard, idCard);
        wq.orderByDesc(TjReservation::getCreateTime);
        TjReservation one = reservationService.list(wq).get(0);
        if (null != one) {
//            for (TjReservation one : reservationList) {
            TjPackage tjPackage = packageService.getById(one.getPacId());
            if (null != tjPackage) {
                LambdaQueryWrapper<TjPackageProject> wq1 = new LambdaQueryWrapper<>();
                wq1.eq(TjPackageProject::getPacId, one.getPacId());
                List<TjPackageProject> projectList = ppservice.list(wq1);
                if (null != projectList && projectList.size() > 0) {
                    List<TjProject> tjProjectList = new ArrayList<>();
                    for (TjPackageProject packageProject : projectList) {
                        tjProjectList.add(projectService.getById(packageProject.getProId()));
                    }
                    tjPackage.setTjProjectList(tjProjectList);
                }
                one.setTjPackage(tjPackage);
            }
            if (null != one.getProIds()) {
                String[] proIds = StringUtils.split(one.getProIds(), ",");
                if (null != proIds && proIds.length > 0) {
                    List<TjProject> projectList = new ArrayList<>();
                    for (String proId : proIds) {
                        projectList.add(projectService.getById(proId));
                    }
                    one.setProjectList(projectList);
                }
            }
//            }
            return AjaxResult.success(one);
        }
        return AjaxResult.success("暂无预约信息");
    }
}