mirror of
https://gitee.com/dromara/hmily.git
synced 2024-12-03 03:38:33 +08:00
demo commit .
This commit is contained in:
parent
153121a11a
commit
7348a226ce
@ -73,14 +73,14 @@ public class AccountServiceImpl implements AccountService {
|
||||
@Override
|
||||
@Hmily(confirmMethod = "confirm", cancelMethod = "cancel")
|
||||
public void payment(AccountDTO accountDTO) {
|
||||
//accountMapper.update(accountDTO);
|
||||
accountMapper.update(accountDTO);
|
||||
/*final int i = trycount.incrementAndGet();
|
||||
System.out.println("调用了account try " + i + " 次");*/
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean testPayment(AccountDTO accountDTO) {
|
||||
//accountMapper.update(accountDTO);
|
||||
accountMapper.update(accountDTO);
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
|
@ -71,15 +71,15 @@ public class InventoryServiceImpl implements InventoryService {
|
||||
@Override
|
||||
@Hmily(confirmMethod = "confirmMethod", cancelMethod = "cancelMethod")
|
||||
public Boolean decrease(InventoryDTO inventoryDTO) {
|
||||
//inventoryMapper.decrease(inventoryDTO);
|
||||
/* final int i = tryCount.incrementAndGet();
|
||||
System.out.println("调用了inventory try " + i + " 次");*/
|
||||
inventoryMapper.decrease(inventoryDTO);
|
||||
final int i = tryCount.incrementAndGet();
|
||||
System.out.println("调用了inventory try " + i + " 次");
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean testDecrease(InventoryDTO inventoryDTO) {
|
||||
//inventoryMapper.decrease(inventoryDTO);
|
||||
inventoryMapper.decrease(inventoryDTO);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -182,7 +182,6 @@ public class InventoryServiceImpl implements InventoryService {
|
||||
* @param inventoryDTO the inventory dto
|
||||
* @return the boolean
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean confirmMethod(InventoryDTO inventoryDTO) {
|
||||
inventoryMapper.confirm(inventoryDTO);
|
||||
final int i = confirmCount.incrementAndGet();
|
||||
@ -196,7 +195,6 @@ public class InventoryServiceImpl implements InventoryService {
|
||||
* @param inventoryDTO the inventory dto
|
||||
* @return the boolean
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean cancelMethod(InventoryDTO inventoryDTO) {
|
||||
LOGGER.info("==========调用扣减库存取消方法===========");
|
||||
inventoryMapper.cancel(inventoryDTO);
|
||||
|
@ -58,16 +58,16 @@ public class OrderServiceImpl implements OrderService {
|
||||
|
||||
@Override
|
||||
public String orderPay(Integer count, BigDecimal amount) {
|
||||
/* final Order order = buildTestOrder(count, amount);
|
||||
final Order order = buildOrder(count, amount);
|
||||
final int rows = orderMapper.save(order);
|
||||
if (rows > 0) {
|
||||
final long start = System.currentTimeMillis();
|
||||
paymentService.makePayment(order);
|
||||
System.out.println("切面耗时:" + (System.currentTimeMillis() - start));
|
||||
}*/
|
||||
final long start = System.currentTimeMillis();
|
||||
}
|
||||
/* final long start = System.currentTimeMillis();
|
||||
paymentService.makePayment(new Order());
|
||||
System.out.println("切面耗时:" + (System.currentTimeMillis() - start));
|
||||
System.out.println("切面耗时:" + (System.currentTimeMillis() - start));*/
|
||||
return "success";
|
||||
}
|
||||
|
||||
|
@ -66,8 +66,8 @@ public class PaymentServiceImpl implements PaymentService {
|
||||
@Override
|
||||
@Hmily(confirmMethod = "confirmOrderStatus", cancelMethod = "cancelOrderStatus")
|
||||
public void makePayment(Order order) {
|
||||
/* order.setStatus(OrderStatusEnum.PAYING.getCode());
|
||||
orderMapper.update(order);*/
|
||||
order.setStatus(OrderStatusEnum.PAYING.getCode());
|
||||
orderMapper.update(order);
|
||||
//做库存和资金账户的检验工作 这里只是demo 。。。
|
||||
/* final AccountDO accountDO = accountService.findByUserId(order.getUserId());
|
||||
if (accountDO.getBalance().compareTo(order.getTotalAmount()) <= 0) {
|
||||
|
@ -21,7 +21,7 @@ org:
|
||||
scheduledThreadMax : 10
|
||||
repositorySupport : db
|
||||
started: true
|
||||
async-threads: 500
|
||||
async-threads: 200
|
||||
hmilyDbConfig :
|
||||
driverClassName : com.mysql.jdbc.Driver
|
||||
url : jdbc:mysql://192.168.1.98:3306/tcc?useUnicode=true&characterEncoding=utf8
|
||||
|
@ -17,58 +17,60 @@
|
||||
|
||||
package org.dromara.hmily.demo.springcloud.account.mapper;
|
||||
|
||||
import org.dromara.hmily.demo.springcloud.account.entity.AccountDO;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
import org.dromara.hmily.demo.springcloud.account.dto.AccountDTO;
|
||||
import org.dromara.hmily.demo.springcloud.account.entity.AccountDO;
|
||||
|
||||
/**
|
||||
* The interface Account mapper.
|
||||
*
|
||||
* @author xiaoyu
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public interface AccountMapper {
|
||||
|
||||
/**
|
||||
* 扣减账户余额
|
||||
* Update int.
|
||||
*
|
||||
* @param accountDO 实体类
|
||||
* @return rows
|
||||
* @param accountDTO the account dto
|
||||
* @return the int
|
||||
*/
|
||||
@Update("update account set balance =#{balance}," +
|
||||
" freeze_amount= #{freezeAmount} ,update_time = #{updateTime}" +
|
||||
" where user_id =#{userId} and balance > 0 ")
|
||||
int update(AccountDO accountDO);
|
||||
@Update("update account set balance = balance - #{amount}," +
|
||||
" freeze_amount= freeze_amount + #{amount} ,update_time = now()" +
|
||||
" where user_id =#{userId} and balance > 0 ")
|
||||
int update(AccountDTO accountDTO);
|
||||
|
||||
|
||||
/**
|
||||
* 确认扣减账户余额
|
||||
* Confirm int.
|
||||
*
|
||||
* @param accountDO 实体类
|
||||
* @return rows
|
||||
* @param accountDTO the account dto
|
||||
* @return the int
|
||||
*/
|
||||
@Update("update account set " +
|
||||
" freeze_amount= #{freezeAmount} ,update_time = #{updateTime}" +
|
||||
" freeze_amount= freeze_amount - #{amount}" +
|
||||
" where user_id =#{userId} and freeze_amount >0 ")
|
||||
int confirm(AccountDO accountDO);
|
||||
int confirm(AccountDTO accountDTO);
|
||||
|
||||
|
||||
/**
|
||||
* 取消扣减账户余额
|
||||
* Cancel int.
|
||||
*
|
||||
* @param accountDO 实体类
|
||||
* @return rows
|
||||
* @param accountDO the account do
|
||||
* @return the int
|
||||
*/
|
||||
@Update("update account set balance =#{balance}," +
|
||||
" freeze_amount= #{freezeAmount} ,update_time = #{updateTime}" +
|
||||
@Update("update account set balance = balance + #{amount}," +
|
||||
" freeze_amount= freeze_amount - #{amount} " +
|
||||
" where user_id =#{userId} and freeze_amount >0")
|
||||
int cancel(AccountDO accountDO);
|
||||
|
||||
int cancel(AccountDTO accountDTO);
|
||||
|
||||
/**
|
||||
* 根据userId获取用户账户信息
|
||||
* Find by user id account do.
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @return AccountDO
|
||||
* @param userId the user id
|
||||
* @return the account do
|
||||
*/
|
||||
@Select("select * from account where user_id =#{userId}")
|
||||
@Select("select id,user_id,balance, freeze_amount from account where user_id =#{userId} limit 1")
|
||||
AccountDO findByUserId(String userId);
|
||||
}
|
||||
|
@ -19,14 +19,10 @@ package org.dromara.hmily.demo.springcloud.account.service.impl;
|
||||
|
||||
import org.dromara.hmily.annotation.Hmily;
|
||||
import org.dromara.hmily.common.exception.HmilyRuntimeException;
|
||||
|
||||
import org.dromara.hmily.demo.springcloud.account.dto.AccountDTO;
|
||||
import org.dromara.hmily.demo.springcloud.account.entity.AccountDO;
|
||||
import org.dromara.hmily.demo.springcloud.account.mapper.AccountMapper;
|
||||
import org.dromara.hmily.demo.springcloud.account.service.AccountService;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -34,8 +30,6 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* @author xiaoyu
|
||||
*/
|
||||
@ -64,23 +58,9 @@ public class AccountServiceImpl implements AccountService {
|
||||
*/
|
||||
@Override
|
||||
@Hmily(confirmMethod = "confirm", cancelMethod = "cancel")
|
||||
@Transactional
|
||||
public boolean payment(AccountDTO accountDTO) {
|
||||
LOGGER.debug("============springcloud执行try付款接口===============");
|
||||
final AccountDO accountDO = accountMapper.findByUserId(accountDTO.getUserId());
|
||||
accountDO.setBalance(accountDO.getBalance().subtract(accountDTO.getAmount()));
|
||||
accountDO.setFreezeAmount(accountDO.getFreezeAmount().add(accountDTO.getAmount()));
|
||||
accountDO.setUpdateTime(new Date());
|
||||
|
||||
final int update = accountMapper.update(accountDO);
|
||||
if (update != 1) {
|
||||
throw new HmilyRuntimeException("资金不足!");
|
||||
}
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
accountMapper.update(accountDTO);
|
||||
// throw new RuntimeException("");
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
@ -97,28 +77,13 @@ public class AccountServiceImpl implements AccountService {
|
||||
}
|
||||
|
||||
public boolean confirm(AccountDTO accountDTO) {
|
||||
|
||||
LOGGER.debug("============springcloud tcc 执行确认付款接口===============");
|
||||
|
||||
final AccountDO accountDO = accountMapper.findByUserId(accountDTO.getUserId());
|
||||
accountDO.setFreezeAmount(accountDO.getFreezeAmount().subtract(accountDTO.getAmount()));
|
||||
accountDO.setUpdateTime(new Date());
|
||||
final int rows = accountMapper.confirm(accountDO);
|
||||
if (rows != 1) {
|
||||
throw new HmilyRuntimeException("确认扣减账户异常!");
|
||||
}
|
||||
final int rows = accountMapper.confirm(accountDTO);
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
|
||||
public boolean cancel(AccountDTO accountDTO) {
|
||||
|
||||
LOGGER.debug("============springcloud tcc 执行取消付款接口===============");
|
||||
final AccountDO accountDO = accountMapper.findByUserId(accountDTO.getUserId());
|
||||
accountDO.setBalance(accountDO.getBalance().add(accountDTO.getAmount()));
|
||||
accountDO.setFreezeAmount(accountDO.getFreezeAmount().subtract(accountDTO.getAmount()));
|
||||
accountDO.setUpdateTime(new Date());
|
||||
final int rows = accountMapper.cancel(accountDO);
|
||||
final int rows = accountMapper.cancel(accountDTO);
|
||||
if (rows != 1) {
|
||||
throw new HmilyRuntimeException("取消扣减账户异常!");
|
||||
}
|
||||
|
@ -17,6 +17,7 @@
|
||||
|
||||
package org.dromara.hmily.demo.springcloud.inventory.mapper;
|
||||
|
||||
import org.dromara.hmily.demo.springcloud.inventory.dto.InventoryDTO;
|
||||
import org.dromara.hmily.demo.springcloud.inventory.entity.InventoryDO;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
@ -27,46 +28,49 @@ import org.apache.ibatis.annotations.Update;
|
||||
@SuppressWarnings("all")
|
||||
public interface InventoryMapper {
|
||||
|
||||
/**
|
||||
* 库存扣减.
|
||||
*
|
||||
* @param inventory 实体对象
|
||||
* @return rows
|
||||
*/
|
||||
@Update("update inventory set total_inventory =#{totalInventory}," +
|
||||
" lock_inventory= #{lockInventory} " +
|
||||
" where product_id =#{productId} and total_inventory >0 ")
|
||||
int decrease(InventoryDO inventory);
|
||||
|
||||
/**
|
||||
* 库存扣减confirm.
|
||||
* Decrease int.
|
||||
*
|
||||
* @param inventory 实体对象
|
||||
* @return rows
|
||||
* @param inventoryDTO the inventory dto
|
||||
* @return the int
|
||||
*/
|
||||
@Update("update inventory set total_inventory = total_inventory - #{count}," +
|
||||
" lock_inventory= lock_inventory + #{count} " +
|
||||
" where product_id =#{productId} and total_inventory >0 ")
|
||||
int decrease(InventoryDTO inventoryDTO);
|
||||
|
||||
|
||||
/**
|
||||
* Confirm int.
|
||||
*
|
||||
* @param inventoryDTO the inventory dto
|
||||
* @return the int
|
||||
*/
|
||||
@Update("update inventory set " +
|
||||
" lock_inventory= #{lockInventory} " +
|
||||
" lock_inventory= lock_inventory - #{count} " +
|
||||
" where product_id =#{productId} and lock_inventory >0 ")
|
||||
int confirm(InventoryDO inventory);
|
||||
int confirm(InventoryDTO inventoryDTO);
|
||||
|
||||
|
||||
/**
|
||||
* 库存扣减 cancel.
|
||||
* Cancel int.
|
||||
*
|
||||
* @param inventory 实体对象
|
||||
* @return rows
|
||||
* @param inventoryDTO the inventory dto
|
||||
* @return the int
|
||||
*/
|
||||
@Update("update inventory set total_inventory =#{totalInventory}," +
|
||||
" lock_inventory= #{lockInventory} " +
|
||||
@Update("update inventory set total_inventory = total_inventory + #{count}," +
|
||||
" lock_inventory= lock_inventory - #{count} " +
|
||||
" where product_id =#{productId} and lock_inventory >0 ")
|
||||
int cancel(InventoryDO inventory);
|
||||
int cancel(InventoryDTO inventoryDTO);
|
||||
|
||||
/**
|
||||
* 根据商品id找到库存信息.
|
||||
* Find by product id inventory do.
|
||||
*
|
||||
* @param productId 商品id
|
||||
* @return Inventory
|
||||
* @param productId the product id
|
||||
* @return the inventory do
|
||||
*/
|
||||
@Select("select * from inventory where product_id =#{productId}")
|
||||
@Select("select id,product_id,total_inventory ,lock_inventory from inventory where product_id =#{productId}")
|
||||
InventoryDO findByProductId(String productId);
|
||||
|
||||
}
|
||||
|
@ -19,7 +19,6 @@ package org.dromara.hmily.demo.springcloud.inventory.service.impl;
|
||||
|
||||
import org.dromara.hmily.annotation.Hmily;
|
||||
import org.dromara.hmily.common.exception.HmilyRuntimeException;
|
||||
|
||||
import org.dromara.hmily.demo.springcloud.inventory.dto.InventoryDTO;
|
||||
import org.dromara.hmily.demo.springcloud.inventory.entity.InventoryDO;
|
||||
import org.dromara.hmily.demo.springcloud.inventory.mapper.InventoryMapper;
|
||||
@ -36,7 +35,6 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
* @author xiaoyu
|
||||
*/
|
||||
@Service("inventoryService")
|
||||
@SuppressWarnings("all")
|
||||
public class InventoryServiceImpl implements InventoryService {
|
||||
|
||||
/**
|
||||
@ -60,17 +58,9 @@ public class InventoryServiceImpl implements InventoryService {
|
||||
*/
|
||||
@Override
|
||||
@Hmily(confirmMethod = "confirmMethod", cancelMethod = "cancelMethod")
|
||||
@Transactional
|
||||
public Boolean decrease(InventoryDTO inventoryDTO) {
|
||||
LOGGER.info("==========springcloud调用扣减库存decrease===========");
|
||||
final InventoryDO entity = inventoryMapper.findByProductId(inventoryDTO.getProductId());
|
||||
entity.setTotalInventory(entity.getTotalInventory() - inventoryDTO.getCount());
|
||||
entity.setLockInventory(entity.getLockInventory() + inventoryDTO.getCount());
|
||||
final int decrease = inventoryMapper.decrease(entity);
|
||||
if (decrease != 1) {
|
||||
throw new HmilyRuntimeException("库存不足");
|
||||
}
|
||||
// throw new RuntimeException("测试");
|
||||
inventoryMapper.decrease(inventoryDTO);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -104,10 +94,7 @@ public class InventoryServiceImpl implements InventoryService {
|
||||
e.printStackTrace();
|
||||
}
|
||||
LOGGER.info("==========springcloud调用扣减库存mockWithTryTimeout===========");
|
||||
final InventoryDO entity = inventoryMapper.findByProductId(inventoryDTO.getProductId());
|
||||
entity.setTotalInventory(entity.getTotalInventory() - inventoryDTO.getCount());
|
||||
entity.setLockInventory(entity.getLockInventory() + inventoryDTO.getCount());
|
||||
final int decrease = inventoryMapper.decrease(entity);
|
||||
final int decrease = inventoryMapper.decrease(inventoryDTO);
|
||||
if (decrease != 1) {
|
||||
throw new HmilyRuntimeException("库存不足");
|
||||
}
|
||||
@ -123,18 +110,14 @@ public class InventoryServiceImpl implements InventoryService {
|
||||
e.printStackTrace();
|
||||
}
|
||||
LOGGER.info("==========Springcloud调用扣减库存确认方法===========");
|
||||
final InventoryDO entity = inventoryMapper.findByProductId(inventoryDTO.getProductId());
|
||||
entity.setLockInventory(entity.getLockInventory() - inventoryDTO.getCount());
|
||||
inventoryMapper.decrease(entity);
|
||||
inventoryMapper.decrease(inventoryDTO);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean confirmMethodException(InventoryDTO inventoryDTO) {
|
||||
LOGGER.info("==========Springcloud调用扣减库存确认方法===========");
|
||||
final InventoryDO entity = inventoryMapper.findByProductId(inventoryDTO.getProductId());
|
||||
entity.setLockInventory(entity.getLockInventory() - inventoryDTO.getCount());
|
||||
final int decrease = inventoryMapper.decrease(entity);
|
||||
final int decrease = inventoryMapper.decrease(inventoryDTO);
|
||||
if (decrease != 1) {
|
||||
throw new HmilyRuntimeException("库存不足");
|
||||
}
|
||||
@ -145,24 +128,13 @@ public class InventoryServiceImpl implements InventoryService {
|
||||
|
||||
public Boolean confirmMethod(InventoryDTO inventoryDTO) {
|
||||
LOGGER.info("==========Springcloud调用扣减库存确认方法===========");
|
||||
final InventoryDO entity = inventoryMapper.findByProductId(inventoryDTO.getProductId());
|
||||
entity.setLockInventory(entity.getLockInventory() - inventoryDTO.getCount());
|
||||
final int rows = inventoryMapper.confirm(entity);
|
||||
if (rows != 1) {
|
||||
throw new HmilyRuntimeException("确认库存操作失败!");
|
||||
}
|
||||
final int rows = inventoryMapper.confirm(inventoryDTO);
|
||||
return true;
|
||||
}
|
||||
|
||||
public Boolean cancelMethod(InventoryDTO inventoryDTO) {
|
||||
LOGGER.info("==========Springcloud调用扣减库存取消方法===========");
|
||||
final InventoryDO entity = inventoryMapper.findByProductId(inventoryDTO.getProductId());
|
||||
entity.setTotalInventory(entity.getTotalInventory() + inventoryDTO.getCount());
|
||||
entity.setLockInventory(entity.getLockInventory() - inventoryDTO.getCount());
|
||||
int rows = inventoryMapper.cancel(entity);
|
||||
if (rows != 1) {
|
||||
throw new HmilyRuntimeException("取消库存操作失败!");
|
||||
}
|
||||
int rows = inventoryMapper.cancel(inventoryDTO);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -39,6 +39,7 @@
|
||||
<property name="scheduledDelay" value="120"/>
|
||||
<property name="scheduledThreadMax" value="4"/>
|
||||
<property name="repositorySupport" value="db"/>
|
||||
<property name="started" value="false"/>
|
||||
<property name="hmilyDbConfig">
|
||||
<bean class="org.dromara.hmily.common.config.HmilyDbConfig">
|
||||
<property name="url"
|
||||
|
@ -39,6 +39,8 @@
|
||||
<property name="scheduledDelay" value="120"/>
|
||||
<property name="scheduledThreadMax" value="4"/>
|
||||
<property name="repositorySupport" value="db"/>
|
||||
<property name="asyncThreads" value="200"/>
|
||||
<property name="started" value="true"/>
|
||||
<property name="hmilyDbConfig">
|
||||
<bean class="org.dromara.hmily.common.config.HmilyDbConfig">
|
||||
<property name="url"
|
||||
|
Loading…
Reference in New Issue
Block a user