标签:
EtlCheckMethod.cs
/* Copyright (c) 2015 Xiamen HaiHui Software Co., Ltd. All rights reserved
*
* Create by huanglc@holworth.com at 2015-04-23 21:52:52
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace Contract.Domain
{
[DataContract(Namespace = "Contract.Domain")]
public class EtlCheckMethod: Framework.Domain.Entity
{
/// <summary>
/// 验证函数名
/// </summary>
[DataMember]
public virtual string Name
{
get;
set;
}
/// <summary>
/// 服务名
/// </summary>
[DataMember]
public virtual string ServiceName
{
get;
set;
}
/// <summary>
/// 扩展参数值1
/// </summary>
[DataMember]
public virtual string ExtendParameter
{
get;
set;
}
/// <summary>
/// 扩展参数值2
/// </summary>
[DataMember]
public virtual string ExtendParameter2
{
get;
set;
}
/// <summary>
/// 扩展参数值3
/// </summary>
[DataMember]
public virtual string ExtendParameter3
{
get;
set;
}
}
}
EtlColumn.cs
/* Copyright (c) 2015 Xiamen HaiHui Software Co., Ltd. All rights reserved
*
* Create by huanglc@holworth.com at 2015-04-15 20:42:23
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace Contract.Domain
{
[DataContract(Namespace = "Contract.Domain")]
public class EtlColumn : Framework.Domain.Entity
{
/// <summary>
/// 映射列索引
/// </summary>
[DataMember]
public virtual int? MapIndex
{
get;
set;
}
/// <summary>
/// 映射列
/// </summary>
[DataMember]
public virtual string MapColumn
{
get;
set;
}
/// <summary>
/// etl_id
/// </summary>
[DataMember]
public virtual string EtlId
{
get;
set;
}
[DataMember]
public virtual Contract.Domain.ConstEnum.DataTypeDimesion DataType
{
get;
set;
}
}
}
EtlDataMap.cs
/* Copyright (c) 2015 Xiamen HaiHui Software Co., Ltd. All rights reserved
*
* Create by huanglc@holworth.com at 2015-04-15 20:42:33
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace Contract.Domain
{
[DataContract(Namespace = "Contract.Domain")]
public class EtlDataMap : Framework.Domain.Entity
{
/// <summary>
/// 映射表名
/// </summary>
[DataMember]
public virtual string MapTableName
{
get;
set;
}
/// <summary>
/// 导入名
/// </summary>
[DataMember]
public virtual string ImportName
{
get;
set;
}
/// <summary>
/// 文件名开头
/// </summary>
[DataMember]
public virtual string FileNameStart
{
get;
set;
}
/// <summary>
/// 数据开始行
/// </summary>
[DataMember]
public virtual Int32 StartRow
{
get;
set;
}
/// <summary>
/// 数据结束标志
/// </summary>
[DataMember]
public virtual string EndStandard
{
get;
set;
}
/// <summary>
/// 当前映射列
/// </summary>
[DataMember]
public virtual IList<EtlColumn> ColumnList
{
get;
set;
}
}
}
EtlTableDef.cs
/* Copyright (c) 2015 Xiamen HaiHui Software Co., Ltd. All rights reserved
*
* Create by huanglc@holworth.com at 2015-05-01 14:33:23
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace Contract.Domain
{
[DataContract(Namespace = "Contract.Domain")]
public class EtlTableDef : Framework.Domain.Entity
{
/// <summary>
/// table_name
/// </summary>
[DataMember]
public virtual string TableName
{
get;
set;
}
[DataMember]
public virtual string PK_Id
{
get;
set;
}
[DataMember]
public virtual IList<VerifyCheck> VerifyCheckList
{
get;
set;
}
}
VerifyCheck.cs
/* Copyright (c) 2015 Xiamen HaiHui Software Co., Ltd. All rights reserved
*
* Create by huanglc@holworth.com at 2015-04-26 09:11:41
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace Contract.Domain
{
[DataContract(Namespace = "Contract.Domain")]
public class VerifyCheck : Framework.Domain.Entity
{
/// <summary>
/// table_name
/// </summary>
[DataMember]
public virtual string TableName
{
get;
set;
}
/// <summary>
/// colum_name
/// </summary>
[DataMember]
public virtual string ColumName
{
get;
set;
}
/// <summary>
/// check_method
/// </summary>
[DataMember]
public virtual string CheckMethod
{
get;
set;
}
/// <summary>
/// 是否必须映射
/// </summary>
[DataMember]
public virtual Int32? IsMustMap
{
get;
set;
}
[DataMember]
public virtual string TableId
{
get;
set;
}
[DataMember]
public virtual bool IsCheck
{
get;
set;
}
}
}
EtlExcelService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Contract.Domain;
using Contract.IService;
namespace ETLBLL
{
public class EtlExcelService : BaseService.BaseService<Framework.Domain.Entity>, IEtlExcelService
{
public void SaveEtlLog(Contract.Domain.EtlLog obj)
{
Dao.SaveOrUpdate(obj);
}
public int SaveImportExcel(string filePath)
{
string fileName = System.IO.Path.GetFileName(filePath).ToLower();
string[] tableName = Holworth.Utility.ExcelUtil.GetExcelTableName(filePath);
System.Data.DataTable table = Holworth.Utility.ExcelUtil.GetExcelSheetContent(filePath, tableName[0]);
getEtlDataMap();
int totalCount = 0;
var k = mapList.Where(m => fileName.StartsWith(m.FileNameStart)).ToList();
bool pkId = false;
if (k != null && k.Count > 0)
{
string extention = System.IO.Path.GetExtension(filePath).ToLower();
foreach (Contract.Domain.EtlDataMap m in k)
{
int start = m.StartRow;
if (extention.IndexOf("xlsx") > -1)
{
start--;
}
System.Text.StringBuilder sb = new StringBuilder();
table.TableName = m.MapTableName;
for (int i = start; i < table.Rows.Count; i++)
{
if (i < 0)
{
continue;
}
if (string.IsNullOrEmpty(table.Rows[i][0].ToString()) || table.Rows[i][0].ToString().Equals(m.EndStandard))
{
break;
}
if (m.ColumnList == null || m.ColumnList.Count == 0)
{
throw new Exception(string.Format("无法找到表名为{0}的映射列.", m.MapTableName));
}
sb.Append(string.Format("insert into {0}(", m.MapTableName));
bool first = false;
bool firstValue = false;
System.Text.StringBuilder valueSb = new StringBuilder();
EtlTableDef def = TablePrimaryKey[m.MapTableName] as EtlTableDef;
if (def.VerifyCheckList != null)
{
foreach (var v in def.VerifyCheckList)
{
v.IsCheck = false;
}
}
foreach (var c in m.ColumnList)
{
if (!first)
{
sb.Append(c.MapColumn);
first = true;
}
else
{
sb.Append("," + c.MapColumn);
}
if (!firstValue)
{
firstValue = true;
valueSb.Append(" values(");
}
else
{
valueSb.Append(",");
}
if (c.MapColumn.ToLower().Equals(def.PK_Id.ToLower()))
{
valueSb.Append("HIBERNATE_SEQUENCE.NEXTVAL");
pkId = true;
}
else
{
string valueObject = table.Rows[i][c.MapIndex.Value].ToString();
string title = "";
string columnName = "";
if (c.MapIndex <= 90)
{
char cc = (char)(c.MapIndex.Value + 65);
columnName = cc.ToString();
}
else
{
columnName = "A" + ((char)(c.MapIndex.Value + 65 - 26)).ToString();
}
object mValue = ChangeValue(valueObject, c.DataType, out title, i, columnName);
if (mValue != null)
{
if (mValue is DateTime)
{
valueObject = ((DateTime)mValue).ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
valueObject = mValue.ToString();
}
}
if (!string.IsNullOrEmpty(title))
{
throw new Exception(title);
}
#region 表列验证并设置列值
if (def.VerifyCheckList != null && def.VerifyCheckList.Count > 0)
{
var checkList = def.VerifyCheckList.Where(km => km.ColumName.Equals(c.MapColumn)).ToList();
if (checkList != null && checkList.Count > 0)
{
object[] returnValue = null;
foreach (var check in checkList)
{
check.IsCheck = true;
string[] ss = check.CheckMethod.Split(new char[] { ‘;‘ });
System.Collections.Hashtable ha = new System.Collections.Hashtable();
ha.Add("obj", valueObject);
ha.Add("table", table);
ha.Add("row", table.Rows[i]);
int jj = 0;
foreach (string s in ss)
{
if (!string.IsNullOrEmpty(s))
{
var eltlMehtod = MethodList.Where(mm => mm.ServiceName.Equals(s)).ToList();
if (eltlMehtod.Count == 0)
{
throw new Exception("系统无法找到验证码为" + ss[0] + "服务");
}
if (jj == 0)
{
ha.Add("column", eltlMehtod[0]);
ha.Add("etlDataMap", m);
}
jj++;
returnValue = (object[])Framework.Proxy.Expressions.ExpressionEvaluator.GetValue(null, s, ha);
if (returnValue[1] != null && !string.IsNullOrEmpty(returnValue[1].ToString()))
{
throw new Exception(string.Format("{0}行{1}列{2},{3}", i, columnName, valueObject, returnValue[1].ToString()));
}
}
}
}
if (returnValue != null)
{
string realValue = string.Empty;
switch (c.DataType)
{
case ConstEnum.DataTypeDimesion.整型:
case ConstEnum.DataTypeDimesion.浮点型:
if (returnValue[2] == null)
{
realValue = "‘‘";
}
else
{
realValue = returnValue[2].ToString();
}
break;
case ConstEnum.DataTypeDimesion.日期:
if (returnValue[2] == null)
{
realValue = "‘‘";
}
else
{
if (returnValue[2] is DateTime)
{
realValue = ((DateTime)returnValue[2]).ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
realValue = DateTime.Parse(returnValue[2].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
}
realValue = string.Format("to_date(‘{0}‘,‘yyyy-mm-dd hh24:mi:ss‘)", realValue);
}
break;
default:
if (returnValue[2] == null)
{
realValue = "‘‘";
}
else
{
realValue = "‘" + returnValue[2].ToString() + "‘";
}
break;
}
valueSb.Append(realValue);
}
else
{
valueSb.Append("‘‘");
}
}
else
{
switch (c.DataType)
{
case ConstEnum.DataTypeDimesion.整型:
case ConstEnum.DataTypeDimesion.浮点型:
if (string.IsNullOrEmpty(valueObject))
{
valueObject = "‘‘";
}
else
{
valueObject = mValue.ToString();
}
break;
case ConstEnum.DataTypeDimesion.日期:
if (string.IsNullOrEmpty(valueObject))
{
valueObject = "‘‘";
}
else
{
valueObject = string.Format("to_date(‘{0}‘,‘yyyy-mm-dd hh24:mi:ss‘)", DateTime.Parse(valueObject).ToString("yyyy-MM-dd HH:mm:ss"));
}
break;
default:
valueObject = valueObject.Replace("‘", "");
valueObject = "‘" + valueObject + "‘";
break;
}
valueSb.Append(valueObject);
}
}
else
{
switch (c.DataType)
{
case ConstEnum.DataTypeDimesion.整型:
case ConstEnum.DataTypeDimesion.浮点型:
if (string.IsNullOrEmpty(valueObject))
{
valueObject = "‘‘";
}
else
{
valueObject = mValue.ToString();
}
break;
case ConstEnum.DataTypeDimesion.日期:
if (string.IsNullOrEmpty(valueObject))
{
valueObject = "‘‘";
}
else
{
valueObject = string.Format("to_date(‘{0}‘,‘yyyy-mm-dd hh24:mi:ss‘)", DateTime.Parse(valueObject).ToString("yyyy-MM-dd HH:mm:ss"));
}
break;
default:
valueObject = valueObject.Replace("‘", "");
valueObject = "‘" + valueObject + "‘";
break;
}
valueSb.Append(valueObject);
}
#endregion
}
}
if (!pkId)
{
valueSb.Append(",HIBERNATE_SEQUENCE.NEXTVAL");
sb.Append(",Id");
}
valueSb.Append(");\n");
sb.Append(")");
sb.Append(valueSb.ToString());
valueSb.Clear();
var tmpCheckList = def.VerifyCheckList.Where(vv => !vv.IsCheck).ToList();
if (tmpCheckList != null && tmpCheckList.Count > 0)
{
System.Text.StringBuilder errorColumn = new StringBuilder();
foreach (var tv in tmpCheckList)
{
if (errorColumn.Length == 0)
{
errorColumn.Append("‘" + tv.ColumName + "‘");
}
else
{
errorColumn.Append(",‘" + tv.ColumName + "‘");
}
}
string where = "(" + errorColumn.ToString() + ")";
Framework.QueryInfo errorInfo = new Framework.QueryInfo();
errorInfo.Parameters.Add("table_name", def.TableName);
errorInfo.CustomSQL = " select column_name,nvl((select t_s.comments from all_col_comments t_s where t_s.column_name = t.column_name and t_s.table_name =:table_name) ,column_name))"
+ " comments from all_tab_columns t"
+ " where table_name = upper(:table_name) and column_name in " + where;
var cds = Dao.ExecuteDataSet(errorInfo);
where = string.Empty;
for (int ii = 0; ii < cds.Tables[0].Rows.Count; ii++)
{
System.Data.DataRow row = cds.Tables[0].Rows[ii];
if (ii == 0)
{
where = row["comments"].ToString();
}
else
{
where += "," + row["comments"].ToString();
}
}
where += "属性必须存在,请设置该属性的映射列";
throw new Exception(where);
}
totalCount++;
}
if (sb.Length > 0)
{
Framework.QueryInfo info = new Framework.QueryInfo();
info.CustomSQL = "begin\n" + sb.ToString() + "end;\n";
int count = Dao.ExecuteNonQuery(info);
return totalCount;
}
}
}
return 0;
}
public System.Data.DataSet ExcuteDataSet(Framework.QueryInfo info)
{
return Dao.ExecuteDataSet(info);
}
private object ChangeValue(string obj, Contract.Domain.ConstEnum.DataTypeDimesion type,out string title,int rowIndex,string column)
{
title = "";
switch (type)
{
case ConstEnum.DataTypeDimesion.字符串:
return obj;
case ConstEnum.DataTypeDimesion.日期:
if (string.IsNullOrEmpty(obj))
{
title = "";
return null;
}
else
{
DateTime a = DateTime.Now;
if (!DateTime.TryParse(obj, out a))
{
title = string.Format("无法将{0}行{1}列{2}转为日期", rowIndex, column, obj);
}
return a;
}
case ConstEnum.DataTypeDimesion.整型:
int k = 0;
if(!int.TryParse(obj,out k))
{
title = string.Format("无法将{0}行{1}列{2}转为整型", rowIndex, column, obj);
}
return k;
case ConstEnum.DataTypeDimesion.浮点型:
decimal d = 0;
if (!decimal.TryParse(obj, out d))
{
title = string.Format("无法将{0}行{1}列{2}转为浮点型", rowIndex, column, obj);
}
return d;
default:
return obj;
}
}
IList<EtlCheckMethod> _methodList = null;
private IList<EtlCheckMethod> MethodList
{
get
{
if (_methodList == null)
{
Framework.QueryInfo info = new Framework.QueryInfo();
info.QueryObject = "EtlCheckMethod";
_methodList = Holworth.Utility.Utility.ListToT<EtlCheckMethod>(Dao.FindList(info));
}
return _methodList;
}
}
System.Collections.Hashtable _ha = null;
private System.Collections.Hashtable TablePrimaryKey
{
get
{
if (_ha == null)
{
Framework.QueryInfo info = new Framework.QueryInfo();
info.QueryObject = "EtlTableDef";
var list = Holworth.Utility.Utility.ListToT<EtlTableDef>(Dao.FindList(info));
_ha = new System.Collections.Hashtable();
foreach(var def in list)
{
if(!_ha.Contains(def.TableName))
{
var vlist = VerifyCheckList.Where(m => m.TableName.Equals(def.TableName)).ToList();
def.VerifyCheckList = vlist;
_ha.Add(def.TableName, def);
}
}
}
return _ha;
}
}
IList<VerifyCheck> _verifyCheckList = null;
private IList<VerifyCheck> VerifyCheckList
{
get
{
if (_verifyCheckList == null)
{
Framework.QueryInfo info = new Framework.QueryInfo();
info.QueryObject = "VerifyCheck";
_verifyCheckList = Holworth.Utility.Utility.ListToT<VerifyCheck>(Dao.FindList(info));
}
return _verifyCheckList;
}
}
private IList< EtlDataMap> mapList
{
get;
set;
}
public IList<EtlDataMap> getEtlDataMap()
{
if (mapList == null)
{
Framework.QueryInfo info = new Framework.QueryInfo();
info.QueryObject = "EtlDataMap";
mapList = Holworth.Utility.Utility.ListToT<EtlDataMap>(Dao.FindList(info));
info = new Framework.QueryInfo();
info.QueryObject = "EtlColumn";
var list = Holworth.Utility.Utility.ListToT<EtlColumn>(Dao.FindList(info));
var ds=InitColumnType();
System.Data.DataView view =new System.Data.DataView( ds.Tables[0]);
if (mapList != null)
{
foreach (EtlDataMap map in mapList)
{
view.RowFilter = "table_name=‘" + map.MapTableName + "‘";
var t = list.Where(m => m.EtlId == map.Id).ToList();
map.ColumnList = t;
foreach (var c in map.ColumnList)
{
foreach (System.Data.DataRowView r in view)
{
if (r["COLUMN_NAME"].ToString() ==c.MapColumn)
{
c.DataType = GetType(r["Data_Type"].ToString());
}
}
}
}
}
}
return mapList;
}
private Contract.Domain.ConstEnum.DataTypeDimesion GetType(string oracleType)
{
oracleType = oracleType.ToLower();
switch (oracleType)
{
case "date":
return ConstEnum.DataTypeDimesion.日期;
break;
case "number":
return ConstEnum.DataTypeDimesion.浮点型;
break;
case "integer":
return ConstEnum.DataTypeDimesion.整型;
break;
default:
return ConstEnum.DataTypeDimesion.字符串;
break;
}
}
private System.Data.DataSet InitColumnType()
{
Framework.QueryInfo info = new Framework.QueryInfo();
info.CustomSQL = "select d.Code from SysDict d";
info.AddParam("TypeCode", "ETLDataTable");
var list = Dao.FindList(info);
string tableName = "";
foreach (string s in list)
{
if (string.IsNullOrEmpty(tableName))
{
tableName = "‘" + s + "‘";
}
else
{
tableName += ",‘" + s + "‘";
}
}
info = new Framework.QueryInfo();
info.CustomSQL = "select * from ALL_TAB_COLUMNS where table_name in(" + tableName + ")";
var ds = Dao.ExecuteDataSet(info);
return ds;
}
}
}
EtlDataMapService.cs
/* Copyright (c) 2015 Xiamen Weixin Software Co., Ltd. All rights reserved
*
* Create by huanglc@holworth.com at 2015-04-15 21:01:15
*
*/
using System;
using System.Text;
using System.Collections.Generic;
using Contract.IService;
using Framework;
using System.Collections;
using Contract.Domain;
using BaseService;
using Contract.IService;
namespace ETLBLL
{
public class EtlDataMapService : BaseService.EntityService<Contract.Domain.EtlDataMap>, IEtlDataMapService
{
public override EtlDataMap SaveOrUpdate(EtlDataMap obj)
{
if (!string.IsNullOrEmpty(obj.Id))
{
Framework.QueryInfo info = new QueryInfo();
info.CustomSQL = "delete from EtlColumn ";
info.AddParam("EtlId", obj.Id);
Dao.ExecuteUpdate(info);
}
if (!obj.State.Deleted)
{
if (obj.ColumnList != null)
{
var list = obj.ColumnList;
obj = base.SaveOrUpdate(obj);
foreach (var o in list)
{
o.EtlId = obj.Id;
o.State.MarkNew();
}
Dao.SaveOrUpdateAll<EtlColumn>(list);
}
}
else
{
obj = base.SaveOrUpdate(obj);
}
return obj;
}
}
}
EtlCheckMethodService.cs
/* Copyright (c) 2015 Xiamen Weixin Software Co., Ltd. All rights reserved
*
* Create by huanglc@holworth.com at 2015-04-23 21:52:45
*
*/
using System;
using System.Text;
using System.Collections.Generic;
using Contract.IService;
using Framework;
using System.Collections;
using Contract.Domain;
using BaseService;
using System.Linq;
namespace ETLBLL
{
public class EtlCheckMethodService : BaseService.EntityService<Contract.Domain.EtlCheckMethod>, IEtlCheckMethodService
{
/// <summary>
/// 检验是否>=0
/// </summary>
/// <param name="obj"></param>
/// <param name="table"></param>
/// <param name="row"></param>
/// <param name="column"></param>
/// <returns></returns>
public object[] GEZero(string obj, System.Data.DataTable table, System.Data.DataRow row, EtlCheckMethod column, EtlDataMap etlDataMap)
{
object[] returnObject = new object[3];
if (string.IsNullOrEmpty(obj))
{
returnObject[0] = false;
returnObject[1] = "不能空";
returnObject[2] = 0;
}
else
{
decimal a = 0;
if (decimal.TryParse(obj, out a))
{
if (a >= 0)
{
returnObject[0] = true;
returnObject[1] = "";
returnObject[2] = a;
}
else
{
returnObject[0] = false;
returnObject[1] = "小于0";
}
}
else
{
returnObject[0] = false;
returnObject[1] = "不是数值";
}
}
return returnObject;
}
/// <summary>
/// 检验日期
/// </summary>
/// <param name="obj"></param>
/// <param name="table"></param>
/// <param name="row"></param>
/// <param name="column"></param>
/// <returns></returns>
public object[] CheckDateTime(string obj, System.Data.DataTable table, System.Data.DataRow row, EtlCheckMethod column, EtlDataMap etlDataMap)
{
object[] returnObject = new object[3];
if (string.IsNullOrEmpty(obj))
{
returnObject[0] = true;
returnObject[2] = null;
}
else
{
DateTime a = DateTime.Now;
if (DateTime.TryParse(obj, out a))
{
returnObject[0] = true;
returnObject[2] = a;
}
else
{
returnObject[0] = false;
returnObject[1] = "不是为日期类型";
}
}
return returnObject;
}
/// <summary>
/// 检验数值
/// </summary>
/// <param name="obj"></param>
/// <param name="table"></param>
/// <param name="row"></param>
/// <param name="column"></param>
/// <returns></returns>
public object[] CheckNumber(string obj, System.Data.DataTable table, System.Data.DataRow row, EtlCheckMethod column, EtlDataMap etlDataMap)
{
object[] returnObject = new object[3];
if (string.IsNullOrEmpty(obj))
{
returnObject[0] = true;
returnObject[1] = "";
returnObject[2] = 0;
}
else
{
decimal a = 0;
if (decimal.TryParse(obj, out a))
{
returnObject[0] = true;
returnObject[2] = a;
}
else
{
returnObject[0] = false;
returnObject[1] = "不是为数值类型";
}
}
return returnObject;
}
public object[] CheckNull(string obj, System.Data.DataTable table, System.Data.DataRow row, EtlCheckMethod column, EtlDataMap etlDataMap)
{
object[] returnObject = new object[3];
returnObject[0] = !string.IsNullOrEmpty(obj);
returnObject[2] = obj;
if (string.IsNullOrEmpty(obj))
{
returnObject[1] = "不能空";
}
return returnObject;
}
/// <summary>
/// 验证范围
/// </summary>
/// <param name="obj"></param>
/// <param name="table"></param>
/// <param name="row"></param>
/// <param name="column"></param>
/// <returns></returns>
public object[] CheckRange(string obj, System.Data.DataTable table, System.Data.DataRow row, EtlCheckMethod column, EtlDataMap etlDataMap)
{
object[] returnObject = new object[3];
returnObject[2] = obj;
if (string.IsNullOrEmpty(obj))
{
returnObject[1] = "不能空";
returnObject[0] = false;
}
else
{
if (obj.CompareTo(column.ExtendParameter) >= 0 && obj.CompareTo(column.ExtendParameter2) <= 0)
{
returnObject[0] = true;
}
else
{
returnObject[0] = false;
returnObject[1] = string.Format("值不在{0}和{1}之间", column.ExtendParameter, column.ExtendParameter2);
}
}
return returnObject;
}
/// <summary>
/// 验证某个列日期不能大于某列
/// </summary>
/// <param name="obj"></param>
/// <param name="table"></param>
/// <param name="row"></param>
/// <param name="column"></param>
/// <param name="etlDataMap"></param>
/// <returns></returns>
public object[] CheckRangeDateTimeColumn(string obj, System.Data.DataTable table, System.Data.DataRow row, EtlCheckMethod column,EtlDataMap etlDataMap)
{
object[] returnObject = new object[3];
string errorColumn = string.Empty;
if (etlDataMap.ColumnList.Where(m => m.MapColumn.Equals(column.ExtendParameter)).Count() <= 0)
{
throw new Exception(string.Format("不存在映射列{0}",column.ExtendParameter));
}
if (etlDataMap.ColumnList.Where(m => m.MapColumn.Equals(column.ExtendParameter2)).Count() <= 0)
{
throw new Exception(string.Format("不存在映射列{0}", column.ExtendParameter2));
}
var c1 = etlDataMap.ColumnList.Where(m => m.MapColumn.Equals(column.ExtendParameter)).First();
var c2 = etlDataMap.ColumnList.Where(m => m.MapColumn.Equals(column.ExtendParameter2)).First();
DateTime a = DateTime.Now;
if (!DateTime.TryParse(row[c1.MapIndex.Value].ToString(), out a))
{
returnObject[1] = "不是为日期类型";
returnObject[0] = false;
return returnObject;
}
DateTime v1 = a;
if (!DateTime.TryParse(row[c2.MapIndex.Value].ToString(), out a))
{
returnObject[1] = "不是为日期类型";
returnObject[0] = false;
return returnObject;
}
DateTime v2 = a;
if (v1.CompareTo(v2) <= 0)
{
returnObject[0] = true;
returnObject[2] = DateTime.Parse(obj);
}
else
{
returnObject[0] = false;
string col1 = string.Empty;
string col2 = string.Empty;
if (c1.MapIndex <= 90)
{
col1 = ((char)(c1.MapIndex.Value + 65)).ToString();
}
else
{
col1 = "A" + ((char)(c1.MapIndex.Value + 65 - 26)).ToString();
}
if (c2.MapIndex <= 90)
{
col2 = ((char)(c2.MapIndex.Value + 65)).ToString();
}
else
{
col2 = "A" + ((char)(c2.MapIndex.Value + 65 - 26)).ToString();
}
returnObject[1] = string.Format("{0}大于{1}",col1,col2);
}
return returnObject;
}
/// <summary>
/// 判断列不能同时存在
/// </summary>
/// <param name="obj"></param>
/// <param name="table"></param>
/// <param name="row"></param>
/// <param name="column"></param>
/// <param name="etlDataMap"></param>
/// <returns></returns>
public object[] CheckColumnSimpleExist(string obj, System.Data.DataTable table, System.Data.DataRow row, EtlCheckMethod column, EtlDataMap etlDataMap)
{
object[] returnObject = new object[3];
int i= etlDataMap.ColumnList.Where(m => m.MapColumn.Equals(column.ExtendParameter) || m.MapColumn.Equals(column.ExtendParameter2)).Count();
if (i == 0)
{
returnObject[0] = false;
returnObject[1] = string.Format("{0}、{1}必须存在一列", column.ExtendParameter, column.ExtendParameter2);
}
else if (i > 1)
{
returnObject[0] = false;
returnObject[1] = string.Format("{0}、{1}不能同时存在", column.ExtendParameter, column.ExtendParameter2);
}
else
{
returnObject[0] = true;
returnObject[2] = obj;
}
return returnObject;
}
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Contract.IService;
using System.IO;
using Contract.Domain;
namespace ETLAPP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.ReadOnly = true;
this.textBox1.Text = System.Windows.Forms.Application.StartupPath + @"\ImportFile";
watcher.Path = textBox1.Text;
SetData();
watcher.Changed += new FileSystemEventHandler(OnProcess);
watcher.Created += new FileSystemEventHandler(OnProcess);
watcher.Deleted += new FileSystemEventHandler(OnProcess);
watcher.EnableRaisingEvents = true;
time = new System.Timers.Timer(1000 * 10);
time.Elapsed += monitor_Tick;
time.Start();
button1.Enabled = false;
}
private System.Data.DataSet ds = null;
private void SetData()
{
Framework.QueryInfo info = new Framework.QueryInfo();
info.CustomSQL = "select * from ETL_LOG";
info.OrderBy.Add("CREATE_DATE desc");
ds = svc.ExcuteDataSet(info);
dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.DataSource = ds.Tables[0];
this.dataGridView1.Refresh();
}
void OnProcess(object source, FileSystemEventArgs e)
{
change = true;
}
private System.Timers.Timer time = null;
FileSystemWatcher watcher = new FileSystemWatcher();
private void textBox1_Click(object sender, EventArgs e)
{
FolderBrowserDialog folder = new FolderBrowserDialog();
if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.textBox1.Text = folder.SelectedPath;
}
// FileDialog
}
bool change = true;
private void monitor_Tick(object sender, EventArgs e)
{
this.Invoke(new Action(() =>
{
System.IO.DirectoryInfo path = new DirectoryInfo(watcher.Path);
System.IO.FileInfo[] files = path.GetFiles();
if (files != null && files.Length > 0)
{
change = true;
for (int i = 0; i < files.Length; i++)
{
string fileName = files[i].FullName;
try
{
int count = 0;
string extention = System.IO.Path.GetExtension(fileName);
if (extention.ToLower().IndexOf("xls") > -1)
{
count = svc.SaveImportExcel(fileName);
}
string success = watcher.Path + @"\成功文件\" + DateTime.Now.ToString("yyyy-MM-ddHHmmss") + System.IO.Path.GetFileName(fileName);
System.IO.File.Move(fileName, success);
Contract.Domain.EtlLog log = new EtlLog();
log.CreateDate = DateTime.Now;
log.Status = "成功";
log.Remark = string.Format("共导入{0}条", count);
log.FileName = System.IO.Path.GetFileName(success);
log.State.MarkNew();
svc.SaveEtlLog(log);
var row = ds.Tables[0].NewRow();
row["FILE_NAME"] = System.IO.Path.GetFileName(success);
row["CREATE_DATE"] = DateTime.Now;
row["STATUS"] = "成功";
row["Remark"] = log.Remark;
ds.Tables[0].Rows.InsertAt(row, 0);
this.dataGridView1.DataSource = ds.Tables[0];
this.dataGridView1.Refresh();
}
catch (Exception ex)
{
Contract.Domain.EtlLog log = new Contract.Domain.EtlLog();
log.CreateDate = DateTime.Now;
log.Status = "失败";
log.Remark = string.Format("导入失败,因为{0}", ex.Message);
var row = ds.Tables[0].NewRow();
string errorFile = watcher.Path + @"\错误文件\" + DateTime.Now.ToString("yyyy-MM-ddHHmmss") + System.IO.Path.GetFileName(fileName);
row["FILE_NAME"] = System.IO.Path.GetFileName(errorFile);
log.FileName = System.IO.Path.GetFileName(fileName);
log.State.MarkNew();
svc.SaveEtlLog(log);
row["CREATE_DATE"] = DateTime.Now;
row["STATUS"] = "失败"; ;
row["Remark"] = log.Remark;
ds.Tables[0].Rows.InsertAt(row, 0);
this.dataGridView1.DataSource = ds.Tables[0];
this.dataGridView1.Refresh();
System.IO.File.Move(fileName, errorFile);
Utility.JSUtil.log(ex);
}
}
}
else
{
change = false;
}
}));
}
protected Framework.Proxy.IApplicationContext ctx
{
get
{
return Framework.Proxy.Context.ApplicationContext.GetContext();
}
}
Contract.IService.IEtlExcelService _svc;
Contract.IService.IEtlExcelService svc
{
get
{
if (_svc == null)
{
_svc = (IEtlExcelService)ctx.GetObject("EtlExcelService");
}
return _svc;
}
}
private void button1_Click(object sender, EventArgs e)
{
this.button2.Enabled = true;
button1.Enabled = false;
time.Start();
}
private void button2_Click(object sender, EventArgs e)
{
this.button2.Enabled = true;
button1.Enabled = false;
time.Stop();
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/kexb/p/4502304.html