using AutoMapper;
|
using Magicodes.ExporterAndImporter.Core;
|
using Magicodes.ExporterAndImporter.Core.Models;
|
using Magicodes.ExporterAndImporter.Excel;
|
using MailKit.Search;
|
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
using Org.BouncyCastle.Asn1.X509;
|
using SqlSugar;
|
using System;
|
using System.Collections.Generic;
|
using System.Globalization;
|
using System.Linq;
|
using System.Reflection;
|
using System.Reflection.Metadata;
|
using System.Text;
|
using System.Threading.Tasks;
|
using WIDESEA_Core;
|
using WIDESEA_Core.Attributes;
|
using WIDESEA_Core.BaseRepository;
|
using WIDESEA_Core.BaseServices;
|
using WIDESEA_Core.CodeConfigEnum;
|
using WIDESEA_Core.Enums;
|
using WIDESEA_Core.Helper;
|
using WIDESEA_Core.Utilities;
|
using WIDESEA_DTO;
|
using WIDESEA_DTO.Basic;
|
using WIDESEA_DTO.Inbound;
|
using WIDESEA_IBasicService;
|
using WIDESEA_IInboundRepository;
|
using WIDESEA_IInboundService;
|
using WIDESEA_IStockService;
|
using WIDESEA_ITaskInfoRepository;
|
using WIDESEA_Model.Models;
|
using OrderByType = SqlSugar.OrderByType;
|
|
namespace WIDESEA_InboundService
|
{
|
public partial class InboundOrderService : ServiceBase<Dt_InboundOrder, IInboundOrderRepository>, IInboundOrderService
|
{
|
|
/// <summary>
|
/// 根据入库单号获取入库单(含明细)
|
/// </summary>
|
/// <param name="inboundOrderNo"></param>
|
/// <returns></returns>
|
public Dt_InboundOrder GetInboundOrder(string inboundOrderNo)
|
{
|
return BaseDal.Db.Queryable<Dt_InboundOrder>().Includes(x => x.Details).First(x => x.OrderNo == inboundOrderNo); ;
|
}
|
|
/// <summary>
|
/// 组盘数据更新
|
/// </summary>
|
/// <param name="inboundOrder">入库单</param>
|
/// <param name="inboundOrderDetails">入库单明细</param>
|
/// <param name="stockInfo">组盘数据</param>
|
/// <returns></returns>
|
public WebResponseContent MaterielGroupUpdateData(Dt_InboundOrder inboundOrder, List<Dt_InboundOrderDetail> inboundOrderDetails, Dt_StockInfo stockInfo)
|
{
|
try
|
{
|
_unitOfWorkManage.BeginTran();
|
UpdateData(inboundOrder);
|
_inboundOrderDetailService.UpdateData(inboundOrderDetails);
|
_stockService.StockInfoService.AddMaterielGroup(stockInfo);
|
_unitOfWorkManage.CommitTran();
|
return WebResponseContent.Instance.OK();
|
}
|
catch (Exception ex)
|
{
|
_unitOfWorkManage.RollbackTran();
|
return WebResponseContent.Instance.Error(ex.Message);
|
}
|
}
|
|
/// <summary>
|
/// 验证组盘数据
|
/// </summary>
|
/// <param name="materielGroupDTO">物料组盘DTO</param>
|
/// <param name="matSerialNumberDTOs">扫码序列号</param>
|
/// <param name="materielInfos">物料信息</param>
|
/// <param name="materielCodes">物料编号</param>
|
/// <param name="inboundOrder">入库单据</param>
|
/// <param name="stockInfo">组盘信息</param>
|
/// <returns></returns>
|
//public (bool, string, object?) CheckMaterielGroupParam(MaterielGroupDTO materielGroupDTO, List<MatSerialNumberDTO> matSerialNumberDTOs, List<Dt_MaterielInfo> materielInfos, List<string> materielCodes, Dt_InboundOrder inboundOrder, Dt_StockInfo stockInfo)
|
//{
|
// (bool, string, object?) result = ModelValidate.ValidateModelData(materielGroupDTO);
|
// if (!result.Item1) return result;
|
|
// if (_taskRepository.QueryFirst(x => x.PalletCode == materielGroupDTO.PalletCode) != null)
|
// {
|
// return (false, "该托盘号已有任务", materielGroupDTO);
|
// }
|
|
// if (stockInfo != null && !string.IsNullOrEmpty(stockInfo.LocationCode) && stockInfo.StockStatus != StockStatusEmun.组盘暂存.ObjToInt())
|
// {
|
// return (false, "已上架的托盘不能再次组盘", materielGroupDTO);
|
// }
|
|
// if (_stockService.StockInfoDetailService.ExistSerialNumbers(materielGroupDTO.SerialNumbers))
|
// {
|
// return (false, "有序列号在库存中已存在", materielGroupDTO);
|
// }
|
|
// if (materielInfos.Count != materielCodes.Count)
|
// {
|
// return (false, "有物料信息未录入,请录入物料信息", materielGroupDTO);
|
// }
|
|
// if (materielCodes.Count > 1 && materielInfos.FirstOrDefault(x => !x.IsMixMateriel) != null)
|
// {
|
// return (false, "有物料不可混料组盘", materielGroupDTO);
|
// }
|
|
// List<string> batchs = matSerialNumberDTOs.GroupBy(x => x.BatchNo).Select(x => x.Key).ToList();
|
// if (batchs.Count > 1 && materielInfos.FirstOrDefault(x => !x.IsMixMateriel) != null)
|
// {
|
// return (false, "有物料不可混批组盘", materielGroupDTO);
|
// }
|
|
// if (inboundOrder == null)
|
// {
|
// return (false, "单据不存在", materielGroupDTO);
|
// }
|
|
// if (inboundOrder.Details == null || inboundOrder.Details.Count == 0)
|
// {
|
// return (false, "无单据明细信息", materielGroupDTO);
|
// }
|
|
// if (inboundOrder.OrderStatus != InboundStatusEnum.未开始.ObjToInt() && inboundOrder.OrderStatus != InboundStatusEnum.入库中.ObjToInt())
|
// {
|
// return (false, "该单据不可再组盘", materielGroupDTO);
|
// }
|
|
// List<Dt_InboundOrderDetail> inboundOrderDetails = inboundOrder.Details.Where(x => materielCodes.Contains(x.MaterielCode)).ToList();
|
|
// if (inboundOrderDetails.GroupBy(x => x.MaterielCode).Count() != materielCodes.Count)
|
// {
|
// return (false, "有物料不在单据内", materielGroupDTO);
|
// }
|
|
// IGrouping<string, Dt_InboundOrderDetail>? temp = inboundOrder.Details.Where(x => materielCodes.Contains(x.MaterielCode)).GroupBy(x => x.MaterielCode).FirstOrDefault(x => x.Sum(v => v.OverInQuantity) >= x.Sum(v => v.OrderQuantity) || x.Sum(v => v.ReceiptQuantity) >= x.Sum(v => v.OrderQuantity));
|
// if (temp != null)
|
// {
|
// return (false, "有物料超出单据数量", materielGroupDTO);
|
// }
|
|
// return (true, "成功", materielGroupDTO);
|
//}
|
|
/// <summary>
|
/// PDA分页查询数据
|
/// </summary>
|
/// <param name="pageNo">页码</param>
|
/// <param name="orderNo">订单号</param>
|
/// <returns></returns>
|
public WebResponseContent QueryOrderInfo(int pageNo, string orderNo)
|
{
|
if (string.IsNullOrEmpty(orderNo))
|
{
|
object obj = Repository.QueryPage(x => true, pageNo, 10, new Dictionary<string, OrderByType> { { nameof(Dt_InboundOrder.CreateDate), OrderByType.Desc } }).Rows.Select(x => new
|
{
|
x.OrderNo,
|
x.UpperOrderNo,
|
x.CreateDate,
|
x.Creater
|
});
|
return WebResponseContent.Instance.OK(data: obj);
|
}
|
else
|
{
|
object obj = Repository.QueryPage(x => x.OrderNo == orderNo, pageNo, 10, new Dictionary<string, OrderByType> { { nameof(Dt_InboundOrder.CreateDate), OrderByType.Desc } }).Rows.Select(x => new
|
{
|
x.OrderNo,
|
x.UpperOrderNo,
|
x.CreateDate,
|
x.Creater
|
});
|
return WebResponseContent.Instance.OK(data: obj);
|
}
|
}
|
|
//下载模版
|
public virtual WebResponseContent DownLoadTemplate()
|
{
|
WebResponseContent content = new WebResponseContent();
|
IExporter exporter = new ExcelExporter();
|
Inbounddaoc inbounddaoc = new Inbounddaoc();
|
byte[] data = exporter.ExportHeaderAsByteArray(inbounddaoc).Result;
|
string fileName = "";
|
fileName ="模板.xlsx";
|
string savePath = AppDomain.CurrentDomain.BaseDirectory + $"ExcelImprotTemplate";
|
FileHelper.WriteFile(savePath, fileName, data);
|
|
content = WebResponseContent.Instance.OK(data: savePath + "\\" + fileName);
|
return content;
|
}
|
|
//导入
|
public virtual WebResponseContent Import(List<IFormFile> files)
|
{
|
try
|
{
|
if (files == null || files.Count == 0)
|
return new WebResponseContent { Status = true, Message = "请选择上传的文件" };
|
|
Microsoft.AspNetCore.Http.IFormFile formFile = files[0];
|
|
// 使用Path.Combine,避免硬编码路径分隔符
|
string dicPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ExcelImport", DateTime.Now.ToString("yyyyMMdd"));
|
if (!Directory.Exists(dicPath))
|
Directory.CreateDirectory(dicPath);
|
|
string fileName = $"{Guid.NewGuid()}_{formFile.FileName}";
|
string fullPath = Path.Combine(dicPath, fileName);
|
|
// 使用FileStream保存文件
|
using (FileStream stream = new FileStream(fullPath, FileMode.Create))
|
{
|
formFile.CopyTo(stream);
|
}
|
|
// 初始化Excel导入工具
|
ExcelImporter importer = new ExcelImporter();
|
|
// 导入Excel数据
|
ImportResult<Inbounddaoc> importResult = importer.Import<Inbounddaoc>(fullPath, "").Result;
|
|
|
if (importResult.HasError)
|
{
|
return WebResponseContent.Instance.Error(importResult.TemplateErrors.Serialize());
|
}
|
// 假设BaseDal是数据访问层,进行数据存储
|
List<Dt_InboundOrder> dt_Inbounds = new List<Dt_InboundOrder>();
|
|
|
|
foreach (var item in importResult.Data)
|
{
|
int Startingcolumn = 0;
|
int Terminationcolumn = 0;
|
if (item.OrderType == "成品")
|
{
|
if (item.UpperOrderNo.Contains("回料"))
|
{
|
Startingcolumn = 14;
|
Terminationcolumn = 37;
|
}
|
}
|
|
//Dt_InboundOrder dt_2 = new Dt_InboundOrder()
|
//{
|
// OrderName = item.OrderName,
|
// UpperOrderNo = item.UpperOrderNo,
|
// OrderType = item.OrderType == "原材料" ? (int)InventoryMaterialType.原材料 : (int)InventoryMaterialType.成品,
|
// OrderNo = item.OrderNo,
|
// CreateType = (int)CreateType.daoru,
|
// Creater = "导入",
|
// CreateDate = DateTime.Now,
|
// Startingcolumn= Startingcolumn,
|
// Terminationcolumn= Terminationcolumn
|
//};
|
//dt_Inbounds.Add(dt_2);
|
}
|
BaseDal.AddData(dt_Inbounds);
|
|
return WebResponseContent.Instance.OK();
|
}
|
catch (Exception ex)
|
{
|
// 捕获异常并返回错误消息
|
return WebResponseContent.Instance.Error(ex.Message);
|
}
|
}
|
|
public class Inbounddaoc
|
{
|
[ImporterHeader(Name = "物料号")]
|
[ExporterHeader(DisplayName = "物料号")]
|
[SugarColumn(IsNullable = true, ColumnDescription = "物料号", ColumnName = "物料号")]
|
public string OrderName { get; set; }
|
|
[ImporterHeader(Name = "物料名称")]
|
[ExporterHeader(DisplayName = "物料名称")]
|
[SugarColumn(IsNullable = true, Length = 50, ColumnDescription = "物料名称", ColumnName = "物料名称")]
|
public string UpperOrderNo { get; set; }
|
|
[ImporterHeader(Name = "类型")]
|
[ExporterHeader(DisplayName = "类型")]
|
[SugarColumn(IsNullable = true, ColumnDescription = "类型", ColumnName = "类型")]
|
public string OrderType { get; set; }
|
|
[ImporterHeader(Name = "物料详情")]
|
[ExporterHeader(DisplayName = "物料详情")]
|
[SugarColumn(IsNullable = true, Length = 50, ColumnDescription = "物料详情", ColumnName = "物料详情")]
|
public string OrderNo { get; set; }
|
}
|
}
|
}
|