Files
kunshan-bzfm-mes-backend/DOAN.Admin.WebApi/Controllers/MES/Material/productionMaterial/MmMaterialController.cs
2026-01-07 18:06:16 +08:00

327 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DOAN.Admin.WebApi.Filters;
using DOAN.Model;
using DOAN.Model.BZFM;
using DOAN.Model.BZFM.Dto;
using DOAN.Model.MES.product;
using DOAN.Model.System;
using DOAN.Model.System.Dto;
using DOAN.Service.BZFM;
using DOAN.Service.BZFM.IBZFMService;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
//创建时间2025-12-25
namespace DOAN.Admin.WebApi.Controllers.BZFM
{
/// <summary>
/// 物料表
/// </summary>
[Verify]
[Route("mes/productionMaterial/MmMaterial")]
public class MmMaterialController : BaseController
{
/// <summary>
/// 物料表接口
/// </summary>
private readonly IMmMaterialService _MmMaterialService;
public MmMaterialController(IMmMaterialService MmMaterialService)
{
_MmMaterialService = MmMaterialService;
}
/// <summary>
/// 查询物料表列表
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
[HttpGet("list")]
[ActionPermissionFilter(Permission = "mmmaterial:list")]
public IActionResult QueryMmMaterial([FromQuery] MmMaterialQueryDto parm)
{
var response = _MmMaterialService.GetList(parm);
return SUCCESS(response);
}
/// <summary>
/// 查询物料表详情
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
[HttpGet("{Id}")]
[ActionPermissionFilter(Permission = "mmmaterial:query")]
public IActionResult GetMmMaterial(int Id)
{
var response = _MmMaterialService.GetInfo(Id);
var info = response.Adapt<MmMaterialDto>();
return SUCCESS(info);
}
/// <summary>
/// 添加物料表
/// </summary>
/// <returns></returns>
[HttpPost]
[ActionPermissionFilter(Permission = "mmmaterial:add")]
[Log(Title = "物料表", BusinessType = BusinessType.INSERT)]
public IActionResult AddMmMaterial([FromBody] MmMaterialDto parm)
{
var modal = parm.Adapt<MmMaterial>().ToCreate(HttpContext);
var response = _MmMaterialService.AddMmMaterial(modal);
return SUCCESS(response);
}
/// <summary>
/// 更新物料表
/// </summary>
/// <returns></returns>
[HttpPut]
[ActionPermissionFilter(Permission = "mmmaterial:edit")]
[Log(Title = "物料表", BusinessType = BusinessType.UPDATE)]
public IActionResult UpdateMmMaterial([FromBody] MmMaterialDto parm)
{
var modal = parm.Adapt<MmMaterial>().ToUpdate(HttpContext);
var response = _MmMaterialService.UpdateMmMaterial(modal);
return ToResponse(response);
}
/// <summary>
/// 删除物料表
/// </summary>
/// <returns></returns>
[HttpPost("delete/{ids}")]
[ActionPermissionFilter(Permission = "mmmaterial:delete")]
[Log(Title = "物料表", BusinessType = BusinessType.DELETE)]
public IActionResult DeleteMmMaterial([FromRoute]string ids)
{
var idArr = Tools.SplitAndConvert<int>(ids);
return ToResponse(_MmMaterialService.Delete(idArr));
}
/// <summary>
/// 获取物料表类别下拉框
/// </summary>
/// <returns></returns>
[HttpPost("GetMmMaterialCategoryOptions")]
[AllowAnonymous]
public IActionResult GetMmMaterialCategoryOptions([FromBody] MmMaterialCategoryDto parm)
{
var response = _MmMaterialService.GetMmMaterialCategoryOptions(parm);
return SUCCESS(response);
}
/// <summary>
/// 导入
/// </summary>
/// <param name="formFile">使用IFromFile必须使用name属性否则获取不到文件</param>
/// <returns></returns>
[HttpPost("importData")]
[Log(Title = "物料清单导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)]
[ActionPermissionFilter(Permission = "mmmaterial:import")]
public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
{
//List<MmMaterialExcelDto> material = new();
DateTime dateValue = DateTime.MinValue;
using (var stream = formFile.OpenReadStream())
{
try
{
IWorkbook workbook = new XSSFWorkbook(stream);
ISheet sheet = workbook.GetSheetAt(0);
// 处理第2行 获取日期
IRow secondRow = sheet.GetRow(1);
NPOI.SS.UserModel.ICell cell = secondRow.GetCell(0);
// 将单元格的数字值转换为DateTime
dateValue = cell.DateCellValue.Value;
#region excel
// 遍历每一行
for (int row = 4; row <= sheet.LastRowNum; row++)
{
IRow currentRow = sheet.GetRow(row);
if (currentRow != null) // 确保行不为空
{
MmMaterial material = new MmMaterial();
//01 物料标号
NPOI.SS.UserModel.ICell currentCell_01 = currentRow.GetCell(1);
material.Type = currentCell_01?.ToString();
if (currentCell_01 == null || string.IsNullOrEmpty(material.Type))
{
continue;
}
//02物料编码
NPOI.SS.UserModel.ICell currentCell_02 = currentRow.GetCell(2);
material.MaterialCode = currentCell_02?.ToString();
if (
currentCell_02 == null
|| string.IsNullOrEmpty(material.MaterialCode)
)
{
continue;
}
//03物料名称
NPOI.SS.UserModel.ICell currentCell_03 = currentRow.GetCell(3);
material.MaterialName = currentCell_03?.ToString();
if (
currentCell_03 == null
|| string.IsNullOrEmpty(material.MaterialName)
)
{
continue;
}
//04规格
NPOI.SS.UserModel.ICell currentCell_04 = currentRow.GetCell(4);
material.Specification = currentCell_04?.ToString();
if (currentCell_04 == null || string.IsNullOrEmpty(material.Specification))
{
continue;
}
//05 类型编码
NPOI.SS.UserModel.ICell currentCell_05 = currentRow.GetCell(5);
material.CategoryCode = currentCell_05?.ToString();
if (
currentCell_05 == null
|| string.IsNullOrEmpty(material.CategoryCode)
)
{
continue;
}
//06 类型名称
NPOI.SS.UserModel.ICell currentCell_06 = currentRow.GetCell(6);
material.CategoryName = currentCell_06?.ToString();
if (
currentCell_06 == null
|| string.IsNullOrEmpty(material.CategoryName)
)
{
continue;
}
//07 单位
NPOI.SS.UserModel.ICell currentCell_07 = currentRow.GetCell(7);
material.Unit = currentCell_07?.ToString();
if (
currentCell_07 == null
|| string.IsNullOrEmpty(material.Unit)
)
{
continue;
}
//08 供应商编码
NPOI.SS.UserModel.ICell currentCell_08 = currentRow.GetCell(8);
material.SupplierCode = currentCell_08?.ToString();
if (currentCell_08 == null || string.IsNullOrEmpty(material.SupplierCode))
{
continue;
}
//09 供应商名称
NPOI.SS.UserModel.ICell currentCell_09 = currentRow.GetCell(9);
material.SupplierName = currentCell_09?.ToString();
if (
currentCell_09 == null
|| string.IsNullOrEmpty(material.SupplierName)
)
{
continue;
}
//
//10 状态
NPOI.SS.UserModel.ICell currentCell_10 = currentRow.GetCell(10);
material.Status = currentCell_10?.ToString();
if (currentCell_10 == null || string.IsNullOrEmpty(material.Status))
{
continue;
}
//10指示日期
NPOI.SS.UserModel.ICell cell17 = currentRow.GetCell(10);
// 将单元格的数字值转换为DateTime
material.CreatedTime = cell17.DateCellValue.Value;
//装箱容积
/*NPOI.SS.UserModel.ICell currentCell_0811 = currentRow.GetCell(6);
workorder.PackageCapacity = (int)currentCell_0811?.NumericCellValue;*/
}
}
#endregion
}
catch (Exception ex)
{
return -1;
}
//material = stream.Query<MmMaterialExcelDto>(startCell: "A2").ToList();
}
return SUCCESS(_MmMaterialService.Importmaterial(material));
}
/// <summary>
/// 下载物料导入模板
/// </summary>
/// <returns></returns>
[HttpGet("importTemplate")]
[Log(Title = "物料模板", BusinessType = BusinessType.EXPORT)]
[AllowAnonymous]
public IActionResult ImportTemplateExcel()
{
// create an empty sample list of export DTO to generate header row
var sample = new List<MmMaterialExcelDto>();
var result = DownloadImportTemplate(sample, "material");
return ExportExcel(result.Item2, result.Item1);
}
/// <summary>
/// 物料清单导出
/// </summary>
/// <param name="material"></param>
/// <returns></returns>
[HttpGet("export")]
[Log(Title = "物料清单导出", BusinessType = BusinessType.EXPORT)]
[ActionPermissionFilter(Permission = "mmmaterial:export")]
public IActionResult MaterialExport([FromQuery] MmMaterialQueryDto material)
{
var list = _MmMaterialService.SelectMaterialList(material, new PagerInfo(1, 10000));
var data = (list?.Result ?? new List<MmMaterialExcelDto>());
var result = ExportExcelMini(data, "material", "物料清单");
return ExportExcel(result.Item2, result.Item1);
}
}
}