17 changed files with 413 additions and 157 deletions
@ -0,0 +1,102 @@ |
|||||
|
package cc.hiver.mall.controller; |
||||
|
|
||||
|
import cc.hiver.core.common.utils.ResultUtil; |
||||
|
import cc.hiver.core.common.vo.Result; |
||||
|
import cc.hiver.mall.entity.UserAddress; |
||||
|
import cc.hiver.mall.pojo.vo.UserAddressVo; |
||||
|
import cc.hiver.mall.service.mybatis.UserAddressService; |
||||
|
import io.swagger.annotations.Api; |
||||
|
import io.swagger.annotations.ApiOperation; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMethod; |
||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
@Slf4j |
||||
|
@RestController |
||||
|
@Api(tags = "用户收货地址接口") |
||||
|
@RequestMapping(value = "/hiver/app/userAddress/") |
||||
|
public class UserAddressController { |
||||
|
|
||||
|
@Autowired |
||||
|
private UserAddressService userAddressService; |
||||
|
|
||||
|
@RequestMapping(value = "/save", method = RequestMethod.POST) |
||||
|
@ApiOperation(value = "新增用户收货地址") |
||||
|
public Result save(UserAddress userAddress) { |
||||
|
|
||||
|
// 如果是第一条地址或者传过来就是默认,设置默认逻辑可在这里增加
|
||||
|
// 为了安全起见这里直接调用 save,不处理复杂逻辑
|
||||
|
boolean result = userAddressService.save(userAddress); |
||||
|
if (result) { |
||||
|
// 如果传过来的就是默认地址,可以调用把其他的设置为非默认
|
||||
|
if (userAddress.getIsDefault() != null && userAddress.getIsDefault() == 1) { |
||||
|
userAddressService.setDefaultAddress(userAddress.getId(), userAddress.getUserId()); |
||||
|
} |
||||
|
return ResultUtil.success("添加成功"); |
||||
|
} else { |
||||
|
return ResultUtil.error("添加失败"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@RequestMapping(value = "/edit", method = RequestMethod.POST) |
||||
|
@ApiOperation(value = "编辑用户收货地址") |
||||
|
public Result edit(UserAddress userAddress) { |
||||
|
if (StringUtils.isBlank(userAddress.getId())) { |
||||
|
return ResultUtil.error("地址ID不能为空"); |
||||
|
} |
||||
|
// 不能修改别人的地址
|
||||
|
UserAddress old = userAddressService.getById(userAddress.getId()); |
||||
|
|
||||
|
boolean result = userAddressService.updateById(userAddress); |
||||
|
if (result) { |
||||
|
// 如果改成了默认地址,则更新其他为非默认
|
||||
|
if (userAddress.getIsDefault() != null && userAddress.getIsDefault() == 1) { |
||||
|
userAddressService.setDefaultAddress(userAddress.getId(), old.getUserId()); |
||||
|
} |
||||
|
return ResultUtil.success("修改成功"); |
||||
|
} else { |
||||
|
return ResultUtil.error("修改失败"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@RequestMapping(value = "/delById", method = RequestMethod.POST) |
||||
|
@ApiOperation(value = "删除用户收货地址") |
||||
|
public Result delById(@RequestParam("id")String id) { |
||||
|
if (StringUtils.isBlank(id)) { |
||||
|
return ResultUtil.error("地址ID不能为空"); |
||||
|
} |
||||
|
boolean result = userAddressService.removeById(id); |
||||
|
if (result) { |
||||
|
return ResultUtil.success("删除成功"); |
||||
|
} else { |
||||
|
return ResultUtil.error("删除失败"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@RequestMapping(value = "/list", method = RequestMethod.GET) |
||||
|
@ApiOperation(value = "获取当前用户的收货地址列表") |
||||
|
public Result list(@RequestParam("userId") String userId) { |
||||
|
if (userId == null || StringUtils.isEmpty(userId)) { |
||||
|
return ResultUtil.error("未获取到当前登录用户信息"); |
||||
|
} |
||||
|
List<UserAddressVo> list = userAddressService.getAddressListByUserId(userId); |
||||
|
return new ResultUtil<List<UserAddressVo>>().setData(list); |
||||
|
} |
||||
|
|
||||
|
@RequestMapping(value = "/setDefault", method = RequestMethod.POST) |
||||
|
@ApiOperation(value = "设置默认收货地址") |
||||
|
public Result setDefault(@RequestParam("id") String id,@RequestParam("userId") String userId) { |
||||
|
if (StringUtils.isBlank(id)) { |
||||
|
return ResultUtil.error("地址ID不能为空"); |
||||
|
} |
||||
|
userAddressService.setDefaultAddress(id, userId); |
||||
|
return ResultUtil.success("设置成功"); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
package cc.hiver.mall.dao.mapper; |
||||
|
|
||||
|
import cc.hiver.mall.entity.UserAddress; |
||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
|
import org.springframework.stereotype.Repository; |
||||
|
|
||||
|
/** |
||||
|
* 用户收货地址 Mapper 接口 |
||||
|
*/ |
||||
|
@Repository |
||||
|
public interface UserAddressMapper extends BaseMapper<UserAddress> { |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,47 @@ |
|||||
|
package cc.hiver.mall.entity; |
||||
|
|
||||
|
import cc.hiver.core.base.HiverBaseEntity; |
||||
|
import com.baomidou.mybatisplus.annotation.TableName; |
||||
|
import io.swagger.annotations.ApiModel; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.Data; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
|
||||
|
@Data |
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
@ApiModel(value = "用户收货地址") |
||||
|
@TableName(value = "t_user_address", autoResultMap = true) |
||||
|
public class UserAddress extends HiverBaseEntity { |
||||
|
|
||||
|
private static final long serialVersionUID = 1L; |
||||
|
|
||||
|
@ApiModelProperty(value = "用户ID/会员ID") |
||||
|
private String userId; |
||||
|
|
||||
|
@ApiModelProperty(value = "收货人姓名") |
||||
|
private String receiverName; |
||||
|
|
||||
|
@ApiModelProperty(value = "收货人电话") |
||||
|
private String receiverPhone; |
||||
|
|
||||
|
@ApiModelProperty(value = "性别 (0未知 1男 2女)") |
||||
|
private Integer sex; |
||||
|
|
||||
|
@ApiModelProperty(value = "楼座ID (对应数据字典, 可匹配配送员)") |
||||
|
private String areaId; |
||||
|
|
||||
|
@ApiModelProperty(value = "楼层") |
||||
|
private String floor; |
||||
|
|
||||
|
@ApiModelProperty(value = "具体门牌号") |
||||
|
private String roomNum; |
||||
|
|
||||
|
@ApiModelProperty(value = "地址标签 (如:宿舍、实验室、教学楼)") |
||||
|
private String addressTag; |
||||
|
|
||||
|
@ApiModelProperty(value = "区域名称") |
||||
|
private String areaName; |
||||
|
|
||||
|
@ApiModelProperty(value = "是否默认地址 (0否 1是)") |
||||
|
private Integer isDefault; |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
package cc.hiver.mall.pojo.vo; |
||||
|
|
||||
|
import cc.hiver.mall.entity.UserAddress; |
||||
|
import io.swagger.annotations.ApiModel; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.Data; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
|
||||
|
@Data |
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
@ApiModel(value = "用户收货地址VO") |
||||
|
public class UserAddressVo extends UserAddress { |
||||
|
|
||||
|
private static final long serialVersionUID = 1L; |
||||
|
|
||||
|
@ApiModelProperty(value = "楼座名称(供前端展示)") |
||||
|
private String areaName; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
package cc.hiver.mall.pojo.vo; |
||||
|
|
||||
|
import cc.hiver.mall.entity.WorkerRelaPrice; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.Data; |
||||
|
import lombok.experimental.Accessors; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
import java.math.BigDecimal; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* 兼职信息 |
||||
|
* @author 王富康 |
||||
|
*/ |
||||
|
@Data |
||||
|
@Accessors(chain = true) |
||||
|
public class WorkerRealPriceVo implements Serializable { |
||||
|
|
||||
|
private static final long serialVersionUID = 1L; |
||||
|
|
||||
|
@ApiModelProperty(value = " 配送规则") |
||||
|
private String userId; |
||||
|
|
||||
|
@ApiModelProperty(value = " 配送规则") |
||||
|
private String workerName; |
||||
|
|
||||
|
@ApiModelProperty(value = " 配送规则") |
||||
|
private String cardPicture; |
||||
|
|
||||
|
@ApiModelProperty(value = " 配送规则") |
||||
|
private BigDecimal highFloorFee; |
||||
|
|
||||
|
@ApiModelProperty(value = " 配送规则") |
||||
|
private List<WorkerRelaPrice> workerRelaPriceList; |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
package cc.hiver.mall.service.mybatis; |
||||
|
|
||||
|
import cc.hiver.mall.entity.UserAddress; |
||||
|
import cc.hiver.mall.pojo.vo.UserAddressVo; |
||||
|
import com.baomidou.mybatisplus.extension.service.IService; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* 用户收货地址 服务类 |
||||
|
*/ |
||||
|
public interface UserAddressService extends IService<UserAddress> { |
||||
|
|
||||
|
/** |
||||
|
* 查询用户收货地址列表 |
||||
|
* @param userId 用户ID |
||||
|
* @return 地址列表 |
||||
|
*/ |
||||
|
List<UserAddressVo> getAddressListByUserId(String userId); |
||||
|
|
||||
|
/** |
||||
|
* 设置默认地址 |
||||
|
* @param id 地址ID |
||||
|
* @param userId 用户ID |
||||
|
*/ |
||||
|
void setDefaultAddress(String id, String userId); |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,61 @@ |
|||||
|
package cc.hiver.mall.serviceimpl.mybatis; |
||||
|
|
||||
|
import cc.hiver.mall.dao.mapper.UserAddressMapper; |
||||
|
import cc.hiver.mall.entity.UserAddress; |
||||
|
import cc.hiver.mall.pojo.vo.UserAddressVo; |
||||
|
import cc.hiver.mall.service.mybatis.UserAddressService; |
||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
|
import org.springframework.beans.BeanUtils; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
/** |
||||
|
* 用户收货地址 服务实现类 |
||||
|
*/ |
||||
|
@Service |
||||
|
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService { |
||||
|
|
||||
|
@Override |
||||
|
public List<UserAddressVo> getAddressListByUserId(String userId) { |
||||
|
QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>(); |
||||
|
queryWrapper.eq("user_id", userId); |
||||
|
// 按默认地址排序(1在前面),然后再按创建时间降序
|
||||
|
queryWrapper.orderByDesc("is_default"); |
||||
|
queryWrapper.orderByDesc("create_time"); |
||||
|
|
||||
|
List<UserAddress> list = this.list(queryWrapper); |
||||
|
if (list == null || list.isEmpty()) { |
||||
|
return new ArrayList<>(); |
||||
|
} |
||||
|
|
||||
|
return list.stream().map(address -> { |
||||
|
UserAddressVo vo = new UserAddressVo(); |
||||
|
BeanUtils.copyProperties(address, vo); |
||||
|
// 这里可以补充查询对应的楼座名称(字典值) 封装到 vo.setAreaName(...)
|
||||
|
return vo; |
||||
|
}).collect(Collectors.toList()); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
@Transactional(rollbackFor = Exception.class) |
||||
|
public void setDefaultAddress(String id, String userId) { |
||||
|
// 先将该用户的所有地址设置为非默认
|
||||
|
UpdateWrapper<UserAddress> updateWrapper = new UpdateWrapper<>(); |
||||
|
updateWrapper.eq("user_id", userId); |
||||
|
updateWrapper.set("is_default", 0); |
||||
|
this.update(updateWrapper); |
||||
|
|
||||
|
// 再将指定的地址设置为默认
|
||||
|
UpdateWrapper<UserAddress> defaultWrapper = new UpdateWrapper<>(); |
||||
|
defaultWrapper.eq("id", id); |
||||
|
defaultWrapper.eq("user_id", userId); // 增加安全校验,防止修改别人的地址
|
||||
|
defaultWrapper.set("is_default", 1); |
||||
|
this.update(defaultWrapper); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
||||
|
<mapper namespace="cc.hiver.mall.dao.mapper.UserAddressMapper"> |
||||
|
|
||||
|
<resultMap id="BaseResultMap" type="cc.hiver.mall.entity.UserAddress"> |
||||
|
<id column="id" jdbcType="VARCHAR" property="id" /> |
||||
|
<result column="user_id" jdbcType="VARCHAR" property="userId" /> |
||||
|
<result column="receiver_name" jdbcType="VARCHAR" property="receiverName" /> |
||||
|
<result column="receiver_phone" jdbcType="VARCHAR" property="receiverPhone" /> |
||||
|
<result column="sex" jdbcType="INTEGER" property="sex" /> |
||||
|
<result column="area_id" jdbcType="VARCHAR" property="areaId" /> |
||||
|
<result column="floor" jdbcType="VARCHAR" property="floor" /> |
||||
|
<result column="room_num" jdbcType="VARCHAR" property="roomNum" /> |
||||
|
<result column="address_tag" jdbcType="VARCHAR" property="addressTag" /> |
||||
|
<result column="is_default" jdbcType="INTEGER" property="isDefault" /> |
||||
|
|
||||
|
<result column="create_by" jdbcType="VARCHAR" property="createBy" /> |
||||
|
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> |
||||
|
<result column="update_by" jdbcType="VARCHAR" property="updateBy" /> |
||||
|
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" /> |
||||
|
<result column="del_flag" jdbcType="INTEGER" property="delFlag" /> |
||||
|
</resultMap> |
||||
|
|
||||
|
<sql id="Base_Column_List"> |
||||
|
id, user_id, receiver_name, receiver_phone, sex, area_id, floor, room_num, address_tag, is_default, |
||||
|
create_by, create_time, update_by, update_time, del_flag |
||||
|
</sql> |
||||
|
|
||||
|
</mapper> |
||||
Loading…
Reference in new issue