using System.Dynamic; using MES.Service.Dto.service; using MES.Service.Modes; using MES.Service.service.QC; using MES.Service.util; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; namespace MESApplication.Controllers.QC; [Route("api/[controller]")] [ApiController] public class LljController : ControllerBase { [HttpPost("getPage")] public ResponseResult getPage([FromBody] XJPageResult queryObj) { try { dynamic resultInfos = new ExpandoObject(); var (item, totalCount) = new LljService().GetPage(queryObj); var tbBillList = resultInfos.tbBillList = item; return new ResponseResult { status = 0, message = "OK", data = resultInfos, TotalCount = totalCount }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } [HttpPost("setJYItem")] public ResponseResult setJYItem([FromBody] JObject data) { var itemNo = Convert.ToDecimal(data["itemNo"].ToString()); var quantity = Convert.ToDecimal(data["quantity"].ToString()); var releaseNo = data["releaseNo"].ToString(); try { dynamic resultInfos = new ExpandoObject(); var tbBillList = new LljService().SetItems(itemNo, quantity, releaseNo); resultInfos.tbBillList = tbBillList; return new ResponseResult { status = 0, message = "OK", data = resultInfos }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } [HttpPost("saveItem")] public ResponseResult saveItem([FromBody] LLJDto lljDto) { try { dynamic resultInfos = new ExpandoObject(); var tbBillList = new LljService().saveItem(lljDto); resultInfos.tbBillList = tbBillList; return new ResponseResult { status = 0, message = "OK", data = resultInfos }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } //getItems [HttpPost("getJYItem")] public ResponseResult getJYItem([FromBody] JObject data) { var id = data["id"]?.ToString(); var releaseNo = data["releaseNo"]?.ToString(); try { dynamic resultInfos = new ExpandoObject(); var tbBillList = new LljService().GetItems(releaseNo, Convert.ToDecimal(id)); resultInfos.tbBillList = tbBillList; return new ResponseResult { status = 0, message = "OK", data = resultInfos }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } //getXjDetail02ById [HttpPost("getXjDetail02ById")] public ResponseResult getXjDetail02ById([FromBody] JObject data) { var id = data["id"]?.ToString(); try { dynamic resultInfos = new ExpandoObject(); var tbBillList = new LljService().getXjDetail02ById(Convert.ToDecimal(id)); resultInfos.tbBillList = tbBillList; return new ResponseResult { status = 0, message = "OK", data = resultInfos }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } [HttpPost("SetQSItemDetail")] public ResponseResult SetQSItemDetail( [FromBody] MesQaItemsDetectDetail12 detail) { try { dynamic resultInfos = new ExpandoObject(); var tbBillList = new LljService(); var detail021 = tbBillList.SetQSItemDetail(detail); resultInfos.tbBillList = detail021; return new ResponseResult { status = 0, message = "OK", data = resultInfos }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } [HttpPost("UpdateQSItemDetail")] public ResponseResult UpdateQSItemDetail( [FromBody] MesQaItemsDetectDetail12 detail) { try { dynamic resultInfos = new ExpandoObject(); var tbBillList = new LljService(); var detail021 = tbBillList.UpdateQSItemDetail(detail); resultInfos.tbBillList = detail021; return new ResponseResult { status = 0, message = "OK", data = resultInfos }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } //saveRemarksGid 主表添加不合格描述 [HttpPost("saveRemarksGid")] public ResponseResult saveRemarksGid([FromBody] LLJDto rkjDto) { try { dynamic resultInfos = new ExpandoObject(); var tbBillList = new LljService().saveRemarksGid(rkjDto); resultInfos.tbBillList = tbBillList; return new ResponseResult { status = 0, message = "OK", data = resultInfos }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } //saveRemarksPid 子表添加不合格描述 [HttpPost("saveRemarksPid")] public ResponseResult saveRemarksPid([FromBody] LLJDto rkjDto) { try { dynamic resultInfos = new ExpandoObject(); var tbBillList = new LljService().saveRemarksPid(rkjDto); resultInfos.tbBillList = tbBillList; return new ResponseResult { status = 0, message = "OK", data = resultInfos }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } //removeXJ 删除 [HttpPost("removeXJ")] public ResponseResult removeXJ([FromBody] JObject data) { var releaseNo = data["releaseNo"]?.ToString(); try { dynamic resultInfos = new ExpandoObject(); var tbBillList = new LljService().removeXJ(releaseNo); resultInfos.tbBillList = tbBillList; return new ResponseResult { status = 0, message = "OK", data = resultInfos }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } //IqcQaSubmit [HttpPost("IqcQaSubmit")] public ResponseResult IqcQaSubmit(LLJDto rkjDto) { try { dynamic resultInfos = new ExpandoObject(); var tbBillList = new LljService().IqcQaSubmit(rkjDto); resultInfos.tbBillList = tbBillList; return new ResponseResult { status = 0, message = "OK", data = resultInfos }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } [HttpPost("EmergencyRelease")] public ResponseResult EmergencyRelease([FromBody] JObject data) { var id = Convert.ToInt32(data["id"].ToString()); try { dynamic resultInfos = new ExpandoObject(); var tbBillList = new LljService().EmergencyRelease(id); resultInfos.tbBillList = tbBillList; return new ResponseResult { status = 0, message = "OK", data = resultInfos }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } [HttpPost("WithdrawEmergencyRelease")] public ResponseResult WithdrawEmergencyRelease([FromBody] JObject data) { var id = Convert.ToInt32(data["id"].ToString()); try { dynamic resultInfos = new ExpandoObject(); var tbBillList = new LljService().WithdrawEmergencyRelease(id); resultInfos.tbBillList = tbBillList; return new ResponseResult { status = 0, message = "OK", data = resultInfos }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } [HttpPost("getAttachments")] public ResponseResult GetAttachments([FromBody] JObject data) { var releaseNo = data["releaseNo"]?.ToString(); try { dynamic resultInfos = new System.Dynamic.ExpandoObject(); var tbBillList = new LljService().GetAttachments(releaseNo); if (tbBillList == null || tbBillList.Count == 0) { return new ResponseResult { status = 1, message = "该检验单未上传附件信息!", data = null }; } resultInfos.tbBillList = tbBillList; return new ResponseResult { status = 0, message = "OK", data = resultInfos }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } [HttpGet("PreviewFtpFile")] public IActionResult PreviewFtpFile([FromQuery] string itemNo, [FromQuery] string fileName, [FromQuery] string ftpServer) { try { // 添加CORS响应头 Response.Headers.Add("Access-Control-Allow-Origin", "*"); Response.Headers.Add("Access-Control-Allow-Methods", "GET, OPTIONS"); Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type"); Response.Headers.Add("Access-Control-Expose-Headers", "Content-Type, Content-Length"); var service = new LljService(); var fileBytes = service.GetFtpFile(itemNo, fileName, ftpServer); if (fileBytes == null || fileBytes.Length == 0) { return NotFound("文件在FTP服务器上不存在"); } var contentType = service.GetContentType(fileName); fileName = fileName?.Trim().Replace("\r", "").Replace("\n", ""); return File(fileBytes, contentType); } catch (Exception ex) { return StatusCode(500, $"预览文件失败:{ex.Message}"); } } [HttpGet("DownloadFtpFile")] public IActionResult DownloadFtpFile([FromQuery] string itemNo, [FromQuery] string fileName, [FromQuery] string ftpServer) { try { // 添加CORS响应头 - 关键配置用于解决跨域问题 Response.Headers.Add("Access-Control-Allow-Origin", "*"); Response.Headers.Add("Access-Control-Allow-Methods", "GET, OPTIONS"); Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Authorization"); Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition, Content-Length, Content-Type"); var service = new LljService(); var fileBytes = service.GetFtpFile(itemNo, fileName, ftpServer); if (fileBytes == null || fileBytes.Length == 0) { return NotFound("文件在FTP服务器上不存在"); } var contentType = service.GetContentType(fileName); fileName = fileName?.Trim().Replace("\r", "").Replace("\n", ""); // 设置正确的Content-Disposition响应头以支持文件下载 var result = File(fileBytes, "application/octet-stream", fileName); // 确保Content-Disposition头正确设置,支持中文文件名 if (!string.IsNullOrEmpty(fileName)) { var encodedFileName = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8); Response.Headers.Add("Content-Disposition", $"attachment; filename*=UTF-8''{encodedFileName}"); } return result; } catch (Exception ex) { return StatusCode(500, $"下载文件失败:{ex.Message}"); } } [HttpOptions("PreviewFtpFile")] [HttpOptions("DownloadFtpFile")] public IActionResult HandleOptions() { // 处理CORS预检请求 Response.Headers.Add("Access-Control-Allow-Origin", "*"); Response.Headers.Add("Access-Control-Allow-Methods", "GET, OPTIONS"); Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Authorization"); Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition, Content-Length, Content-Type"); Response.Headers.Add("Access-Control-Max-Age", "86400"); return Ok(); } }