/// <summary> /// 寫入 Txt Log /// </summary> /// <param name="tableName">table名</param> /// <param name="falg">成功或失敗</param> /// <param name="start">開始時間</param> /// <param name="folderPath">檔案路徑</param> public static void txtLog(Table_Type tableName, bool falg, DateTime start, string folderPath) { try { string txtData = string.Empty; try //試著抓取舊資料 { txtData = File.ReadAllText(folderPath, System.Text.Encoding.Default); } catch { } string txt = string.Format("{0}({1})_{2}_{3}", tableName.GetDescription(), tableName.ToString(), start.ToString("yyyyMMddHHmmss"), falg ? "Y" : "N"); if (!string.IsNullOrWhiteSpace(txtData)) //有舊資料就換行寫入下一筆 { txtData += string.Format("\r\n{0}", txt); } else //沒有就直接寫入 { txtData = txt; } FileStream fs = new FileStream(folderPath, FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default); sw.Write(txtData); //存檔 sw.Close(); } catch (Exception ex) { } }
/// <summary> /// Log資料存到Sql(IFRS9_Log) /// </summary> /// <param name="tableType">table簡寫</param> /// <param name="tableName">table名</param> /// <param name="fileName">檔案名</param> /// <param name="programName">專案名</param> /// <param name="falg">成功失敗</param> /// <param name="deptType">B:債券 M:房貸 (共用同一table時做區分)</param> /// <param name="start">開始時間</param> /// <param name="end">結束時間</param> /// <returns>回傳成功或失敗</returns> public bool saveLog( Table_Type table, string fileName, string programName, bool falg, string deptType, DateTime start, DateTime end) { bool flag = true; try { var tableName = table.GetDescription(); db.IFRS9_Log.Add(new IFRS9_Log() //寫入DB { Table_type = table.ToString(), Table_name = tableName.Substring(0, (tableName.Length > 40 ? 40 : tableName.Length)), File_name = fileName, Program_name = programName, Create_date = start.ToString("yyyyMMdd"), Create_time = start.ToString("HH:mm:ss"), End_date = end.ToString("yyyyMMdd"), End_time = end.ToString("HH:mm:ss"), TYPE = falg ? "Y" : "N", Debt_Type = deptType }); db.SaveChanges(); //DB SAVE } catch { flag = false; } return(flag); }
internal static Type GetInstanceType(Table_Type type) { FieldInfo data = typeof(Table_Type).GetField(type.ToString()); Attribute attribute = Attribute.GetCustomAttribute(data, typeof(CommucationAttribute)); CommucationAttribute result = (CommucationAttribute)attribute; return(result.InstanceType); }
public List <IViewModel> getViewModel <T>(IEnumerable <T> dbModel, Table_Type type) where T : class { List <IViewModel> datas = new List <IViewModel>(); var obj = FactoryRegistry.GetInstance(type); if (dbModel == null || obj == null) { return(datas); } var viewpros = obj.GetType().GetProperties(); if (!dbModel.Any()) { return(datas); } var dbpros = dbModel.First().GetType().GetProperties(); dbModel.ToList().ForEach(db => { obj = FactoryRegistry.GetInstance(type); foreach (var item in viewpros) { var p = dbpros.FirstOrDefault(x => x.Name.ToUpper() == item.Name.ToUpper()); if (p != null) { var _pt = p.PropertyType; if (_pt == typeof(string)) { item.SetValue(obj, p.GetValue(db)); } else if (_pt == typeof(DateTime) || _pt == typeof(Nullable <DateTime>)) { if (p.GetValue(db) != null) { item.SetValue(obj, TypeTransfer.objDateToString(p.GetValue(db))); } else { item.SetValue(obj, string.Empty); } } else { if (p.GetValue(db) != null) { item.SetValue(obj, p.GetValue(db).ToString()); } else { item.SetValue(obj, string.Empty); } } } } datas.Add(obj); }); return(datas); }
public static string tableNameGetDescription(this Table_Type type) { string result = string.Empty; Attribute at = Attribute.GetCustomAttribute( typeof(Table_Type).GetField(type.ToString()), typeof(Infrastructure.NameAttribute)); if (at != null) { result = ((Infrastructure.NameAttribute)at).name; } return(result); }
/// <summary> /// DataTable to ViewModel /// </summary> /// <param name="dt"></param> /// <param name="type"></param> /// <returns></returns> public List <IViewModel> getViewModel(DataTable dt, Table_Type type) { if (dt == null) { return(new List <IViewModel>()); } List <string> titles = dt.Columns .Cast <DataColumn>() .Where(x => x.ColumnName != null) .Select(x => x.ColumnName.Trim().Replace("\t", string.Empty)) .ToList(); return(getViewModel(dt.AsEnumerable().Select(x => x), titles, type)); }
public static string tableNameGetDescription(this string tableName) { if (tableName.IsNullOrWhiteSpace()) { return(tableName); } string result = string.Empty; var types = EnumUtil.GetValues <Table_Type>(); if (!types.Any(x => x.ToString() == tableName)) { return(tableName); } Table_Type t = EnumUtil.GetValues <Table_Type>().First(x => x.ToString() == tableName); result = tableNameGetDescription(t); return(result); }
/// <summary> /// Log資料存到Sql(IFRS9_Log) /// </summary> /// <param name="tableType">table簡寫</param> /// <param name="tableName">table名</param> /// <param name="fileName">檔案名</param> /// <param name="programName">專案名</param> /// <param name="falg">成功失敗</param> /// <param name="deptType">B:債券 M:房貸 (共用同一table時做區分)</param> /// <param name="start">開始時間</param> /// <param name="end">結束時間</param> /// <param name="userAccount">執行帳號</param> /// <param name="version">版本</param> /// <param name="reportDate">基準日</param> /// <returns>回傳成功或失敗</returns> public bool saveLog( Table_Type table, string fileName, string programName, bool falg, string deptType, DateTime start, DateTime end, string userAccount, int version = 1, Nullable <DateTime> date = null) { bool flag = true; try { using (IFRS9DBEntities db = new IFRS9DBEntities()) { var tableName = table.GetDescription(); db.IFRS9_Log.Add(new IFRS9_Log() //寫入DB { Table_type = table.ToString(), Table_name = tableName.Substring(0, (tableName.Length > 40 ? 40 : tableName.Length)), File_name = fileName, Program_name = programName, Version = version, Create_date = start.ToString("yyyyMMdd"), Create_time = start.ToString("HH:mm:ss"), End_date = end.ToString("yyyyMMdd"), End_time = end.ToString("HH:mm:ss"), TYPE = falg ? "Y" : "N", Debt_Type = deptType, User_Account = userAccount, Report_Date = date }); db.SaveChanges(); //DB SAVE } } catch { flag = false; } return(flag); }
/// <summary> /// dataRow to ViewModel /// </summary> /// <param name="item"></param> /// <param name="titles"></param> /// <param name="type"></param> /// <returns></returns> public List <IViewModel> getViewModel(IEnumerable <DataRow> items, List <string> titles, Table_Type type) { List <IViewModel> datas = new List <IViewModel>(); var obj = FactoryRegistry.GetInstance(type); if (obj == null) { return(datas); } var pros = obj.GetType().GetProperties(); if (!items.Any() || items.First().ItemArray.Count() != titles.Count) { return(datas); } items.ToList().ForEach(item => { obj = FactoryRegistry.GetInstance(type); for (int i = 0; i < titles.Count; i++) //每一行所有資料 { string data = null; if (item[i].GetType().Name.Equals("DateTime")) { data = TypeTransfer.objDateToString(item[i]); } else { data = TypeTransfer.objToString(item[i]); } if (!data.IsNullOrWhiteSpace()) //資料有值 { var PInfo = pros.Where(x => x.Name.Trim().ToLower() == titles[i].Trim().ToLower()) .FirstOrDefault(); if (PInfo != null) { PInfo.SetValue(obj, data); } } } datas.Add(obj); }); return(datas); }
public static IViewModel GetInstance(Table_Type type) { Type t = TableTypeHelper.GetInstanceType(type); return((IViewModel)Activator.CreateInstance(t)); }