using Npgsql;
|
using System;
|
using System.Collections.Generic;
|
using System.Data.Common;
|
using System.Data.SQLite;
|
using System.Drawing;
|
using System.Drawing.Imaging;
|
using System.IO;
|
using System.Linq;
|
using System.Reflection;
|
using System.Security.Cryptography;
|
using System.Web;
|
|
namespace JiangSu.cs
|
{
|
public class Tools
|
{
|
private static PostgreHelper _pgHelper;
|
|
/// <summary>
|
/// DB帮助类
|
/// </summary>
|
public static PostgreHelper PGHelper
|
{
|
get
|
{
|
if (null == _pgHelper)
|
{
|
_pgHelper = new PostgreHelper();
|
}
|
|
return _pgHelper;
|
}
|
}
|
|
/// <summary>
|
/// 获取参数列表
|
/// </summary>
|
public static DbParameter[] GetPGParams<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);
|
NpgsqlParameter dp = new NpgsqlParameter("@" + name, value);
|
list.Add(dp);
|
}
|
|
start = sql.IndexOf("@", end);
|
}
|
|
return list.ToArray();
|
}
|
|
/// <summary>
|
/// 获取参数列表
|
/// </summary>
|
public static SQLiteParameter[] GetSQLiteParams<T>(string sql, T t)
|
{
|
List<SQLiteParameter> list = new List<SQLiteParameter>();
|
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);
|
SQLiteParameter dp = new SQLiteParameter("@" + name, value);
|
list.Add(dp);
|
}
|
|
start = sql.IndexOf("@", end);
|
}
|
|
return list.ToArray();
|
}
|
|
/// <summary>
|
/// 字节格式化
|
/// </summary>
|
public static string FormatBytes(long bytes)
|
{
|
string[] Suffix = { "Byte", "KB", "MB", "GB", "TB" };
|
|
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>
|
/// 计算文件的MD5码
|
/// </summary>
|
public static string CalcFileMD5(string filePath)
|
{
|
using (var md5 = new MD5CryptoServiceProvider())
|
{
|
using (var stream = File.OpenRead(filePath))
|
{
|
byte[] hash = md5.ComputeHash(stream);
|
return BitConverter.ToString(hash).Replace("-", "").ToLower();
|
}
|
}
|
}
|
|
/// <summary>
|
/// 获取图片的GPS信息
|
/// </summary>
|
public static List<string> GetImgGps(string path)
|
{
|
Image img = null;
|
try
|
{
|
img = Image.FromFile(path);
|
var items = img.PropertyItems.OrderByDescending(x => x.Id);
|
|
List<string> list = new List<string> { "0", "0", "0" };
|
foreach (PropertyItem item in items)
|
{
|
if (item.Id >= 0x0000 && item.Id <= 0x001e) // 只取Id范围为0x0000到0x001e
|
{
|
switch (item.Id)
|
{
|
case 0x0002: // 设置纬度
|
list[1] = GetGpsVal(item.Value);
|
break;
|
case 0x0004: // 设置经度
|
list[0] = GetGpsVal(item.Value);
|
break;
|
case 0x0006:
|
if (item.Value.Length == 8)
|
{
|
double altitude = BitConverter.ToUInt32(item.Value, 0) * 1.0d / BitConverter.ToUInt32(item.Value, 4);
|
list[2] = altitude.ToString("0.000");
|
}
|
break;
|
}
|
}
|
}
|
|
return list;
|
}
|
catch (Exception ex)
|
{
|
LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
|
return null;
|
}
|
finally
|
{
|
if (null != img) img.Dispose();
|
}
|
}
|
|
public static string GetImgPointZ(string path)
|
{
|
List<string> list = GetImgGps(path);
|
|
return null == list ? null : string.Format("POINT Z ({0})", string.Join(" ", list.ToArray()));
|
}
|
|
/// <summary>
|
/// 获取GPS值
|
/// </summary>
|
private static string GetGpsVal(byte[] bytes)
|
{
|
if (bytes.Length != 24) return "0";
|
|
//degrees(将byte[0]~byte[3]转成uint, 除以byte[4]~byte[7]转成的uint)
|
double d = BitConverter.ToUInt32(bytes, 0) * 1.0d / BitConverter.ToUInt32(bytes, 4);
|
|
//minutes(将byte[8]~byte[11]转成uint, 除以byte[12]~byte[15]转成的uint)
|
double m = BitConverter.ToUInt32(bytes, 8) * 1.0d / BitConverter.ToUInt32(bytes, 12);
|
|
//seconds(将byte[16]~byte[19]转成uint, 除以byte[20]~byte[23]转成的uint)
|
double s = BitConverter.ToUInt32(bytes, 16) * 1.0d / BitConverter.ToUInt32(bytes, 20);
|
|
double val = (((s / 60 + m) / 60) + d);
|
|
return val.ToString("0.00000000");
|
}
|
}
|
}
|