forked from fdzcxy212206413/jty
parent
038760b094
commit
1b19f8ee70
@ -0,0 +1,14 @@
|
||||
package jty.expressdistributionsystem.DTO;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class AddresseeDTO {
|
||||
@NotNull
|
||||
private Long goodsId;
|
||||
|
||||
@NotBlank(message = "取件码不能为空")
|
||||
private String claimCode;
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package jty.expressdistributionsystem.DTO;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class GoodsDTO {
|
||||
@NotBlank(message = "请选择快递公司")
|
||||
private String company;
|
||||
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
@NotNull
|
||||
private Long sendUserId;
|
||||
|
||||
@NotBlank(message = "收件人名字不能为空")
|
||||
private String getUserName;
|
||||
|
||||
@NotBlank(message = "快递名称不能为空")
|
||||
private String name;
|
||||
|
||||
@NotBlank(message = "快递重量不能为空")
|
||||
private String quality;
|
||||
|
||||
@NotBlank(message = "运送地址不能为空")
|
||||
private String address;
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package jty.expressdistributionsystem.DTO;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class PickUpExpressDTO {
|
||||
@NotBlank(message = "收件人名字不能为空")
|
||||
private String master;
|
||||
|
||||
@NotBlank(message = "快递码不能为空")
|
||||
private String expressCode;
|
||||
|
||||
@NotBlank(message = "取件码不能为空")
|
||||
private String claimCode;
|
||||
}
|
@ -1,155 +1,198 @@
|
||||
package jty.expressdistributionsystem.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import jakarta.annotation.Resource;
|
||||
import jty.expressdistributionsystem.DTO.UserLoginByAccountDTO;
|
||||
import jty.expressdistributionsystem.DTO.UserModifyInfoDTO;
|
||||
import jty.expressdistributionsystem.DTO.UserModifyPasswordDTO;
|
||||
import jty.expressdistributionsystem.entity.Result;
|
||||
import jty.expressdistributionsystem.entity.User;
|
||||
import jty.expressdistributionsystem.service.UserService;
|
||||
import jty.expressdistributionsystem.DTO.AddresseeDTO;
|
||||
import jty.expressdistributionsystem.DTO.GoodsDTO;
|
||||
import jty.expressdistributionsystem.DTO.PickUpExpressDTO;
|
||||
import jty.expressdistributionsystem.DTO.SendMessageDTO;
|
||||
import jty.expressdistributionsystem.entity.*;
|
||||
import jty.expressdistributionsystem.service.*;
|
||||
import jty.expressdistributionsystem.utils.ClaimCodeUtil;
|
||||
import jty.expressdistributionsystem.utils.ExpressCodeUtil;
|
||||
import jty.expressdistributionsystem.utils.GetIdUtil;
|
||||
import jty.expressdistributionsystem.utils.JwtUtil;
|
||||
import jty.expressdistributionsystem.utils.Md5Util;
|
||||
import jty.expressdistributionsystem.utils.ThreadLocalUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import jty.expressdistributionsystem.utils.SendMessageUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
@Slf4j
|
||||
public class UserController {
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@Resource
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
// 注册
|
||||
@PostMapping("/register")
|
||||
public Result<String> register(@RequestBody @Validated @NotNull User user) {
|
||||
// 查找用户是否存在
|
||||
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("user_name", user.getUserName())
|
||||
.or()
|
||||
.eq("account", user.getAccount())
|
||||
.or()
|
||||
.eq("phone", user.getPhone());
|
||||
User u = userService.getOne(queryWrapper);
|
||||
if (u != null) {
|
||||
return new Result<>(409, "账号或用户名或手机号已存在", "");
|
||||
}
|
||||
// 密码加密
|
||||
user.setPassword(Md5Util.getMD5String(user.getPassword()));
|
||||
// 用户数据入库
|
||||
userService.save(user);
|
||||
return new Result<>(200, "注册成功", "");
|
||||
private MessageService messageService;
|
||||
|
||||
@Resource
|
||||
private RecordsService recordsService;
|
||||
|
||||
@Resource
|
||||
private CodeService codeService;
|
||||
|
||||
@Resource
|
||||
private GoodsService goodsService;
|
||||
|
||||
@Resource
|
||||
private AddressService addressService;
|
||||
|
||||
// 用户给快递员发消息
|
||||
@PostMapping("/sendMessage")
|
||||
public Result<String> sendMessage(@Validated @RequestBody SendMessageDTO sendMessageDTO) {
|
||||
Message message = SendMessageUtil.sendMessage(sendMessageDTO);
|
||||
messageService.save(message);
|
||||
return new Result<>(200, "发送成功", "");
|
||||
}
|
||||
|
||||
// 通过账号登录
|
||||
@PostMapping("/loginByAccount")
|
||||
public Result<String> login(@RequestBody @Validated @NotNull UserLoginByAccountDTO userLoginByAccountDTO) {
|
||||
// 查询用户是否存在
|
||||
Optional<User> optionalUser = Optional.ofNullable(userService.getOne(new QueryWrapper<User>()
|
||||
.eq("account", userLoginByAccountDTO.getAccount())));
|
||||
if (optionalUser.isEmpty() || !Md5Util.getMD5String(userLoginByAccountDTO.getPassword()).equals(optionalUser.get().getPassword())) {
|
||||
return new Result<>(401, "账号或者密码错误", "");
|
||||
}
|
||||
User user = optionalUser.get();
|
||||
if(user.getDisabled() == 1){
|
||||
return new Result<>(403, "该账号已被禁用, 请联系管理员", "");
|
||||
}
|
||||
user.setLoginTime(LocalDateTime.now());
|
||||
userService.update(user, new UpdateWrapper<User>().eq("id", user.getId()));
|
||||
// 校验通过
|
||||
Map<String, Object> claims = new HashMap<>();
|
||||
claims.put("id", user.getId());
|
||||
claims.put("account", userLoginByAccountDTO.getAccount());
|
||||
// 获取token
|
||||
String token = JwtUtil.genToken(claims);
|
||||
// 存放token到redis当中进行持久化存储
|
||||
ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
|
||||
// 设置过期时间(1天)
|
||||
stringStringValueOperations.set(token, token, 1, TimeUnit.DAYS);
|
||||
// 返回token给前端
|
||||
return new Result<>(200, "登录成功", token);
|
||||
// 给快递员点赞
|
||||
@PostMapping("/likes")
|
||||
public Result<String> like(Long id) {
|
||||
User employee = userService.getById(id);
|
||||
employee.setLikes(employee.getLikes() + 1);
|
||||
userService.save(employee);
|
||||
return new Result<>(200, "点赞成功", "");
|
||||
}
|
||||
|
||||
// 修改个人信息
|
||||
@PutMapping("/modify")
|
||||
public Result<Object> modifyInfo(@RequestBody @Validated @NotNull UserModifyInfoDTO userModifyInfoDTO) {
|
||||
// 获取登录用户id
|
||||
// 用户查看自己的快递记录
|
||||
@GetMapping("/history")
|
||||
public Result<Page<Records>> getHistory(@RequestParam int page, @RequestParam int pageSize) {
|
||||
if (page <= 0 || pageSize == 0) {
|
||||
return new Result<>(400, "页码和每页大小必须大于0", null);
|
||||
}
|
||||
Long id = GetIdUtil.getId();
|
||||
User user = userService.getById(id);
|
||||
// 检查phone和userName是否与当前用户信息一致
|
||||
boolean isPhoneSame = user.getPhone().equals(userModifyInfoDTO.getPhone());
|
||||
boolean isUserNameSame = user.getUserName().equals(userModifyInfoDTO.getUserName());
|
||||
if (isPhoneSame && isUserNameSame) {
|
||||
return new Result<>(400, "请修改您的信息", "");
|
||||
Page<Records> pageInfo = new Page<>(page, pageSize);
|
||||
LambdaQueryWrapper<Records> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(Records::getSendUserId, id);
|
||||
queryWrapper.orderByDesc(Records::getCreateTime);
|
||||
recordsService.page(pageInfo, queryWrapper);
|
||||
return new Result<>(200, "", pageInfo);
|
||||
}
|
||||
|
||||
// 用户收件
|
||||
@PostMapping("/addressee")
|
||||
public Result<String> addressee(@Validated @RequestBody @NotNull AddresseeDTO addresseeDTO) {
|
||||
Long goodsId = addresseeDTO.getGoodsId();
|
||||
QueryWrapper<Code> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("goods_id", goodsId);
|
||||
Code code = codeService.getOne(queryWrapper);
|
||||
// 判断取件码是否输入正确
|
||||
if (!code.getClaim().equals(addresseeDTO.getClaimCode())) {
|
||||
return new Result<>(401, "请输入正确的取件码", "");
|
||||
}
|
||||
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||
if (!isPhoneSame) {
|
||||
queryWrapper.eq("phone", userModifyInfoDTO.getPhone());
|
||||
if (userService.getOne(queryWrapper) != null) {
|
||||
return new Result<>(400, "该手机号已被注册", "");
|
||||
}
|
||||
user.setPhone(userModifyInfoDTO.getPhone());
|
||||
QueryWrapper<Records> recordsQueryWrapper = new QueryWrapper<>();
|
||||
recordsQueryWrapper.eq("goods_id", goodsId);
|
||||
Records records = recordsService.getOne(recordsQueryWrapper);
|
||||
records.setMark(1);
|
||||
// 设置取件人
|
||||
records.setAddresseeId(GetIdUtil.getId());
|
||||
recordsService.save(records);
|
||||
return new Result<>(200, "取件成功", "");
|
||||
}
|
||||
|
||||
// 用户代取快递
|
||||
@PostMapping("/pickUp")
|
||||
public Result<String> pickUpExpress(@RequestBody @Validated @NotNull PickUpExpressDTO pickUpExpressDTO) {
|
||||
// 查询快递原主人信息
|
||||
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
|
||||
userQueryWrapper.eq("user_name", pickUpExpressDTO.getMaster());
|
||||
User master = userService.getOne(userQueryWrapper);
|
||||
if (master == null) {
|
||||
return new Result<>(404, "该用户不存在", "");
|
||||
}
|
||||
queryWrapper.clear();
|
||||
if (!isUserNameSame) {
|
||||
queryWrapper.eq("user_name", userModifyInfoDTO.getUserName());
|
||||
if (userService.getOne(queryWrapper) != null) {
|
||||
return new Result<>(400, "该用户名已被使用", "");
|
||||
}
|
||||
user.setUserName(userModifyInfoDTO.getUserName());
|
||||
// 查询快递信息
|
||||
QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
|
||||
goodsQueryWrapper.eq("express_code", pickUpExpressDTO.getExpressCode());
|
||||
Goods goods = goodsService.getOne(goodsQueryWrapper);
|
||||
if (goods == null) {
|
||||
return new Result<>(404, "快递不存在", "");
|
||||
}
|
||||
user.setUpdateTime(null);
|
||||
userService.updateById(user);
|
||||
return new Result<>(200, "用户信息修改成功", user);
|
||||
// 代取
|
||||
QueryWrapper<Records> recordsQueryWrapper = new QueryWrapper<>();
|
||||
recordsQueryWrapper.eq("goods_id", goods.getId());
|
||||
Records records = recordsService.getOne(recordsQueryWrapper);
|
||||
records.setMark(1);
|
||||
records.setAddresseeId(GetIdUtil.getId());
|
||||
recordsService.save(records);
|
||||
return new Result<>(200, "待取成功", "");
|
||||
}
|
||||
|
||||
// 修改个人密码
|
||||
@PutMapping("/rePassword")
|
||||
public Result<String> modifyPassword(@RequestHeader("Authorization") String token,
|
||||
@RequestBody @Validated @NotNull UserModifyPasswordDTO userModifyPasswordDTO) {
|
||||
// 判断密码
|
||||
if (userModifyPasswordDTO.getOldPassword().equals(userModifyPasswordDTO.getNewPassword())) {
|
||||
return new Result<>(409, "新密码不得和旧密码一致", "");
|
||||
}
|
||||
if (!userModifyPasswordDTO.getNewPassword().equals(userModifyPasswordDTO.getReNewPassword())) {
|
||||
return new Result<>(409, "两次输入密码不相同", "");
|
||||
// 用户查看消息
|
||||
@GetMapping("/getMessage")
|
||||
public Result<Page<Message>> getMessage(@RequestParam int page, @RequestParam int pageSize) {
|
||||
if (page <= 0 || pageSize == 0) {
|
||||
return new Result<>(400, "页码和每页大小必须大于0", null);
|
||||
}
|
||||
Long id = GetIdUtil.getId();
|
||||
User user = userService.getById(id);
|
||||
if (!user.getIdentity().equals(userModifyPasswordDTO.getIdentity())) {
|
||||
return new Result<>(409, "身份证有误", "");
|
||||
}
|
||||
user.setPassword(Md5Util.getMD5String(userModifyPasswordDTO.getNewPassword()));
|
||||
user.setUpdateTime(null);
|
||||
userService.updateById(user);
|
||||
// 删除redis当中的token
|
||||
stringRedisTemplate.delete(token);
|
||||
// 删除当前线程的存储信息
|
||||
ThreadLocalUtil.remove();
|
||||
return new Result<>(200, "密码修改成功, 请重新登陆", "");
|
||||
Page<Message> messagePage = new Page<>(page, pageSize);
|
||||
LambdaQueryWrapper<Message> messageLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
// 添加查询条件
|
||||
messageLambdaQueryWrapper.eq(Message::getSendUserId, id);
|
||||
// 排序条件:先按 mark 对应值排序,然后按时间排序
|
||||
messageLambdaQueryWrapper.orderByDesc(Message::getMark) // mark 值大的排前面
|
||||
.orderByDesc(Message::getCreateTime); // 时间新的排前面
|
||||
messageService.page(messagePage, messageLambdaQueryWrapper);
|
||||
return new Result<>(200, "获取成功", messagePage);
|
||||
}
|
||||
|
||||
// 获取个人信息
|
||||
@GetMapping("/info")
|
||||
public Result<User> getInfo() {
|
||||
Long id = GetIdUtil.getId();
|
||||
User user = userService.getById(id);
|
||||
return new Result<>(200, "", user);
|
||||
// 用户确认查看消息
|
||||
@PostMapping("/confirmMessage")
|
||||
public Result<String> confirmMessage(@RequestParam Long id) {
|
||||
Records records = recordsService.getById(id);
|
||||
records.setMark(0);
|
||||
recordsService.save(records);
|
||||
return new Result<>(200, "消息查看成功", "");
|
||||
}
|
||||
|
||||
// 用户获取自己写过的地址
|
||||
@GetMapping("/getAddress")
|
||||
public Result<List<Address>> listAddress() {
|
||||
LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
addressLambdaQueryWrapper.eq(Address::getUserId, GetIdUtil.getId())
|
||||
.orderByDesc(Address::getAddress);
|
||||
List<Address> list = addressService.list(addressLambdaQueryWrapper);
|
||||
return new Result<>(200, "", list);
|
||||
}
|
||||
|
||||
// 用户寄件
|
||||
@PostMapping("/shipment")
|
||||
public Result<String> shipmentExpress(@RequestBody @Validated @NotNull GoodsDTO goodsDTO) {
|
||||
// 判断用户是否存在
|
||||
LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
userLambdaQueryWrapper.eq(User::getUserName, goodsDTO.getGetUserName());
|
||||
User getUser = userService.getOne(userLambdaQueryWrapper);
|
||||
if(getUser == null) {
|
||||
return new Result<>(404, "用户不存在", null);
|
||||
}
|
||||
// 生成快递码
|
||||
String expressCode = ExpressCodeUtil.getExpressCode(goodsDTO.getCompany());
|
||||
// 生成取件码
|
||||
String claimCode = ClaimCodeUtil.getClaimCode(goodsDTO.getCompany());
|
||||
Goods goods = new Goods();
|
||||
goods.setSendUserId(GetIdUtil.getId());
|
||||
goods.setGetUserId(getUser.getId());
|
||||
goods.setExpressCode(expressCode);
|
||||
goods.setName(goodsDTO.getName());
|
||||
goods.setQuality(goodsDTO.getQuality());
|
||||
goodsService.save(goods);
|
||||
// 存进历史记录
|
||||
Records records = new Records();
|
||||
records.setSendUserId(GetIdUtil.getId());
|
||||
records.setGetUserId(getUser.getId());
|
||||
LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
addressLambdaQueryWrapper.eq(Address::getAddress, goodsDTO.getAddress());
|
||||
Address address = addressService.getOne(addressLambdaQueryWrapper);
|
||||
records.setAddresseeId(address.getId());
|
||||
records.setGoodsId(goods.getId());
|
||||
recordsService.save(records);
|
||||
Code code = new Code();
|
||||
code.setClaim(claimCode);
|
||||
code.setGoodsId(goods.getId());
|
||||
codeService.save(code);
|
||||
return new Result<>(200, "寄件成功", "");
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
package jty.expressdistributionsystem.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import jty.expressdistributionsystem.entity.Code;
|
||||
|
||||
public interface CodeMapper extends BaseMapper<Code> {
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package jty.expressdistributionsystem.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import jty.expressdistributionsystem.entity.Records;
|
||||
|
||||
public interface RecordsMapper extends BaseMapper<Records> {
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package jty.expressdistributionsystem.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import jty.expressdistributionsystem.entity.Code;
|
||||
|
||||
public interface CodeService extends IService<Code> {
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package jty.expressdistributionsystem.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import jty.expressdistributionsystem.entity.Records;
|
||||
|
||||
public interface RecordsService extends IService<Records> {
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package jty.expressdistributionsystem.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import jty.expressdistributionsystem.entity.Code;
|
||||
import jty.expressdistributionsystem.mapper.CodeMapper;
|
||||
import jty.expressdistributionsystem.service.CodeService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class CodeServiceImpl extends ServiceImpl<CodeMapper, Code> implements CodeService {
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package jty.expressdistributionsystem.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import jty.expressdistributionsystem.entity.Records;
|
||||
import jty.expressdistributionsystem.mapper.RecordsMapper;
|
||||
import jty.expressdistributionsystem.service.RecordsService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class RecordsServiceImpl extends ServiceImpl<RecordsMapper, Records> implements RecordsService {
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package jty.expressdistributionsystem.utils;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ClaimCodeUtil {
|
||||
public static @NotNull String getClaimCode(String company) {
|
||||
// 获取当前时间
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
||||
String timeStamp = sdf.format(new Date());
|
||||
// 生成唯一标识符
|
||||
String uniqueId = UUID.randomUUID().toString().replace("-", "").substring(0, 6);
|
||||
// 将公司名、时间戳和唯一ID组合成取件码
|
||||
return company.toUpperCase() + timeStamp + uniqueId;
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package jty.expressdistributionsystem.utils;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ExpressCodeUtil {
|
||||
public static @NotNull String getExpressCode(String company) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
||||
String timeStamp = sdf.format(new Date());
|
||||
// 生成唯一标识符
|
||||
String uniqueId = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
|
||||
// 将公司名、时间戳和唯一ID组合成快递码
|
||||
return company.toUpperCase() + timeStamp + uniqueId;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue