Commit b95e4ca7 authored by Junling Bu's avatar Junling Bu
Browse files

重大调整:除了订单更新采用乐观锁更新,其他表是普通更新

parent e4007338
......@@ -80,7 +80,7 @@ public class LitemallKeywordService {
}
public int updateById(LitemallKeyword keywords) {
return keywordsMapper.updateWithVersionByPrimaryKeySelective(keywords.getVersion(), keywords);
return keywordsMapper.updateByPrimaryKeySelective(keywords);
}
public void deleteById(Integer id) {
......
......@@ -25,12 +25,6 @@ public class LitemallOrderService {
return orderMapper.insertSelective(order);
}
public List<LitemallOrder> query(Integer userId) {
LitemallOrderExample example = new LitemallOrderExample();
example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
return orderMapper.selectByExample(example);
}
public int count(Integer userId) {
LitemallOrderExample example = new LitemallOrderExample();
example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
......@@ -52,12 +46,6 @@ public class LitemallOrderService {
return sb.toString();
}
public LitemallOrder queryByOrderSn(Integer userId, String orderSn){
LitemallOrderExample example = new LitemallOrderExample();
example.or().andUserIdEqualTo(userId).andOrderSnEqualTo(orderSn).andDeletedEqualTo(false);
return orderMapper.selectOneByExample(example);
}
public int countByOrderSn(Integer userId, String orderSn){
LitemallOrderExample example = new LitemallOrderExample();
example.or().andUserIdEqualTo(userId).andOrderSnEqualTo(orderSn).andDeletedEqualTo(false);
......@@ -136,8 +124,8 @@ public class LitemallOrderService {
return (int)orderMapper.countByExample(example);
}
public int updateById(LitemallOrder order) {
return orderMapper.updateWithVersionByPrimaryKeySelective(order.getVersion(), order);
public int updateByIdWithVersion(Integer version, LitemallOrder order) {
return orderMapper.updateWithVersionByPrimaryKeySelective(version, order);
}
public void deleteById(Integer id) {
......
......@@ -25,7 +25,7 @@ public class LitemallProductService {
}
public int updateById(LitemallProduct product) {
return productMapper.updateWithVersionByPrimaryKeySelective(product.getVersion(), product);
return productMapper.updateByPrimaryKeySelective(product);
}
public void deleteById(Integer id) {
......
......@@ -32,7 +32,7 @@ public class LitemallStorageService {
}
public int update(LitemallStorage storageInfo) {
return storageMapper.updateWithVersionByPrimaryKeySelective(storageInfo.getVersion(), storageInfo);
return storageMapper.updateByPrimaryKeySelective(storageInfo);
}
public LitemallStorage findById(Integer id) {
......
......@@ -94,7 +94,7 @@ public class LitemallTopicService {
public int updateById(LitemallTopic topic) {
LitemallTopicExample example = new LitemallTopicExample();
example.or().andIdEqualTo(topic.getId());
return topicMapper.updateWithVersionByExampleWithBLOBs(topic.getVersion(), topic, example);
return topicMapper.updateByExampleWithBLOBs(topic, example);
}
public void deleteById(Integer id) {
......
......@@ -35,7 +35,7 @@ public class LitemallUserFormIdService {
//更新或者删除缓存
if (userFormid.getIsprepay() && userFormid.getUseamount() > 1) {
userFormid.setUseamount(userFormid.getUseamount() - 1);
return formidMapper.updateWithVersionByPrimaryKey(userFormid.getVersion(), userFormid);
return formidMapper.updateByPrimaryKey(userFormid);
} else {
return formidMapper.deleteByPrimaryKey(userFormid.getId());
}
......
......@@ -40,7 +40,7 @@ public class LitemallUserService {
}
public int updateById(LitemallUser user) {
return userMapper.updateWithVersionByPrimaryKeySelective(user.getVersion(), user);
return userMapper.updateByPrimaryKeySelective(user);
}
public List<LitemallUser> querySelective(String username, String mobile, Integer page, Integer size, String sort, String order) {
......
......@@ -167,7 +167,7 @@ public class WxAuthController {
user.setLastLoginTime(LocalDateTime.now());
user.setLastLoginIp(IpUtil.client(request));
if(userService.updateById(user) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
}
......@@ -336,7 +336,7 @@ public class WxAuthController {
user.setPassword(encodedPassword);
if(userService.updateById(user) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok();
......@@ -352,7 +352,7 @@ public class WxAuthController {
LitemallUser user = userService.findById(userId);
user.setMobile(phone);
if(userService.updateById(user) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok();
}
......
......@@ -151,7 +151,7 @@ public class WxCartController {
}
existCart.setNumber((short) num);
if(cartService.updateById(existCart) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
}
......@@ -224,7 +224,7 @@ public class WxCartController {
}
existCart.setNumber((short) num);
if(cartService.updateById(existCart) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
}
......@@ -286,7 +286,7 @@ public class WxCartController {
existCart.setNumber(number.shortValue());
if(cartService.updateById(existCart) == 0){
return ResponseUtil.updatedDateExpired();
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok();
}
......
......@@ -499,6 +499,8 @@ public class WxOrderController {
return ResponseUtil.badArgumentValue();
}
Integer version = order.getVersion();
// 检测是否能够取消
OrderHandleOption handleOption = OrderUtil.build(order);
if (!handleOption.isCancel()) {
......@@ -513,7 +515,7 @@ public class WxOrderController {
// 设置订单已取消状态
order.setOrderStatus(OrderUtil.STATUS_CANCEL);
order.setEndTime(LocalDateTime.now());
if(orderService.updateById(order) == 0){
if(orderService.updateByIdWithVersion(version, order) == 0){
throw new Exception("更新数据已失效");
}
......@@ -569,6 +571,8 @@ public class WxOrderController {
return ResponseUtil.badArgumentValue();
}
Integer version = order.getVersion();
// 检测是否能够取消
OrderHandleOption handleOption = OrderUtil.build(order);
if (!handleOption.isPay()) {
......@@ -611,7 +615,7 @@ public class WxOrderController {
return ResponseUtil.fail(403, "订单不能支付");
}
if(orderService.updateById(order) == 0){
if(orderService.updateByIdWithVersion(version, order) == 0){
return ResponseUtil.updatedDateExpired();
}
return ResponseUtil.ok(result);
......@@ -662,6 +666,8 @@ public class WxOrderController {
return WxPayNotifyResponse.fail("订单不存在 sn=" + orderSn);
}
Integer version = order.getVersion();
// 检查这个订单是否已经处理过
if (OrderUtil.isPayStatus(order) && order.getPayId() != null) {
return WxPayNotifyResponse.success("订单已经处理成功!");
......@@ -675,18 +681,19 @@ public class WxOrderController {
order.setPayId(payId);
order.setPayTime(LocalDateTime.now());
order.setOrderStatus(OrderUtil.STATUS_PAY);
if (orderService.updateById(order) == 0) {
if (orderService.updateByIdWithVersion(version, order) == 0) {
// 这里可能存在这样一个问题,用户支付和系统自动取消订单发生在同时
// 如果数据库首先因为系统自动取消订单而更新了订单状态;
// 此时用户支付完成回调这里也要更新数据库,而由于乐观锁机制这里的更新会失败
// 因此,这里会重新读取数据库检查状态是否是订单自动取消,如果是则更新成支付状态。
order = orderService.findBySn(orderSn);
version = order.getVersion();
int updated = 0;
if(OrderUtil.isAutoCancelStatus(order)){
order.setPayId(payId);
order.setPayTime(LocalDateTime.now());
order.setOrderStatus(OrderUtil.STATUS_PAY);
updated = orderService.updateById(order);
updated = orderService.updateByIdWithVersion(version, order);
}
// 如果updated是0,那么数据库更新失败
......@@ -761,6 +768,8 @@ public class WxOrderController {
return ResponseUtil.badArgumentValue();
}
Integer version = order.getVersion();
OrderHandleOption handleOption = OrderUtil.build(order);
if (!handleOption.isRefund()) {
return ResponseUtil.fail(403, "订单不能取消");
......@@ -768,7 +777,7 @@ public class WxOrderController {
// 设置订单申请退款状态
order.setOrderStatus(OrderUtil.STATUS_REFUND);
if(orderService.updateById(order) == 0){
if(orderService.updateByIdWithVersion(version, order) == 0){
return ResponseUtil.updatedDateExpired();
}
......@@ -808,6 +817,8 @@ public class WxOrderController {
return ResponseUtil.badArgumentValue();
}
Integer version = order.getVersion();
OrderHandleOption handleOption = OrderUtil.build(order);
if (!handleOption.isConfirm()) {
return ResponseUtil.fail(403, "订单不能确认收货");
......@@ -815,7 +826,7 @@ public class WxOrderController {
order.setOrderStatus(OrderUtil.STATUS_CONFIRM);
order.setConfirmTime(LocalDateTime.now());
if(orderService.updateById(order) == 0){
if(orderService.updateByIdWithVersion(version, order) == 0){
return ResponseUtil.updatedDateExpired();
}
return ResponseUtil.ok();
......
Supports Markdown
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