You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
11 KiB
C#

using Amazon.Runtime;
using EntrustSettle.Common;
using EntrustSettle.Common.Const;
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.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
namespace EntrustSettle.Api.Controllers
{
/// <summary>
/// 订单
/// </summary>
public class OrderController : BaseApiController
{
private readonly IOrderService orderService;
private readonly IOrderAnnexService orderAnnexService;
private readonly ILogger<OrderController> logger;
private readonly IUnitOfWorkManage unitOfWorkManage;
private readonly IOrderHistoryService orderHistoryService;
private readonly IHttpClientFactory httpClientFactory;
public OrderController(IOrderService orderService,
IOrderAnnexService orderFileService,
ILogger<OrderController> logger,
IUnitOfWorkManage unitOfWorkManage,
IOrderHistoryService orderHistoryService,
IHttpClientFactory httpClientFactory)
{
this.orderService = orderService;
this.orderAnnexService = orderFileService;
this.logger = logger;
this.unitOfWorkManage = unitOfWorkManage;
this.orderHistoryService = orderHistoryService;
this.httpClientFactory = httpClientFactory;
}
/// <summary>
/// 获取订单列表
/// </summary>
[HttpGet]
public async Task<MessageModel<PageModel<OrderDto>>> 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<OrderDto>()
.ToPageListAsyncExtension(input.pageIndex, input.pageSize);
return SuccessPage(result);
}
[HttpGet]
[AllowAnonymous]
public async Task<MessageModel> Test()
{
await Task.CompletedTask;
return SuccessMsg();
}
/// <summary>
/// 下单
/// </summary>
[HttpPost]
//[UseTran]
public async Task<MessageModel> Submit(OrderSubmitDto inputDto)
{
var validator = new OrderSubmitDtoValidator();
validator.ValidateAndThrow(inputDto);
// 订单信息保存
var orderList = new List<Order>();
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?.Count > 0)
{
var orderFileModelList = new List<OrderAnnex>();
foreach (var orderId in orderIdList)
{
foreach (var annexId in inputDto.AnnexIdList)
{
orderFileModelList.Add(new OrderAnnex()
{
OrderId = orderId,
AnnexId = annexId
});
}
};
await orderAnnexService.Add(orderFileModelList);
}
// 状态历史保存
var historyModelList = new List<OrderHistory>();
foreach (var item in orderIdList)
{
historyModelList.Add(new OrderHistory
{
Pid = item,
Status = (int)OrderStatusEnum.,
StatusTime = DateTime.Now
});
}
await orderHistoryService.Add(historyModelList);
unitOfWorkManage.CommitTran();
return SuccessMsg();
}
/// <summary>
/// 获取订单附件信息列表
/// </summary>
/// <param name="id">订单Id</param>
/// <param name="fileType">附件类型</param>
[HttpGet]
public async Task<MessageModel<List<AnnexDto>>> GetAnnexInfoList(long id, FileTypeEnum fileType)
{
var result = await orderAnnexService.Db.Queryable<OrderAnnex, Annex>((o, a) => o.AnnexId == a.Id)
.Where((o, a) => o.OrderId == id && a.Type == (int)fileType)
.Select((o, a) => new AnnexDto()
{
Name = a.Name,
Type = a.Type,
Id = a.Id
})
.ToListAsync();
return Success(result);
}
/// <summary>
/// 订单状态变更
/// </summary>
[HttpPost]
public async Task<MessageModel> 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();
}
/// <summary>
/// 为订单更新附件或信息
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel> BindAnnexOrInfo(BindAnnexOrInfoDto bindDto)
{
var order = await orderService.QueryById(bindDto.OrderId);
if (order == null)
{
throw new Exception("未找到该订单");
}
unitOfWorkManage.BeginTran();
// 添加要新增绑定的附件信息
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));
}
orderService.Db.Tracking(order);
if (bindDto.OperType == FileTypeEnum.)
{
if (!string.IsNullOrWhiteSpace(bindDto.Remark))
{
string remark = string.IsNullOrEmpty(order.Remark) ?
bindDto.Remark :
order.Remark += (Environment.NewLine + bindDto.Remark);
order.Remark = remark;
}
}
else if (bindDto.OperType == FileTypeEnum.)
{
if (bindDto.MailFlag != null)
{
order.MailFlag = bindDto.MailFlag;
}
order.MailBillNo = bindDto.MailBillNo;
}
await orderService.Db.Updateable(order).ExecuteCommandAsync();
unitOfWorkManage.CommitTran();
return SuccessMsg();
}
/// <summary>
/// 获取订单详情
/// </summary>
[HttpGet]
public async Task<MessageModel<OrderDetailDto>> Detail(long id)
{
var result = await orderService.AsQueryable()
.Where(x => x.Id == id)
.Select<OrderDetailDto>()
.FirstAsync();
if (result == null)
{
throw new Exception("为找到该订单,请刷新后重试");
}
var annexList = await orderAnnexService.Db.Queryable<OrderAnnex, Annex>((o, a) => o.AnnexId == a.Id)
.Where((o, a) => o.OrderId == id)
.Where((o, a) => a.Type == (int)FileTypeEnum. || a.Type == (int)FileTypeEnum.)
.Select<AnnexDto>()
.ToListAsync();
result.AnnexList = annexList;
return Success(result);
}
}
}