ldj/ePickSqlite/utils/Utils.cs

532 lines
14 KiB
C#
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.Web;
namespace DeiNiu.Utils
{
// public enum op_flag { add = 1, update, delete, getObj, queryAll, queryExample,queryActived,getPk, getCount};
// public enum ProjectStatus { 新增未提交, 新增待预审, 新增预审通过, 新增预审未通过, 新增审核通过, 新增审核未通过, 新增考核通过, 新增考核未通过,删减未提交,删减待预审,删减预审通过,删减预审未通过,删减审核通过,删减审核未通过,删减考核通过,删减考核未通过,项目完成 };
public enum FileType {Project,ProjectDetail,,,, };
public struct ProjectDetailStatus
{
public static string
{
get
{
return "审核未通过";
}
}
public static string
{
get
{
return "考核通过";
}
}
public static string
{
get
{
return "考核未通过";
}
}
public static string
{
get
{
return "月度未提交";
}
}
public static string
{
get
{
return "月度已提交";
}
}
public static string
{
get
{
return "待预审";
}
}
public static string
{
get
{
return "预审通过";
}
}
public static string
{
get
{
return "预审未通过";
}
}
public static string
{
get
{
return "审核通过";
}
}
}
public struct ApproveRoles
{
public static string YuShen
{
get
{
return "项目预审";
}
}
public static string ShenHe
{
get
{
return "项目审核";
}
}
public static string KaoHe
{
get
{
return "项目考核";
}
}
public static string
{
get
{
return "项目维护";
}
}
}
public struct ProjectStatus
{
public static string
{
get
{
return "项目已分配";
}
}
public static string
{
get
{
return "项目未分配";
}
}
public static string
{
get
{
return "新增未提交";
}
}
public static string
{
get
{
return "新增已提交";
}
}
public static string
{
get
{
return "新增考核通过";
}
}
public static string
{
get
{
return "项目冻结";
}
}
public static string
{
get
{
return "项目解冻";
}
}
public static string
{
get
{
return "新增审核通过";
}
}
public static string
{
get
{
return "新增审核未通过";
}
}
public static string
{
get
{
return "新增预审未通过";
}
}
public static string
{
get
{
return "新增预审通过";
}
}
public static string
{
get
{
return "删减审核未通过";
}
}
public static string
{
get
{
return "删减审核通过";
}
}
public static string
{
get
{
return "删减预审未通过";
}
}
public static string
{
get
{
return "删减预审通过";
}
}
public static string
{
get
{
return "删减未提交";
}
}
public static string
{
get
{
return "删减已提交";
}
}
public static string
{
get
{
return "新增考核未通过";
}
}
public static string
{
get
{
return "项目完成";
}
}
public static string
{
get
{
return "项目未完成";
}
}
public static string
{
get
{
return "删减考核未通过";
}
}
public static string
{
get
{
return "删减考核通过";
}
}
}
  public struct appScope
{
/// <summary>
/// used for storing logic object in session
/// when page changes in server side, logic object in session
/// will be replaced by new one.
/// </summary>
public static string PagelevelObj
{
get
{
return "PageLevelObj";
}
}
}
public class MyTracer
{
private static TraceSwitch appSwitch = new TraceSwitch("TraceSwitch", "Switch in config file");
private static string render(object clz, string msg)
{
//time
StringBuilder sb = new StringBuilder();
DateTime dt = DateTime.Now;
sb.Append(dt.ToLongDateString()).Append(" ").Append(dt.ToLongTimeString()).Append(" - ");
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(2);
//class name
sb.Append(clz.GetType().FullName).Append(" [");
//method name
MethodBase methodBase = stackFrame.GetMethod();
sb.Append(methodBase.Name).Append("] ");
//the msg
sb.Append(msg);
return sb.ToString();
}
public static void Error(object clz, string msg)
{
Trace.WriteLineIf(appSwitch.TraceError, render(clz, "[ERROR] " + msg));
}
public static void Warning(object clz, string msg)
{
Trace.WriteLineIf(appSwitch.TraceWarning, render(clz, "[WARNING] " + msg));
}
public static void Info(object clz, string msg)
{
Trace.WriteLineIf(appSwitch.TraceInfo, render(clz, "[INFO] " + msg));
}
public static void Verbose(object clz, string msg)
{
Trace.WriteLineIf(appSwitch.TraceVerbose, render(clz, "[VERBOSE] " + msg));
}
}
public class Util
{
public static string Encrypt(string password)//加密函数
{
Byte[] clearBytes = new UnicodeEncoding().GetBytes(password);
Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
return BitConverter.ToString(hashedBytes);
}
/// <summary>
/// 组合in sql,return like " 1,2,3"
/// </summary>
/// <param name="al"></param>
/// <returns></returns>
public static string buildWhereIntIn(ArrayList al)
{
string whereString = string.Empty;
for (int i = 0; i < al.Count; i++)
{
whereString += "'"+ al[i] + "',";
}
if (whereString.EndsWith(","))
{
whereString = whereString.Substring(0, whereString.Length - 1);
}
return whereString;
}
public static DataSet Excel2DataSet(string filepath)
{
DataSet ds = new DataSet();
ArrayList SheeNames = ExcelSheetName(filepath);
for(int i=0;i<SheeNames.Count;i++)
{
DataTable dt = (ExcelDataSource(filepath, SheeNames[i].ToString())).Tables[0].Copy();
dt.TableName = SheeNames[i].ToString();
dt.TableName = dt.TableName.Substring(0, dt.TableName.IndexOf("$"));
ds.Tables.Add(dt);
}
return ds;
}
//该方法实现从Excel中导出数据到DataSet中其中filepath为Excel文件的绝对路径 sheetname为excel文件中的表名
public static DataSet ExcelDataSource(string filepath, string sheetname)
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "]", strConn);
DataSet ds = new DataSet();
oada.Fill(ds);
conn.Close();
return ds;
}
//获得Excel中的所有sheetname。
public static ArrayList ExcelSheetName(string filepath)
{
ArrayList al = new ArrayList();
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataTable sheetNames = conn.GetOleDbSchemaTable
(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
conn.Close();
foreach (DataRow dr in sheetNames.Rows)
{
al.Add(dr[2]);
}
return al;
}
public static DataView GetTopDataViewRows(DataView dv, int n)
       {
           DataTable dt = dv.Table.Clone();
   
           for (int i = 0; i < n - 1; i++)
           {
               if (i >= dv.Count)
               {
                   break;
             }
             dt.ImportRow(dv[i].Row);
          }
        return new DataView(dt, dv.RowFilter, dv.Sort,  dv.RowStateFilter);
     }
/// <summary>
/// 读取配置文件某项的值
/// </summary>
/// <param name="key">appSettings的key</param>
/// <returns>appSettings的Value</returns>
//public static string GetConfig(string key)
//{
//string _value = string.Empty;
//System.Configuration.ConfigurationManager config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//if (config.AppSettings.Settings[key] != null)
//{
// _value = config.AppSettings.Settings[key].Value;
//}
//return _value;
//}
//条形码转换
public static string getCode128(string codeIn)
{
//string str ="";
string result;
int checksum = 104;
for (int i = 0; i < codeIn.Length; i++)
{
if (codeIn[i] >= 32)
{
checksum += (codeIn[i] - 32) * (i + 1);
}
else
{
checksum += (codeIn[i] + 64) * (i + 1);
}
}
checksum = checksum % 103;
if (checksum < 95)
{
checksum += 32;
}
else
{
checksum += 100;
}
result = Convert.ToChar(204) + codeIn +
Convert.ToChar(checksum) + Convert.ToChar(206);
return result;
}
public static string getOrderNo(enumCreateOrderType orderType,int seq)
{
string prefix = "UN";
switch (orderType)
{
case enumCreateOrderType.repOrderIn:
prefix = "RPI";
break;
}
return string.Format("{0}{1:D2}{2:D10}",prefix,DateTime.Now.Month,seq);
}
public static string getLightKey(int color,int labelId, int address)
{
return string.Format("{0}-{1}-{2}", color, labelId, address);
}
public static Dictionary<int,string> convertEnumToDic(Type enumType){
Dictionary<int, string> dic = new Dictionary<int, string>();
foreach (int myCode in Enum.GetValues(enumType))
{
string strName = Enum.GetName(enumType, myCode);//获取名称
string strVaule = myCode.ToString();//获取值
dic[myCode] = strName;
}
return dic;
}
}
}