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 EntrustSettle.Services; using FluentValidation; using Mapster; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Quartz.Util; using System.ComponentModel.DataAnnotations; namespace EntrustSettle.Api.Controllers { /// /// 订单 /// public class OrderController : BaseApiController { private readonly IOrderService orderService; private readonly IOrderAnnexService orderAnnexService; private readonly IAnnexService annexService; private readonly ILogger logger; private readonly IUnitOfWorkManage unitOfWorkManage; private readonly IOrderHistoryService orderHistoryService; private readonly IHYDService hydService; public OrderController(IOrderService orderService, IOrderAnnexService orderFileService, ILogger logger, IUnitOfWorkManage unitOfWorkManage, IOrderHistoryService orderHistoryService, IHYDService hydService, IAnnexService annexService) { this.orderService = orderService; this.orderAnnexService = orderFileService; this.logger = logger; this.unitOfWorkManage = unitOfWorkManage; this.orderHistoryService = orderHistoryService; this.hydService = hydService; this.annexService = annexService; } /// /// 获取订单列表 /// [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); } [HttpGet] [AllowAnonymous] public async Task Test() { await Task.CompletedTask; return SuccessMsg(); } /// /// 下单 /// [HttpPost] //[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?.Count > 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); } // 状态历史保存 var historyModelList = new List(); 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(); } /// /// 获取订单附件信息列表 /// /// 订单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 == (int)fileType) .Select((o, a) => new AnnexDto() { Name = a.Name, Type = a.Type, Id = a.Id }) .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(); } /// /// 为订单更新附件或信息 /// /// [HttpPost] public async Task 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 hydFeedbackDtoList = new List(); 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((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); } 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(); } /// /// 获取订单详情 /// [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 == (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); } } }