| | |
| | | using MES.Service.DB; |
| | | using System.Data; |
| | | using MES.Service.DB; |
| | | using MES.Service.Dto.service; |
| | | using MES.Service.Modes; |
| | | using MES.Service.util; |
| | |
| | | private readonly BaseService _baseService = new(); |
| | | |
| | | |
| | | private readonly string[] statusArray = ["开工", "待开工"]; |
| | | private readonly string[] statusArray = ["已开工", "未开工", "待开工"]; |
| | | |
| | | //生成最新的检验单号 |
| | | public string getMaxReleaseNo() |
| | |
| | | var number = "0001"; |
| | | var olReleaseNo = executeScalar.ToString(); |
| | | //判断今天是否生成了巡检单 |
| | | if (string.IsNullOrEmpty(olReleaseNo)) return "XJN" + date + number; |
| | | if (string.IsNullOrEmpty(olReleaseNo)) return "XJ" + date + number; |
| | | //截取后四位的流水号累加 |
| | | var substring = Convert.ToInt32(olReleaseNo.Substring(10)); |
| | | substring += 1; |
| | | number = substring.ToString("D4"); |
| | | |
| | | //如果为空直接返回0001 |
| | | return "XJN" + date + number; |
| | | return "XJ" + date + number; |
| | | } |
| | | |
| | | public List<Womdaa> getItem(string daa001) |
| | |
| | | }).OrderBy("b.LINE_NO").ToList(); |
| | | } |
| | | |
| | | public List<Womdaa> getDaa001(string daa020, string item) |
| | | public List<Womdaa> getDaa001(string daa020) |
| | | { |
| | | var db = SqlSugarHelper.GetInstance(); |
| | | return db.Queryable<Womdaa, MesItems>((a, b) => |
| | | new JoinQueryInfos(JoinType.Inner, a.Daa002 == b.Id.ToString())) |
| | | .Where((a, b) => |
| | | a.Daa015 == daa020 && |
| | | b.ItemNo == item && |
| | | statusArray.Contains(a.Daa018)) |
| | | .Select<Womdaa>(a => new Womdaa |
| | | return db.Queryable<Womdaa>() |
| | | .Where(a => a.Daa015 == daa020 && statusArray.Contains(a.Daa018)) |
| | | .Select(a => new Womdaa |
| | | { |
| | | Id = a.Id, |
| | | Daa001 = a.Daa001, |
| | | Daa003 = a.Daa003, |
| | | Daa008 = a.Daa008 |
| | | }).ToList(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取工单详细信息(包含物料信息) |
| | | /// </summary> |
| | | /// <param name="daa020">线体编号</param> |
| | | /// <returns>工单列表(包含物料信息)</returns> |
| | | public List<WorkOrderWithItemDto> getWorkOrderWithItem(string daa020) |
| | | { |
| | | var db = SqlSugarHelper.GetInstance(); |
| | | return db.Queryable<Womdaa, MesItems>((a, b) => |
| | | new JoinQueryInfos(JoinType.Inner, a.Daa002 == b.Id.ToString())) |
| | | .Where((a, b) => a.Daa015 == daa020 && statusArray.Contains(a.Daa018)) |
| | | .Select((a, b) => new WorkOrderWithItemDto |
| | | { |
| | | Id = a.Id, |
| | | Daa001 = a.Daa001, |
| | | Daa002 = a.Daa002, |
| | | Daa003 = a.Daa003, |
| | | Daa008 = a.Daa008, |
| | | Daa015 = a.Daa015, |
| | | Daa020 = a.Daa020, |
| | | ItemNo = b.ItemNo, |
| | | ItemName = b.ItemName, |
| | | ItemModel = b.ItemModel |
| | | }).ToList(); |
| | | } |
| | | |
| | |
| | | }).ToList(); |
| | | } |
| | | |
| | | public List<QsQaItemXj> getPage(XJPageResult queryObj) |
| | | public (List<QsQaItemXj> item, int TotalCount) getPage(XJPageResult queryObj) |
| | | { |
| | | var db = SqlSugarHelper.GetInstance(); |
| | | |
| | |
| | | if (StringUtil.IsNotNullOrEmpty(queryObj.createUser)) |
| | | lineNo = _baseService.getUserLineNo(queryObj.createUser); |
| | | |
| | | return db |
| | | var totalCount = 0; |
| | | |
| | | // 构建基础查询 - 使用LEFT JOIN避免因关联数据缺失导致查询不到 |
| | | var query = db |
| | | .Queryable<QsQaItemXj, Womdaa, MesLine, |
| | | MesItems>((s, a, c, b) => |
| | | new JoinQueryInfos( |
| | | JoinType.Inner, s.BillNo == a.Daa001, |
| | | JoinType.Left, s.BillNo == a.Daa001, |
| | | JoinType.Left, a.Daa015 == c.LineNo, |
| | | JoinType.Left, s.ItemId == b.Id |
| | | ) |
| | | ) |
| | | .WhereIF(lineNo != null && lineNo.Length > 0, |
| | | (s, a, c, b) => lineNo.Contains(c.LineNo)) |
| | | .WhereIF( |
| | | StringUtil.IsNotNullOrEmpty(queryObj.result) && |
| | | "未完成".Equals(queryObj.result), |
| | | (s, a, c, b) => s.FcheckResu == null) |
| | | .WhereIF( |
| | | StringUtil.IsNotNullOrEmpty(queryObj.result) && |
| | | !"未完成".Equals(queryObj.result), |
| | | (s, a, c, b) => s.FcheckResu != null) |
| | | .WhereIF(id > 0, (s, a, c, b) => s.Id == id) |
| | | ); |
| | | |
| | | // 添加线体筛选条件 |
| | | if (lineNo != null && lineNo.Length > 0) |
| | | { |
| | | query = query.Where((s, a, c, b) => lineNo.Contains(c.LineNo)); |
| | | } |
| | | |
| | | // 添加状态筛选条件 - 明确匹配前端传递的值 |
| | | if (StringUtil.IsNotNullOrEmpty(queryObj.result)) |
| | | { |
| | | if ("未完成".Equals(queryObj.result)) |
| | | { |
| | | query = query.Where((s, a, c, b) => s.Fsubmit == 0); |
| | | } |
| | | else if ("已完成".Equals(queryObj.result)) |
| | | { |
| | | query = query.Where((s, a, c, b) => s.Fsubmit == 1); |
| | | } |
| | | } |
| | | |
| | | // 添加ID筛选条件 |
| | | if (id > 0) |
| | | { |
| | | query = query.Where((s, a, c, b) => s.Id == id); |
| | | } |
| | | |
| | | var data = query |
| | | .Select((s, a, c, b) => new QsQaItemXj |
| | | { |
| | | Daa020 = c.LineNo, |
| | | Daa020 = SqlFunc.IsNull(c.LineNo, ""), |
| | | Id = s.Id, |
| | | PlanQty = a.Daa008, |
| | | PlanQty = SqlFunc.IsNull(a.Daa008, 0), |
| | | CreateBy = s.CreateBy, |
| | | CreateDate = s.CreateDate, |
| | | ReleaseNo = s.ReleaseNo, |
| | | ItemNo = s.ItemNo, |
| | | BillNo = s.BillNo, |
| | | ItemName = b.ItemName, |
| | | ItemModel = b.ItemModel, |
| | | ItemName = SqlFunc.IsNull(b.ItemName, ""), |
| | | ItemModel = SqlFunc.IsNull(b.ItemModel, ""), |
| | | FcheckBy = s.FcheckBy, |
| | | FcheckResu = s.FcheckResu, |
| | | Remarks = s.Remarks |
| | | }).OrderBy(s => s.CreateDate, OrderByType.Desc) |
| | | .ToPageList(queryObj.PageIndex, queryObj.Limit); |
| | | .ToPageList(queryObj.PageIndex, queryObj.Limit, ref totalCount); |
| | | |
| | | return (data, totalCount); |
| | | } |
| | | |
| | | |
| | |
| | | |
| | | xj.CreateBy = userNo; |
| | | xj.CreateDate = DateTime.Now; |
| | | xj.Fsubmit = 0; // 设置默认状态为未提交 |
| | | |
| | | var pid = db.Insertable(xj).ExecuteReturnIdentity(); |
| | | |
| | |
| | | return commit; |
| | | }); |
| | | |
| | | // 重新获取检验项目(包含ID) |
| | | xjDto.items = getJYItem(xj.Id, null); |
| | | |
| | | // 自动为没有上下限的检验项目生成默认的合格记录 |
| | | xjDto.items.ForEach(s => |
| | | { |
| | | if (s.MaxValue != null || s.StandardValue != null || |
| | |
| | | detail.count = (int?)s.LevelNum; |
| | | SetQSItemDetail(detail); |
| | | }); |
| | | |
| | | // 重新获取检验项目(包含自动生成的结果) |
| | | xjDto.items = getJYItem(xj.Id, null); |
| | | |
| | | return Convert.ToInt32(xj.Id); |
| | | } |
| | |
| | | .ExecuteCommand(); |
| | | }); |
| | | } |
| | | |
| | | //刷新检验项目 |
| | | public (int result, string message) GenUpdate(decimal? id, string? no, string? user) |
| | | { |
| | | var outputResult = new SugarParameter("PO_RESULT", null, System.Data.DbType.Int32, ParameterDirection.Output, 4000); |
| | | var outputMessage = new SugarParameter("PO_TEXT", null, System.Data.DbType.String, ParameterDirection.Output, 4000); |
| | | |
| | | var parameters = new List<SugarParameter> |
| | | { |
| | | new("P_ID", id, System.Data.DbType.Decimal, ParameterDirection.Input), |
| | | new("P_NO", no, System.Data.DbType.String, ParameterDirection.Input), |
| | | new("P_USER", user, System.Data.DbType.String, ParameterDirection.Input), |
| | | outputResult, |
| | | outputMessage |
| | | }; |
| | | |
| | | var db = SqlSugarHelper.GetInstance(); |
| | | db.Ado.ExecuteCommand( |
| | | "BEGIN PRC_GEN_UPDATE_XJ(:P_ID, :P_NO, :P_USER, :PO_RESULT, :PO_TEXT); END;", |
| | | parameters.ToArray()); |
| | | |
| | | int result = outputResult.Value == null ? -1 : Convert.ToInt32(outputResult.Value); |
| | | string message = outputMessage.Value?.ToString() ?? ""; |
| | | |
| | | return (result, message); |
| | | } |
| | | |
| | | public bool SjSubmit(SJDto dto) |
| | | { |
| | | try |
| | | { |
| | | var db = SqlSugarHelper.GetInstance(); |
| | | |
| | | // 直接更新Fsubmit字段为1(已提交状态) |
| | | var result = db.Updateable<QsQaItemXj>() |
| | | .SetColumns(s => s.Fsubmit == 1) |
| | | .SetColumns(s => s.FsubmitBy == dto.userNo) |
| | | .SetColumns(s => s.FsubmitDate == DateTime.Now) |
| | | .Where(s => s.Id == dto.id) |
| | | .ExecuteCommand(); |
| | | |
| | | if (result > 0) |
| | | { |
| | | return true; |
| | | } |
| | | else |
| | | { |
| | | throw new Exception("提交失败:未找到对应的检验单"); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | throw new Exception(ex.Message); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取有线体的部门列表 |
| | | /// </summary> |
| | | /// <returns>部门列表</returns> |
| | | public List<SysDepartment> GetDepartmentsWithLines() |
| | | { |
| | | var db = SqlSugarHelper.GetInstance(); |
| | | |
| | | // 查询有线体的部门,链接条件是线体的DEPOT_ID等于部门的Departmentid |
| | | var departments = db.Queryable<SysDepartment, MesLine>((dept, line) => |
| | | new JoinQueryInfos(JoinType.Inner, line.DepotId == dept.Departmentid)) |
| | | .Where((dept, line) => line.LineNo != null && line.LineName != null) |
| | | .GroupBy((dept, line) => new |
| | | { |
| | | dept.Departmentid, |
| | | dept.Departmentname |
| | | }) |
| | | .Select((dept, line) => new SysDepartment |
| | | { |
| | | Departmentid = dept.Departmentid, |
| | | Departmentname = dept.Departmentname |
| | | }) |
| | | .OrderBy(dept => dept.Departmentname) |
| | | .ToList(); |
| | | |
| | | return departments; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 保存部门选择 |
| | | /// </summary> |
| | | /// <param name="id">巡检单ID</param> |
| | | /// <param name="departmentId">部门ID</param> |
| | | /// <param name="departmentName">部门名称</param> |
| | | /// <returns>保存结果</returns> |
| | | public int SaveDepartmentSelection(decimal id, string departmentId, string departmentName) |
| | | { |
| | | return SqlSugarHelper.UseTransactionWithOracle(db => |
| | | { |
| | | return db.Updateable<QsQaItemXj>() |
| | | .SetColumns(it => it.WorkShop == departmentName) |
| | | .SetColumns(it => it.DepartmentId == departmentId) |
| | | .Where(it => it.Id == id) |
| | | .ExecuteCommand(); |
| | | }); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据部门ID获取该部门下的线体列表 |
| | | /// </summary> |
| | | /// <param name="departmentId">部门ID</param> |
| | | /// <returns>线体列表</returns> |
| | | public List<MesLine> GetLinesByDepartment(string departmentId) |
| | | { |
| | | var db = SqlSugarHelper.GetInstance(); |
| | | |
| | | // 将字符串转换为decimal进行比较 |
| | | if (!decimal.TryParse(departmentId, out var deptId)) |
| | | { |
| | | return new List<MesLine>(); |
| | | } |
| | | |
| | | return db.Queryable<MesLine>() |
| | | .Where(line => line.DepotId == deptId && |
| | | line.LineNo != null && |
| | | line.LineName != null) |
| | | .OrderBy(line => line.LineNo) |
| | | .ToList(); |
| | | } |
| | | |
| | | /// <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" |
| | | }; |
| | | } |
| | | |
| | | |
| | | } |