Commit 5920f9b1 authored by Junling Bu's avatar Junling Bu
Browse files

采用乐观锁更新数据,需要处理更新失败的情况。

parent 4d1ec91b
...@@ -72,7 +72,11 @@ public class AdminAdController { ...@@ -72,7 +72,11 @@ public class AdminAdController {
if(adminId == null){ if(adminId == null){
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
adService.updateById(ad);
if(adService.updateById(ad) == 0){
return ResponseUtil.updatedDateExpired();
}
return ResponseUtil.ok(ad); return ResponseUtil.ok(ad);
} }
......
...@@ -124,7 +124,10 @@ public class AdminAdminController { ...@@ -124,7 +124,10 @@ public class AdminAdminController {
String encodedPassword = encoder.encode(rawPassword); String encodedPassword = encoder.encode(rawPassword);
admin.setPassword(encodedPassword); admin.setPassword(encodedPassword);
adminService.updateById(admin); if(adminService.updateById(admin) == 0){
return ResponseUtil.updatedDateExpired();
}
return ResponseUtil.ok(admin); return ResponseUtil.ok(admin);
} }
......
...@@ -72,7 +72,9 @@ public class AdminBrandController { ...@@ -72,7 +72,9 @@ public class AdminBrandController {
if(adminId == null){ if(adminId == null){
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
brandService.updateById(brand); if(brandService.updateById(brand) == 0){
return ResponseUtil.updatedDateExpired();
}
return ResponseUtil.ok(brand); return ResponseUtil.ok(brand);
} }
......
...@@ -74,7 +74,9 @@ public class AdminCategoryController { ...@@ -74,7 +74,9 @@ public class AdminCategoryController {
if(adminId == null){ if(adminId == null){
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
categoryService.updateById(category); if(categoryService.updateById(category) == 0){
return ResponseUtil.updatedDateExpired();
}
return ResponseUtil.ok(); return ResponseUtil.ok();
} }
......
...@@ -101,7 +101,9 @@ public class AdminGoodsController { ...@@ -101,7 +101,9 @@ public class AdminGoodsController {
goods.setShareUrl(url); goods.setShareUrl(url);
// 商品基本信息表litemall_goods // 商品基本信息表litemall_goods
goodsService.updateById(goods); if(goodsService.updateById(goods) == 0){
throw new Exception("跟新数据已失效");
}
Integer gid = goods.getId(); Integer gid = goods.getId();
specificationService.deleteByGid(gid); specificationService.deleteByGid(gid);
...@@ -109,12 +111,10 @@ public class AdminGoodsController { ...@@ -109,12 +111,10 @@ public class AdminGoodsController {
productService.deleteByGid(gid); productService.deleteByGid(gid);
// 商品规格表litemall_goods_specification // 商品规格表litemall_goods_specification
Map<String, Integer> specIds = new HashMap<>();
for (LitemallGoodsSpecification specification : specifications) { for (LitemallGoodsSpecification specification : specifications) {
specification.setGoodsId(goods.getId()); specification.setGoodsId(goods.getId());
specification.setAddTime(LocalDateTime.now()); specification.setAddTime(LocalDateTime.now());
specificationService.add(specification); specificationService.add(specification);
specIds.put(specification.getValue(), specification.getId());
} }
// 商品参数表litemall_goods_attribute // 商品参数表litemall_goods_attribute
...@@ -196,15 +196,15 @@ public class AdminGoodsController { ...@@ -196,15 +196,15 @@ public class AdminGoodsController {
//将生成的分享图片地址写入数据库 //将生成的分享图片地址写入数据库
String url = qCodeService.createGoodShareImage(goods.getId().toString(), goods.getPicUrl(), goods.getName()); String url = qCodeService.createGoodShareImage(goods.getId().toString(), goods.getPicUrl(), goods.getName());
goods.setShareUrl(url); goods.setShareUrl(url);
goodsService.updateById(goods); if(goodsService.updateById(goods) == 0){
throw new Exception("跟新数据已失效");
}
// 商品规格表litemall_goods_specification // 商品规格表litemall_goods_specification
Map<String, Integer> specIds = new HashMap<>();
for (LitemallGoodsSpecification specification : specifications) { for (LitemallGoodsSpecification specification : specifications) {
specification.setGoodsId(goods.getId()); specification.setGoodsId(goods.getId());
specification.setAddTime(LocalDateTime.now()); specification.setAddTime(LocalDateTime.now());
specificationService.add(specification); specificationService.add(specification);
specIds.put(specification.getValue(), specification.getId());
} }
// 商品参数表litemall_goods_attribute // 商品参数表litemall_goods_attribute
......
...@@ -126,7 +126,9 @@ public class AdminGrouponController { ...@@ -126,7 +126,9 @@ public class AdminGrouponController {
grouponRules.setExpireTime(expireTime); grouponRules.setExpireTime(expireTime);
grouponRules.setPicUrl(goods.getPicUrl()); grouponRules.setPicUrl(goods.getPicUrl());
rulesService.update(grouponRules); if(rulesService.updateById(grouponRules) == 0){
return ResponseUtil.updatedDateExpired();
}
return ResponseUtil.ok(); return ResponseUtil.ok();
} }
......
...@@ -72,7 +72,10 @@ public class AdminIssueController { ...@@ -72,7 +72,10 @@ public class AdminIssueController {
if(adminId == null){ if(adminId == null){
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
issueService.updateById(issue); if(issueService.updateById(issue) == 0){
return ResponseUtil.updatedDateExpired();
}
return ResponseUtil.ok(issue); return ResponseUtil.ok(issue);
} }
......
...@@ -72,7 +72,9 @@ public class AdminKeywordController { ...@@ -72,7 +72,9 @@ public class AdminKeywordController {
if(adminId == null){ if(adminId == null){
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
keywordService.updateById(keywords); if(keywordService.updateById(keywords) == 0){
return ResponseUtil.updatedDateExpired();
}
return ResponseUtil.ok(keywords); return ResponseUtil.ok(keywords);
} }
......
...@@ -134,7 +134,9 @@ public class AdminOrderController { ...@@ -134,7 +134,9 @@ public class AdminOrderController {
try { try {
// 设置订单取消状态 // 设置订单取消状态
order.setOrderStatus(OrderUtil.STATUS_REFUND_CONFIRM); order.setOrderStatus(OrderUtil.STATUS_REFUND_CONFIRM);
orderService.updateById(order); if(orderService.updateById(order) == 0) {
throw new Exception("跟新数据已失效");
}
// 商品货品数量增加 // 商品货品数量增加
List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId); List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId);
...@@ -143,7 +145,9 @@ public class AdminOrderController { ...@@ -143,7 +145,9 @@ public class AdminOrderController {
LitemallProduct product = productService.findById(productId); LitemallProduct product = productService.findById(productId);
Integer number = product.getNumber() + orderGoods.getNumber(); Integer number = product.getNumber() + orderGoods.getNumber();
product.setNumber(number); product.setNumber(number);
productService.updateById(product); if(productService.updateById(product) == 0){
throw new Exception("跟新数据已失效");
}
} }
} catch (Exception ex) { } catch (Exception ex) {
txManager.rollback(status); txManager.rollback(status);
...@@ -204,15 +208,13 @@ public class AdminOrderController { ...@@ -204,15 +208,13 @@ public class AdminOrderController {
order.setShipSn(shipSn); order.setShipSn(shipSn);
order.setShipChannel(shipChannel); order.setShipChannel(shipChannel);
order.setShipTime(LocalDateTime.now()); order.setShipTime(LocalDateTime.now());
orderService.updateById(order); if(orderService.updateById(order) == 0){
return ResponseUtil.updatedDateExpired();
}
//TODO 发送邮件和短信通知,这里采用异步发送 //TODO 发送邮件和短信通知,这里采用异步发送
// 发货会发送通知短信给用户 // 发货会发送通知短信给用户: *
/** // "您的订单已经发货,快递公司 {1},快递单 {2} ,请注意查收"
*
* 您的订单已经发货,快递公司 {1},快递单 {2} ,请注意查收
*
*/
notifyService.notifySmsTemplate(order.getMobile(), NotifyType.SHIP, new String[]{shipChannel, shipSn}); notifyService.notifySmsTemplate(order.getMobile(), NotifyType.SHIP, new String[]{shipChannel, shipSn});
return ResponseUtil.ok(); return ResponseUtil.ok();
...@@ -230,7 +232,7 @@ public class AdminOrderController { ...@@ -230,7 +232,7 @@ public class AdminOrderController {
*/ */
@Scheduled(fixedDelay = 30 * 60 * 1000) @Scheduled(fixedDelay = 30 * 60 * 1000)
public void checkOrderUnpaid() { public void checkOrderUnpaid() {
logger.debug(LocalDateTime.now()); logger.info("系统开启任务检查订单是否已经超期自动取消订单");
List<LitemallOrder> orderList = orderService.queryUnpaid(); List<LitemallOrder> orderList = orderService.queryUnpaid();
for (LitemallOrder order : orderList) { for (LitemallOrder order : orderList) {
...@@ -249,7 +251,9 @@ public class AdminOrderController { ...@@ -249,7 +251,9 @@ public class AdminOrderController {
// 设置订单已取消状态 // 设置订单已取消状态
order.setOrderStatus(OrderUtil.STATUS_AUTO_CANCEL); order.setOrderStatus(OrderUtil.STATUS_AUTO_CANCEL);
order.setEndTime(LocalDateTime.now()); order.setEndTime(LocalDateTime.now());
orderService.updateById(order); if(orderService.updateById(order) == 0){
throw new Exception("跟新数据已失效");
}
// 商品货品数量增加 // 商品货品数量增加
Integer orderId = order.getId(); Integer orderId = order.getId();
...@@ -259,13 +263,17 @@ public class AdminOrderController { ...@@ -259,13 +263,17 @@ public class AdminOrderController {
LitemallProduct product = productService.findById(productId); LitemallProduct product = productService.findById(productId);
Integer number = product.getNumber() + orderGoods.getNumber(); Integer number = product.getNumber() + orderGoods.getNumber();
product.setNumber(number); product.setNumber(number);
productService.updateById(product); if(productService.updateById(product) == 0){
throw new Exception("跟新数据已失效");
}
} }
} catch (Exception ex) { } catch (Exception ex) {
txManager.rollback(status); txManager.rollback(status);
logger.error("系统内部错误", ex); logger.info("订单 ID=" + order.getId() + " 数据已经更新,放弃自动确认收货");
return;
} }
txManager.commit(status); txManager.commit(status);
logger.info("订单 ID=" + order.getId() + " 已经超期自动取消订单");
} }
} }
...@@ -288,7 +296,7 @@ public class AdminOrderController { ...@@ -288,7 +296,7 @@ public class AdminOrderController {
*/ */
@Scheduled(cron = "0 0 3 * * ?") @Scheduled(cron = "0 0 3 * * ?")
public void checkOrderUnconfirm() { public void checkOrderUnconfirm() {
logger.debug(LocalDateTime.now()); logger.info("系统开启任务检查订单是否已经超期自动确认收货");
List<LitemallOrder> orderList = orderService.queryUnconfirm(); List<LitemallOrder> orderList = orderService.queryUnconfirm();
for (LitemallOrder order : orderList) { for (LitemallOrder order : orderList) {
...@@ -301,7 +309,12 @@ public class AdminOrderController { ...@@ -301,7 +309,12 @@ public class AdminOrderController {
// 设置订单已取消状态 // 设置订单已取消状态
order.setOrderStatus(OrderUtil.STATUS_AUTO_CONFIRM); order.setOrderStatus(OrderUtil.STATUS_AUTO_CONFIRM);
order.setConfirmTime(now); order.setConfirmTime(now);
orderService.updateById(order); if(orderService.updateById(order) == 0){
logger.info("订单 ID=" + order.getId() + " 数据已经更新,放弃自动确认收货");
}
else{
logger.info("订单 ID=" + order.getId() + " 已经超期自动确认收货");
}
} }
} }
} }
...@@ -73,7 +73,9 @@ public class AdminStorageController { ...@@ -73,7 +73,9 @@ public class AdminStorageController {
if (adminId == null) { if (adminId == null) {
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
litemallStorageService.update(litemallStorage); if(litemallStorageService.update(litemallStorage) == 0){
return ResponseUtil.updatedDateExpired();
}
return ResponseUtil.ok(litemallStorage); return ResponseUtil.ok(litemallStorage);
} }
......
...@@ -72,7 +72,9 @@ public class AdminTopicController { ...@@ -72,7 +72,9 @@ public class AdminTopicController {
if(adminId == null){ if(adminId == null){
return ResponseUtil.unlogin(); return ResponseUtil.unlogin();
} }
topicService.updateById(topic); if(topicService.updateById(topic) == 0){
return ResponseUtil.updatedDateExpired();
}
return ResponseUtil.ok(topic); return ResponseUtil.ok(topic);
} }
......
...@@ -100,7 +100,9 @@ public class AdminUserController { ...@@ -100,7 +100,9 @@ public class AdminUserController {
String encodedPassword = encoder.encode(password); String encodedPassword = encoder.encode(password);
user.setPassword(encodedPassword); user.setPassword(encodedPassword);
userService.update(user); if(userService.updateById(user) == 0){
return ResponseUtil.updatedDateExpired();
}
return ResponseUtil.ok(user); return ResponseUtil.ok(user);
} }
} }
...@@ -2,6 +2,8 @@ package org.linlinjava.litemall.core.notify; ...@@ -2,6 +2,8 @@ package org.linlinjava.litemall.core.notify;
import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage; import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.db.domain.LitemallUserFormid; import org.linlinjava.litemall.db.domain.LitemallUserFormid;
import org.linlinjava.litemall.db.service.LitemallUserFormIdService; import org.linlinjava.litemall.db.service.LitemallUserFormIdService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -13,11 +15,13 @@ import java.util.List; ...@@ -13,11 +15,13 @@ import java.util.List;
* 微信模版消息通知 * 微信模版消息通知
*/ */
public class WxTemplateSender { public class WxTemplateSender {
private final Log logger = LogFactory.getLog(WxTemplateSender.class);
@Autowired @Autowired
WxMaService wxMaService; private WxMaService wxMaService;
@Autowired @Autowired
LitemallUserFormIdService formIdService; private LitemallUserFormIdService formIdService;
/** /**
* 发送微信消息(模板消息),不带跳转 * 发送微信消息(模板消息),不带跳转
...@@ -59,7 +63,9 @@ public class WxTemplateSender { ...@@ -59,7 +63,9 @@ public class WxTemplateSender {
try { try {
wxMaService.getMsgService().sendTemplateMsg(msg); wxMaService.getMsgService().sendTemplateMsg(msg);
formIdService.updateUserFormId(userFormid); if(formIdService.updateUserFormId(userFormid) == 0){
logger.warn("更新数据已失效");
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -41,17 +41,18 @@ public class ResponseUtil { ...@@ -41,17 +41,18 @@ public class ResponseUtil {
return obj; return obj;
} }
public static Object badArgument(){ public static Object badArgument(){
return fail(401, "参数不对"); return fail(401, "参数不对");
} }
public static Object badArgumentValue(){ public static Object badArgumentValue(){
return fail(402, "参数值不对"); return fail(402, "参数值不对");
} }
public static Object updatedDateExpired(){
return fail(403, "更新数据已经失效");
}
public static Object unlogin(){ public static Object unlogin(){
return fail(501, "请登录"); return fail(501, "请登录");
} }
......
...@@ -29,7 +29,7 @@ public class LitemallAddressService { ...@@ -29,7 +29,7 @@ public class LitemallAddressService {
return addressMapper.insertSelective(address); return addressMapper.insertSelective(address);
} }
public int update(LitemallAddress address) { public int updateId(LitemallAddress address) {
return addressMapper.updateWithVersionByPrimaryKeySelective(address.getVersion(), address); return addressMapper.updateWithVersionByPrimaryKeySelective(address.getVersion(), address);
} }
......
...@@ -25,7 +25,7 @@ public class LitemallCartService { ...@@ -25,7 +25,7 @@ public class LitemallCartService {
cartMapper.insertSelective(cart); cartMapper.insertSelective(cart);
} }
public int update(LitemallCart cart) { public int updateById(LitemallCart cart) {
return cartMapper.updateWithVersionByPrimaryKeySelective(cart.getVersion(), cart); return cartMapper.updateWithVersionByPrimaryKeySelective(cart.getVersion(), cart);
} }
......
...@@ -65,9 +65,7 @@ public class LitemallGrouponRulesService { ...@@ -65,9 +65,7 @@ public class LitemallGrouponRulesService {
* @return * @return
*/ */
public boolean isExpired(LitemallGrouponRules rules) { public boolean isExpired(LitemallGrouponRules rules) {
if (rules == null || rules.getExpireTime().isBefore(LocalDateTime.now())) return (rules == null || rules.getExpireTime().isBefore(LocalDateTime.now()));
return true;
return false;
} }
/** /**
...@@ -109,7 +107,7 @@ public class LitemallGrouponRulesService { ...@@ -109,7 +107,7 @@ public class LitemallGrouponRulesService {
mapper.logicalDeleteByPrimaryKey(id); mapper.logicalDeleteByPrimaryKey(id);
} }
public int update(LitemallGrouponRules grouponRules) { public int updateById(LitemallGrouponRules grouponRules) {
return mapper.updateWithVersionByPrimaryKeySelective(grouponRules.getVersion(), grouponRules); return mapper.updateWithVersionByPrimaryKeySelective(grouponRules.getVersion(), grouponRules);
} }
} }
...@@ -90,7 +90,7 @@ public class LitemallGrouponService { ...@@ -90,7 +90,7 @@ public class LitemallGrouponService {
return (int) mapper.countByExample(example); return (int) mapper.countByExample(example);
} }
public int update(LitemallGroupon groupon) { public int updateById(LitemallGroupon groupon) {
return mapper.updateWithVersionByPrimaryKeySelective(groupon.getVersion(), groupon); return mapper.updateWithVersionByPrimaryKeySelective(groupon.getVersion(), groupon);
} }
......
...@@ -98,10 +98,6 @@ public class LitemallOrderService { ...@@ -98,10 +98,6 @@ public class LitemallOrderService {
return (int)orderMapper.countByExample(example); return (int)orderMapper.countByExample(example);
} }
public int update(LitemallOrder order) {
return orderMapper.updateByPrimaryKeySelective(order);
}
public List<LitemallOrder> querySelective(Integer userId, String orderSn, List<Short> orderStatusArray, Integer page, Integer size, String sort, String order) { public List<LitemallOrder> querySelective(Integer userId, String orderSn, List<Short> orderStatusArray, Integer page, Integer size, String sort, String order) {
LitemallOrderExample example = new LitemallOrderExample(); LitemallOrderExample example = new LitemallOrderExample();
LitemallOrderExample.Criteria criteria = example.createCriteria(); LitemallOrderExample.Criteria criteria = example.createCriteria();
...@@ -140,8 +136,8 @@ public class LitemallOrderService { ...@@ -140,8 +136,8 @@ public class LitemallOrderService {
return (int)orderMapper.countByExample(example); return (int)orderMapper.countByExample(example);
} }
public void updateById(LitemallOrder order) { public int updateById(LitemallOrder order) {
orderMapper.updateByPrimaryKeySelective(order); return orderMapper.updateWithVersionByPrimaryKeySelective(order.getVersion(), order);
} }
public void deleteById(Integer id) { public void deleteById(Integer id) {
......
...@@ -31,13 +31,13 @@ public class LitemallUserFormIdService { ...@@ -31,13 +31,13 @@ public class LitemallUserFormIdService {
* *
* @param userFormid * @param userFormid
*/ */
public void updateUserFormId(LitemallUserFormid userFormid) { public int updateUserFormId(LitemallUserFormid userFormid) {
//更新或者删除缓存 //更新或者删除缓存
if (userFormid.getIsprepay() && userFormid.getUseamount() > 1) { if (userFormid.getIsprepay() && userFormid.getUseamount() > 1) {
userFormid.setUseamount(userFormid.getUseamount() - 1); userFormid.setUseamount(userFormid.getUseamount() - 1);
formidMapper.updateWithVersionByPrimaryKey(userFormid.getVersion(), userFormid); return formidMapper.updateWithVersionByPrimaryKey(userFormid.getVersion(), userFormid);
} else { } else {
formidMapper.deleteByPrimaryKey(userFormid.getId()); return formidMapper.deleteByPrimaryKey(userFormid.getId());
} }
} }
......
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