183 lines
6.5 KiB
C#
183 lines
6.5 KiB
C#
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Linq;
|
||
using OpenAuth.App.Request;
|
||
using SqlSugar;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Diagnostics;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace OpenAuth.App.Common
|
||
{
|
||
public class CommonData
|
||
{
|
||
private ISqlSugarClient db;
|
||
|
||
public CommonData(ISqlSugarClient client)
|
||
{
|
||
db = client;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 执行cmd命令
|
||
/// </summary>
|
||
/// <param name="strDir"></param>
|
||
/// <param name="_path"></param>
|
||
public static void ExeCmdProcess(string strDir, string _path)
|
||
{
|
||
Process p = new Process();
|
||
p.StartInfo.FileName = "cmd.exe ";
|
||
p.StartInfo.UseShellExecute = false;
|
||
p.StartInfo.RedirectStandardInput = true;
|
||
p.StartInfo.RedirectStandardOutput = true;
|
||
p.StartInfo.RedirectStandardError = true;
|
||
p.StartInfo.CreateNoWindow = false;
|
||
|
||
//指定执行程序的目录
|
||
p.StartInfo.WorkingDirectory = _path;
|
||
//开始执行
|
||
p.Start();
|
||
//Console.WriteLine("command:" + strInput);
|
||
p.StandardInput.WriteLine(strDir.ToString() + "&exit");
|
||
|
||
//执行结果返回
|
||
string output = p.StandardOutput.ReadToEnd();
|
||
|
||
//等待执行完成
|
||
p.WaitForExit();
|
||
|
||
//关闭程序
|
||
p.Close();
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// Pgsql获取GeoJson数据
|
||
/// </summary>
|
||
/// <param name="req"></param>
|
||
/// <param name="_configuration"></param>
|
||
/// <returns></returns>
|
||
public JToken PgsqlGeoJsonCommon(QueryGeoJsonCommonReq req)
|
||
{
|
||
//如果有的参数为空,设置参数的默认值
|
||
req.init();
|
||
|
||
//查询列的数据
|
||
StringBuilder sqlColumn = new StringBuilder();
|
||
sqlColumn.AppendFormat(" select {1} from {0} {3} order by \"{2}\"", req.tablename, req.column, req.order, req.where);
|
||
|
||
StringBuilder sqlGeo = new StringBuilder();
|
||
sqlGeo.AppendFormat(" select st_asgeojson(\"{0}\") geom from \"{1}\" {3} order by \"{2}\"", req.geom, req.tablename, req.order, req.where);
|
||
|
||
return PgsqlGeoJsonCommon(sqlColumn, sqlGeo, req.pageIndex, req.limit);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 查询GeoJson公共方法
|
||
/// </summary>
|
||
/// <param name="sqlColumn"></param>
|
||
/// <param name="sqlGeom"></param>
|
||
/// <param name="pageIndex"></param>
|
||
/// <param name="pageSize"></param>
|
||
/// <param name="_configuration"></param>
|
||
/// <returns></returns>
|
||
public JToken PgsqlGeoJsonCommon(StringBuilder sqlColumn, StringBuilder sqlGeom, int pageIndex, int pageSize)
|
||
{
|
||
//获取到数据库操作实例
|
||
int total = 0;
|
||
int totalPage = 0;
|
||
|
||
//查询列
|
||
var dt_properties = db.SqlQueryable<dynamic>(sqlColumn.ToString()).ToPageList(pageIndex, pageSize, ref total, ref totalPage);
|
||
//列数据转换为字符串
|
||
string str_properties = JsonConvert.SerializeObject(dt_properties);
|
||
//列数据转换为json对象
|
||
JToken jtoken_properties = (JToken)JsonConvert.DeserializeObject(str_properties);
|
||
|
||
//查询geojson数据
|
||
var dt_geom = db.SqlQueryable<dynamic>(sqlGeom.ToString()).ToDataTablePage(pageIndex, pageSize);
|
||
|
||
//组装最终数据
|
||
JObject obj = new JObject();
|
||
JArray array = new JArray();
|
||
obj.Add("type", "FeatureCollection");
|
||
obj.Add("totalNumber", total);
|
||
obj.Add("totalPage", totalPage);
|
||
//遍历geojson数据
|
||
for (var i = 0; i < dt_geom.Rows.Count; i++)
|
||
{
|
||
//行数据
|
||
DataRow item = dt_geom.Rows[i];
|
||
//单条geojson
|
||
string _geom = item["geom"] == DBNull.Value ? "" : item["geom"].ToString();
|
||
//给数据赋值
|
||
JObject featureObj = new JObject();
|
||
featureObj.Add("type", "Feature");
|
||
featureObj.Add("geometry", (JToken)JsonConvert.DeserializeObject(_geom));
|
||
featureObj.Add("properties", jtoken_properties[i]);
|
||
//添加到数组
|
||
array.Add(featureObj);
|
||
}
|
||
obj.Add("features", array);
|
||
return obj;
|
||
|
||
}
|
||
|
||
public string GetMaxKeyVal(string _column, string tablename, int _gid_const)
|
||
{
|
||
string _val = "";
|
||
//查询最大的gid
|
||
var column_list = SearchColumnsList(tablename);
|
||
string _type = column_list[0].column_type;
|
||
if (_type != "integer")
|
||
{
|
||
_val = Guid.NewGuid().ToString();
|
||
return _val;
|
||
}
|
||
|
||
var _maxgid = db.SqlQueryable<dynamic>($"SELECT max(\"{_column}\") \"maxGid\" FROM \"{tablename}\" where \"{_column}\" < 6000000").First().maxGid;
|
||
//gid为空时,说明一条数据都没有
|
||
if (_maxgid == null)
|
||
_val = _gid_const.ToString();
|
||
else
|
||
{
|
||
//如果大于设定值,在此基础上加一,否则设置为默认值
|
||
if (_maxgid >= _gid_const)
|
||
_val = (_maxgid + 1).ToString();
|
||
else
|
||
_val = _gid_const.ToString();
|
||
}
|
||
return _val;
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取列名
|
||
/// </summary>
|
||
/// <param name="tablename"></param>
|
||
/// <returns></returns>
|
||
public List<dynamic> SearchColumnsList(string tablename)
|
||
{
|
||
//查询列名和注释描述
|
||
StringBuilder columnSql = new StringBuilder();
|
||
columnSql.AppendFormat(@$" SELECT a.attname column_name,col_description(a.attrelid,a.attnum) as description,format_type(a.atttypid,a.atttypmod) as column_type
|
||
FROM pg_class as c,pg_attribute as a
|
||
where c.relname = '{tablename}'
|
||
and a.attrelid = c.oid
|
||
and a.attnum>0
|
||
and attstattarget = -1");
|
||
//查询结果
|
||
|
||
var column_list = db.SqlQueryable<dynamic>(columnSql.ToString()).ToList();
|
||
return column_list;
|
||
|
||
}
|
||
|
||
}
|
||
}
|