using System; using System.Data; using System.Collections.Generic; using System.Text; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.Areas.CommMng.Models; using DSWeb.EntityDA; using HcUtility.Comm; using DSWeb.Areas.CommMng.DAL; using DSWeb.Areas.Account.Models.Chfee_Bankdata; using HcUtility.Core; using System.IO; namespace DSWeb.Areas.Account.DAL.Chfee_bankdata { public class ChBankdataDAL { #region 列表 static public List GetDataList(string strCondition, string userid = "", string usercode = "", string orgcode = "", string sort = null) { var strSql = new StringBuilder(); strSql.Append(" SELECT [GID],[PCNO],[SFNO],[TRANSTYPE],[DRAWEE_BANK] ,[DRAWEE_ACCOUNT],[DRAWEE_NAME],[PAYEE_BANK],[PAYEE_ACCOUNT]"); strSql.Append(",[PAYEE_NAME],[TRANSDATE],[CURRENCY],[AMOUNT],[TRANSNO],[IMPORTDATE],[IMPORTER],[ISFIT],[FITCOUNT],[INVNO],[FITDATE]"); strSql.Append(",[FITTYPE],[PRINTDATE],[ISPRINT],[PRINTER],[SALE],[ATTITIONAL],[REMARK],[SUMMARY],FITUSER,CREATEUSER,CREATETIME"); strSql.Append(" FROM ch_fee_bankdata where CORPID='"+orgcode+"' "); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } var sortstring = DatasetSort.Getsortstring(sort); if (!string.IsNullOrEmpty(sortstring)) { strSql.Append(" order by " + sortstring); } else { strSql.Append(" order by PCNO DESC,SFNO"); } return SetData(strSql); } static public ChBankdata GetData(string condition, string userid = "", string usercode = "", string orgcode = "") { var list = GetDataList(condition, userid, usercode, orgcode, ""); if (list.Count > 0) return list[0]; return new ChBankdata(); } private static List SetData(StringBuilder strSql) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { ChBankdata data = new ChBankdata(); #region Set DB data to Object data.GID = Convert.ToString(reader["GID"]); if (reader["PCNO"] != DBNull.Value) data.PCNO = Convert.ToInt32(reader["PCNO"]); if (reader["SFNO"] != DBNull.Value) data.SFNO = Convert.ToInt32(reader["SFNO"]); data.TRANSTYPE = Convert.ToString(reader["TRANSTYPE"]); data.DRAWEE_BANK = Convert.ToString(reader["DRAWEE_BANK"]); data.DRAWEE_ACCOUNT = Convert.ToString(reader["DRAWEE_ACCOUNT"]); data.DRAWEE_NAME = Convert.ToString(reader["DRAWEE_NAME"]); data.PAYEE_BANK = Convert.ToString(reader["PAYEE_BANK"]); data.PAYEE_ACCOUNT = Convert.ToString(reader["PAYEE_ACCOUNT"]); data.PAYEE_NAME = Convert.ToString(reader["PAYEE_NAME"]); if (reader["TRANSDATE"] != DBNull.Value) data.TRANSDATE = Convert.ToDateTime(reader["TRANSDATE"]).ToString("yyyy-MM-dd HH:mm:ss"); data.CURRENCY = Convert.ToString(reader["CURRENCY"]); if (reader["AMOUNT"] != DBNull.Value) data.AMOUNT = Convert.ToDecimal(reader["AMOUNT"]); // data.CHEQUENO = Convert.ToString(reader["CHEQUENO"]); data.TRANSNO = Convert.ToString(reader["TRANSNO"]); if (reader["IMPORTDATE"] != DBNull.Value) data.IMPORTDATE = Convert.ToDateTime(reader["IMPORTDATE"]).ToString("yyyy-MM-dd HH:mm:ss"); data.IMPORTER = Convert.ToString(reader["IMPORTER"]); if (reader["ISFIT"] != DBNull.Value) data.ISFIT = Convert.ToBoolean(reader["ISFIT"]); if (reader["FITCOUNT"] != DBNull.Value) data.FITCOUNT = Convert.ToInt32(reader["FITCOUNT"]); data.INVNO = Convert.ToString(reader["INVNO"]); if (reader["FITDATE"] != DBNull.Value) data.FITDATE = Convert.ToDateTime(reader["FITDATE"]).ToString("yyyy-MM-dd HH:mm:ss"); data.FITTYPE = Convert.ToString(reader["FITTYPE"]); if (reader["PRINTDATE"] != DBNull.Value) data.PRINTDATE = Convert.ToDateTime(reader["PRINTDATE"]).ToString("yyyy-MM-dd HH:mm:ss"); if (reader["ISPRINT"] != DBNull.Value) data.ISPRINT = Convert.ToBoolean(reader["ISPRINT"]); data.PRINTER = Convert.ToString(reader["PRINTER"]); data.SALE = Convert.ToString(reader["SALE"]); data.ATTITIONAL = Convert.ToString(reader["ATTITIONAL"]); data.REMARK = Convert.ToString(reader["REMARK"]); data.SUMMARY = Convert.ToString(reader["SUMMARY"]); data.FITUSER = Convert.ToString(reader["FITUSER"]); data.CREATEUSER = Convert.ToString(reader["CREATEUSER"]); data.CREATEUSERREF = Convert.ToString(reader["CREATEUSERREF"]); if (reader["CREATETIME"] != DBNull.Value) data.CREATETIME = Convert.ToDateTime(reader["CREATETIME"]).ToString("yyyy-MM-dd HH:mm:ss"); #endregion headList.Add(data); } reader.Close(); } return headList; } #endregion public static DBResult ImpBankData(DataTable table,string pcno, string userid, string username, string companyid) { var result = new DBResult(); var PreCUSTNO = ""; var BSNO = ""; Database db = DatabaseFactory.CreateDatabase(); var cmdInsert = db.GetSqlStringCommand( @"insert into ch_fee_bankdata (GID,PCNO,SFNO,TRANSTYPE,DRAWEE_BANK,DRAWEE_ACCOUNT,DRAWEE_NAME,PAYEE_BANK ,PAYEE_ACCOUNT,PAYEE_NAME,TRANSDATE,CURRENCY,AMOUNT,TRANSNO,IMPORTDATE,IMPORTER,ATTITIONAL,SUMMARY) values (@GID,@PCNO,@SFNO,@TRANSTYPE,@DRAWEE_BANK,@DRAWEE_ACCOUNT,@DRAWEE_NAME,@PAYEE_BANK ,@PAYEE_ACCOUNT,@PAYEE_NAME,@TRANSDATE,@CURRENCY,@AMOUNT,@TRANSNO,@IMPORTDATE,@IMPORTER,@ATTITIONAL,@SUMMARY) "); var _date = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); try { var CUSTOMERNAME = ""; var TRUCKER = ""; var TRUCKER2 = ""; var CARRIER = ""; int num = 0; foreach (DataRow row in table.Rows) { if (row[1].ToString() != "") { num = num + 1; var TRANSTYPE = Convert.ToString(row["交易类型[ Transaction Type ]"]).Replace("\"","").Replace("\t", ""); if (!string.IsNullOrEmpty(TRANSTYPE) && TRANSTYPE== "来账") { var headData = new ChBankdata(); headData.GID = System.Guid.NewGuid().ToString(); headData.PCNO = Convert.ToInt32(pcno); headData.SFNO = num; headData.TRANSTYPE = TRANSTYPE; headData.DRAWEE_BANK = Convert.ToString(row["付款人开户行名[ Payer account bank ]"]).Replace("\"", "").Replace("\t", ""); headData.DRAWEE_ACCOUNT = Convert.ToString(row["付款人账号[ Debit Account No. ]"]).Replace("\"", "").Replace("\t", ""); headData.DRAWEE_NAME = Convert.ToString(row["付款人名称[ Payer's Name ]"]).Replace("\"", "").Replace("\t", ""); headData.PAYEE_BANK = Convert.ToString(row["收款人开户行名[ Beneficiary account bank ]"]).Replace("\"", "").Replace("\t", ""); headData.PAYEE_ACCOUNT = Convert.ToString(row["收款人账号[ Payee's Account Number ]"]).Replace("\"", "").Replace("\t", ""); headData.PAYEE_NAME = Convert.ToString(row["收款人名称[ Payee's Name ]"]).Replace("\"", "").Replace("\t", ""); var transdate= Convert.ToString(row["交易日期[ Transaction Date ]"]).Replace("\"", ""); transdate = transdate.Substring(0, 4) + "-" + transdate.Substring(4, 2) + "-" + transdate.Substring(6, 2); transdate= transdate+" "+ Convert.ToString(row["交易时间[ Transaction time ]"]).Replace("\"", ""); headData.TRANSDATE = transdate.Replace("\t",""); var curr= Convert.ToString(row["交易货币[ Trade Currency ]"]).Replace("\"", "").Replace("\t", ""); if (curr == "CNY") headData.CURRENCY = "RMB"; headData.AMOUNT =Convert.ToDecimal(Convert.ToString(row["交易金额[ Trade Amount ]"]).Replace("\"", "").Replace("\t", "")); headData.TRANSNO= Convert.ToString(row["交易流水号[ Transaction reference number ]"]).Replace("\"", "").Replace("\t", ""); headData.TRANSNO = Convert.ToString(row["交易流水号[ Transaction reference number ]"]).Replace("\"", "").Replace("\t", ""); headData.ATTITIONAL = Convert.ToString(row["交易附言[ Remark ]"]).Replace("\"", "").Replace("\t", ""); headData.SUMMARY = Convert.ToString(row["摘要[ Reference ]"]).Replace("\"", "").Replace("\t", ""); headData.REMARK = Convert.ToString(row["备注[ Remarks ]"]).Replace("\"", "").Replace("\t", ""); } } } result.Success = true; result.Message = "导入成功"; } catch (Exception ex) { result.Success = false; result.Message = "导入中出错:" + ex.Message; } return result; } public static System.Data.DataTable CSV2DataTable(string fileName, out string strMsg) { var result = new DBResult(); System.Data.DataTable dt=new System.Data.DataTable(); FileStream fs= new FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); StreamReader sr=new StreamReader(fs, System.Text.Encoding.Default); //记录每次读取的一行记录 string strLine=""; //记录每行记录中的各字段内容 string[] aryLine; //标示列数 int columnCount=0; int startline = 8; int line = 0; //标示是否是读取的第一行 bool IsFirst=true; try { //逐行读取CSV中的数据 while ((strLine = sr.ReadLine()) != null) { line = line + 1; if (line>= startline&&!string.IsNullOrEmpty(strLine)) { aryLine = strLine.Split(','); if (IsFirst == true) { IsFirst = false; columnCount = aryLine.Length; //创建列 for (int i = 0; i < columnCount; i++) { DataColumn dc = new DataColumn(aryLine[i]); dt.Columns.Add(dc); } } else { DataRow dr = dt.NewRow(); for (int j = 0; j < columnCount; j++) { dr[j] = aryLine[j]; } dt.Rows.Add(dr); } } } sr.Close(); fs.Close(); strMsg = ""; return dt; } catch (Exception ex) { strMsg ="导入中出错:" + ex.Message; return dt; } } } }