xwt
2025-08-16 823e5cae379ee1a259f79060ca0015d381c61f79
StandardInterface/MES.Service/service/QC/RKJService.cs
@@ -1,10 +1,14 @@
using MES.Service.DB;
using MES.Service.Dto.service;
using MES.Service.Modes;
using MES.Service.Modes.QcIssueResult;
using MES.Service.service.QcIssueResult;
using MES.Service.util;
using SqlSugar;
using System.Linq;
using System.Data;
using System.Net;
using System.Web;
namespace MES.Service.service.QC;
@@ -624,6 +628,13 @@
                !"未完成".Equals(queryObj.result),
                (a, da, b) => a.FcheckResu != null && a.FcheckResu != "")
            .WhereIF(id > 0, (a, da, b) => a.Id == id)
            // 添加fsubmit字段过滤逻辑
            .WhereIF(
                StringUtil.IsNotNullOrEmpty(queryObj.fsubmit) && queryObj.fsubmit == "1",
                (a, da, b) => a.Fsubmit == 1)
            .WhereIF(
                StringUtil.IsNullOrEmpty(queryObj.fsubmit) || queryObj.fsubmit == "0",
                (a, da, b) => a.Fsubmit == 0 || a.Fsubmit == null)
            .Select((a, da, b) => new QsItemOqcReq
            {
                Id = a.Id,
@@ -641,6 +652,11 @@
                Remarks = a.Remarks,
                WorkShop = a.WorkShop,
                DepartmentId = a.DepartmentId,
                Fsubmit = a.Fsubmit, // 添加fsubmit字段到返回结果
                Blyy = a.Blyy, // 添加不良原因字段
                Pszt = a.Pszt, // 添加评审状态字段
                Sscj = a.Sscj, // 添加所属车间字段
                FngDesc = a.FngDesc, // 添加不良描述字段
                // 从关联表获取的字段
                ItemName = b.ItemName,
                ItemModel = b.ItemModel,
@@ -811,6 +827,78 @@
    {
        return SqlSugarHelper.UseTransactionWithOracle(db =>
        {
            // 获取检验单详细信息
            var inspectionData = db.Queryable<QsItemOqcReq>()
                .Where(it => it.Id == id)
                .First();
            if (inspectionData == null)
            {
                throw new Exception("检验单不存在");
            }
            // 检查钉钉推送条件:PSZT为待判,且不良原因、不良描述、所属车间不为空
            bool shouldPushToDingTalk = inspectionData.Pszt == "待判" &&
                                       !string.IsNullOrEmpty(inspectionData.Blyy) &&
                                       !string.IsNullOrEmpty(inspectionData.FngDesc) &&
                                       !string.IsNullOrEmpty(inspectionData.Sscj);
            // 如果满足推送条件,则推送到钉钉
            if (shouldPushToDingTalk)
            {
                try
                {
                    // 构建钉钉推送数据
                    var dingTalkData = new GetQcIssueResultDetail
                    {
                        FbatchQty = inspectionData.Quantity?.ToString(), // 送检数量
                        ItemName = inspectionData.ItemName,
                        ItemNo = inspectionData.ItemNo,
                        SuppName = "生产入库", // 固定值
                        AppicationReason = inspectionData.FngDesc, // 不良描述
                        BadReason = inspectionData.Blyy, // 不良原因
                        remark = inspectionData.Remarks,
                        ReleaseNo = inspectionData.ReleaseNo,
                        StaffNo = userNo,
                        IqcStatus = inspectionData.Pszt, // 评审状态
                        Department = inspectionData.Sscj, // 使用所属车间作为部门
                        WorkShop = inspectionData.Sscj, // 所属车间
                        EMERGENCY = "0" // 固定值为0
                    };
                    // 调用钉钉推送服务
                    var qcIssueResultManager = new QcIssueResultManager();
                    string dingTalkResult = qcIssueResultManager.GetProcessNo(dingTalkData, "RKJ");
                    // 解析钉钉返回结果,获取流程实例ID
                    if (dingTalkResult.Contains("钉钉工作流实例启动成功"))
                    {
                        // 提取instanceId
                        int startIndex = dingTalkResult.IndexOf("\"InstanceId\":\"") + 14;
                        int endIndex = dingTalkResult.IndexOf("\"", startIndex);
                        if (startIndex > 13 && endIndex > startIndex)
                        {
                            string instanceId = dingTalkResult.Substring(startIndex, endIndex - startIndex);
                            // 保存钉钉流程信息
                            qcIssueResultManager.SaveProcessNo(
                                inspectionData.ReleaseNo,
                                instanceId,
                                userNo,
                                inspectionData.Sscj,
                                inspectionData.Pszt
                            );
                        }
                    }
                }
                catch (Exception ex)
                {
                    // 钉钉推送失败不影响检验提交,只记录日志
                    Console.WriteLine($"钉钉推送失败: {ex.Message}");
                }
            }
            // 更新检验单状态
            return db.Updateable<QsItemOqcReq>()
                .SetColumns(it => it.Fsubmit == 1) // 设置提交状态为1(已提交)
                .SetColumns(it => it.FcheckBy == userNo) // 设置检验人
@@ -851,4 +939,220 @@
        return (result, message);
    }
    /// <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服务器获取文件
    /// </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文件路径 - RKJ使用FQC文件夹
        string ftpPath = $"{normalizedServer}/FQC/{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($"获取FTP文件失败: {ex.Message}");
        }
    }
    /// <summary>
    /// 获取文件内容类型
    /// </summary>
    /// <param name="fileName">文件名</param>
    /// <returns>内容类型</returns>
    public string GetContentType(string fileName)
    {
        if (string.IsNullOrEmpty(fileName))
            return "application/octet-stream";
        var extension = System.IO.Path.GetExtension(fileName).ToLowerInvariant();
        return extension switch
        {
            ".pdf" => "application/pdf",
            ".jpg" or ".jpeg" => "image/jpeg",
            ".png" => "image/png",
            ".gif" => "image/gif",
            ".bmp" => "image/bmp",
            ".txt" => "text/plain",
            ".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",
            _ => "application/octet-stream"
        };
    }
    /// <summary>
    /// 标准化FTP服务器地址
    /// </summary>
    /// <param name="ftpServer">FTP服务器地址</param>
    /// <returns>标准化后的地址</returns>
    private string NormalizeFtpServer(string ftpServer)
    {
        if (string.IsNullOrEmpty(ftpServer))
            throw new ArgumentException("FTP服务器地址不能为空");
        // 移除可能的协议前缀
        var normalized = ftpServer.Trim();
        if (normalized.StartsWith("ftp://", StringComparison.OrdinalIgnoreCase))
        {
            normalized = normalized.Substring(6);
        }
        else if (normalized.StartsWith("ftps://", StringComparison.OrdinalIgnoreCase))
        {
            normalized = normalized.Substring(7);
        }
        // 确保以ftp://开头
        if (!normalized.StartsWith("ftp://", StringComparison.OrdinalIgnoreCase))
        {
            normalized = "ftp://" + normalized;
        }
        return normalized;
    }
    /// <summary>
    /// 保存下拉框字段(不良原因、评审状态、所属车间)
    /// </summary>
    /// <param name="dto">包含字段数据的DTO</param>
    /// <returns>更新结果</returns>
    public int SaveDropdownFields(RKJDto dto)
    {
        return SqlSugarHelper.UseTransactionWithOracle(db =>
        {
            return db.Updateable<QsItemOqcReq>()
                .SetColumns(it => it.Blyy == dto.BLYY)
                .SetColumns(it => it.Pszt == dto.PSZT)
                .SetColumns(it => it.Sscj == dto.SSCJ)
                .Where(it => it.ReleaseNo == dto.releaseNo)
                .ExecuteCommand();
        });
    }
    /// <summary>
    /// 保存不良描述
    /// </summary>
    /// <param name="gid">检验单ID</param>
    /// <param name="fngDesc">不良描述</param>
    /// <returns>更新结果</returns>
    public int SaveFngDesc(decimal gid, string fngDesc)
    {
        return SqlSugarHelper.UseTransactionWithOracle(db =>
        {
            return db.Updateable<QsItemOqcReq>()
                .SetColumns(it => it.FngDesc == fngDesc)
                .Where(it => it.Id == gid)
                .ExecuteCommand();
        });
    }
}