|
|
|
@ -0,0 +1,190 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* https://www.mall4j.com/
|
|
|
|
|
*
|
|
|
|
|
* 未经允许,不可做商业用途!
|
|
|
|
|
*
|
|
|
|
|
* 版权所有,侵权必究!
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.yami.shop.api.controller;
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
import com.yami.shop.bean.app.dto.UserAddrDto;
|
|
|
|
|
import com.yami.shop.bean.app.param.AddrParam;
|
|
|
|
|
import com.yami.shop.bean.model.UserAddr;
|
|
|
|
|
import com.yami.shop.common.exception.YamiShopBindException;
|
|
|
|
|
import com.yami.shop.common.response.ServerResponseEntity;
|
|
|
|
|
import com.yami.shop.security.api.util.SecurityUtils;
|
|
|
|
|
import com.yami.shop.service.UserAddrService;
|
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
import jakarta.validation.Valid;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author lanhai
|
|
|
|
|
*/
|
|
|
|
|
@RestController // 标注这是一个控制器类,并且其返回结果直接写入HTTP响应体中,而不是视图名称
|
|
|
|
|
@RequestMapping("/p/address") // 定义请求路径的根地址为/p/address
|
|
|
|
|
@Tag(name = "地址接口") // 给API文档添加标签,描述这个控制器的功能
|
|
|
|
|
@AllArgsConstructor // Lombok注解,用于生成全参构造函数
|
|
|
|
|
public class AddrController {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private UserAddrService userAddrService; // 自动注入用户地址服务类
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 选择订单配送地址
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/list")
|
|
|
|
|
@Operation(summary = "用户地址列表" , description = "获取用户的所有地址信息")
|
|
|
|
|
public ServerResponseEntity<List<UserAddrDto>> dvyList() {
|
|
|
|
|
// 获取当前用户ID
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
// 查询用户的所有地址信息,并按是否常用和更新时间倒序排序
|
|
|
|
|
List<UserAddr> userAddrs = userAddrService.list(
|
|
|
|
|
new LambdaQueryWrapper<UserAddr>()
|
|
|
|
|
.eq(UserAddr::getUserId, userId)
|
|
|
|
|
.orderByDesc(UserAddr::getCommonAddr)
|
|
|
|
|
.orderByDesc(UserAddr::getUpdateTime)
|
|
|
|
|
);
|
|
|
|
|
// 将地址信息转换成DTO对象并返回
|
|
|
|
|
return ServerResponseEntity.success(BeanUtil.copyToList(userAddrs, UserAddrDto.class));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/addAddr")
|
|
|
|
|
@Operation(summary = "新增用户地址" , description = "新增用户地址")
|
|
|
|
|
public ServerResponseEntity<String> addAddr(@Valid @RequestBody AddrParam addrParam) {
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
|
|
|
|
|
// 检查地址是否已存在
|
|
|
|
|
if (addrParam.getAddrId() != null && addrParam.getAddrId() != 0) {
|
|
|
|
|
return ServerResponseEntity.showFailMsg("该地址已存在");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 统计用户已有的地址数量
|
|
|
|
|
long addrCount = userAddrService.count(new LambdaQueryWrapper<UserAddr>().eq(UserAddr::getUserId, userId));
|
|
|
|
|
UserAddr userAddr = BeanUtil.copyProperties(addrParam, UserAddr.class);
|
|
|
|
|
|
|
|
|
|
// 如果没有地址,设为常用地址,否则设为非常用
|
|
|
|
|
if (addrCount == 0) {
|
|
|
|
|
userAddr.setCommonAddr(1);
|
|
|
|
|
} else {
|
|
|
|
|
userAddr.setCommonAddr(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userAddr.setUserId(userId);
|
|
|
|
|
userAddr.setStatus(1); // 设置状态为有效
|
|
|
|
|
userAddr.setCreateTime(new Date()); // 设置创建时间
|
|
|
|
|
userAddr.setUpdateTime(new Date()); // 设置更新时间
|
|
|
|
|
|
|
|
|
|
// 保存地址信息
|
|
|
|
|
userAddrService.save(userAddr);
|
|
|
|
|
|
|
|
|
|
// 如果是常用地址,清除默认地址缓存
|
|
|
|
|
if (userAddr.getCommonAddr() == 1) {
|
|
|
|
|
userAddrService.removeUserAddrByUserId(0L, userId);
|
|
|
|
|
}
|
|
|
|
|
return ServerResponseEntity.success("添加地址成功");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 修改订单配送地址
|
|
|
|
|
*/
|
|
|
|
|
@PutMapping("/updateAddr")
|
|
|
|
|
@Operation(summary = "修改订单用户地址" , description = "修改用户地址")
|
|
|
|
|
public ServerResponseEntity<String> updateAddr(@Valid @RequestBody AddrParam addrParam) {
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
|
|
|
|
|
// 根据用户ID和地址ID获取地址信息
|
|
|
|
|
UserAddr dbUserAddr = userAddrService.getUserAddrByUserId(addrParam.getAddrId(), userId);
|
|
|
|
|
if (dbUserAddr == null) {
|
|
|
|
|
return ServerResponseEntity.showFailMsg("该地址已被删除");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新地址信息
|
|
|
|
|
UserAddr userAddr = BeanUtil.copyProperties(addrParam, UserAddr.class);
|
|
|
|
|
userAddr.setUserId(userId);
|
|
|
|
|
userAddr.setUpdateTime(new Date());
|
|
|
|
|
userAddrService.updateById(userAddr);
|
|
|
|
|
|
|
|
|
|
// 清除缓存
|
|
|
|
|
userAddrService.removeUserAddrByUserId(addrParam.getAddrId(), userId);
|
|
|
|
|
userAddrService.removeUserAddrByUserId(0L, userId);
|
|
|
|
|
|
|
|
|
|
return ServerResponseEntity.success("修改地址成功");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除订单配送地址
|
|
|
|
|
*/
|
|
|
|
|
@DeleteMapping("/deleteAddr/{addrId}")
|
|
|
|
|
@Operation(summary = "删除订单用户地址" , description = "根据地址id,删除用户地址")
|
|
|
|
|
@Parameter(name = "addrId", description = "地址ID" , required = true)
|
|
|
|
|
public ServerResponseEntity<String> deleteDvy(@PathVariable("addrId") Long addrId) {
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
|
|
|
|
|
// 根据用户ID和地址ID获取地址信息
|
|
|
|
|
UserAddr userAddr = userAddrService.getUserAddrByUserId(addrId, userId);
|
|
|
|
|
if (userAddr == null) {
|
|
|
|
|
return ServerResponseEntity.showFailMsg("该地址已被删除");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查是否为默认地址,默认地址无法删除
|
|
|
|
|
if (userAddr.getCommonAddr() == 1) {
|
|
|
|
|
return ServerResponseEntity.showFailMsg("默认地址无法删除");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除地址信息
|
|
|
|
|
userAddrService.removeById(addrId);
|
|
|
|
|
userAddrService.removeUserAddrByUserId(addrId, userId);
|
|
|
|
|
return ServerResponseEntity.success("删除地址成功");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置默认地址
|
|
|
|
|
*/
|
|
|
|
|
@PutMapping("/defaultAddr/{addrId}")
|
|
|
|
|
@Operation(summary = "设置默认地址" , description = "根据地址id,设置默认地址")
|
|
|
|
|
public ServerResponseEntity<String> defaultAddr(@PathVariable("addrId") Long addrId) {
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
|
|
|
|
|
// 更新默认地址
|
|
|
|
|
userAddrService.updateDefaultUserAddr(addrId, userId);
|
|
|
|
|
|
|
|
|
|
// 清除缓存
|
|
|
|
|
userAddrService.removeUserAddrByUserId(0L, userId);
|
|
|
|
|
userAddrService.removeUserAddrByUserId(addrId, userId);
|
|
|
|
|
|
|
|
|
|
return ServerResponseEntity.success("修改地址成功");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取地址信息订单配送地址
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/addrInfo/{addrId}")
|
|
|
|
|
@Operation(summary = "获取地址信息" , description = "根据地址id,获取地址信息")
|
|
|
|
|
@Parameter(name = "addrId", description = "地址ID" , required = true)
|
|
|
|
|
public ServerResponseEntity<UserAddrDto> addrInfo(@PathVariable("addrId") Long addrId) {
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
|
|
|
|
|
// 根据用户ID和地址ID获取地址信息
|
|
|
|
|
UserAddr userAddr = userAddrService.getUserAddrByUserId(addrId, userId);
|
|
|
|
|
if (userAddr == null) {
|
|
|
|
|
throw new YamiShopBindException("该地址已被删除");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 转换为DTO对象并返回
|
|
|
|
|
return ServerResponseEntity.success(BeanUtil.copyProperties(userAddr, UserAddrDto.class));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|