Commit 6ffd8c55 authored by 技术部-任文超's avatar 技术部-任文超

从Form重复提交幂等性考虑,解决Jira128(跳过异常返回成功)

parent 0972d902
......@@ -502,17 +502,13 @@ public class InnerController implements IBaseController {
addressObj.setUpdateAt(now);
}
addressObj.setUserId(userId);
// 替换所有,UTF-8编码时4字节的Emoji表情字符
addressObj.setProvince(EmojiUtil.filterUnicode4(province));
addressObj.setProvince(province);
addressObj.setProvinceCode(provinceCode);
// 替换所有,UTF-8编码时4字节的Emoji表情字符
addressObj.setCity(EmojiUtil.filterUnicode4(city));
addressObj.setCity(city);
addressObj.setCityCode(cityCode);
// 替换所有,UTF-8编码时4字节的Emoji表情字符
addressObj.setDistrict(EmojiUtil.filterUnicode4(district));
addressObj.setDistrict(district);
addressObj.setDistrictCode(districtCode);
// 替换所有,UTF-8编码时4字节的Emoji表情字符
addressObj.setAddress(EmojiUtil.filterUnicode4(address));
addressObj.setAddress(address);
addressObj = addressService.save(addressObj);
log.info("保存后地址详情:{}", addressObj);
return JsonResult.buildSuccessResult(null, AddressRet.address2AddressRet(addressObj));
......
......@@ -9,7 +9,6 @@ import cn.quantgroup.xyqb.aspect.limit.PasswordFreeAccessValidator;
import cn.quantgroup.xyqb.entity.*;
import cn.quantgroup.xyqb.model.JsonResult;
import cn.quantgroup.xyqb.service.user.*;
import cn.quantgroup.xyqb.util.EmojiUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import io.swagger.annotations.ApiOperation;
......@@ -121,10 +120,6 @@ public class UserCenterController {
if (null == userId || userId == 0L) {
return JsonResult.buildErrorStateResult("该用户不存在", null);
}
if (StringUtils.isNotBlank(nick)) {
// 替换所有,UTF-8编码时4字节的Emoji表情字符
nick = EmojiUtil.filterUnicode4(nick);
}
UserAttached userAttached = userCenterService.saveUserNick(userId, nick);
return JsonResult.buildSuccessResult(null, userAttached.getNick());
}
......@@ -289,11 +284,10 @@ public class UserCenterController {
}
//查询用户是否有保存地址信息
Address addressInfo = addressService.findByUserId(userId);
Timestamp now = new Timestamp(System.currentTimeMillis());
if (null == addressInfo) {
Timestamp now = new Timestamp(System.currentTimeMillis());
addressInfo = new Address();
addressInfo.setCreatedAt(now);
addressInfo.setUpdateAt(now);
}
addressInfo.setUserId(userId);
addressInfo.setProvince(province);
......@@ -303,12 +297,7 @@ public class UserCenterController {
addressInfo.setDistrictCode(districtCode);
addressInfo.setDistrict(district);
addressInfo.setAddress(address);
if (StringUtils.isNotBlank(address)) {
// 替换所有,UTF-8编码时4字节的Emoji表情字符
addressInfo.setAddress(EmojiUtil.filterUnicode4(address));
}
//更新时间
addressInfo.setUpdateAt(new Timestamp(System.currentTimeMillis()));
addressInfo.setUpdateAt(now);
addressService.save(addressInfo);
return JsonResult.buildSuccessResult(null, addressInfo);
}
......@@ -393,25 +382,6 @@ public class UserCenterController {
return JsonResult.buildSuccessResult(null, userExtInfo);
}
/**
* 创建用户附加信息实体
*
* @param userId
* @param avatar
* @param nick
* @return
*/
private UserAttached createUserAttached(Long userId, String avatar, String nick) {
UserAttached userAttached = new UserAttached();
Timestamp now = new Timestamp(System.currentTimeMillis());
userAttached.setUserId(userId);
userAttached.setAvatar(avatar);
userAttached.setNick(nick);
userAttached.setCreatedAt(now);
userAttached.setUpdatedAt(now);
return userAttached;
}
/**
* 根据手机号查询用户id, nodejs调用接口无法获取到userId.
* 所以增加该接口来查询用户id
......
......@@ -13,7 +13,5 @@ public interface IAddressService {
Address save(Address addressObj);
Address saveAddress(Address address);
List<Address> findByUserIds(List<Long> userIds);
}
......@@ -3,6 +3,7 @@ package cn.quantgroup.xyqb.service.user.impl;
import cn.quantgroup.xyqb.entity.Address;
import cn.quantgroup.xyqb.repository.IAddressRepository;
import cn.quantgroup.xyqb.service.user.IAddressService;
import cn.quantgroup.xyqb.util.EmojiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
......@@ -28,15 +29,14 @@ public class AddressServiceImpl implements IAddressService {
@Override
@CacheEvict(value = "addresscache", key = "'address' + #addressObj.userId", cacheManager = "cacheManager")
public Address save(Address addressObj) {
/* 替换所有,UTF-8编码时4字节的Emoji表情字符 */
addressObj.setProvince(EmojiUtil.filterUnicode4(addressObj.getProvince()));
addressObj.setCity(EmojiUtil.filterUnicode4(addressObj.getCity()));
addressObj.setDistrict(EmojiUtil.filterUnicode4(addressObj.getDistrict()));
addressObj.setAddress(EmojiUtil.filterUnicode4(addressObj.getAddress()));
return addressRepository.save(addressObj);
}
@Override
public Address saveAddress(Address address) {
return addressRepository.save(address);
}
@Override
public List<Address> findByUserIds(List<Long> userIds) {
return addressRepository.findAll((root, query, cb) -> {
......
......@@ -3,7 +3,10 @@ package cn.quantgroup.xyqb.service.user.impl;
import cn.quantgroup.xyqb.entity.UserAttached;
import cn.quantgroup.xyqb.repository.IUserAttachedRepository;
import cn.quantgroup.xyqb.service.user.UserCenterService;
import cn.quantgroup.xyqb.util.EmojiUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
......@@ -16,6 +19,7 @@ import java.util.Optional;
/**
* Created by 11 on 2017/3/22.
*/
@Slf4j
@Service
public class UserCenterServiceImpl implements UserCenterService {
......@@ -51,9 +55,15 @@ public class UserCenterServiceImpl implements UserCenterService {
userAttached = createUserAttached(userId, null, nick);
}
if(!Objects.equals(nick, userAttached.getNick())){
userAttached.setNick(nick);
// 替换所有,UTF-8编码时4字节的Emoji表情字符
userAttached.setNick(EmojiUtil.filterUnicode4(nick));
userAttached.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
userAttached = userAttachedRepository.save(userAttached);
try {
userAttached = userAttachedRepository.save(userAttached);
}catch (ConstraintViolationException e){
// Sql唯一约束异常(诱因是Form重复提交,因为该操作是幂等的,故此不需额外处理,可返回成功)
log.error("Sql约束异常[uni_idx_user_id]重复提交Form是幂等操作,不影响处理结果", e);
}
}
return userAttached;
}
......
......@@ -72,10 +72,8 @@ public class TestUserService {
return Arrays.asList(new Object[][]{{addressObj}});
}
@Test
public void testAddress() {
addressService.save(addressObj);
addressObj=addressService.findByUserId(addressObj.getUserId());
System.out.println("测试地址"+addressObj);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment