分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-07-19 7a4c218909936721fe281737491d10efc7378e09
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 *所有关于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<dt_Machineinfo>(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<dt_Machineinfo>(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<IFormFile> files)
        {
 
            WebResponseContent Response = new WebResponseContent();
            if (files == null || files.Count == 0) return Response.Error("请上传文件");
            List<string> requestTemps = new List<string>();
 
            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
    }
}