/**
* 修改默认地址
* @param userId
* @param addressId
*/
public void updateUserAddressToBeDefault(String userId, String addressId);
/**
* @Desc: 是否 枚举
*/
public enum YesOrNo {
NO(0, "否"),
YES(1, "是");
public final Integer type;
public final String value;
YesOrNo(Integer type, String value) {
this.type = type;
this.value = value;
}
}
@Transactional(propagation = Propagation.REQUIRED)
@Override
public void updateUserAddressToBeDefault(String userId, String addressId) {
// 1. 查找默认地址,设置为不默认
UserAddress queryAddress = new UserAddress();
queryAddress.setUserId(userId);
queryAddress.setIsDefault(YesOrNo.YES.type);
List<UserAddress> list = userAddressMapper.select(queryAddress);
for (UserAddress ua : list) {
ua.setIsDefault(YesOrNo.NO.type);
userAddressMapper.updateByPrimaryKeySelective(ua);
}
// 2. 根据地址id修改为默认的地址
UserAddress defaultAddress = new UserAddress();
defaultAddress.setId(addressId);
defaultAddress.setUserId(userId);
defaultAddress.setIsDefault(YesOrNo.YES.type);
userAddressMapper.updateByPrimaryKeySelective(defaultAddress);
}
@ApiOperation(value = "用户设置默认地址", notes = "用户设置默认地址", httpMethod = "POST")
@PostMapping("/setDefalut")
public JSONResult setDefalut(
@RequestParam String userId,
@RequestParam String addressId) {
if (StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)) {
return JSONResult.errorMsg("");
}
addressService.updateUserAddressToBeDefault(userId, addressId);
return JSONResult.ok();
}