管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2023-03-21 1bb82963c0d0f38c87aeeffee1d70062e8a9049a
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
using ExportMap.cs;
using ExportMap.Models;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Web;
 
namespace ExportMap.db
{
    public class PubDBHelper
    {
        private static List<string> imgList = new List<string>() { "tif", "tiff", "img", "mpt" };
 
        private static string insertPublishSql = "insert into lf.sys_publish(name, url, path, type, status, dirid, depid, min, max, json, create_user, geom, bak) values (@name, @url, @path, @type, @status, @dirid, @depid, @min, @max, @json, @create_user, {0}, @bak) returning id";
 
        private static string insertMetaPubSql = "insert into lf.sys_meta_pub (metaid, pubid, create_user) select {0}, {1}, {2} from (select 1) temp where not exists (select 1 from lf.sys_meta_pub where metaid = {0} and pubid = {1})";
 
        // 模型-@cnName,model-@enName,自动发布模型-@name,http:// - @url,Tileset-@serve,1-@user,7-@pubid
        private static string insertLayerSql = "insert into lf.sys_layer (pid, cn_name, en_name, url, type, level, order_num, is_show, create_user, data_type, serve_type, pubid, elev) values ((select coalesce(id, 0) from lf.sys_layer where cn_name = @name limit 1), @cnName, @enName, @url, 2, (select coalesce(level, 0) + 1 from lf.sys_layer where cn_name = @name limit 1), (select coalesce(max(order_num), 0) + 1 from lf.sys_layer where pid = (select id from lf.sys_layer where cn_name = @name limit 1)), 0, @user, @name, @serve, @pubid, @elev);";
 
        private static string updateMetaSql = "update lf.sys_meta set path = @path where id = @id";
 
        /// <summary>
        /// 是/否发布过
        /// </summary>
        public static bool IsPublish(int metaid)
        {
            string sql = "select count(*) from lf.sys_meta_pub a inner join lf.sys_publish b on a.pubid = b.id where a.metaid = " + metaid;
 
            object obj = Tool.DBHelper.GetScalar(sql);
            int count = obj == null ? 0 : Convert.ToInt32(obj);
 
            return count > 0;
        }
 
        /// <summary>
        /// 是/否发布过
        /// </summary>
        public static bool IsPublish(string dirid)
        {
            string sql = "select count(*) from lf.sys_meta_pub a inner join lf.sys_publish b on a.pubid = b.id where b.type = 'DOM' and b.dirid = @dirid";
            DbParameter dp = new NpgsqlParameter("@dirid", dirid);
 
            object obj = Tool.DBHelper.GetScalar(sql, dp);
            int count = obj == null ? 0 : Convert.ToInt32(obj);
 
            return count > 0;
        }
 
        /// <summary>
        /// 插入元数据-数据发布表记录
        /// </summary>
        public static int InsertMetaPub(int metaid, int pubid, int userId)
        {
            string sql = string.Format(insertMetaPubSql, metaid, pubid, userId);
 
            return Tool.DBHelper.ExecuteNonQuery(sql);
        }
 
        /// <summary>
        /// 插入数据发布表记录
        /// </summary>
        public static int InsertPublish(SysPublish sys)
        {
            List<DbParameter> list = Tool.GetParams<SysPublish>(insertPublishSql, sys);
            string sql = string.Format(insertPublishSql, sys.geom == null ? "null" : sys.geom);
 
            object obj = Tool.DBHelper.GetScalar(sql, list.ToArray());
 
            return obj == null ? 0 : Convert.ToInt32(obj);
        }
 
        /// <summary>
        /// 插入图层表记录
        /// </summary>
        public static int InsertLayer(SysPublish sys, SysMeta meta, double elev = 0)
        {
            bool isImg = imgList.Contains(meta.type);
            string cnName = meta.name.Split(new char[] { '.' })[0];
            string name = isImg ? "影像" : "自动发布模型";
            string serve = isImg ? (meta.type.Equals("mpt") ? "Mpt" : "TMS") : "Tileset";
 
            List<DbParameter> list = new List<DbParameter>();
            list.Add(new NpgsqlParameter("@cnName", cnName));
            list.Add(new NpgsqlParameter("@enName", meta.name));
            list.Add(new NpgsqlParameter("@name", name));
            list.Add(new NpgsqlParameter("@url", sys.url));
            list.Add(new NpgsqlParameter("@serve", serve));
            list.Add(new NpgsqlParameter("@pubid", sys.id));
            list.Add(new NpgsqlParameter("@user", sys.create_user));
            list.Add(new NpgsqlParameter("@elev", elev));
 
            int rows = Tool.DBHelper.ExecuteNonQuery(insertLayerSql, list.ToArray());
 
            return rows;
        }
 
        /// <summary>
        /// 更新元数据文件路径
        /// </summary>
        public static int UpdateMetaPath(SysMeta meta)
        {
            List<DbParameter> list = new List<DbParameter>();
            list.Add(new NpgsqlParameter("@id", meta.id));
            list.Add(new NpgsqlParameter("@path", meta.path));
 
            int rows = Tool.DBHelper.ExecuteNonQuery(updateMetaSql, list.ToArray());
 
            return rows;
        }
 
        /// <summary>
        /// 获取参数列表
        /// </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;
        }
    }
}