From 84b2413220bea058c5fa7e695377cec975e0dc5f Mon Sep 17 00:00:00 2001 From: zhangyeguang Date: Thu, 12 Sep 2024 16:25:05 +0800 Subject: [PATCH] region and bug fixed --- .../jiagutech/ams/mapper/RegionMapper.java | 2 +- .../com/jiagutech/ams/mapper/UserMapper.java | 2 + .../com/jiagutech/ams/model/UserDetail.java | 3 + .../ams/controller/RegionController.java | 17 ++++++ .../jiagutech/ams/event/JobFinishEvent.java | 27 +++++++++ .../ams/listener/JobFinishListener.java | 31 +++++++++++ .../jiagutech/ams/model/dto/DeviceDTO.java | 5 +- .../ams/model/dto/TrackImageDTO.java | 3 +- .../ams/service/DeviceServiceImpl.java | 3 + .../jiagutech/ams/service/JobServiceImpl.java | 20 ++++++- .../jiagutech/ams/service/RegionService.java | 9 +++ .../ams/service/RegionServiceImpl.java | 24 ++++++++ .../ams/controller/UserController.java | 17 +++++- .../com/jiagutech/ams/model/UserMapping.java | 4 ++ .../ams/model/request/LoginRequest.java | 2 +- .../ams/model/request/UserFarmerRequest.java | 55 +++++++++++++++++++ .../ams/model/request/UserRequest.java | 1 - .../jiagutech/ams/service/UserService.java | 5 ++ .../ams/service/UserServiceImpl.java | 35 +++++++++++- .../src/main/resources/mapper/UserMapper.xml | 16 +++++- web/pom.xml | 6 +- .../ams/common/GlobalExceptionHandler.java | 30 +++++----- web/src/main/resources/application.yml | 4 +- 23 files changed, 293 insertions(+), 28 deletions(-) create mode 100644 social/src/main/java/com/jiagutech/ams/event/JobFinishEvent.java create mode 100644 social/src/main/java/com/jiagutech/ams/listener/JobFinishListener.java create mode 100644 social/src/main/java/com/jiagutech/ams/service/RegionService.java create mode 100644 social/src/main/java/com/jiagutech/ams/service/RegionServiceImpl.java create mode 100644 system/src/main/java/com/jiagutech/ams/model/request/UserFarmerRequest.java diff --git a/common/src/main/java/com/jiagutech/ams/mapper/RegionMapper.java b/common/src/main/java/com/jiagutech/ams/mapper/RegionMapper.java index 96e0892..d2fe167 100644 --- a/common/src/main/java/com/jiagutech/ams/mapper/RegionMapper.java +++ b/common/src/main/java/com/jiagutech/ams/mapper/RegionMapper.java @@ -17,7 +17,7 @@ import java.util.List; * @Version 1.0 * @description: **/ -public interface RegionMapper extends BaseMapper { +public interface RegionMapper extends BaseMapper { List getRegionsByParentCode(@Param("regionCode") long regionCode, @Param("level") int level); diff --git a/common/src/main/java/com/jiagutech/ams/mapper/UserMapper.java b/common/src/main/java/com/jiagutech/ams/mapper/UserMapper.java index f25b709..13b212e 100644 --- a/common/src/main/java/com/jiagutech/ams/mapper/UserMapper.java +++ b/common/src/main/java/com/jiagutech/ams/mapper/UserMapper.java @@ -31,4 +31,6 @@ public interface UserMapper extends BaseMapper { Page userPage(Page page,@Param("ew") QueryWrapper queryWrapper); + + List selectFarmersByRegionCode(@Param("ew") QueryWrapper query); } diff --git a/common/src/main/java/com/jiagutech/ams/model/UserDetail.java b/common/src/main/java/com/jiagutech/ams/model/UserDetail.java index bc0f71d..8ca01db 100644 --- a/common/src/main/java/com/jiagutech/ams/model/UserDetail.java +++ b/common/src/main/java/com/jiagutech/ams/model/UserDetail.java @@ -8,6 +8,7 @@ package com.jiagutech.ams.model; * @description: **/ +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.jiagutech.ams.model.dto.DeptDTO; @@ -19,6 +20,7 @@ import lombok.experimental.Accessors; public class UserDetail { @JsonSerialize(using = ToStringSerializer.class) private Long userId; + @JsonIgnore private String userName; private String email; private String phone; @@ -27,4 +29,5 @@ public class UserDetail { private Long regionCode; private String regionName; private String userIdStr; + private String contactPhone; } diff --git a/social/src/main/java/com/jiagutech/ams/controller/RegionController.java b/social/src/main/java/com/jiagutech/ams/controller/RegionController.java index fe5c642..7d9c2ba 100644 --- a/social/src/main/java/com/jiagutech/ams/controller/RegionController.java +++ b/social/src/main/java/com/jiagutech/ams/controller/RegionController.java @@ -1,10 +1,17 @@ package com.jiagutech.ams.controller; +import com.jiagutech.ams.model.common.R; +import com.jiagutech.ams.model.dto.RegionDTO; +import com.jiagutech.ams.service.RegionService; +import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.List; + /** * @ClassName RegionController * @author: zhangyeguang @@ -19,4 +26,14 @@ import org.springframework.web.bind.annotation.RestController; public class RegionController { + private final RegionService regionService; + + @Operation(summary = "获取下一级的所有区域列表") + @GetMapping("getChildren") + public R> getChildren(Long regionCode) { + List regionDTOList = regionService.getChildren(regionCode); + return R.ok(regionDTOList); + } + + } diff --git a/social/src/main/java/com/jiagutech/ams/event/JobFinishEvent.java b/social/src/main/java/com/jiagutech/ams/event/JobFinishEvent.java new file mode 100644 index 0000000..416f72e --- /dev/null +++ b/social/src/main/java/com/jiagutech/ams/event/JobFinishEvent.java @@ -0,0 +1,27 @@ +package com.jiagutech.ams.event; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.context.ApplicationEvent; + +/** + * @ClassName JobFinishEvent + * @author: zhangyeguang + * @create: 2024-09-12 11:15 + * @Version 1.0 + * @description: + **/ + + +public class JobFinishEvent extends ApplicationEvent { + @Setter + @Getter + private Long jobId; + + + public JobFinishEvent(Object source, Long jobId) { + super(source); + this.jobId = jobId; + } + +} diff --git a/social/src/main/java/com/jiagutech/ams/listener/JobFinishListener.java b/social/src/main/java/com/jiagutech/ams/listener/JobFinishListener.java new file mode 100644 index 0000000..1b05a67 --- /dev/null +++ b/social/src/main/java/com/jiagutech/ams/listener/JobFinishListener.java @@ -0,0 +1,31 @@ +package com.jiagutech.ams.listener; + +import com.jiagutech.ams.event.JobFinishEvent; +import com.jiagutech.ams.model.TrackItem; +import com.jiagutech.ams.service.JobService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @ClassName JobFinishListener + * @author: zhangyeguang + * @create: 2024-09-12 11:17 + * @Version 1.0 + * @description: + **/ +@Slf4j +@Component +@RequiredArgsConstructor +public class JobFinishListener { + private final JobService jobService; + + @EventListener(JobFinishEvent.class) + public void finishJob(JobFinishEvent event) { + log.info("JobFinishListener:{}", event.getJobId()); + List trackList = jobService.getTrackList(event.getJobId()); + } +} diff --git a/social/src/main/java/com/jiagutech/ams/model/dto/DeviceDTO.java b/social/src/main/java/com/jiagutech/ams/model/dto/DeviceDTO.java index 927922f..41a7961 100644 --- a/social/src/main/java/com/jiagutech/ams/model/dto/DeviceDTO.java +++ b/social/src/main/java/com/jiagutech/ams/model/dto/DeviceDTO.java @@ -1,5 +1,7 @@ package com.jiagutech.ams.model.dto; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @@ -14,7 +16,8 @@ import lombok.Data; @TableName("ams_device_info") public class DeviceDTO { - private Long id; + @TableId(type = IdType.AUTO) + private Integer id; private String deviceNum; diff --git a/social/src/main/java/com/jiagutech/ams/model/dto/TrackImageDTO.java b/social/src/main/java/com/jiagutech/ams/model/dto/TrackImageDTO.java index 34887b2..3595574 100644 --- a/social/src/main/java/com/jiagutech/ams/model/dto/TrackImageDTO.java +++ b/social/src/main/java/com/jiagutech/ams/model/dto/TrackImageDTO.java @@ -2,6 +2,7 @@ package com.jiagutech.ams.model.dto; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; +import org.springframework.data.relational.core.sql.In; /** * @ClassName TrackImageDTO @@ -16,7 +17,7 @@ public class TrackImageDTO { private Long id; - private Long deviceId; + private Integer deviceId; private Long ts; diff --git a/social/src/main/java/com/jiagutech/ams/service/DeviceServiceImpl.java b/social/src/main/java/com/jiagutech/ams/service/DeviceServiceImpl.java index 64f3e9b..b143860 100644 --- a/social/src/main/java/com/jiagutech/ams/service/DeviceServiceImpl.java +++ b/social/src/main/java/com/jiagutech/ams/service/DeviceServiceImpl.java @@ -45,6 +45,9 @@ public class DeviceServiceImpl implements DeviceService { public List onlineAndRound(double maxLng, double maxLat, double minLng, double minLat) { Set keys = stringRedisTemplate.keys(TRACK_PREFIX_REDIS_KEY); List deviceInfos = new ArrayList<>(); + + DeviceInfo deviceInfo1 = new DeviceInfo().setBoxNum("0").setStatus(2).setDeviceId(321).setBoxNum("5ds3a22w").setLng(118.73929470486111).setLat(31.9823046875); + deviceInfos.add(deviceInfo1); if (CollectionUtils.isNotEmpty(keys)) { List values = stringRedisTemplate.opsForValue().multiGet(keys); List trackDataJGS = JSON.parseArray(JSON.toJSONString(values), TrackDataJG.class); diff --git a/social/src/main/java/com/jiagutech/ams/service/JobServiceImpl.java b/social/src/main/java/com/jiagutech/ams/service/JobServiceImpl.java index aa09bc0..a36cbfc 100644 --- a/social/src/main/java/com/jiagutech/ams/service/JobServiceImpl.java +++ b/social/src/main/java/com/jiagutech/ams/service/JobServiceImpl.java @@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.jiagutech.ams.constant.UserConstants; +import com.jiagutech.ams.event.JobFinishEvent; import com.jiagutech.ams.exception.BizCode; import com.jiagutech.ams.exception.BusinessException; import com.jiagutech.ams.mapper.*; @@ -25,9 +26,12 @@ import com.jiagutech.ams.utils.ExcelUtil; import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Service; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.function.Predicate; @@ -57,6 +61,8 @@ public class JobServiceImpl implements JobService { private final TrackImageMapper trackImageMapper; + private final ApplicationContext applicationContext; + @Override public JobCreateResponse createJob(JobCreateRequest jobCreateRequest) { @@ -80,7 +86,9 @@ public class JobServiceImpl implements JobService { public void updateJobStatus(Long jobId, Integer status) { JobDTO jobDTO = new JobDTO().setId(jobId).setStatus(status).setEndTime(System.currentTimeMillis()); jobMapper.updateById(jobDTO); - + if (ObjectUtil.isNotNull(status) && status == 2) { + applicationContext.publishEvent(new JobFinishEvent(this, jobId)); + } } @@ -88,6 +96,7 @@ public class JobServiceImpl implements JobService { public PageResult getPages(PageRequest pageRequest) { JobPageRequest requestParam = pageRequest.getRequest(); QueryWrapper queryWrapper = buildQueryWrapper(requestParam); + queryWrapper.orderByDesc("j.start_time"); Page page = new Page(pageRequest.getPageNum(), pageRequest.getPageSize()); Page result = jobMapper.jobPage(page, queryWrapper); return PageResult.of((int) result.getTotal(), (int) result.getSize(), (int) result.getCurrent(), result.getRecords()); @@ -128,16 +137,23 @@ public class JobServiceImpl implements JobService { if (jobId != null) { JobDTO job = jobMapper.selectById(jobId); DeviceDTO deviceDTO = deviceMapper.selectById(job.getDeviceId()); + if (ObjectUtil.isEmpty(job) || ObjectUtil.isEmpty(deviceDTO)) { + return Collections.emptyList(); + } List locusList = trackRestClient.getTractorByDroneIdAndTime(deviceDTO.getBoxNum(), job.getStartTime(), job.getEndTime()); QueryWrapper query = Wrappers.query(); List trackImageDTOS = trackImageMapper.selectList( query.eq("device_id", job.getDeviceId()) .between("ts", job.getStartTime(), job.getEndTime()) ); + if (locusList == null && CollectionUtil.isEmpty(locusList)) { + return Collections.emptyList(); + + } List trackItems = new ArrayList<>(locusList.size()); for (Locus locus : locusList) { TrackItem trackItem = TrackMapping.INSTANCE.convertToTrackItemByLocus(locus); - Optional imageDTOOptional = trackImageDTOS.stream().filter(t -> t.getTs() .equals( locus.getTimestamp())).findFirst(); + Optional imageDTOOptional = trackImageDTOS.stream().filter(t -> t.getTs().equals(locus.getTimestamp())).findFirst(); if (imageDTOOptional.isPresent()) { trackItem.setImageUrl(imageDTOOptional.get().getImageUrl()); diff --git a/social/src/main/java/com/jiagutech/ams/service/RegionService.java b/social/src/main/java/com/jiagutech/ams/service/RegionService.java new file mode 100644 index 0000000..7ab7c6c --- /dev/null +++ b/social/src/main/java/com/jiagutech/ams/service/RegionService.java @@ -0,0 +1,9 @@ +package com.jiagutech.ams.service; + +import com.jiagutech.ams.model.dto.RegionDTO; + +import java.util.List; + +public interface RegionService { + List getChildren(Long regionCode); +} diff --git a/social/src/main/java/com/jiagutech/ams/service/RegionServiceImpl.java b/social/src/main/java/com/jiagutech/ams/service/RegionServiceImpl.java new file mode 100644 index 0000000..eefef8b --- /dev/null +++ b/social/src/main/java/com/jiagutech/ams/service/RegionServiceImpl.java @@ -0,0 +1,24 @@ +package com.jiagutech.ams.service; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.jiagutech.ams.mapper.RegionMapper; +import com.jiagutech.ams.model.dto.RegionDTO; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +@RequiredArgsConstructor +public class RegionServiceImpl implements RegionService { + + private final RegionMapper regionMapper; + + @Override + public List getChildren(Long regionCode) { + QueryWrapper query = Wrappers.query(); + return regionMapper.selectList(query.eq("parent_code", regionCode).orderByAsc("region_code")); + } + +} diff --git a/system/src/main/java/com/jiagutech/ams/controller/UserController.java b/system/src/main/java/com/jiagutech/ams/controller/UserController.java index f265c39..14c7bd7 100644 --- a/system/src/main/java/com/jiagutech/ams/controller/UserController.java +++ b/system/src/main/java/com/jiagutech/ams/controller/UserController.java @@ -8,6 +8,7 @@ import com.jiagutech.ams.model.common.PageResult; import com.jiagutech.ams.model.common.R; import com.jiagutech.ams.model.request.LoginRequest; import com.jiagutech.ams.model.request.PageUserRequest; +import com.jiagutech.ams.model.request.UserFarmerRequest; import com.jiagutech.ams.model.request.UserRequest; import com.jiagutech.ams.model.response.LoginResponse; import com.jiagutech.ams.model.UserDetail; @@ -61,16 +62,30 @@ public class UserController { @RequestParam(value = "phone", required = false) String phone) { return R.ok(userService.getUserDetail(userId, phone)); } + @Operation(summary = "更新用户信息") @PutMapping("/update") - public R updateUser(@Validated @RequestBody UserRequest user){ + public R updateUser(@Validated @RequestBody UserRequest user) { userService.updateUser(user); return R.ok(); } + @Operation(summary = "更新农户人员信息") + @PutMapping("/updateFarmer") + public R updateFarmerUser(@Validated @RequestBody UserFarmerRequest user) { + userService.updateFarmerUser(user); + return R.ok(); + } + @Operation(summary = "分页获取用户列表") @PostMapping("/page") public R> getUserList(@RequestBody PageRequest pageRequest) { return R.ok(userService.getUserPage(pageRequest)); } + + @Operation(summary = "根据区域码获取农户列表") + @GetMapping("/listFarmerByRegionCode") + public R> getAllFarmersByRegionCode(Long regionCode) { + return R.ok(userService.getAllFarmersByRegionCode(regionCode)); + } } diff --git a/system/src/main/java/com/jiagutech/ams/model/UserMapping.java b/system/src/main/java/com/jiagutech/ams/model/UserMapping.java index 9b63d3d..6435d23 100644 --- a/system/src/main/java/com/jiagutech/ams/model/UserMapping.java +++ b/system/src/main/java/com/jiagutech/ams/model/UserMapping.java @@ -1,6 +1,7 @@ package com.jiagutech.ams.model; import com.jiagutech.ams.model.dto.UserDTO; +import com.jiagutech.ams.model.request.UserFarmerRequest; import com.jiagutech.ams.model.request.UserRequest; import org.mapstruct.Mapper; import org.mapstruct.Mapping; @@ -21,6 +22,9 @@ public interface UserMapping { @Mapping(source = "userId", target = "id") UserDTO userToUserDTO(UserRequest user); + @Mapping(source = "userId", target = "id") + UserDTO userFarmerToUserDTO(UserFarmerRequest userFarmer); + LoginUser userDTOToLoginUser(UserDTO userDTO); } diff --git a/system/src/main/java/com/jiagutech/ams/model/request/LoginRequest.java b/system/src/main/java/com/jiagutech/ams/model/request/LoginRequest.java index 54282aa..e337541 100644 --- a/system/src/main/java/com/jiagutech/ams/model/request/LoginRequest.java +++ b/system/src/main/java/com/jiagutech/ams/model/request/LoginRequest.java @@ -54,6 +54,6 @@ public class LoginRequest implements Serializable { private String phone; - private Integer clientType = 1; + private int clientType = 1; } diff --git a/system/src/main/java/com/jiagutech/ams/model/request/UserFarmerRequest.java b/system/src/main/java/com/jiagutech/ams/model/request/UserFarmerRequest.java new file mode 100644 index 0000000..dd36e86 --- /dev/null +++ b/system/src/main/java/com/jiagutech/ams/model/request/UserFarmerRequest.java @@ -0,0 +1,55 @@ +package com.jiagutech.ams.model.request; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.jiagutech.ams.utils.StringToLongDeserializer; +import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.Pattern; +import jakarta.validation.constraints.Size; +import lombok.Data; + +/** + * @ClassName UserRequest + * @author: zhangyeguang + * @create: 2024-08-31 10:00 + * @Version 1.0 + * @description: + **/ +@Data +public class UserFarmerRequest { + /** + * 用户ID + */ + private Long userId; + + /** + * 用户昵称 + */ + @Size(min = 2, max = 30, message = "用户昵称长度不能超过{max}个字符") + private String nickName; + + + /** + * 用户邮箱 + */ + @Email(message = "邮箱格式不正确") + @Size(min = 0, max = 50, message = "邮箱长度不能超过{max}个字符") + private String email; + + /** + * 手机号码 + */ + @Pattern(regexp = "^1[34578]\\d{9}$", message = "手机号码格式不正确") + private String phone; + + + /** + * 密码 + */ + private String password; + + + private Long regionCode; + + private String contactPhone; +} diff --git a/system/src/main/java/com/jiagutech/ams/model/request/UserRequest.java b/system/src/main/java/com/jiagutech/ams/model/request/UserRequest.java index cc8eb36..e545f43 100644 --- a/system/src/main/java/com/jiagutech/ams/model/request/UserRequest.java +++ b/system/src/main/java/com/jiagutech/ams/model/request/UserRequest.java @@ -39,7 +39,6 @@ public class UserRequest { /** * 用户昵称 */ - @NotBlank(message = "用户昵称不能为空") @Size(min = 2, max = 30, message = "用户昵称长度不能超过{max}个字符") private String nickName; diff --git a/system/src/main/java/com/jiagutech/ams/service/UserService.java b/system/src/main/java/com/jiagutech/ams/service/UserService.java index 818efa9..8d88027 100644 --- a/system/src/main/java/com/jiagutech/ams/service/UserService.java +++ b/system/src/main/java/com/jiagutech/ams/service/UserService.java @@ -6,6 +6,7 @@ import com.jiagutech.ams.model.common.PageRequest; import com.jiagutech.ams.model.common.PageResult; import com.jiagutech.ams.model.request.LoginRequest; import com.jiagutech.ams.model.request.PageUserRequest; +import com.jiagutech.ams.model.request.UserFarmerRequest; import com.jiagutech.ams.model.request.UserRequest; import com.jiagutech.ams.model.response.LoginResponse; @@ -21,7 +22,11 @@ public interface UserService { void updateUser(UserRequest user); + void updateFarmerUser(UserFarmerRequest userFarmer); + List getUserList(String roleKey); PageResult getUserPage(PageRequest pageRequest); + + List getAllFarmersByRegionCode(Long regionCode); } diff --git a/system/src/main/java/com/jiagutech/ams/service/UserServiceImpl.java b/system/src/main/java/com/jiagutech/ams/service/UserServiceImpl.java index 0a90717..6d51122 100644 --- a/system/src/main/java/com/jiagutech/ams/service/UserServiceImpl.java +++ b/system/src/main/java/com/jiagutech/ams/service/UserServiceImpl.java @@ -27,6 +27,7 @@ import com.jiagutech.ams.model.dto.UserDeptDTO; import com.jiagutech.ams.model.dto.UserRoleDTO; import com.jiagutech.ams.model.request.LoginRequest; import com.jiagutech.ams.model.request.PageUserRequest; +import com.jiagutech.ams.model.request.UserFarmerRequest; import com.jiagutech.ams.model.request.UserRequest; import com.jiagutech.ams.model.response.LoginResponse; import lombok.RequiredArgsConstructor; @@ -64,10 +65,15 @@ public class UserServiceImpl implements UserService { String password = loginRequest.getPassword(); String code = loginRequest.getCode(); String uuid = loginRequest.getUuid(); - -// validateCaptcha(code, uuid); + if (loginRequest.getClientType() == 1) { + validateCaptcha(code, uuid); + } Integer clientType = loginRequest.getClientType(); LoginUser user = userMapper.selectLoginUserByPhone(phone); + if (user == null) { + throw new BusinessException(BizCode.USER_NOT_FOUND); + } + Predicate predicate = role -> role.getKey().equals("manager") || role.getKey().equals("machinist"); if (clientType == 2 && user.getRoles().stream().noneMatch(predicate)) { throw new BusinessException(BizCode.LOGIN_TYPE_ERROR); @@ -77,6 +83,7 @@ public class UserServiceImpl implements UserService { throw new BusinessException(BizCode.PASSWORD_ERROR); } StpUtil.login(user.getUserId()); + user.setLoginTime(System.currentTimeMillis()); StpUtil.getSession().set(UserConstants.SYS_SESSION, user); LoginResponse loginVO = new LoginResponse(); loginVO.setAccessToken(StpUtil.getTokenValue()); @@ -111,10 +118,22 @@ public class UserServiceImpl implements UserService { @Override public void updateUser(UserRequest user) { UserDTO userDTO = UserMapping.INSTANCE.userToUserDTO(user); + if (StringUtils.isNotBlank(user.getPassword())) { + userDTO.setPassword(BCrypt.hashpw(user.getPassword())); + } userMapper.updateById(userDTO); updateUserRoleAndDept(user); } + @Override + public void updateFarmerUser(UserFarmerRequest userFarmer) { + UserDTO userDTO = UserMapping.INSTANCE.userFarmerToUserDTO(userFarmer); + if (StringUtils.isNotBlank(userFarmer.getPassword())) { + userDTO.setPassword(BCrypt.hashpw(userFarmer.getPassword())); + } + userMapper.updateById(userDTO); + } + private void insertUserRoleAndDept(UserDTO user, String roleKey, Long deptId) { if (StringUtils.isNotBlank(roleKey)) { userRoleMapper.insertUserRole(user.getId(), roleKey); @@ -154,6 +173,18 @@ public class UserServiceImpl implements UserService { return PageResult.of((int) result.getTotal(), (int) result.getSize(), (int) result.getCurrent(), result.getRecords()); } + + @Override + public List getAllFarmersByRegionCode(Long regionCode) { + + QueryWrapper query = Wrappers.query(); + query.eq("u.region_code", regionCode); + query.eq("u.del_flag", 0); + query.eq("r.key", "farmer"); + + return userMapper.selectFarmersByRegionCode(query); + } + private QueryWrapper buildQueryWrapper(PageUserRequest requestParam) { QueryWrapper queryWrapper = new QueryWrapper<>(); if (requestParam != null) { diff --git a/system/src/main/resources/mapper/UserMapper.xml b/system/src/main/resources/mapper/UserMapper.xml index 781e23e..9db48b6 100644 --- a/system/src/main/resources/mapper/UserMapper.xml +++ b/system/src/main/resources/mapper/UserMapper.xml @@ -64,7 +64,7 @@ - select id, nick_name, region_code from ams_user where region_path like CONCAT(#{regionPath}, '%') @@ -85,5 +85,19 @@ left join ams_user_dept ud on u.id=ud.user_id ${ew.getCustomSqlSegment} + \ No newline at end of file diff --git a/web/pom.xml b/web/pom.xml index 18d83f9..8ac05e0 100644 --- a/web/pom.xml +++ b/web/pom.xml @@ -84,9 +84,13 @@ + + + + cn.dev33 - sa-token-redis + sa-token-redis-jackson diff --git a/web/src/main/java/com/jiagutech/ams/common/GlobalExceptionHandler.java b/web/src/main/java/com/jiagutech/ams/common/GlobalExceptionHandler.java index d190501..f21ed93 100644 --- a/web/src/main/java/com/jiagutech/ams/common/GlobalExceptionHandler.java +++ b/web/src/main/java/com/jiagutech/ams/common/GlobalExceptionHandler.java @@ -8,6 +8,8 @@ import com.jiagutech.ams.exception.BizCode; import com.jiagutech.ams.exception.BusinessException; import com.jiagutech.ams.model.common.R; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.validation.BindException; import org.springframework.validation.FieldError; import org.springframework.web.bind.MethodArgumentNotValidException; @@ -24,51 +26,51 @@ import java.util.Map; public class GlobalExceptionHandler { @ExceptionHandler(BindException.class) - public R handleBindExceptions(BindException ex) { + public ResponseEntity handleBindExceptions(BindException ex) { log.error("参数绑定失败", ex); Map errorMap = new HashMap<>(); for (FieldError fieldError : ex.getFieldErrors()) { errorMap.put(fieldError.getField(), fieldError.getDefaultMessage()); } - return R.fail(BizCode.General_ParameterInvalid.getCode(), JSON.toJSONString(errorMap)); + return new ResponseEntity<>(R.fail(BizCode.General_ParameterInvalid.getCode(), JSON.toJSONString(errorMap)), HttpStatus.BAD_REQUEST); } @ExceptionHandler(MethodArgumentNotValidException.class) - public R handleValidationExceptions(MethodArgumentNotValidException ex) { + public ResponseEntity handleValidationExceptions(MethodArgumentNotValidException ex) { log.error("参数校验失败", ex); Map errorMap = new HashMap<>(); for (FieldError fieldError : ex.getBindingResult().getFieldErrors()) { errorMap.put(fieldError.getField(), fieldError.getDefaultMessage()); } - return R.fail(BizCode.General_ParameterInvalid.getCode(), JSON.toJSONString(errorMap)); + return new ResponseEntity<>(R.fail(BizCode.General_ParameterInvalid.getCode(), JSON.toJSONString(errorMap)), HttpStatus.BAD_REQUEST); } @ExceptionHandler(BusinessException.class) - public R handleBusinessException(BusinessException ex) { - return R.fail(ex.getCode(), ex.getMessage()); + public ResponseEntity handleBusinessException(BusinessException ex) { + return new ResponseEntity<>(R.fail(ex.getCode(), ex.getMessage()), HttpStatus.BAD_REQUEST); } @ExceptionHandler(MethodArgumentTypeMismatchException.class) - public R handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException ex) { + public ResponseEntity handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException ex) { log.error("参数类型错误", ex); - return R.fail(new BusinessException("参数类型错误")); + return new ResponseEntity<>(R.fail(new BusinessException("参数类型错误")), HttpStatus.BAD_REQUEST); } @ExceptionHandler(NotLoginException.class) - public R handleLoginException(Exception ex) { + public ResponseEntity handleLoginException(Exception ex) { log.error("系统异常", ex); - return R.fail(BizCode.USER_NOT_LOGIN.getCode(), BizCode.USER_NOT_LOGIN.getMsg()); + return new ResponseEntity<>(R.fail(BizCode.USER_NOT_LOGIN.getCode(), BizCode.USER_NOT_LOGIN.getMsg()), HttpStatus.UNAUTHORIZED); } @ExceptionHandler(NotRoleException.class) - public R handlePermissionException(Exception ex) { + public ResponseEntity handlePermissionException(Exception ex) { log.error("鉴权失败", ex); - return R.fail(BizCode.PERMISSION_NOT_FOUND.getCode(), BizCode.PERMISSION_NOT_FOUND.getMsg()); + return new ResponseEntity<>(R.fail(BizCode.PERMISSION_NOT_FOUND.getCode(), BizCode.PERMISSION_NOT_FOUND.getMsg()), HttpStatus.FORBIDDEN); } @ExceptionHandler(SQLIntegrityConstraintViolationException.class) - public R handleDuplicateException(Exception ex){ + public ResponseEntity handleDuplicateException(Exception ex) { log.error("数据重复", ex); - return R.fail(BizCode.USER_PHONE_EXIST.getCode(), BizCode.USER_PHONE_EXIST.getMsg()); + return new ResponseEntity<>(R.fail(BizCode.USER_PHONE_EXIST.getCode(), BizCode.USER_PHONE_EXIST.getMsg()), HttpStatus.BAD_REQUEST); } } diff --git a/web/src/main/resources/application.yml b/web/src/main/resources/application.yml index 4480cb8..fb2dc3d 100644 --- a/web/src/main/resources/application.yml +++ b/web/src/main/resources/application.yml @@ -21,9 +21,9 @@ spring: # 空闲连接存活最大时间,默认10分钟 idleTimeout: 600000 # 此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认30分钟 - maxLifetime: 1800000 + maxLifetime: 600000 # 多久检查一次连接的活性 - keepaliveTime: 30000 + keepaliveTime: 60000 connection-test-query: SELECT 1 data: redis: