SerCarInfoServiceImpl.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.ydtech.modules.ser.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.ydtech.core.page.HttpResult;
  5. import com.ydtech.modules.admin.model.SysUser;
  6. import com.ydtech.modules.base.controller.BaseController;
  7. import com.ydtech.modules.ser.dao.SerCarInfoMapper;
  8. import com.ydtech.modules.ser.model.SerCarInfo;
  9. import com.ydtech.modules.ser.service.SerCarInfoService;
  10. import com.ydtech.utils.idgen.IdGenerate;
  11. import io.swagger.models.auth.In;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Service;
  14. import java.time.*;
  15. import java.util.Date;
  16. import java.util.List;
  17. @Service
  18. public class SerCarInfoServiceImpl implements SerCarInfoService {
  19. @Autowired
  20. SerCarInfoMapper serCarInfoMapper;
  21. @Override
  22. public HttpResult queryCarInfoByUserId() {
  23. LambdaQueryWrapper<SerCarInfo> queryWrapper = new LambdaQueryWrapper();
  24. SysUser user = BaseController.getUser();
  25. queryWrapper.eq(SerCarInfo::getUserId,user.getId());
  26. List<SerCarInfo> list= serCarInfoMapper.selectList(queryWrapper);
  27. return HttpResult.ok(list);
  28. }
  29. @Override
  30. public HttpResult addCarInfo(SerCarInfo serCarInfo) {
  31. // 设置id、userId、createdTime
  32. SysUser user = BaseController.getUser();
  33. serCarInfo.setId(IdGenerate.nextId());
  34. serCarInfo.setUserId(user.getId());
  35. LocalDateTime currentTime = LocalDateTime.now();
  36. serCarInfo.setCreateTime(currentTime);
  37. //查询车辆是否重复
  38. QueryWrapper<SerCarInfo> queryWrapper = new QueryWrapper<>();
  39. queryWrapper.eq("licenseno", serCarInfo.getLicenseNo());
  40. boolean exist = serCarInfoMapper.exists(queryWrapper);
  41. if (exist) {
  42. return HttpResult.error("该车已被其他用户添加,请检查");
  43. }
  44. // 校验年检到期时间不可早于注册时间
  45. if (serCarInfo.getRegisterTime() == null && serCarInfo.getExpireTime() == null) {
  46. return HttpResult.error("注册时间与到期时间不可为空");
  47. }
  48. Instant registerInstant = serCarInfo.getRegisterTime().toInstant();
  49. Instant expireInstant = serCarInfo.getExpireTime().toInstant();
  50. Instant nowInstant = new Date().toInstant();
  51. if (expireInstant.isBefore(registerInstant)) {
  52. return HttpResult.error("年检到期时间不可早于注册时间");
  53. }
  54. // 计算逾期时间 距离时间
  55. if (expireInstant.equals(nowInstant)) {
  56. serCarInfo.setOverdueTime("0");
  57. serCarInfo.setDistanceTime("0");
  58. } else if (expireInstant.isBefore(nowInstant)) {
  59. Duration duration = Duration.between(expireInstant, nowInstant);
  60. serCarInfo.setOverdueTime(String.valueOf(duration.toDays()));
  61. serCarInfo.setDistanceTime("0");
  62. } else if (nowInstant.isBefore(expireInstant)) {
  63. Duration duration = Duration.between(nowInstant, expireInstant);
  64. serCarInfo.setOverdueTime("0");
  65. serCarInfo.setDistanceTime(String.valueOf(duration.toDays()));
  66. }
  67. serCarInfoMapper.insert(serCarInfo);
  68. return HttpResult.ok("新增车辆信息成功", serCarInfo);
  69. }
  70. @Override
  71. public HttpResult updateCarInfo(SerCarInfo serCarInfo) {
  72. //查询车辆是否重复
  73. QueryWrapper<SerCarInfo> queryWrapper = new QueryWrapper<>();
  74. queryWrapper.eq("licenseno", serCarInfo.getLicenseNo());
  75. queryWrapper.ne("userid", BaseController.getUser().getId());
  76. boolean exist = serCarInfoMapper.exists(queryWrapper);
  77. if (exist) {
  78. return HttpResult.error("该车已被其他用户添加,请检查");
  79. }
  80. // 校验年检到期时间不可早于注册时间
  81. if (serCarInfo.getRegisterTime() == null && serCarInfo.getExpireTime() == null) {
  82. return HttpResult.error("注册时间与到期时间不可为空");
  83. }
  84. Instant registerInstant = serCarInfo.getRegisterTime().toInstant();
  85. Instant expireInstant = serCarInfo.getExpireTime().toInstant();
  86. Instant nowInstant = new Date().toInstant();
  87. if (expireInstant.isBefore(registerInstant)) {
  88. return HttpResult.error("年检到期时间不可早于注册时间");
  89. }
  90. // 计算逾期时间 距离时间
  91. if (expireInstant.equals(nowInstant)) {
  92. serCarInfo.setOverdueTime("0");
  93. serCarInfo.setDistanceTime("0");
  94. } else if (expireInstant.isBefore(nowInstant)) {
  95. Duration duration = Duration.between(expireInstant, nowInstant);
  96. serCarInfo.setOverdueTime(String.valueOf(duration.toDays()));
  97. serCarInfo.setDistanceTime("0");
  98. } else if (nowInstant.isBefore(expireInstant)) {
  99. Duration duration = Duration.between(nowInstant, expireInstant);
  100. serCarInfo.setOverdueTime("0");
  101. serCarInfo.setDistanceTime(String.valueOf(duration.toDays()));
  102. }
  103. serCarInfoMapper.updateById(serCarInfo);
  104. return HttpResult.ok("编辑成功", serCarInfo);
  105. }
  106. }