using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using System;
using WMS.Entity.BllQualityEntity;
using WMS.IBLL.IBllQualityServer;
using Model.ModelVm.BllQualityVm;
using Model.ModelVm;
using Wms.Tools;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Linq;
namespace Wms.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize]
public class BllQualityController : ControllerBase
{
private readonly IQualityInspectServer _qualityServer; //质检信息
private readonly IInspectionRequest _inspectionRequestServer; // 质检请验
private readonly ApiUrlConfig _config; //接口交互路径
///
/// 构造函数
///
/// 质检信息
public BllQualityController(IQualityInspectServer qualityServer,IInspectionRequest inspectionRequestServer, IOptions setting)
{
_qualityServer = qualityServer;
_inspectionRequestServer = inspectionRequestServer;
_config = setting.Value;
}
#region 质检信息
///
/// 获取质检信息
///
/// 质检信息实体模型
///
[HttpPost]
public IActionResult GetBllQualityList(BllQualityInspect model)
{
try
{
var bolls = _qualityServer.GetBllQualityList(model);
return Ok(new { code = 0, msg = "质检信息", data = bolls });
}
catch (Exception e)
{
return Ok(new { code = 1, msg = e.Message });
}
}
///
/// 添加物料质检信息
///
/// 质检信息实体模型
///
[HttpPost]
public IActionResult InsertQuality(BllQualityInspect model)
{
try
{
//获取当前登录的用户ID
var claimsIdentity = this.User.Identity as ClaimsIdentity;
if (claimsIdentity == null)
{
return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
}
var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
if (string.IsNullOrWhiteSpace(userId))
{
return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
}
model.CreateUser = int.Parse(userId);
_qualityServer.InsertQuality(model);
return Ok(new { code = 0, count = 0, msg = "物料质检信息操作成功", data = "" });
}
catch (Exception e)
{
return Ok(new { code = 1, msg = e.Message });
}
}
#endregion
#region 质检请验
///
/// 获取请验单信息
///
/// 查询条件
///
[HttpPost]
public IActionResult GetInspectionRequest(BllQualityInspectionRequestVm model)
{
try
{
var models = _inspectionRequestServer.GetInspectionRequest(model, out int count);
return Ok(new { code = 0, count, msg = "获取请验单信息成功", data = models });
}
catch (Exception e)
{
return Ok(new { code = 1, msg = e.Message });
}
}
///
/// 获取请验单对应的取样信息
///
/// 查询条件
///
[HttpPost]
public IActionResult GetSamplingDetails(BllSamplingDetailsVm model)
{
try
{
var models = _inspectionRequestServer.GetSamplingDetails(model, out int count);
return Ok(new { code = 0, count, msg = "获取取样信息功能", data = models });
}
catch (Exception e)
{
return Ok(new { code = 1, msg = e.Message });
}
}
///
/// 删除取样记录
///
/// 主键ID
///
[HttpPost]
public IActionResult DelSamplingDetails(IdVm model)
{
try
{
var claimsIdentity = this.User.Identity as ClaimsIdentity;
if (claimsIdentity == null)
{
return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
}
var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
if (string.IsNullOrWhiteSpace(userId))
{
return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
}
var models = _inspectionRequestServer.DelSamplingDetails(model, int.Parse(userId));
return Ok(new { code = 0, count=0, msg = "删除取样记录成功", data = models });
}
catch (Exception e)
{
return Ok(new { code = 1, msg = e.Message });
}
}
[HttpPost]
public IActionResult upLoadRequest(IdVm model)
{
try
{
var claimsIdentity = this.User.Identity as ClaimsIdentity;
if (claimsIdentity == null)
{
return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
}
var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
if (string.IsNullOrWhiteSpace(userId))
{
return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
}
var userName = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier)?.Value;
return Ok(new { code = 0, count = 0, msg = "上传图片成功!"});
}
catch (Exception e)
{
return Ok(new { code = 1, msg = e.Message });
}
}
///
/// 删除请验单信息
///
/// 主键ID
///
[HttpPost]
public IActionResult DelInspectionRequest(IdVm model)
{
try
{
var claimsIdentity = this.User.Identity as ClaimsIdentity;
if (claimsIdentity == null)
{
return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
}
var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
if (string.IsNullOrWhiteSpace(userId))
{
return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
}
var models = _inspectionRequestServer.DelInspectionRequest(model, int.Parse(userId));
return Ok(new { code = 0, count=0, msg = "删除请验单信息成功", data = models });
}
catch (Exception e)
{
return Ok(new { code = 1, msg = e.Message });
}
}
///
/// 执行取样出库动作
///
/// 主键ID
///
[HttpPost]
public IActionResult OutWhInspectionRequest(IdVm model)
{
try
{
var claimsIdentity = this.User.Identity as ClaimsIdentity;
if (claimsIdentity == null)
{
return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
}
var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
if (string.IsNullOrWhiteSpace(userId))
{
return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
}
string url = _config.WcsHost + _config.IssueComApiUrl;
var models = _inspectionRequestServer.OutWhInspectionRequest(model, int.Parse(userId), url);
return Ok(new { code = 0, count=0, msg = "取样出库操作成功!", data = models });
}
catch (Exception e)
{
return Ok(new { code = 1, msg = e.Message });
}
}
///
/// 完成请验单据
///
/// 主键ID
///
[HttpPost]
public IActionResult ClossInspectionRequest(IdVm model)
{
try
{
var claimsIdentity = this.User.Identity as ClaimsIdentity;
if (claimsIdentity == null)
{
return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
}
var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
if (string.IsNullOrWhiteSpace(userId))
{
return Ok(new { code = 1, msg = "未获取到当前操作人信息" });
}
string url = _config.WcsHost + _config.IssueComApiUrl;
var models = _inspectionRequestServer.ClossInspectionRequest(model, int.Parse(userId),url);
return Ok(new { code = 0, msg = "完成请验单", data = models });
}
catch (Exception e)
{
return Ok(new { code = 1, msg = e.Message });
}
}
///
/// 图片上传
///
///
///
///
[HttpPost]
public async Task UploadImage([FromForm] IFormFile file, [FromForm] int qualityId)
{
try
{
var claimsIdentity = this.User.Identity as ClaimsIdentity;
var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
if (file == null || file.Length == 0)
return BadRequest(new { code = 1, msg = "请选择上传文件" });
// 验证文件类型
var allowedExtensions = new[] { ".jpg", ".jpeg", ".png", ".gif" };
var fileExtension = Path.GetExtension(file.FileName).ToLower();
if (!allowedExtensions.Contains(fileExtension))
return BadRequest(new { code = 2, msg = "只允许上传图片文件" });
// 限制文件大小 (5MB)
if (file.Length > 5 * 1024 * 1024)
return BadRequest(new { code = 3, msg = "文件大小不能超过5MB" });
// 创建质量相关的图片目录
var uploadPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwRoot", "uploads", "quality", qualityId.ToString());
if (!Directory.Exists(uploadPath))
Directory.CreateDirectory(uploadPath);
// 生成唯一文件名
var fileName = $"{DateTime.Now:yyyyMMddHHmmss}_{userId}{fileExtension}";
var filePath = Path.Combine(uploadPath, fileName);
// 保存文件
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
// 返回文件访问路径
var fileUrl = $"/wwwRoot/uploads/quality/{qualityId}/{fileName}";
// 这里可以添加数据库记录等操作
// _qualityService.AddImageRecord(qualityId, userId, fileUrl, fileName);
return Ok(new
{
code = 0,
msg = "上传成功",
data = new
{
url = fileUrl,
fileName = fileName
}
});
}
catch (Exception ex)
{
return StatusCode(500, new { code = 500, msg = $"上传失败: {ex.Message}" });
}
}
///
/// 图片获取
///
///
///
[HttpPost]
public IActionResult GetImage(BllQualityInspect model)
{
try
{
var fullPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwRoot", "uploads", "quality", model.Id.ToString());
if (!Directory.Exists(fullPath))
{
return StatusCode(1, new { code = 1, msg = "图片不存在" });
}
var files = Directory.GetFiles(fullPath);
if (files.Length <= 0)
return StatusCode(1, new { code = 1, msg = "图片不存在" });
List images = new List();
foreach (var file in files)
{
images.Add($"{Request.Scheme}://{Request.Host}/uploads/quality/{model.Id.ToString()}/{Path.GetFileName(file)}");
}
return Ok(new
{
code = 0,
msg = "图片" + files.Length + "张",
data = images
});
}
catch (Exception ex)
{
return StatusCode(500, new { code = 500, msg = $"获取图片失败: {ex.Message}" });
}
}
///
/// 删除图片
///
/// 图片信息
///
[HttpPost]
public IActionResult DeleteImage(QualityImageVM data)
{
try
{
string fileName = Path.GetFileName(data.ImageUrl);
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwRoot", "uploads", "quality", data.QualityId.ToString(), fileName);
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
return Ok(new { code = 0, msg = "删除成功" });
}
else
{
return Ok(new { code = 1, msg = "图片不存在" });
}
}
catch (Exception ex)
{
return Ok(new { code = 1, msg = ex.Message });
}
}
#endregion
}
}