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

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

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