1
huanghongfeng
2024-12-08 885bd60ed54a0642c48c57a3d685cba24e4c763b
´úÂë¹ÜÀí/WMS/WIDESEA_WMSServer/WIDESEA_InboundService/Service/InboundOrderService.cs
@@ -1,12 +1,21 @@
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.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.Enums;
@@ -20,6 +29,7 @@
using WIDESEA_IStockService;
using WIDESEA_ITaskInfoRepository;
using WIDESEA_Model.Models;
using OrderByType = SqlSugar.OrderByType;
namespace WIDESEA_InboundService
{
@@ -246,6 +256,121 @@
            }
        }
        
        //下载模版
        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; }
        }
    }
}