using EntrustSettle.Common;
using EntrustSettle.Common.Extensions;
using EntrustSettle.Controllers;
using EntrustSettle.IServices;
using EntrustSettle.Model;
using EntrustSettle.Model.Dtos;
using EntrustSettle.Model.Models;
using EntrustSettle.Model.Validator;
using EntrustSettle.Repository.UnitOfWorks;
using FluentValidation;
using Mapster;
using Microsoft.AspNetCore.Mvc;
namespace EntrustSettle.Api.Controllers
{
///
/// 订单
///
public class OrderController : BaseApiController
{
private readonly IOrderService orderService;
private readonly IOrderAnnexService orderAnnexService;
private readonly ILogger logger;
private readonly IUnitOfWorkManage unitOfWorkManage;
private readonly IOrderHistoryService orderHistoryService;
public OrderController(IOrderService orderService,
IOrderAnnexService orderFileService,
ILogger logger,
IUnitOfWorkManage unitOfWorkManage,
IOrderHistoryService orderHistoryService)
{
this.orderService = orderService;
this.orderAnnexService = orderFileService;
this.logger = logger;
this.unitOfWorkManage = unitOfWorkManage;
this.orderHistoryService = orderHistoryService;
}
///
/// 获取订单列表
///
[HttpGet]
public async Task>> List([FromQuery] OrderListInputDto input)
{
if (input.QueryType == 2)
{
if (!App.User.CompanyName.Contains("东胜伟业") && !App.User.CompanyName.Contains("大简云"))
{
throw new Exception("访问权限与实际拥有菜单权限不符");
}
}
var result = await orderService.AsQueryable()
.WhereIF(input.QueryType != 2, x => x.CompanyId == App.User.CompanyId)
.WhereIF(!string.IsNullOrWhiteSpace(input.Mblno), x => x.Mblno == input.Mblno)
.WhereIF(!string.IsNullOrWhiteSpace(input.CompanyName), x => x.CompanyName.Contains(input.CompanyName))
.WhereIF(!string.IsNullOrWhiteSpace(input.Remark), x => x.Remark.Contains(input.Remark))
.WhereIF(input.BusinessType != null, x => x.BusinessType == input.BusinessType)
.WhereIF(input.Status != null, x => x.Status == (int)input.Status)
.WhereIF(input.CreateTimeStart != null, x => x.CreateTime >= input.CreateTimeStart)
.WhereIF(input.CreateTimeEnd != null, x => x.CreateTime <= input.CreateTimeEnd)
.Select()
.ToPageListAsyncExtension(input.pageIndex, input.pageSize);
return SuccessPage(result);
}
///
/// 下单
///
[HttpPost]
// test:[UseTran]
public async Task Submit(OrderSubmitDto inputDto)
{
var validator = new OrderSubmitDtoValidator();
validator.ValidateAndThrow(inputDto);
var orderList = new List();
foreach (var item in inputDto.MblnoList)
{
var order = new Order
{
Mblno = item,
CompanyId = inputDto.CompanyId,
CompanyName = inputDto.CompanyName,
BusinessType = inputDto.BusinessType,
Status = (int)OrderStatusEnum.已下单,
ContactId = inputDto.ContactId,
ContactName = inputDto.ContactName,
ContactTel = inputDto.ContactTel,
Remark = inputDto.Remark
};
orderList.Add(order);
}
unitOfWorkManage.BeginTran();
var orderIdList = await orderService.Add(orderList);
if (inputDto.AnnexIdList.Length > 0)
{
var orderFileModelList = new List();
foreach (var orderId in orderIdList)
{
foreach (var annexId in inputDto.AnnexIdList)
{
orderFileModelList.Add(new OrderAnnex()
{
OrderId = orderId,
AnnexId = annexId
});
}
};
await orderAnnexService.Add(orderFileModelList);
}
unitOfWorkManage.CommitTran();
// todo:测试是否不需要rollback
return SuccessMsg();
}
///
/// 获取订单附件信息列表
///
/// 订单Id
/// 附件类型
[HttpGet]
public async Task>> GetAnnexInfoList(long id, FileTypeEnum fileType)
{
var result = await orderAnnexService.Db.Queryable((o, a) => o.AnnexId == a.Id)
.Where((o, a) => o.OrderId == id && a.Type == (byte)fileType)
.Select()
.ToListAsync();
return Success(result);
}
///
/// 订单状态变更
///
[HttpPost]
public async Task ChangeStatus(ChangeStatusDto changeStatusDto)
{
new ChangeStatusDtoValidator().ValidateAndThrow(changeStatusDto);
var order = await orderService.QueryById(changeStatusDto.Id);
if (order == null)
{
throw new Exception("未找到该订单");
}
order.Status = (int)changeStatusDto.Status;
order.Amount = changeStatusDto.Amount;
unitOfWorkManage.BeginTran();
// test是否会更新UpdateTIme
await orderService.Update(order, x => new { x.Status, x.Amount });
var orderHistory = new OrderHistory
{
Pid = order.Id,
Status = (int)changeStatusDto.Status,
Remark = changeStatusDto.Remark,
Amount = changeStatusDto.Amount,
StatusTime = DateTime.Now,
};
await orderHistoryService.Add(orderHistory);
unitOfWorkManage.CommitTran();
return SuccessMsg();
}
///
/// 为订单更新附件或信息
///
///
[HttpGet]
public async Task BindAnnexOrInfo(BindAnnexOrInfoDto bindDto)
{
var order = await orderService.QueryById(bindDto.OrderId);
if (order == null)
{
throw new Exception("未找到该订单");
}
// 添加要新增绑定的附件信息
var orderAnnexModelList = bindDto.NewAnnexIdList.Select(x => new OrderAnnex()
{
AnnexId = x,
OrderId = bindDto.OrderId
}).ToList();
if (orderAnnexModelList.Any())
{
await orderAnnexService.Add(orderAnnexModelList);
}
// 删除要解绑的附件信息
if (bindDto.DelAnnexIdList.Any())
{
await orderAnnexService.Delete(x => bindDto.DelAnnexIdList.Contains(x.AnnexId));
}
var updateable = orderService.AsUpdateable(order);
if (bindDto.OperType == FileTypeEnum.反馈附件)
{
if (!string.IsNullOrWhiteSpace(bindDto.Remark))
{
order.Remark += (Environment.NewLine + bindDto.Remark);
updateable.UpdateColumns(x => x.Remark);
}
}
else if (bindDto.OperType == FileTypeEnum.发票)
{
if (bindDto.MailFlag != null)
{
order.MailFlag = bindDto.MailFlag;
updateable.UpdateColumns(x => x.MailFlag);
}
if (!string.IsNullOrWhiteSpace(bindDto.MailBillNo))
{
order.MailBillNo = bindDto.MailBillNo;
updateable.UpdateColumns(x => x.MailBillNo);
}
}
await updateable.Where(x => x.Id == bindDto.OrderId).ExecuteCommandHasChangeAsync();
return SuccessMsg();
}
///
/// 获取订单详情
///
[HttpGet]
public async Task> Detail(long id)
{
var result = await orderService.AsQueryable()
.Where(x => x.Id == id)
.Select()
.FirstAsync();
if (result != null)
{
throw new Exception("为找到该订单,请刷新后重试");
}
var annexList = await orderAnnexService.Db.Queryable((o, a) => o.AnnexId == a.Id)
.Where((o, a) => o.OrderId == id)
.Where((o, a) => a.Type == (byte)FileTypeEnum.原始附件 || a.Type == (byte)FileTypeEnum.反馈附件)
.Select()
.ToListAsync();
result.AnnexList = annexList;
return Success(result);
}
}
}