管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2024-09-03 a9b99add3e1baa7fc49049247b8bf071e70a6005
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using Npgsql;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Reflection;
 
namespace DataLoader.CS
{
    /// <summary>
    /// 工具类
    /// </summary>
    public class Tools
    {
        /// <summary>
        /// 基础目录
        /// </summary>
        public static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
 
        private static PostgreHelper _dbHelper;
 
        /// <summary>
        /// DB帮助类
        /// </summary>
        public static PostgreHelper DBHelper
        {
            get
            {
                if (null == _dbHelper)
                {
                    _dbHelper = new PostgreHelper();
                }
 
                return _dbHelper;
            }
        }
 
        /// <summary>
        /// 字节格式化
        /// </summary>
        public static string FormatBytes(long bytes)
        {
            string[] Suffix = { "B", "KB", "MB", "GB", "TB" }; // Byte
 
            int i = 0;
            double dblSByte = bytes;
            if (bytes > 1024)
                for (i = 0; (bytes / 1024) > 0; i++, bytes /= 1024)
                    dblSByte = bytes / 1024.0;
 
            return string.Format("{0:0.##} {1}", dblSByte, Suffix[i]);
        }
 
        /// <summary>
        /// 换算为MB
        /// </summary>
        public static double SizeToMb(long size)
        {
            if (size < 1050)
            {
                return 0.001;
            }
 
            string str = string.Format("{0:F3}", size / 1024.0 / 1024.0);
 
            return double.Parse(str);
        }
 
        /// <summary>
        /// 保留3位小数
        /// </summary>
        public static double Round3(double val)
        {
            string str = string.Format("{0:F3}", val);
 
            return double.Parse(str);
        }
 
        /// <summary>
        /// 获取设置
        /// </summary>
        public static string GetSetting(string key)
        {
            if (!ConfigurationManager.AppSettings.AllKeys.Contains(key)) return null;
 
            return ConfigurationManager.AppSettings[key];
        }
 
        /// <summary>
        /// 获取Db参数
        /// </summary>
        public static List<DbParameter> GetParams<T>(string sql, T t)
        {
            List<DbParameter> list = new List<DbParameter>();
            Type tType = typeof(T);
            BindingFlags flags = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
 
            int start = sql.IndexOf("@");
            while (start != -1)
            {
                int end = sql.IndexOf(",", start);
                if (end == -1) end = sql.IndexOf(")", start);
                if (end == -1) end = sql.IndexOf(" ", start);
                if (end == -1) end = sql.Length;
 
                string name = sql.Substring(start + 1, end - start - 1);
                PropertyInfo pi = tType.GetProperty(name, flags);
                if (pi != null)
                {
                    object value = pi.GetValue(t, null);
                    DbParameter dp = new NpgsqlParameter("@" + name, value);
                    list.Add(dp);
                }
 
                start = sql.IndexOf("@", end);
            }
 
            return list;
        }
 
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="list">命令集合</param>
        public static string ExecCmd(List<string> list)
        {
            string str = null;
            Process p = null;
            try
            {
                p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.Start();
 
                StreamWriter si = p.StandardInput;
                StreamReader se = p.StandardError;
 
                LogOut.Info("cmd = " + string.Join(",", list));
                si.AutoFlush = true;
                foreach (string cmd in list)
                {
                    si.WriteLine(cmd);
                }
                si.WriteLine("exit");
 
                str = se.ReadToEnd();
                se.Close();
                si.Close();
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                str = ex.Message;
            }
            finally
            {
                if (p != null)
                {
                    p.Close();
                    p = null;
                }
            }
            return str;
        }
 
        /// <summary>
        /// 删除路径
        /// </summary>
        public static string DelPath(string path)
        {
            List<string> list = new List<string>();
            list.Add(string.Format("rd \"{0}\" /s /q", path));
 
            return ExecCmd(list);
        }
 
        /// <summary>
        /// 获取本机IP
        /// </summary>
        /// <returns></returns>
        public static string GetLocalIP()
        {
            var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    return ip.ToString();
                }
            }
 
            return GetSetting("localIP");
        }
    }
}