shgx_tz_mom/ZR.Admin.WebApi/Controllers/mes/wms/WmMaterialController.cs

224 lines
8.5 KiB
C#
Raw Normal View History

2024-03-17 14:53:16 +08:00
using Microsoft.AspNetCore.Mvc;
2024-04-15 18:25:01 +08:00
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
2024-04-28 23:12:08 +08:00
using SqlSugar;
2024-03-17 14:53:16 +08:00
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-17 14:53:16 +08:00
//创建时间2024-03-16
namespace ZR.Admin.WebApi.Controllers
{
/// <summary>
/// 物料记录表增删改查
/// </summary>
[Verify]
[Route("/mes/wm/WmMaterial")]
public class WmMaterialController : BaseController
{
/// <summary>
/// 物料记录表接口
/// </summary>
private readonly IWmMaterialService _WmMaterialService;
public WmMaterialController(IWmMaterialService WmMaterialService)
{
_WmMaterialService = WmMaterialService;
}
/// <summary>
/// 查询物料记录表列表
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
[HttpGet("list")]
[ActionPermissionFilter(Permission = "wms:wmmaterial:list")]
public IActionResult QueryWmMaterial([FromQuery] WmMaterialQueryDto parm)
{
var response = _WmMaterialService.GetList(parm);
return SUCCESS(response);
}
/// <summary>
/// 查询物料记录表详情
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
[HttpGet("{Id}")]
[ActionPermissionFilter(Permission = "wms:wmmaterial:query")]
public IActionResult GetWmMaterial(string Id)
{
var response = _WmMaterialService.GetInfo(Id);
2024-03-19 11:08:28 +08:00
return SUCCESS(response);
2024-03-17 14:53:16 +08:00
}
/// <summary>
/// 添加物料记录表
/// </summary>
/// <returns></returns>
[HttpPost]
[ActionPermissionFilter(Permission = "wms:wmmaterial:add")]
[Log(Title = "物料记录表", BusinessType = BusinessType.INSERT)]
public IActionResult AddWmMaterial([FromBody] WmMaterialDto parm)
{
var modal = parm.Adapt<WmMaterial>().ToCreate(HttpContext);
var response = _WmMaterialService.AddWmMaterial(modal);
return SUCCESS(response);
}
/// <summary>
/// 更新物料记录表
/// </summary>
/// <returns></returns>
[HttpPut]
[ActionPermissionFilter(Permission = "wms:wmmaterial:edit")]
[Log(Title = "物料记录表", BusinessType = BusinessType.UPDATE)]
public IActionResult UpdateWmMaterial([FromBody] WmMaterialDto parm)
{
var modal = parm.Adapt<WmMaterial>().ToUpdate(HttpContext);
var response = _WmMaterialService.UpdateWmMaterial(modal);
return ToResponse(response);
}
/// <summary>
/// 删除物料记录表
/// </summary>
/// <returns></returns>
[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 不能为空")); }
2024-03-19 11:08:28 +08:00
var response = _WmMaterialService.Delete(idsArr);
2024-03-17 14:53:16 +08:00
return ToResponse(response);
}
2024-03-23 14:31:50 +08:00
[HttpGet("getInfoByPatchCode")]
[Log(Title = "物料记录表", BusinessType = BusinessType.QUERY)]
public IActionResult GetInfoByPatchCode(string patchCode)
{
if (string.IsNullOrEmpty(patchCode))
{
return SUCCESS(null);
}
WmGoodsNowProduction nowProduction = _WmMaterialService.GetInfoByPatchCode(patchCode);
2024-03-23 14:31:50 +08:00
return SUCCESS(nowProduction);
}
2024-04-15 11:36:08 +08:00
[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);
}
2024-04-15 18:25:01 +08:00
[HttpPost("importData")]
2024-04-15 11:36:08 +08:00
[Log(Title = "物料清单批量导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)]
[AllowAnonymous] //不需要授权 就可以访问
public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile, bool updateSupport)
{
2024-04-16 08:40:41 +08:00
try
2024-04-15 11:36:08 +08:00
{
2024-04-16 08:40:41 +08:00
//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"));
2024-04-15 11:36:08 +08:00
2024-04-16 08:40:41 +08:00
}
//2.0 解析 excel
2024-04-15 11:36:08 +08:00
2024-04-16 08:40:41 +08:00
List<WmMaterial> materials = new List<WmMaterial>();
using (var stream = formFile.OpenReadStream())
2024-04-15 11:36:08 +08:00
{
2024-04-16 08:40:41 +08:00
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);
}
2024-04-15 11:36:08 +08:00
2024-04-16 08:40:41 +08:00
stream.Position = 0; //这一句不加就会上面的异常错误
IWorkbook workbook = new XSSFWorkbook(stream);
ISheet sheet = workbook.GetSheetAt(0); // 读取第一个工作表
2024-04-15 18:25:01 +08:00
2024-04-15 11:36:08 +08:00
2024-04-28 23:12:08 +08:00
var time = DateTime.Now.ToLocalTime();
2024-04-16 08:40:41 +08:00
for (int i = 2; i <= sheet.LastRowNum; i++)
2024-04-15 18:25:01 +08:00
{
2024-04-16 08:40:41 +08:00
IRow row = sheet.GetRow(i);
if (row != null)
2024-04-15 18:25:01 +08:00
{
2024-04-16 08:40:41 +08:00
if (row.GetCell(0) != null && row.GetCell(0).ToString() != "")
{
2024-06-07 11:04:26 +08:00
2024-04-16 08:40:41 +08:00
WmMaterial material = new WmMaterial();
2024-04-28 23:12:08 +08:00
// SnowFlakeSingle.Instance.NextId().ToString();
material.Id = SnowFlakeSingle.Instance.NextId().ToString();
2024-04-16 08:40:41 +08:00
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();
2024-06-07 11:04:26 +08:00
material.Type = int.Parse(row.GetCell(12).ToString());
2024-04-16 08:40:41 +08:00
material.Status = 1;
material.Sort = i;
2024-04-28 23:12:08 +08:00
material.CreatedBy = "Excel导入";
material.CreatedTime = time;
//material.ToCreate(HttpContext);
2024-04-16 08:40:41 +08:00
materials.Add(material);
}
2024-04-15 18:25:01 +08:00
}
}
2024-04-16 08:40:41 +08:00
2024-04-15 18:25:01 +08:00
}
2024-04-15 11:36:08 +08:00
2024-04-16 08:40:41 +08:00
(int, int) result = _WmMaterialService.ExcelADD(materials);
return SUCCESS(result);
}
catch (Exception ex)
{
2024-06-07 11:04:26 +08:00
return ToResponse(new ApiResult(210, "文件异常" + ex.Message));
2024-04-15 11:36:08 +08:00
}
2024-04-16 08:40:41 +08:00
2024-04-15 11:36:08 +08:00
}
2024-03-23 14:31:50 +08:00
2024-03-17 14:53:16 +08:00
}
}