<summary>
[
HttpPost("AddOrder")]
public async Task<IActionResult> AddOrder([FromBody]AddOrderDto dto)
{
if (dto?.ProductList==
null)
return BadRequest(
"参数不能为空");
Uow.BeginTransaction();
try
{
var orderHeader =
new OrderHeader
{
CreateTime = DateTime.UtcNow,
CustomerNo = dto.CustomerNo,
State =
1,
OrderNo = Guid.NewGuid().ToString(
"N")
};
await OrderHeaderRepository.InsertAsync(orderHeader);
var orderDetailList =
new List<OrderDetail>();
var totalAmount =
0m;
dto.ProductList.ForEach(it =>
{
var orderDetail =
new OrderDetail
{
OrderHeaderId = orderHeader.Id,
ProductNo = it.ProductNo,
ProductName = it.ProductName,
Quantity = it.Quantity,
Price = it.Price
};
orderDetailList.Add(orderDetail);
totalAmount += it.Quantity * it.Price;
});
await OrderDetailRepository.BatchInsertAsync(orderDetailList);
var success =
await CustomerRepository.UpdateCustomerAmount(dto.CustomerNo, totalAmount);
if (!success)
{
Uow.RollBack();
return BadRequest();
}
}
catch (Exception e)
{
Uow.RollBack();
}
Uow.Commit();
return Ok();
}
[
HttpGet("DeleteDatabase")]
public async Task DeleteDatabase()
{
await OrderHeaderRepository.DeleteAllOrder();
}
[
HttpGet("CancelOrderByCustomerNo")]
public async Task<bool> CancelOrderByCustomerNo(string customerNo)
{
var count =
await OrderHeaderRepository.CancelOrderByCustomerNoAsync(customerNo);
return count >
0;
}
[
HttpGet("QueryOrderDetailByCustomerNoByPage")]
public async Task<Page<OrderDetail>> QueryOrderDetailByCustomerNoByPage(
int pageNumber,
int pageSize,
string customerNo)
{
var page=
new Pageable(pageNumber,pageSize);
var result =
await OrderDetailRepository.GetOrderDetailByCustomerNoByPageAsync(page,customerNo);
return result;
}
[
HttpGet("QueryOrderDetailByCustomerNo")]
public async Task<List<OrderDetail>> QueryOrderDetailByCustomerNo(
string customerNo)
{
var result=
await OrderDetailRepository.GetOrderDetailByCustomerNoAsync(customerNo);
return result;
}
动态生成接口实现类的原理
??最开始写的时候,思路是,采用AOP思想,castle动态生成接口实现类,实现类里的方法都没有具体实现,然后在切面里对方法进行拦截并且模拟方法的实现,提供返回值,java中很多骚操作就是这么实现的,但是后来发现在.net
中这种实现方式有很大的弊端,因为.net有异步方法,而castle对于异步方法的支持是很弱的,不足以实现最开始的设想,于是,我换了一种思路,在数据库操作和http调用中,抛开动态实现类这个壳子,具体执行操作的类是恒定不变的,
那么利用emit技术动态路由到要执行的方法就行了。
写在最后
??如果这篇文章对你有所启发不妨点个赞吧。
??github地址:https://github.com/TripleView/SummerBoot,欢迎star!
??QQ群:799648362
??nuget包:SummerBoot
net core天马行空系列:降低net core门槛,数据库操作和http访问仅需写接口,实现类由框架动态生成
标签:query delete 删库 操作 += 数据库 cep exce tps