Bladeren bron

添加物资入库Excel下载功能及相关权限验证

在 PersonnelModuleController.cs 中添加了 using 语句,并将 url 和 path 的声明拆分为两行。新增 GoodsStorageExcelDownload API 方法及其权限验证,并在 GoodsReceiveList 方法中添加了 Excel 文件下载权限验证逻辑。

在 GoodsDTO.cs 中添加了 IsExcelDownload 属性,用于标识是否导出 Excel。在 GoodsInfoView.cs 中添加了 GoodsStorageExcelDownloadView 类,用于物资入库的 Excel 下载视图模型。

在 OASystem.Infrastructure.csproj 中添加了对 Aspose.Cells 的引用。

在 DelegationInfoRepository.cs 中删除了一些注释和空行。

在 GoodsRepository.cs 中添加了对 Aspose.Cells 和其他命名空间的引用,新增 _url 和 _excelPath 字段并在构造函数中初始化,添加了 GoodsStorageExcelDownload 方法用于生成物资入库的 Excel 文件,并在 GoodsReceiveList 方法中添加了 Excel 文件导出逻辑及分页处理。
LEIYI 2 maanden geleden
bovenliggende
commit
3b69e88d1a

+ 51 - 3
OASystem/OASystem.Api/Controllers/PersonnelModuleController.cs

@@ -10,6 +10,7 @@ using OASystem.Domain.Dtos.Groups;
 using OASystem.Domain.Dtos.PersonnelModule;
 using OASystem.Domain.Entities.Groups;
 using OASystem.Domain.Entities.PersonnelModule;
+using OASystem.Domain.ViewModels.JuHeExchangeRate;
 using OASystem.Domain.ViewModels.PersonnelModule;
 using OASystem.Domain.ViewModels.QiYeWeChat;
 using OASystem.Infrastructure.Repositories.PersonnelModule;
@@ -39,7 +40,8 @@ namespace OASystem.API.Controllers
         private readonly IHubContext<ChatHub, IChatClient> _hubContext;
         private readonly GoodsRepository _goodsRep;
 
-        private string url, path;
+        private string url;
+        private string path;
 
         /// <summary>
         /// 初始化
@@ -1886,6 +1888,35 @@ namespace OASystem.API.Controllers
             return Ok(await _goodsRep.GoodsStorageDel(id, currUserInfo.UserId));
         }
 
+
+        /// <summary>
+        /// 物资进销存
+        /// 入库 excelDownload
+        /// </summary>
+        /// <returns></returns>
+        [HttpPost]
+        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
+        public async Task<IActionResult> GoodsStorageExcelDownload()
+        {
+            //token验证
+            var currUserInfo = JwtHelper.SerializeJwt(HttpContext.Request.Headers.Authorization);
+            if (currUserInfo == null) return Ok(JsonView(false, "请传入token!"));
+
+            //文件下载权限验证
+            int userId = currUserInfo.UserId,
+                pageId = 191;
+
+            PageFunAuthViewBase pageFunAuthView = new PageFunAuthViewBase();
+            #region 页面操作权限验证
+            pageFunAuthView = await GeneralMethod.PostUserPageFuncDatas(userId, pageId);
+
+            if (pageFunAuthView.FilesDownloadAuth == 0) return Ok(JsonView(false, "您未分配文件下载权限!"));
+            #endregion
+
+            return Ok(await _goodsRep.GoodsStorageExcelDownload());
+        }
+
+
         /// <summary>
         /// 物资进销存
         /// 领用 List
@@ -1895,12 +1926,29 @@ namespace OASystem.API.Controllers
         [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
         public async Task<IActionResult> GoodsReceiveList(GoodsReceiveListDTO dto)
         {
-            //var currUserInfo = JwtHelper.SerializeJwt(HttpContext.Request.Headers.Authorization);
-            //if (currUserInfo == null) return Ok(JsonView(false, "请传入token!"));
+            //token验证
+            var currUserInfo = JwtHelper.SerializeJwt(HttpContext.Request.Headers.Authorization);
+            if (currUserInfo == null) return Ok(JsonView(false, "请传入token!"));
 
             if (dto.PortType < 1 || dto.PortType > 3) return Ok(JsonView(false, MsgTips.Port));
             if (dto.PageIndex < 1 || dto.PageSize < 1) return Ok(JsonView(false, MsgTips.PageIndex));
 
+            //文件下载权限验证
+            int userId = currUserInfo.UserId,
+                pageId = 191;
+
+            if (dto.IsExcelDownload )
+            {
+                if (userId < 1) return Ok(JsonView(false, "excel导出时,需传入正确的当前操作人UserId!"));
+
+                PageFunAuthViewBase pageFunAuthView = new PageFunAuthViewBase();
+                #region 页面操作权限验证
+                pageFunAuthView = await GeneralMethod.PostUserPageFuncDatas(userId,pageId);
+
+                if (pageFunAuthView.FilesDownloadAuth == 0) return Ok(JsonView(false, "您未分配文件下载权限!"));
+                #endregion
+            }
+
             return Ok(await _goodsRep.GoodsReceiveList(dto));
         }
 

+ 5 - 0
OASystem/OASystem.Domain/Dtos/PersonnelModule/GoodsDTO.cs

@@ -243,6 +243,11 @@ namespace OASystem.Domain.Dtos.PersonnelModule
         /// AuditIds
         /// </summary>
         public string AuditLabel { get; set; }
+
+        /// <summary>
+        /// 是否导出Excel
+        /// </summary>
+        public bool IsExcelDownload { get; set; } = false;
     }
 
     /// <summary>

+ 20 - 0
OASystem/OASystem.Domain/ViewModels/PersonnelModule/GoodsInfoView.cs

@@ -238,4 +238,24 @@ namespace OASystem.Domain.ViewModels.PersonnelModule
         public int StorageId { get; set; }
         public decimal Quantity { get; set; }
     }
+
+
+    public class GoodsStorageExcelDownloadView
+    {
+        public int Id { get; set; }
+        public string Name { get; set; }
+        /// <summary>
+        /// 开始库存
+        /// </summary>
+        public decimal SQ_Total { get; set; }
+        /// <summary>
+        /// 入库库存
+        /// </summary>
+        public decimal SQ_Total1 { get; set; }
+        public string StorageLabel { get; set; }
+        public decimal OQ_Total { get; set; }
+        public string ReceiveLabel { get; set; }
+        public decimal StockQuantity { get; set; }
+        public string Unit { get; set; }
+    }
 }

+ 3 - 0
OASystem/OASystem.Infrastructure/OASystem.Infrastructure.csproj

@@ -38,6 +38,9 @@
   </ItemGroup>
 
   <ItemGroup>
+    <Reference Include="Aspose.Cells">
+      <HintPath>..\OASystem.Api\bin\Debug\net6.0\Aspose.Cells.dll</HintPath>
+    </Reference>
     <Reference Include="Aspose.Words">
       <HintPath>..\package\Aspose.Words.dll</HintPath>
     </Reference>

+ 0 - 3
OASystem/OASystem.Infrastructure/Repositories/Groups/DelegationInfoRepository.cs

@@ -171,7 +171,6 @@ namespace OASystem.Infrastructure.Repositories.Groups
         }
         #region 团组
 
-
         /// <summary>
         /// 统一团组国家分割字符
         /// </summary>
@@ -1102,7 +1101,6 @@ namespace OASystem.Infrastructure.Repositories.Groups
 
         #endregion
 
-
         #region 团组&出入境费用
 
         /// <summary>
@@ -1139,7 +1137,6 @@ ORDER BY
             result.Data = await _sqlSugar.SqlQueryable<EnterExitCostGroupNameView>(groupSql).ToListAsync();
             result.Code = 0;
             return result;
-
         }
 
         #endregion

+ 225 - 40
OASystem/OASystem.Infrastructure/Repositories/PersonnelModule/GoodsRepository.cs

@@ -1,11 +1,21 @@
-using AutoMapper;
+using Aspose.Cells;
+using Aspose.Cells.Drawing;
+using Aspose.Words.Drawing;
+using AutoMapper;
+using EyeSoft.IO;
 using EyeSoft.Reflection;
 using Newtonsoft.Json;
+using NPOI.OpenXmlFormats.Vml;
+using NPOI.SS.UserModel;
+using NPOI.Util;
 using OASystem.Domain.Dtos.PersonnelModule;
 using OASystem.Domain.Entities.Groups;
 using OASystem.Domain.Entities.PersonnelModule;
 using OASystem.Domain.ViewModels.PersonnelModule;
+using OASystem.Infrastructure.Tools;
 using System.Drawing;
+using System.IO;
+using System.Security.Policy;
 
 namespace OASystem.Infrastructure.Repositories.PersonnelModule
 {
@@ -17,10 +27,18 @@ namespace OASystem.Infrastructure.Repositories.PersonnelModule
     {
         private readonly IMapper _mapper;
         private JsonView _jv;
+        private string _url;
+        private string _excelPath;
         public GoodsRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
         {
             _mapper = mapper;
             _jv = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "操作失败!" };
+            _url = AppSettingsHelper.Get("ExcelBaseUrl");
+            _excelPath = $"{AppSettingsHelper.Get("ExcelBasePath")}";
+            if (!Directory.Exists(_excelPath))
+            {
+                Directory.CreateDirectory(_excelPath);
+            }
         }
 
         /// <summary>
@@ -523,6 +541,136 @@ namespace OASystem.Infrastructure.Repositories.PersonnelModule
             return _jv;
         }
 
+        /// <summary>
+        /// 物品入库
+        /// excelDownload
+        /// </summary>
+        /// <param name="dto"></param>
+        /// <returns></returns>
+        public async Task<JsonView> GoodsStorageExcelDownload()
+        {
+            var fileName = $"物资入库{Guid.NewGuid()}.xlsx";
+
+            var excelTempPath = $"{_excelPath}Template/物资入库Temp.xlsx";
+            if (!File.Exists(excelTempPath))
+            {
+                _jv.Code = StatusCodes.Status204NoContent;
+                _jv.Msg = $"该模板文件不存在!";
+                return _jv;
+            }
+
+            _url = $"{_url}Office/Excel/GoodsFiles/";
+            _excelPath = $"{_excelPath}GoodsFiles";
+            if (!Directory.Exists(_excelPath))
+            {
+                Directory.CreateDirectory(_excelPath);
+            }
+
+            //入库记录
+            var storageData = await _sqlSugar.Queryable<Pm_GoodsStorage>()
+                .LeftJoin<Sys_Users>((gs, su) => gs.StorageUserId == su.Id)
+                .Where((gs, su) => gs.IsDel == 0)
+                .Select((gs, su) => new
+                {
+                    gs.GoodsId,
+                    gs.Quantity,
+                    su.CnName,
+                    gs.StorageTime
+                })
+                .ToListAsync();
+
+            //出库记录
+            var receiveData = await _sqlSugar.Queryable<Pm_GoodsReceive>()
+                .LeftJoin<Sys_Users>((gr, su) => gr.CreateUserId == su.Id)
+                .Where((gr, su) => gr.IsDel == 0)
+                .Select((gr, su) => new
+                {
+                    gr.GoodsId,
+                    gr.Quantity,
+                    su.CnName,
+                    gr.CreateTime,
+                    gr.Reason
+                })
+                .ToListAsync();
+
+            var data = await _sqlSugar.Queryable<Pm_GoodsInfo>()
+                .Where(gi => gi.IsDel == 0)
+                .Select(gi => new GoodsStorageExcelDownloadView
+                {
+                    Id = gi.Id,
+                    Name = gi.Name,
+                    SQ_Total = gi.SQ_Total,
+                    OQ_Total = gi.OQ_Total,
+                    StockQuantity = gi.StockQuantity,
+                    Unit = gi.Unit
+                })
+                .ToListAsync();
+
+            foreach (var item in data)
+            {
+                var storageData1 = storageData.Where(x => x.GoodsId == item.Id).ToList();
+                if (storageData1.Any())
+                {
+                    item.SQ_Total1 = storageData1.Sum(x => x.Quantity);
+                    item.SQ_Total -= item.SQ_Total1;
+                    item.StorageLabel = string.Join("\r\n", storageData1.Select(x => "数量:【" + x.Quantity.ToString("#0.00") + "】  入库人:【" + x.CnName + "】  入库时间:【" + x.StorageTime + "】").ToList());
+                }
+
+                var receiveData1 = receiveData.Where(x => x.GoodsId == item.Id).ToList();
+                if (receiveData1.Any())
+                {
+                    item.ReceiveLabel = string.Join("\r\n", receiveData1.Select(x => "数量:【" + x.Quantity.ToString("#0.00") + "】  申请人:【" + x.CnName + "】  申请时间:【" + x.CreateTime.ToString("yyyy-MM-dd HH:mm:ss") + "】  原因:【" + x.Reason + "】").ToList());
+                }
+                
+            }
+
+            //载入模板
+            WorkbookDesigner designer = new WorkbookDesigner();
+            designer.Workbook = new Workbook(excelTempPath);
+
+            designer.SetDataSource("Export", data);
+
+            designer.Process();
+
+            #region 渲染Cell批注
+            var sheet = designer.Workbook.Worksheets[0];
+            for (int i = 0; i < data.Count; i++)
+            {
+                string storageComment = $"C{i + 2}",
+                       receiveComment = $"D{i + 2}",
+                       storageCommentText = data[i].StorageLabel,
+                       receiveCommentText = data[i].ReceiveLabel;
+                if (!string.IsNullOrEmpty(storageCommentText))
+                {
+                    int storageIndex = sheet.Comments.Add(storageComment);
+                    Aspose.Cells.Comment comment = sheet.Comments[storageIndex];
+                    comment.Note = storageCommentText;
+                    comment.Width = 500;
+                    comment.Height = 200;
+                    //comment.Font.Color = Color.Red;
+                }
+                if (!string.IsNullOrEmpty(receiveCommentText))
+                {
+                    int receiveIndex = sheet.Comments.Add(receiveComment);
+                    Aspose.Cells.Comment comment = sheet.Comments[receiveIndex];
+                    comment.Note = receiveCommentText;
+                    comment.Width = 800;
+                    comment.Height = 200;
+                    //comment.Font.Color = Color.Red;
+                }
+            }
+
+            #endregion
+
+
+            string serverPath = $"{_url}{fileName}";
+            designer.Workbook.Save($"{_excelPath}/{fileName}");
+
+            _jv.Code = StatusCodes.Status200OK;
+            _jv.Data = new { url = serverPath };
+            _jv.Msg = $"操作成功";
+            return _jv;
+        }
 
         /// <summary>
         /// 物品领用列表
@@ -589,53 +737,90 @@ namespace OASystem.Infrastructure.Repositories.PersonnelModule
             var endBool = DateTime.TryParse(!string.IsNullOrEmpty(dto.EndDt) ? $"{dto.EndDt} 00:00:00" : string.Empty, out var end);
 
             RefAsync<int> total = 0;
-            var data = await _sqlSugar.Queryable<Pm_GoodsReceive>()
-                                      .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
-                                      .LeftJoin<Sys_SetData>((gr, gi, sd) => gi.Type == sd.Id)
-                                      .LeftJoin<Sys_Users>((gr, gi, sd, u1) => gr.AuditUserId == u1.Id)
-                                      .LeftJoin<Sys_Users>((gr, gi, sd, u1, u2) => gr.CreateUserId == u2.Id)
-                                      .LeftJoin<Grp_DelegationInfo>((gr, gi, sd, u1, u2, di) => gr.GroupId == di.Id)
-                                      .Where((gr, gi, sd, u1, u2, di) => gr.IsDel == 0)
-                                      .WhereIF(dto.GoodsId > 0, (gr, gi, sd, u1, u2, di) => gr.GoodsId == dto.GoodsId)
-                                      .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), (gr, gi, sd, u1, u2, di) => gi.Name.Contains(dto.GoodsName))
-                                      .WhereIF(auditLabel.Length > 0, (gr, gi, sd, u1, u2, di) => auditLabel.Contains((int)gr.AuditStatus))
-                                      .WhereIF(typeLabel.Length > 0, (gr, gi, sd, u1, u2, di) => typeLabel.Contains(gi.Type))
-                                      .WhereIF(userLabel.Length > 0, (gr, gi, sd, u1, u2, di) => userLabel.Contains(gr.CreateUserId))
-                                      .WhereIF(groupLabel.Length > 0, (gr, gi, sd, u1, u2, di) => groupLabel.Contains(gr.GroupId))
-                                      .WhereIF(beginBool && endBool, (gr, gi, sd, u1, u2, di) => gr.CreateTime >= begin && gr.CreateTime <= end)
-                                      .Select((gr, gi, sd, u1, u2, di) => new GoodsReceiveListMobileView
-                                      {
-                                          Id = gr.Id,
-                                          GroupId = gr.GroupId,
-                                          GroupName = di.TeamName,
-                                          GoodsId = gr.GoodsId,
-                                          GoodsName = gi.Name,
-                                          GoodsType = sd.Name,
-                                          Quantity = gr.Quantity,
-                                          Unit = gi.Unit,
-                                          Reason = gr.Reason,
-                                          Remark = gr.Remark,
-                                          AuditStatus = gr.AuditStatus,
-                                          //AuditStatusText = gr.AuditStatus.GetEnumDescription(),
-                                          AuditUserId = gr.AuditUserId,
-                                          AuditUserName = u1.CnName,
-                                          AuditTime = gr.AuditTime,
-                                          CreateUserName = u2.CnName,
-                                          CreateTime = gr.CreateTime
-                                      })
-                                      .OrderByDescending(gr => gr.CreateTime)
-                                      .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
+            var data = _sqlSugar.Queryable<Pm_GoodsReceive>()
+                .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
+                .LeftJoin<Sys_SetData>((gr, gi, sd) => gi.Type == sd.Id)
+                .LeftJoin<Sys_Users>((gr, gi, sd, u1) => gr.AuditUserId == u1.Id)
+                .LeftJoin<Sys_Users>((gr, gi, sd, u1, u2) => gr.CreateUserId == u2.Id)
+                .LeftJoin<Grp_DelegationInfo>((gr, gi, sd, u1, u2, di) => gr.GroupId == di.Id)
+                .Where((gr, gi, sd, u1, u2, di) => gr.IsDel == 0)
+                .WhereIF(dto.GoodsId > 0, (gr, gi, sd, u1, u2, di) => gr.GoodsId == dto.GoodsId)
+                .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), (gr, gi, sd, u1, u2, di) => gi.Name.Contains(dto.GoodsName))
+                .WhereIF(auditLabel.Length > 0, (gr, gi, sd, u1, u2, di) => auditLabel.Contains((int)gr.AuditStatus))
+                .WhereIF(typeLabel.Length > 0, (gr, gi, sd, u1, u2, di) => typeLabel.Contains(gi.Type))
+                .WhereIF(userLabel.Length > 0, (gr, gi, sd, u1, u2, di) => userLabel.Contains(gr.CreateUserId))
+                .WhereIF(groupLabel.Length > 0, (gr, gi, sd, u1, u2, di) => groupLabel.Contains(gr.GroupId))
+                .WhereIF(beginBool && endBool, (gr, gi, sd, u1, u2, di) => gr.CreateTime >= begin && gr.CreateTime <= end)
+                .Select((gr, gi, sd, u1, u2, di) => new GoodsReceiveListMobileView
+                {
+                    Id = gr.Id,
+                    GroupId = gr.GroupId,
+                    GroupName = di.TeamName,
+                    GoodsId = gr.GoodsId,
+                    GoodsName = gi.Name,
+                    GoodsType = sd.Name,
+                    Quantity = gr.Quantity,
+                    Unit = gi.Unit,
+                    Reason = gr.Reason,
+                    Remark = gr.Remark,
+                    AuditStatus = gr.AuditStatus,
+                    //AuditStatusText = gr.AuditStatus.GetEnumDescription(),
+                    AuditUserId = gr.AuditUserId,
+                    AuditUserName = u1.CnName,
+                    AuditTime = gr.AuditTime,
+                    CreateUserName = u2.CnName,
+                    CreateTime = gr.CreateTime
+                })
+                .OrderByDescending(gr => gr.CreateTime);
+                                     
 
+            //excel导出
+            if (dto.IsExcelDownload)
+            {
+                var fileName = $"物资领用{Guid.NewGuid()}.xlsx";
 
-            
+                var excelTempPath = $"{_excelPath}Template/物资领用Temp.xlsx";
+                if (!File.Exists(excelTempPath))
+                {
+                    _jv.Code = StatusCodes.Status204NoContent;
+                    _jv.Msg = $"该模板文件不存在!";
+                    return _jv;
+                }
+
+                _url = $"{_url}Office/Excel/GoodsFiles/";
+                _excelPath = $"{_excelPath}GoodsFiles";
+                if (!Directory.Exists(_excelPath))
+                {
+                    Directory.CreateDirectory(_excelPath);
+                }
+
+                //载入模板
+                WorkbookDesigner designer = new WorkbookDesigner();
+                designer.Workbook = new Workbook(excelTempPath);
+
+                var tableData = await data.ToListAsync();
+                designer.SetDataSource("Export", tableData);
+
+                designer.Process();
+
+                string serverPath = $"{_url}{fileName}";
+                designer.Workbook.Save($"{_excelPath}/{fileName}");
 
+                _jv.Code = StatusCodes.Status200OK;
+                _jv.Data = new { url = serverPath };
+                _jv.Msg = $"操作成功";
+                return _jv;
+            }
+            
+            //返回分页数据
+            var view = await data.ToPageListAsync(dto.PageIndex, dto.PageSize, total);
             if (dto.PortType == 2 || dto.PortType == 3)
             {
-                _jv.Data = data;
+                _jv.Data = view;
             }
             else if (dto.PortType == 1)
             {
-                _jv.Data = _mapper.Map<List<GoodsReceiveListView>>(data);
+                _jv.Data = _mapper.Map<List<GoodsReceiveListView>>(view);
             }
 
             _jv.Code = StatusCodes.Status200OK;