using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Collections.Generic;
using System.Linq.Expressions;
using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Filters;
using ZR.Model.MES.wms;
using ZR.Model.MES.wms.Dto;
using ZR.Service.mes.wms.IService;
//创建时间:2024-03-16
namespace ZR.Admin.WebApi.Controllers
{
///
/// 物料记录表增删改查
///
[Verify]
[Route("/mes/wm/WmMaterial")]
public class WmMaterialController : BaseController
{
///
/// 物料记录表接口
///
private readonly IWmMaterialService _WmMaterialService;
public WmMaterialController(IWmMaterialService WmMaterialService)
{
_WmMaterialService = WmMaterialService;
}
///
/// 查询物料记录表列表
///
///
///
[HttpGet("list")]
[ActionPermissionFilter(Permission = "wms:wmmaterial:list")]
public IActionResult QueryWmMaterial([FromQuery] WmMaterialQueryDto parm)
{
var response = _WmMaterialService.GetList(parm);
return SUCCESS(response);
}
///
/// 查询物料记录表详情
///
///
///
[HttpGet("{Id}")]
[ActionPermissionFilter(Permission = "wms:wmmaterial:query")]
public IActionResult GetWmMaterial(string Id)
{
var response = _WmMaterialService.GetInfo(Id);
return SUCCESS(response);
}
///
/// 添加物料记录表
///
///
[HttpPost]
[ActionPermissionFilter(Permission = "wms:wmmaterial:add")]
[Log(Title = "物料记录表", BusinessType = BusinessType.INSERT)]
public IActionResult AddWmMaterial([FromBody] WmMaterialDto parm)
{
var modal = parm.Adapt().ToCreate(HttpContext);
var response = _WmMaterialService.AddWmMaterial(modal);
return SUCCESS(response);
}
///
/// 更新物料记录表
///
///
[HttpPut]
[ActionPermissionFilter(Permission = "wms:wmmaterial:edit")]
[Log(Title = "物料记录表", BusinessType = BusinessType.UPDATE)]
public IActionResult UpdateWmMaterial([FromBody] WmMaterialDto parm)
{
var modal = parm.Adapt().ToUpdate(HttpContext);
var response = _WmMaterialService.UpdateWmMaterial(modal);
return ToResponse(response);
}
///
/// 删除物料记录表
///
///
[HttpDelete("{ids}")]
[ActionPermissionFilter(Permission = "wms:wmmaterial:delete")]
[Log(Title = "物料记录表", BusinessType = BusinessType.DELETE)]
public IActionResult DeleteWmMaterial(string ids)
{
long[] idsArr = Tools.SpitLongArrary(ids);
if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); }
var response = _WmMaterialService.Delete(idsArr);
return ToResponse(response);
}
[HttpGet("getInfoByPatchCode")]
[Log(Title = "物料记录表", BusinessType = BusinessType.QUERY)]
public IActionResult GetInfoByPatchCode(string patchCode)
{
if (string.IsNullOrEmpty(patchCode))
{
return SUCCESS(null);
}
WmGoodsNowProduction nowProduction = _WmMaterialService.GetInfoByPatchCode(patchCode);
return SUCCESS(nowProduction);
}
[HttpGet("download_template")]
[Log(Title = "下载物料清单模版", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)]
[AllowAnonymous] //不需要授权 就可以访问
public IActionResult DownloadTemplate()
{
(string, string) result = DownloadImportTemplate("物料清单模版");//返回文件名和路径
return ExportExcel(result.Item2, result.Item1);
}
[HttpPost("importData")]
[Log(Title = "物料清单批量导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)]
[AllowAnonymous] //不需要授权 就可以访问
public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile, bool updateSupport)
{
try
{
//1.0 读取excel 文件 保存在指定位置
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
string sFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + formFile.FileName;
string target = Path.Combine(webHostEnvironment.WebRootPath, "wmmaterial", sFileName);
if (!Directory.Exists(Path.Combine(webHostEnvironment.WebRootPath, "wmmaterial")))
{
// 如果目录不存在就创建
Directory.CreateDirectory(Path.Combine(webHostEnvironment.WebRootPath, "wmmaterial"));
}
//2.0 解析 excel
List materials = new List();
using (var stream = formFile.OpenReadStream())
{
FileStream targetFileStream = new FileStream(target, FileMode.Create, FileAccess.Write);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
targetFileStream.Write(buffer, 0, bytesRead);
}
stream.Position = 0; //这一句不加就会上面的异常错误
IWorkbook workbook = new XSSFWorkbook(stream);
ISheet sheet = workbook.GetSheetAt(0); // 读取第一个工作表
for (int i = 2; i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
if (row != null)
{
if (row.GetCell(0) != null && row.GetCell(0).ToString() != "")
{
WmMaterial material = new WmMaterial();
material.Partnumber = row.GetCell(0)?.ToString();
material.U8InventoryCode = row.GetCell(1)?.ToString();
material.BlankNum = row.GetCell(2)?.ToString();
material.Unit = row.GetCell(3)?.ToString();
material.ProductName = row.GetCell(4)?.ToString();
material.Color = row.GetCell(5)?.ToString();
material.Specification = row.GetCell(6)?.ToString();
material.Description = row.GetCell(7)?.ToString();
material.Version = row.GetCell(8)?.ToString();
material.Remarks = row.GetCell(9)?.ToString();
material.Search1 = row.GetCell(10)?.ToString();
material.Search2 = row.GetCell(11)?.ToString();
material.Status = 1;
material.Sort = i;
material.ToCreate(HttpContext);
materials.Add(material);
}
}
}
}
(int, int) result = _WmMaterialService.ExcelADD(materials);
return SUCCESS(result);
}
catch (Exception ex)
{
return ToResponse(new ApiResult(210, "文件异常"+ex.Message));
}
}
}
}