package com.sinata.rest.modular.mall.controller;
|
|
import com.alibaba.fastjson.JSON;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.sinata.rest.common.ApiUtils;
|
import com.sinata.rest.modular.auth.util.ThreadPoolUtil;
|
import com.sinata.rest.modular.mall.controller.body.BodyMallUserAddress;
|
import com.sinata.rest.modular.mall.controller.vo.VoMallUserAddress;
|
import com.sinata.rest.modular.mall.model.MallUserAddress;
|
import com.sinata.rest.modular.mall.service.IMallUserAddressService;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiImplicitParam;
|
import io.swagger.annotations.ApiImplicitParams;
|
import io.swagger.annotations.ApiOperation;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.util.Assert;
|
import org.springframework.util.StringUtils;
|
import org.springframework.validation.annotation.Validated;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.validation.constraints.NotNull;
|
import java.util.Arrays;
|
import java.util.List;
|
|
/**
|
* <p>
|
* 收货地址 前端控制器
|
* </p>
|
*
|
* @author goku
|
* @since 2023-03-18
|
*/
|
@Validated
|
@RestController
|
@RequestMapping("/mallUserAddress")
|
@Api(tags = "用户收货地址接口", description = "用户收货地址")
|
public class MallUserAddressController {
|
|
@Autowired
|
IMallUserAddressService userAddressService;
|
|
@GetMapping("/list")
|
@ApiOperation(value = "获取用户收货地址列表", notes = "用户收货地址列表", response = VoMallUserAddress.class)
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "current", value = "当前页数", defaultValue = "1", dataType = "Int", paramType = "query", required = true),
|
@ApiImplicitParam(name = "size", value = "每页条数", defaultValue = "20", dataType = "Int", paramType = "query", required = true),
|
})
|
public Object addressList(Integer current, Integer size) {
|
// 获取用户ID
|
Integer userId = ThreadPoolUtil.getUserId();
|
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<MallUserAddress>().eq(MallUserAddress::getUserId, userId);
|
|
Page page = new Page<MallUserAddress>(current, size);
|
List list = userAddressService.page(page, wrapper).getRecords();
|
return ApiUtils.returnOK(list);
|
}
|
|
@PostMapping("/add")
|
@ApiOperation(value = "添加/修改用户收货地址", notes = "添加/修改用户收货地址", response = ApiUtils.class)
|
public Object add(@RequestBody BodyMallUserAddress body){
|
if(body.getId() == null) {
|
if (StringUtils.isEmpty(body.getPhone()) || StringUtils.isEmpty(body.getTakeName()) || StringUtils.isEmpty(body.getAddress())) {
|
return ApiUtils.returnNG(null, "收货信息不完善!");
|
}
|
if ("null".equals(body.getPhone()) || "null".equals(body.getTakeName()) || "null".equals(body.getAddress())) {
|
return ApiUtils.returnNG(null, "收货信息错误!");
|
}
|
}
|
|
// 获取用户ID
|
Integer userId = ThreadPoolUtil.getUserId();
|
|
MallUserAddress userAddress = JSON.parseObject(JSON.toJSONString(body), MallUserAddress.class);
|
userAddress.setUserId(userId);
|
|
userAddressService.saveOrUpdate(userAddress);
|
|
if(userAddress.getIsDefault() == true){
|
// 设置当前为默认地址
|
userAddressService.updateDefault(userAddress.getId(), userId);
|
}
|
return ApiUtils.returnOK();
|
}
|
|
@DeleteMapping("/delete")
|
@ApiOperation(value = "删除用户收获地址",notes = "删除用户收获地址", response = ApiUtils.class)
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "ids", value = "地址ID串", defaultValue = "1,2", dataType = "Int", paramType = "query", required = true),
|
})
|
public Object delete(@NotNull String ids) {
|
Assert.notNull(ids, "");
|
|
// 分隔ID
|
String[] idArray = ids.split(",");
|
// 批量删除
|
userAddressService.removeByIds(Arrays.asList(idArray));
|
return ApiUtils.returnOK();
|
}
|
|
@GetMapping("/getDefault")
|
@ApiOperation(value = "获取默认用户收货地址",notes = "获取默认用户收货地址", response = MallUserAddress.class)
|
public Object getDefault() {
|
// 获取用户ID
|
Integer userId = ThreadPoolUtil.getUserId();
|
|
MallUserAddress one = userAddressService.getOne(
|
new LambdaQueryWrapper<MallUserAddress>()
|
.eq(MallUserAddress::getUserId, userId)
|
.eq(MallUserAddress::getIsDefault, 1)
|
);
|
return ApiUtils.returnOK(one);
|
}
|
|
}
|