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.
251 lines
9.8 KiB
C#
251 lines
9.8 KiB
C#
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
|
|
{
|
|
/// <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;
|
|
|
|
public OrderController(IOrderService orderService,
|
|
IOrderAnnexService orderFileService,
|
|
ILogger<OrderController> logger,
|
|
IUnitOfWorkManage unitOfWorkManage,
|
|
IOrderHistoryService orderHistoryService)
|
|
{
|
|
this.orderService = orderService;
|
|
this.orderAnnexService = orderFileService;
|
|
this.logger = logger;
|
|
this.unitOfWorkManage = unitOfWorkManage;
|
|
this.orderHistoryService = orderHistoryService;
|
|
}
|
|
/// <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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 下单
|
|
/// </summary>
|
|
[HttpPost]
|
|
// test:[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.Length > 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);
|
|
}
|
|
unitOfWorkManage.CommitTran();
|
|
|
|
// todo:测试是否不需要rollback
|
|
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 == (byte)fileType)
|
|
.Select<AnnexDto>()
|
|
.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("未找到该订单");
|
|
}
|
|
|
|
// 添加要新增绑定的附件信息
|
|
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();
|
|
}
|
|
|
|
/// <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 == (byte)FileTypeEnum.原始附件 || a.Type == (byte)FileTypeEnum.反馈附件)
|
|
.Select<AnnexDto>()
|
|
.ToListAsync();
|
|
result.AnnexList = annexList;
|
|
|
|
return Success(result);
|
|
}
|
|
}
|
|
}
|