/* *所有关于dt_Machineinfo类的业务代码应在此处编写 *可使用repository.调用常用方法,获取EF/Dapper等信息 *如果需要事务请使用repository.DbContextBeginTransaction *也可使用DBServerProvider.手动获取数据库相关信息 *用户信息、权限、角色等使用UserContext.Current操作 *dt_MachineinfoService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter */ using WIDESEA_Core.BaseProvider; using WIDESEA_Core.Extensions.AutofacManager; using WIDESEA_Entity.DomainModels; using System.Linq; using WIDESEA_Core.Utilities; using System.Linq.Expressions; using WIDESEA_Core.Extensions; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore.Http; using WIDESEA_WCS.IRepositories; using Newtonsoft.Json; using Microsoft.AspNetCore.Mvc.RazorPages; using OfficeOpenXml.FormulaParsing.Excel.Functions.Text; using System.Diagnostics; using WIDESEA_Core.Services; using WIDESEA_Comm.LogInfo; using WIDESEA_Core.ManageUser; namespace WIDESEA_WCS.Services { public partial class dt_MachineinfoService { private readonly IHttpContextAccessor _httpContextAccessor; private readonly Idt_MachineinfoRepository _repository;//访问数据库 [ActivatorUtilitiesConstructor] public dt_MachineinfoService( Idt_MachineinfoRepository dbRepository, IHttpContextAccessor httpContextAccessor ) : base(dbRepository) { _httpContextAccessor = httpContextAccessor; _repository = dbRepository; //多租户会用到这init代码,其他情况可以不用 //base.Init(dbRepository); } public override WebResponseContent Update(SaveModel saveModel) { return base.Update(saveModel); } #region 传输文件状态设为允许 public WebResponseContent UpdateEnable(string saveModel) { WebResponseContent content = new WebResponseContent(); var model = JsonConvert.DeserializeObject(saveModel); model.Enable = 1; var cont = _repository.Update(model, true); if (cont > 0) content.OK(model.Name + "设置为允许传输文件成功!"); else content.Error(model.Name + "设置为允许传输文件失败!"); //var return content; } #endregion #region 传输文件状态设为禁止 public WebResponseContent UpdateDisEnable(string saveModel) { WebResponseContent content = new WebResponseContent(); var model = JsonConvert.DeserializeObject(saveModel); model.Enable = 0; var cont = _repository.Update(model, true); if (cont > 0) content.OK(model.Name + "设置为禁止传输文件成功!"); else content.Error(model.Name + "设置为禁止传输文件失败!"); //var return content; } #endregion #region 远程文件传输 public override WebResponseContent Upload(List files) { WebResponseContent Response = new WebResponseContent(); if (files == null || files.Count == 0) return Response.Error("请上传文件"); List requestTemps = new List(); string filePath = ""; var Machineinfos = _repository.Find(x => x.Enable == 1); if (Machineinfos.Count < 1) return Response.Error("未找到允许传输文件的机床!"); try { foreach (var machineinfo in Machineinfos) { Process process = new Process(); process.StartInfo.FileName = "cmd.exe"; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.CreateNoWindow = true; process.Start(); string IP = "\\\\" + machineinfo.IP + "\\ipc$"; filePath = $"\\\\" + machineinfo.IP + "\\" + machineinfo.FolderName; string fullPath = filePath.MapPath(true); string fileName = ""; try { string dosLine = "net use " + IP + " " + machineinfo.Password + " /user:" + machineinfo.User; process.StandardInput.WriteLine(dosLine); process.StandardInput.WriteLine("exit"); while (!process.HasExited) { process.WaitForExit(1000); } string msg = process.StandardError.ReadToEnd(); process.StandardError.Close(); if (msg != "") throw new Exception(machineinfo.Name + msg); if (!Directory.Exists(fullPath)) Directory.CreateDirectory(fullPath); int i = 0; for (i = 0; i < files.Count; i++) { fileName = files[i].FileName; fullPath = fullPath + "/" + fileName; using (var stream = new FileStream(fullPath, FileMode.Create)) { files[i].CopyTo(stream); } //FileStream outFileStream = new FileStream(fullPath, FileMode.Create); //files[i].CopyTo(outFileStream); var requestTemp = machineinfo.Name + ":下载文件路径:" + fullPath + ";下载文件名称:" + fileName + ";下载成功!"; requestTemps.Add(requestTemp); } } catch (Exception ex) { var requestTemp = machineinfo.Name + ":下载文件路径:" + fullPath + ";下载文件名称:" + fileName + ";下载失败!" + ex.Message; requestTemps.Add(requestTemp); throw; } finally { process.Close(); process.Dispose(); } } WriteDBLog.Success($"机床程序下载", new { 数据 = requestTemps }, "PCS", UserContext.Current.UserTrueName); return Response.OK("文件传输成功!", filePath); } catch (Exception ex) { WriteDBLog.Error($"机床程序下载", new { 数据 = requestTemps }, "PCS", UserContext.Current.UserTrueName); return Response.Error(ex.Message + "文件传输失败!"); } } #endregion } }