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.

357 lines
14 KiB
C#

9 months ago
using Amazon.Runtime;
using EntrustSettle.Common;
using EntrustSettle.Common.Const;
9 months ago
using EntrustSettle.Common.Extensions;
using EntrustSettle.Controllers;
9 months ago
using EntrustSettle.IServices;
using EntrustSettle.Model;
using EntrustSettle.Model.Dtos;
using EntrustSettle.Model.Models;
using EntrustSettle.Model.Validator;
using EntrustSettle.Repository.UnitOfWorks;
using EntrustSettle.Services;
using FluentValidation;
using Mapster;
9 months ago
using Microsoft.AspNetCore.Authorization;
9 months ago
using Microsoft.AspNetCore.Mvc;
using Quartz.Util;
9 months ago
using System.ComponentModel.DataAnnotations;
9 months ago
namespace EntrustSettle.Api.Controllers
{
/// <summary>
/// 订单
/// </summary>
9 months ago
public class OrderController : BaseApiController
{
private readonly IOrderService orderService;
private readonly IOrderAnnexService orderAnnexService;
private readonly IAnnexService annexService;
9 months ago
private readonly ILogger<OrderController> logger;
private readonly IUnitOfWorkManage unitOfWorkManage;
private readonly IOrderHistoryService orderHistoryService;
private readonly IHYDService hydService;
9 months ago
public OrderController(IOrderService orderService,
IOrderAnnexService orderFileService,
ILogger<OrderController> logger,
IUnitOfWorkManage unitOfWorkManage,
9 months ago
IOrderHistoryService orderHistoryService,
IHYDService hydService,
IAnnexService annexService)
9 months ago
{
this.orderService = orderService;
this.orderAnnexService = orderFileService;
9 months ago
this.logger = logger;
this.unitOfWorkManage = unitOfWorkManage;
this.orderHistoryService = orderHistoryService;
this.hydService = hydService;
this.annexService = annexService;
9 months ago
}
9 months ago
/// <summary>
/// 获取订单列表
/// </summary>
[HttpGet]
public async Task<MessageModel<PageModel<OrderDto>>> List([FromQuery] OrderListInputDto input)
{
if (input.QueryType == 2)
9 months ago
{
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)
9 months ago
.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)
9 months ago
.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);
}
9 months ago
[HttpGet]
[AllowAnonymous]
public async Task<MessageModel> Test()
{
await Task.CompletedTask;
return SuccessMsg();
}
9 months ago
/// <summary>
/// 下单
/// </summary>
[HttpPost]
9 months ago
//[UseTran]
public async Task<MessageModel> Submit(OrderSubmitDto inputDto)
{
var validator = new OrderSubmitDtoValidator();
validator.ValidateAndThrow(inputDto);
9 months ago
// 订单信息保存
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);
9 months ago
// 附件关联信息保存
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);
}
9 months ago
// 状态历史保存
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)
9 months ago
.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>
9 months ago
[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>
[HttpPost]
public async Task<MessageModel> BindAnnexOrInfo([FromBody] BindAnnexOrInfoDto bindDto)
{
var order = await orderService.QueryById(bindDto.OrderId);
if (order == null)
{
throw new Exception("未找到该订单");
}
// 反馈附件需要请求海运达接口,所以需要先判断订单状态
if (bindDto.OperType == FileTypeEnum.)
{
// todo: 待放开 || order.Bsno == null
if (order.Status == (int)OrderStatusEnum.)
{
throw new Exception("请等待接收后再进行反馈");
}
if (order.Status == (int)OrderStatusEnum.)
{
throw new Exception("该订单已完成,无法进行反馈");
}
List<HydFeedbackDto> hydFeedbackDtoList = new List<HydFeedbackDto>();
if (bindDto.AnnexIdList != null && bindDto.AnnexIdList.Count >= 0)
{
string domainUrl = AppSettings.app(["Startup", "Domain"]);
var annexList = await annexService.Query(x => bindDto.AnnexIdList.Contains(x.Id));
foreach (Annex item in annexList)
{
string fileUrl = string.Join(",", annexList.Select(x => $"{domainUrl}/api/Annex/get?key={x.Key}"));
hydFeedbackDtoList.Add(new HydFeedbackDto()
{
fileName = item.Name,
fileUrl = fileUrl,
orderNo = (long)order.Bsno,
remark = bindDto.Remark
});
}
}
else
{
hydFeedbackDtoList.Add(new HydFeedbackDto()
{
orderNo = (long)order.Bsno,
remark = bindDto.Remark
});
}
bool isSuccess = await hydService.FeedBack(hydFeedbackDtoList);
if (!isSuccess)
{
logger.LogError($"提单号:{order.Mblno}向海运达反馈时发生异常");
throw new Exception("反馈失败,请稍后重试");
}
else
{
logger.LogInformation($"提单号:{order.Mblno}向海运达反馈成功");
}
}
if (bindDto.AnnexIdList != null && bindDto.AnnexIdList.Count > 0)
{
unitOfWorkManage.BeginTran();
if (bindDto.OperType == FileTypeEnum. || bindDto.OperType == FileTypeEnum.)
{
var deleteAnnexIdList = await orderAnnexService.AsQueryable()
.InnerJoin<Annex>((o, a) => o.AnnexId == a.Id)
.Where((o, a) => o.OrderId == bindDto.OrderId && a.Type == (int)bindDto.OperType)
.Select((o, a) => a.Id)
.ToListAsync();
await orderAnnexService.Delete(x => x.OrderId == bindDto.OrderId && deleteAnnexIdList.Contains(x.AnnexId));
}
// 添加要新增绑定的附件信息
var orderAnnexModelList = bindDto.AnnexIdList.Select(x => new OrderAnnex()
{
AnnexId = x,
OrderId = bindDto.OrderId
}).ToList();
await orderAnnexService.Add(orderAnnexModelList);
}
9 months ago
orderService.Db.Tracking(order);
if (bindDto.OperType == FileTypeEnum.)
{
if (!string.IsNullOrWhiteSpace(bindDto.Remark))
{
9 months ago
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;
}
9 months ago
order.MailBillNo = bindDto.MailBillNo;
}
9 months ago
await orderService.Db.Updateable(order).ExecuteCommandAsync();
unitOfWorkManage.CommitTran();
return SuccessMsg();
}
/// <summary>
/// 获取订单详情
/// </summary>
[HttpGet]
public async Task<MessageModel<OrderDetailDto>> Detail(long id)
9 months ago
{
var result = await orderService.AsQueryable()
.Where(x => x.Id == id)
.Select<OrderDetailDto>()
.FirstAsync();
9 months ago
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)
9 months ago
.Where((o, a) => a.Type == (int)FileTypeEnum. || a.Type == (int)FileTypeEnum.)
.Select((o, a) => new AnnexDto()
{
Id = a.Id,
Name = a.Name,
Type = a.Type
})
.ToListAsync();
result.AnnexList = annexList;
return Success(result);
9 months ago
}
}
}