管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2023-09-07 093df84b78ebbf020fcb4f752b900925aac07565
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
using MoonExp.cs;
using MoonExp.Models;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;
 
namespace MoonExp
{
    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>
        /// 根据停牌获取用户ID
        /// </summary>
        public static int SelectUserIdByToken(string token)
        {
            string sql = "select create_user from lf.sys_token where token=@token order by create_time desc limit 1;";
            DbParameter dp1 = new NpgsqlParameter("@token", token);
 
            object obj = DBHelper.GetScalar(sql, dp1);
 
            return null == obj ? 0 : int.Parse(obj.ToString());
        }
 
        /// <summary>
        /// 执行CMD
        /// </summary>
        /// <param name="cmd">命令行</param>
        /// <param name="isPy">是否为QGIS Py脚本</param>
        /// <param name="isOut">是否输出错误</param>
        /// <returns>执行结果或出错信息</returns>
        public static string ExecCmd(string cmd, bool isPy = false, bool isOut = false)
        {
            List<string> list = new List<string>();
            if (isPy)
            {
                list.Add("cd \"C:\\Program Files\\QGIS 3.16\\apps\\Python37\"");
                list.Add("\"C:\\Program Files\\QGIS 3.16\\bin\\qgis_process-qgis-ltr.bat\"");
                //list.Add("\"C:\\Program Files\\QGIS 3.16\\bin\\python-qgis-ltr.bat\"");
                //list.Add("exit()");
            }
            list.Add(cmd);
 
            string str = ExecCmd(list, isOut);
 
            return str;
        }
 
        /// <summary>
        /// 执行CMD
        /// </summary>
        /// <param name="task">任务</param>
        /// <param name="list">命令集合</param>
        /// <param name="isOut">是否输出错误</param>
        /// <returns>执行结果或出错信息</returns>
        public static string ExecCmd(List<string> list, bool isOut = false)
        {
            string str;
            try
            {
                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                //p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = isOut;
                p.StartInfo.RedirectStandardError = true;
                p.Start();
 
                StreamWriter si = p.StandardInput; // 标准输入流 
                StreamReader so = isOut ? p.StandardOutput : null; // 标准输出流 
                StreamReader se = p.StandardError; // 标准错误流
 
                LogOut.Info("cmd = " + string.Join(",", list));
                si.AutoFlush = true;
                foreach (string cmd in list)
                {
                    si.WriteLine(cmd);
                }
                si.WriteLine("exit");
 
                string info = null == so ? null : so.ReadToEnd();
                str = se.ReadToEnd();
 
                //if (!string.IsNullOrEmpty(info)) LogOut.Debug(info);
                if (!string.IsNullOrEmpty(str) && !str.Contains("@jit(cache=True, nogil=True)")) LogOut.Error(str);
                if (p.HasExited == false) p.Kill();
 
                if (null != so) so.Close();
                se.Close();
                si.Close();
                p.Close();
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                str = ex.Message;
            }
 
            return str;
        }
    }
}