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
{
///
/// 物料表
///
[Verify]
[Route("mes/productionMaterial/MmMaterial")]
public class MmMaterialController : BaseController
{
///
/// 物料表接口
///
private readonly IMmMaterialService _MmMaterialService;
public MmMaterialController(IMmMaterialService MmMaterialService)
{
_MmMaterialService = MmMaterialService;
}
///
/// 查询物料表列表
///
///
///
[HttpGet("list")]
[ActionPermissionFilter(Permission = "mmmaterial:list")]
public IActionResult QueryMmMaterial([FromQuery] MmMaterialQueryDto parm)
{
var response = _MmMaterialService.GetList(parm);
return SUCCESS(response);
}
///
/// 查询物料表详情
///
///
///
[HttpGet("{Id}")]
[ActionPermissionFilter(Permission = "mmmaterial:query")]
public IActionResult GetMmMaterial(int Id)
{
var response = _MmMaterialService.GetInfo(Id);
var info = response.Adapt();
return SUCCESS(info);
}
///
/// 添加物料表
///
///
[HttpPost]
[ActionPermissionFilter(Permission = "mmmaterial:add")]
[Log(Title = "物料表", BusinessType = BusinessType.INSERT)]
public IActionResult AddMmMaterial([FromBody] MmMaterialDto parm)
{
var modal = parm.Adapt().ToCreate(HttpContext);
var response = _MmMaterialService.AddMmMaterial(modal);
return SUCCESS(response);
}
///
/// 更新物料表
///
///
[HttpPut]
[ActionPermissionFilter(Permission = "mmmaterial:edit")]
[Log(Title = "物料表", BusinessType = BusinessType.UPDATE)]
public IActionResult UpdateMmMaterial([FromBody] MmMaterialDto parm)
{
var modal = parm.Adapt().ToUpdate(HttpContext);
var response = _MmMaterialService.UpdateMmMaterial(modal);
return ToResponse(response);
}
///
/// 删除物料表
///
///
[HttpPost("delete/{ids}")]
[ActionPermissionFilter(Permission = "mmmaterial:delete")]
[Log(Title = "物料表", BusinessType = BusinessType.DELETE)]
public IActionResult DeleteMmMaterial([FromRoute]string ids)
{
var idArr = Tools.SplitAndConvert(ids);
return ToResponse(_MmMaterialService.Delete(idArr));
}
///
/// 获取物料表类别下拉框
///
///
[HttpPost("GetMmMaterialCategoryOptions")]
[AllowAnonymous]
public IActionResult GetMmMaterialCategoryOptions([FromBody] MmMaterialCategoryDto parm)
{
var response = _MmMaterialService.GetMmMaterialCategoryOptions(parm);
return SUCCESS(response);
}
///
/// 导入
///
/// 使用IFromFile必须使用name属性否则获取不到文件
///
[HttpPost("importData")]
[Log(Title = "物料清单导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)]
[ActionPermissionFilter(Permission = "mmmaterial:import")]
public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
{
List materialList = 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;
}
}
}
#endregion
}
catch (Exception ex)
{
//return -1;
}
material = stream.Query(startCell: "A2").ToList();
}
return SUCCESS(_MmMaterialService.Importmaterial(material));
}
///
/// 下载物料导入模板
///
///
[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();
var result = DownloadImportTemplate(sample, "material");
return ExportExcel(result.Item2, result.Item1);
}
///
/// 物料清单导出
///
///
///
[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());
var result = ExportExcelMini(data, "material", "物料清单");
return ExportExcel(result.Item2, result.Item1);
}
}
}