xwt
2025-10-30 dabfdd9dbf0364b1134daaad86af7e13f6437295
StandardInterface/MESApplication/Controllers/QC/LljController.cs
@@ -204,6 +204,51 @@
        }
    }
    //savePhsyGid 主表添加破坏实验
    [HttpPost("savePhsyGid")]
    public ResponseResult savePhsyGid([FromBody] LLJDto rkjDto)
    {
        try
        {
            dynamic resultInfos = new ExpandoObject();
            var tbBillList =
                new LljService().savePhsyGid(rkjDto);
            resultInfos.tbBillList = tbBillList;
            return new ResponseResult
            {
                status = 0,
                message = "OK",
                data = resultInfos
            };
        }
        catch (Exception ex)
        {
            return ResponseResult.ResponseError(ex);
        }
    }
    [HttpPost("saveDropdownFields")]
    public ResponseResult saveDropdownFields([FromBody] LLJDto rkjDto)
    {
        try
        {
            dynamic resultInfos = new ExpandoObject();
            var tbBillList =
                new LljService().saveDropdownFields(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)
@@ -316,4 +361,504 @@
            return ResponseResult.ResponseError(ex);
        }
    }
    [HttpPost("getAttachments")]
    public ResponseResult GetAttachments([FromBody] JObject data)
    {
        var itemNo = data["itemNo"]?.ToString();
        var fversion = data["fversion"]?.ToString();
        var fromPage = data["fromPage"]?.ToString();
        // 根据来源页面决定是否过滤
        string filterFversion = null;
        if (fromPage == "Detail" && !string.IsNullOrEmpty(fversion))
        {
            filterFversion = fversion;  // Detail页面需要过滤
        }
        // Add页面不传递filterFversion,显示所有附件
        try
        {
            dynamic resultInfos = new System.Dynamic.ExpandoObject();
            var tbBillList = new LljService().GetAttachments(itemNo, filterFversion);
            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, [FromQuery] string fversion = null)
    {
        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, fversion);
            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, [FromQuery] string fversion = null)
    {
        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, fversion);
            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();
    }
[HttpPost("getWomdab")]
    public ResponseResult getWomdab([FromBody] GetWomdabRequest data)
    {
        var daa001 = data.daa001?.ToString();
        try
        {
            dynamic resultInfos = new System.Dynamic.ExpandoObject();
            var tbBillList = new LljService().GetWomdab(daa001);
            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);
        }
    }
    [HttpPost("GetWomdabById")]
    public ResponseResult GetWomdabById([FromBody] GetWomdabRequest data)
    {
        var daa001 = data.daa001?.ToString();
        var ItemNo = data.ItemNo?.ToString();
        try
        {
            dynamic resultInfos = new System.Dynamic.ExpandoObject();
            var tbBillList = new LljService().GetWomdabById(daa001,ItemNo);
            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);
        }
    }
    /// <summary>
    /// 根据二维码查询物料信息
    /// </summary>
    /// <param name="data">包含二维码内容和当前到货单号的请求对象</param>
    /// <returns>物料信息</returns>
    [HttpPost("GetMaterialByBarcode")]
    public ResponseResult GetMaterialByBarcode([FromBody] GetMaterialByBarcodeRequest data)
    {
        try
        {
            if (string.IsNullOrEmpty(data.itemBarcode))
            {
                return new ResponseResult
                {
                    status = 1,
                    message = "二维码内容不能为空",
                    data = null
                };
            }
            dynamic resultInfos = new System.Dynamic.ExpandoObject();
            var materialInfo = new LljService().GetMaterialByBarcode(data.itemBarcode, data.currentBillNo);
            if (materialInfo == null || materialInfo.Count == 0)
            {
                return new ResponseResult
                {
                    status = 1,
                    message = "未找到对应的物料信息,请检查二维码是否正确",
                    data = null
                };
            }
            resultInfos.tbBillList = materialInfo;
            return new ResponseResult
            {
                status = 0,
                message = "查询成功",
                data = resultInfos
            };
        }
        catch (Exception ex)
        {
            return new ResponseResult
            {
                status = 1,
                message = ex.Message, // 直接返回异常信息,包含"该条码不是此检验单条码!"
                data = null
            };
        }
    }
    /// <summary>
    /// 查询破坏实验记录是否存在
    /// </summary>
    /// <param name="data">查询请求数据</param>
    /// <returns>查询结果</returns>
    [HttpPost("CheckPhsyRecord")]
    public ResponseResult CheckPhsyRecord([FromBody] CheckPhsyRecordRequest data)
    {
        try
        {
            if (string.IsNullOrEmpty(data.billNo))
            {
                return new ResponseResult
                {
                    status = 1,
                    message = "到货单号不能为空",
                    data = null
                };
            }
            if (string.IsNullOrEmpty(data.releaseNo))
            {
                return new ResponseResult
                {
                    status = 1,
                    message = "检验单号不能为空",
                    data = null
                };
            }
            var exists = new LljService().CheckPhsyRecordExists(data.billNo, data.releaseNo);
            dynamic resultInfos = new System.Dynamic.ExpandoObject();
            resultInfos.exists = exists;
            return new ResponseResult
            {
                status = 0,
                message = "查询成功",
                data = resultInfos
            };
        }
        catch (Exception ex)
        {
            return new ResponseResult
            {
                status = 1,
                message = $"查询失败: {ex.Message}",
                data = null
            };
        }
    }
    /// <summary>
    /// 调用破坏实验存储过程
    /// </summary>
    /// <param name="data">破坏实验请求数据</param>
    /// <returns>执行结果</returns>
    [HttpPost("CallPhsyUpdateProcedure")]
    public ResponseResult CallPhsyUpdateProcedure([FromBody] PhsyUpdateRequest data)
    {
        try
        {
            if (string.IsNullOrEmpty(data.itemBarcode))
            {
                return new ResponseResult
                {
                    status = 1,
                    message = "条码不能为空",
                    data = null
                };
            }
            if (string.IsNullOrEmpty(data.billNo))
            {
                return new ResponseResult
                {
                    status = 1,
                    message = "到货单号不能为空",
                    data = null
                };
            }
            if (data.yqty <= 0)
            {
                return new ResponseResult
                {
                    status = 1,
                    message = "条码数量必须大于0",
                    data = null
                };
            }
            if (data.cqty <= 0)
            {
                return new ResponseResult
                {
                    status = 1,
                    message = "破坏实验数量必须大于0",
                    data = null
                };
            }
            if (string.IsNullOrEmpty(data.releaseNo))
            {
                return new ResponseResult
                {
                    status = 1,
                    message = "检验单号不能为空",
                    data = null
                };
            }
            var (result, message) = new LljService().CallPhsyUpdateProcedure(
                data.itemBarcode,
                data.yqty,
                data.cqty,
                data.billNo,
                data.lx,
                data.releaseNo,
                data.itemId);
            dynamic resultInfos = new System.Dynamic.ExpandoObject();
            resultInfos.result = result;
            resultInfos.message = message;
            return new ResponseResult
            {
                status = result,
                message = message,
                data = resultInfos
            };
        }
        catch (Exception ex)
        {
            return new ResponseResult
            {
                status = 1,
                message = $"调用存储过程失败: {ex.Message}",
                data = null
            };
        }
    }
    public class GetWomdabRequest
    {
        public string daa001 { get; set; }
        public string ItemNo { get; set; }
    }
    public class GetMaterialByBarcodeRequest
    {
        public string itemBarcode { get; set; }
        public string currentBillNo { get; set; }
    }
    public class PhsyUpdateRequest
    {
        public string itemBarcode { get; set; }
        public decimal yqty { get; set; }
        public decimal cqty { get; set; }
        public string billNo { get; set; }
        public int lx { get; set; } // 操作类型:1新增,2修改,3删除
        public string releaseNo { get; set; } // 检验单号
        public decimal? itemId { get; set; } // 物料ID
    }
    public class CheckPhsyRecordRequest
    {
        public string billNo { get; set; }
        public string releaseNo { get; set; }
    }
    [HttpPost("GetPhsyRecordInfo")]
    public ResponseResult GetPhsyRecordInfo([FromBody] CheckPhsyRecordRequest data)
    {
        try
        {
            if (string.IsNullOrEmpty(data.billNo))
            {
                return new ResponseResult
                {
                    status = 1,
                    message = "到货单号不能为空",
                    data = null
                };
            }
            if (string.IsNullOrEmpty(data.releaseNo))
            {
                return new ResponseResult
                {
                    status = 1,
                    message = "检验单号不能为空",
                    data = null
                };
            }
            var records = new LljService().GetPhsyRecordInfo(data.billNo, data.releaseNo);
            return new ResponseResult
            {
                status = 0,
                message = "查询成功",
                data = new { tbBillList = records }
            };
        }
        catch (Exception ex)
        {
            return new ResponseResult
            {
                status = 1,
                message = $"查询失败: {ex.Message}",
                data = null
            };
        }
    }
    /// <summary>
    /// 设置堵穴信息
    /// </summary>
    /// <param name="data">堵穴设置请求数据</param>
    /// <returns>执行结果</returns>
    [HttpPost("SetBlockedHoles")]
    public ResponseResult SetBlockedHoles([FromBody] SetBlockedHolesRequest data)
    {
        try
        {
            if (string.IsNullOrEmpty(data.releaseNo))
            {
                return new ResponseResult
                {
                    status = 1,
                    message = "检验单号不能为空",
                    data = null
                };
            }
            if (string.IsNullOrEmpty(data.blockedHoles))
            {
                return new ResponseResult
                {
                    status = 1,
                    message = "堵穴信息不能为空",
                    data = null
                };
            }
            if (data.itemId <= 0)
            {
                return new ResponseResult
                {
                    status = 1,
                    message = "检验项目ID不能为空",
                    data = null
                };
            }
            var (result, message) = new LljService().SetBlockedHoles(data.releaseNo, data.blockedHoles, data.itemId);
            return new ResponseResult
            {
                status = result,
                message = message,
                data = new { result = result, message = message }
            };
        }
        catch (Exception ex)
        {
            return new ResponseResult
            {
                status = 1,
                message = $"设置堵穴失败: {ex.Message}",
                data = null
            };
        }
    }
    public class SetBlockedHolesRequest
    {
        public string releaseNo { get; set; }
        public string blockedHoles { get; set; }
        public decimal itemId { get; set; }
    }
}