xwt
2025-09-25 8e5e2fb892df6cc8c61d7c03baa2412522ad11c4
SJ,XJ,RKJ优化
已修改6个文件
395 ■■■■■ 文件已修改
StandardInterface/MES.Service/Dto/service/XJPageResult.cs 7 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
StandardInterface/MES.Service/service/QC/RKJService.cs 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
StandardInterface/MES.Service/service/QC/SJService.cs 210 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
StandardInterface/MES.Service/service/QC/XJService.cs 43 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
StandardInterface/MESApplication/Controllers/QC/SJController.cs 114 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
StandardInterface/MESApplication/appsettings.json 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
StandardInterface/MES.Service/Dto/service/XJPageResult.cs
@@ -17,8 +17,13 @@
    /// <summary>
    /// 搜索条件索引,用于指定搜索哪个字段
    /// 0: 项目(不筛选), 1: 物料编号, 2: 物料名称, 3: 供应商, 4: 到货单号, 5: 检验单号, 6: 物料规格
    /// 0: 工单, 1: 检验单号, 2: 产线, 3: 物料编码, 4: 物料名称
    /// </summary>
    public int? selectedIndex { get; set; }
    /// <summary>
    /// 搜索字段名,用于指定搜索哪个字段
    /// </summary>
    public string? searchField { get; set; }
}
StandardInterface/MES.Service/service/QC/RKJService.cs
@@ -632,6 +632,25 @@
            .WhereIF(
                StringUtil.IsNullOrEmpty(queryObj.fsubmit) || queryObj.fsubmit == "0",
                (a, da, b) => a.Fsubmit == 0 || a.Fsubmit == null)
            // 添加搜索条件 - 根据选择的搜索字段进行精确搜索
            .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && !string.IsNullOrEmpty(queryObj.searchField) && queryObj.searchField == "billNo",  // 工单
                (a, da, b) => a.BillNo.Contains(queryObj.SearchValue))
            .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && !string.IsNullOrEmpty(queryObj.searchField) && queryObj.searchField == "releaseNo",  // 检验单号
                (a, da, b) => a.ReleaseNo.Contains(queryObj.SearchValue))
            .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && !string.IsNullOrEmpty(queryObj.searchField) && queryObj.searchField == "daa015",  // 产线
                (a, da, b) => da.Daa015.Contains(queryObj.SearchValue))
            .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && !string.IsNullOrEmpty(queryObj.searchField) && queryObj.searchField == "itemNo",  // 物料编码
                (a, da, b) => a.ItemNo.Contains(queryObj.SearchValue))
            .WhereIF(!string.IsNullOrEmpty(queryObj.SearchValue) && !string.IsNullOrEmpty(queryObj.searchField) && queryObj.searchField == "itemName",  // 物料名称
                (a, da, b) => b.ItemName.Contains(queryObj.SearchValue))
            // 为了兼容旧版本,如果没有传递 searchField,使用原来的查询逻辑
            .WhereIF(string.IsNullOrEmpty(queryObj.searchField) && !string.IsNullOrEmpty(queryObj.SearchValue),
                (a, da, b) =>
                    a.ItemNo.Contains(queryObj.SearchValue) ||
                    b.ItemName.Contains(queryObj.SearchValue) ||
                    a.BillNo.Contains(queryObj.SearchValue) ||
                    a.ReleaseNo.Contains(queryObj.SearchValue) ||
                    da.Daa015.Contains(queryObj.SearchValue))
            .Select((a, da, b) => new QsItemOqcReq
            {
                Id = a.Id,
StandardInterface/MES.Service/service/QC/SJService.cs
@@ -738,6 +738,216 @@
    }
    /// <summary>
    /// 获取附件信息
    /// </summary>
    /// <param name="itemNo">物料编码</param>
    /// <returns>附件列表</returns>
    public List<QamftpDto> GetAttachments(string itemNo)
    {
        var db = SqlSugarHelper.GetInstance();
        try
        {
            return db.Queryable<MesQamftp>()
                .Where(x => x.ItemNo == itemNo)
                .OrderBy(x => x.Fdate, OrderByType.Desc)
                .Select(x => new QamftpDto
                {
                    Id = x.Id,
                    itemNo = x.ItemNo,
                    Ftype = x.Ftype,
                    Fattach = x.Fattach,
                    Fversion = x.Fversion,
                    Fdate = x.Fdate,
                    CreateBy = x.CreateBy,
                    CreateDate = x.CreateDate,
                    Company = x.Company,
                    Factory = x.Factory,
                    F_type = x.F_type,
                    LastupdateBy = x.LastupdateBy,
                    LastupdateDate = x.LastupdateDate,
                    ItemId = x.ItemId
                }).ToList();
        }
        catch (Exception ex)
        {
            throw new Exception($"查询附件信息失败: {ex.Message}");
        }
    }
    /// <summary>
    /// 从FTP服务器获取文件(首检使用OPC目录)
    /// </summary>
    /// <param name="itemNo">物料编码</param>
    /// <param name="fileName">文件名</param>
    /// <param name="ftpServer">FTP服务器地址</param>
    /// <returns>文件字节数组</returns>
    public byte[] GetFtpFile(string itemNo, string fileName, string ftpServer)
    {
        // 参数验证
        if (string.IsNullOrEmpty(itemNo) || string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(ftpServer))
        {
            throw new ArgumentException("参数不能为空: itemNo, fileName, ftpServer");
        }
        string ftpUser = "hm_ftp";
        string ftpPwd = "dell_123";
        // 标准化FTP服务器地址
        string normalizedServer = NormalizeFtpServer(ftpServer);
        // 构建FTP文件路径 - 首检使用OPC目录
        string ftpPath = $"{normalizedServer}/OPC/{itemNo}/{fileName}";
        try
        {
            var request = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(ftpPath);
            request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
            request.Credentials = new System.Net.NetworkCredential(ftpUser, ftpPwd);
            request.UseBinary = true;
            request.UsePassive = false;
            request.Timeout = 30000; // 30秒超时
            request.ReadWriteTimeout = 30000;
            using (var response = (System.Net.FtpWebResponse)request.GetResponse())
            using (var ftpStream = response.GetResponseStream())
            using (var ms = new System.IO.MemoryStream())
            {
                if (ftpStream == null)
                {
                    throw new Exception("FTP响应流为空");
                }
                ftpStream.CopyTo(ms);
                var fileBytes = ms.ToArray();
                if (fileBytes.Length == 0)
                {
                    return null; // 文件为空或不存在
                }
                return fileBytes;
            }
        }
        catch (System.Net.WebException ex)
        {
            if (ex.Response is System.Net.FtpWebResponse ftpResponse)
            {
                switch (ftpResponse.StatusCode)
                {
                    case System.Net.FtpStatusCode.ActionNotTakenFileUnavailable:
                        return null; // 文件不存在
                    case System.Net.FtpStatusCode.NotLoggedIn:
                        throw new Exception("FTP认证失败,请检查用户名和密码");
                    case System.Net.FtpStatusCode.ActionNotTakenFilenameNotAllowed:
                        throw new Exception("文件名不被允许或路径无效");
                    default:
                        throw new Exception($"FTP错误 ({ftpResponse.StatusCode}): {ftpResponse.StatusDescription}");
                }
            }
            // 处理超时和网络错误
            if (ex.Status == System.Net.WebExceptionStatus.Timeout)
            {
                throw new Exception("FTP连接超时,请稍后重试");
            }
            throw new Exception($"FTP连接失败: {ex.Message}");
        }
        catch (Exception ex)
        {
            throw new Exception($"获取文件失败: {ex.Message}");
        }
    }
    /// <summary>
    /// 标准化FTP服务器地址
    /// </summary>
    /// <param name="ftpServer">FTP服务器地址</param>
    /// <returns>标准化后的FTP服务器地址</returns>
    private string NormalizeFtpServer(string ftpServer)
    {
        if (string.IsNullOrEmpty(ftpServer))
        {
            throw new ArgumentException("FTP服务器地址不能为空");
        }
        // 确保以ftp://开头
        string normalizedServer = ftpServer.StartsWith("ftp://") ? ftpServer : $"ftp://{ftpServer}";
        // 特殊处理已知服务器地址
        if (normalizedServer == "ftp://36.26.21.214")
        {
            normalizedServer = "ftp://36.26.21.214:21";
        }
        else if (!normalizedServer.Contains(":") && normalizedServer.StartsWith("ftp://"))
        {
            normalizedServer += ":21"; // 默认FTP端口
        }
        // 开发环境使用本地服务器
        normalizedServer = "ftp://192.168.1.22:21";
        return normalizedServer;
    }
    /// <summary>
    /// 获取文件的内容类型
    /// </summary>
    /// <param name="fileName">文件名</param>
    /// <returns>MIME类型</returns>
    public string GetContentType(string fileName)
    {
        if (string.IsNullOrEmpty(fileName))
            return "application/octet-stream";
        var extension = System.IO.Path.GetExtension(fileName).ToLower();
        return extension switch
        {
            // PDF文件
            ".pdf" => "application/pdf",
            // 图片文件
            ".jpg" or ".jpeg" => "image/jpeg",
            ".png" => "image/png",
            ".gif" => "image/gif",
            ".bmp" => "image/bmp",
            ".webp" => "image/webp",
            ".svg" => "image/svg+xml",
            ".ico" => "image/x-icon",
            // 文本文件
            ".txt" => "text/plain",
            ".log" => "text/plain",
            ".md" => "text/markdown",
            ".html" or ".htm" => "text/html",
            ".css" => "text/css",
            ".js" => "application/javascript",
            ".json" => "application/json",
            ".xml" => "application/xml",
            // Office文档
            ".doc" => "application/msword",
            ".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
            ".xls" => "application/vnd.ms-excel",
            ".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            ".ppt" => "application/vnd.ms-powerpoint",
            ".pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
            // 其他常见格式
            ".csv" => "text/csv",
            ".zip" => "application/zip",
            ".rar" => "application/x-rar-compressed",
            ".7z" => "application/x-7z-compressed",
            ".tar" => "application/x-tar",
            ".gz" => "application/gzip",
            // 默认
            _ => "application/octet-stream"
        };
    }
    /// <summary>
    /// 生成穴号信息
    /// </summary>
    /// <param name="mnum">开穴总数</param>
StandardInterface/MES.Service/service/QC/XJService.cs
@@ -172,6 +172,49 @@
            }
        }
        
        // 添加搜索条件 - 根据选择的搜索字段进行精确搜索
        if (!string.IsNullOrEmpty(queryObj.SearchValue) && !string.IsNullOrEmpty(queryObj.searchField))
        {
            switch (queryObj.searchField)
            {
                case "billNo":  // 工单
                    query = query.Where((s, a, c, b) => s.BillNo.Contains(queryObj.SearchValue));
                    break;
                case "releaseNo":  // 检验单号
                    query = query.Where((s, a, c, b) => s.ReleaseNo.Contains(queryObj.SearchValue));
                    break;
                case "daa020":    // 产线
                    query = query.Where((s, a, c, b) => c.LineNo.Contains(queryObj.SearchValue));
                    break;
                case "itemNo":  // 物料编码
                    query = query.Where((s, a, c, b) => s.ItemNo.Contains(queryObj.SearchValue));
                    break;
                case "itemName":  // 物料名称
                    query = query.Where((s, a, c, b) => b.ItemName.Contains(queryObj.SearchValue));
                    break;
                default:
                    // 如果没有指定字段或字段不匹配,使用原有的模糊查询逻辑作为兜底方案
                    query = query.Where((s, a, c, b) =>
                        s.ItemNo.Contains(queryObj.SearchValue) ||
                        b.ItemName.Contains(queryObj.SearchValue) ||
                        s.BillNo.Contains(queryObj.SearchValue) ||
                        s.ReleaseNo.Contains(queryObj.SearchValue) ||
                        c.LineNo.Contains(queryObj.SearchValue));
                    break;
            }
        }
        // 为了兼容旧版本,如果没有传递 searchField,使用原来的查询逻辑
        else if (string.IsNullOrEmpty(queryObj.searchField) && !string.IsNullOrEmpty(queryObj.SearchValue))
        {
            // 保持原有的多字段模糊查询逻辑
            query = query.Where((s, a, c, b) =>
                s.ItemNo.Contains(queryObj.SearchValue) ||
                b.ItemName.Contains(queryObj.SearchValue) ||
                s.BillNo.Contains(queryObj.SearchValue) ||
                s.ReleaseNo.Contains(queryObj.SearchValue) ||
                c.LineNo.Contains(queryObj.SearchValue));
        }
        // 添加ID筛选条件
        if (id > 0)
        {
StandardInterface/MESApplication/Controllers/QC/SJController.cs
@@ -371,4 +371,118 @@
            return ResponseResult.ResponseError(ex);
        }
    }
    /// <summary>
    /// 获取附件信息
    /// </summary>
    /// <param name="data">包含itemNo的JSON对象</param>
    /// <returns>附件列表</returns>
    [HttpPost("getAttachments")]
    public ResponseResult GetAttachments([FromBody] JObject data)
    {
        var itemNo = data["itemNo"]?.ToString();
        try
        {
            dynamic resultInfos = new System.Dynamic.ExpandoObject();
            var tbBillList = new SJService().GetAttachments(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>
    /// 预览FTP文件
    /// </summary>
    /// <param name="itemNo">物料编码</param>
    /// <param name="fileName">文件名</param>
    /// <param name="ftpServer">FTP服务器地址</param>
    /// <returns>文件内容</returns>
    [HttpGet("PreviewFtpFile")]
    public IActionResult PreviewFtpFile([FromQuery] string itemNo, [FromQuery] string fileName, [FromQuery] string ftpServer)
    {
        try
        {
            var fileBytes = new SJService().GetFtpFile(itemNo, fileName, ftpServer);
            if (fileBytes == null)
            {
                return NotFound(new ResponseResult
                {
                    status = 1,
                    message = "文件不存在或无法访问",
                    data = null
                });
            }
            var contentType = new SJService().GetContentType(fileName);
            return File(fileBytes, contentType, fileName);
        }
        catch (Exception ex)
        {
            return StatusCode(500, new ResponseResult
            {
                status = 1,
                message = ex.Message,
                data = null
            });
        }
    }
    /// <summary>
    /// 下载FTP文件
    /// </summary>
    /// <param name="itemNo">物料编码</param>
    /// <param name="fileName">文件名</param>
    /// <param name="ftpServer">FTP服务器地址</param>
    /// <returns>文件下载</returns>
    [HttpGet("DownloadFtpFile")]
    public IActionResult DownloadFtpFile([FromQuery] string itemNo, [FromQuery] string fileName, [FromQuery] string ftpServer)
    {
        try
        {
            var fileBytes = new SJService().GetFtpFile(itemNo, fileName, ftpServer);
            if (fileBytes == null)
            {
                return NotFound(new ResponseResult
                {
                    status = 1,
                    message = "文件不存在或无法访问",
                    data = null
                });
            }
            var contentType = new SJService().GetContentType(fileName);
            // 设置下载响应头
            Response.Headers.Add("Content-Disposition", $"attachment; filename=\"{fileName}\"");
            return File(fileBytes, contentType, fileName);
        }
        catch (Exception ex)
        {
            return StatusCode(500, new ResponseResult
            {
                status = 1,
                message = ex.Message,
                data = null
            });
        }
    }
}
StandardInterface/MESApplication/appsettings.json
@@ -10,6 +10,6 @@
  "AppSettings": {
    "TestErpUrl": "http://192.168.11.120:8098/WebService1.asmx/mesToErpinfo",
    "ProductionErpUrl": "http://192.168.11.120:8098/WebService1.asmx/mesToErpinfoFormal",
    "DataBaseConn": "Data Source = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.22)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ORCL))); Persist Security Info=True;User ID = test_dev; Password=hmprd"
    "DataBaseConn": "Data Source = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.22)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ORCL))); Persist Security Info=True;User ID = hm_prd; Password=hmprd"
  }
}