using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.MvcShipping.Models.MsSysThirdPartyAccount; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.Areas.CommMng.Models; using HcUtility.Comm; namespace DSWeb.MvcShipping.DAL.MsSysThirdPartyAccount { public class MsSysThirdPartyAccountDAL { #region Inquery DataList static public List GetDataList(string strCondition, string sort = null) { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append("GID,THIRDPARTY,CUSTOMERNAME,USERID,ACCOUNT,ACCOUNTPSW,MODIFIEDUSER,MODIFIEDTIME,CREATEUSER,CREATETIME"); strSql.Append(",(select top 1 SHOWNAME from [user] where GID=sys_thidparty_account.USERID) USERNAME"); strSql.Append(" from sys_thidparty_account "); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" where " + strCondition); } var sortstring = DatasetSort.Getsortstring(sort); if (!string.IsNullOrEmpty(sortstring) && sortstring.Trim()!="") { strSql.Append(" order by " + sortstring); } else { strSql.Append(" order by THIRDPARTY"); } return SetData(strSql); } static public SysThirdPartyAccount GetData(string condition) { SysThirdPartyAccount data = null; var list = GetDataList(condition); if (list.Count > 0) data = list[0]; if (data == null) { data = new SysThirdPartyAccount(); } return data; } static public SysThirdPartyAccount GetDataByCustomername(string USERID,string CUSTOMERNAME) { return GetData($" USERID='{USERID}' and CUSTOMERNAME='{CUSTOMERNAME}'"); } 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()) { SysThirdPartyAccount data = new SysThirdPartyAccount(); #region Set DB data to Object data.GID = Convert.ToString(reader["GID"]); data.USERID = Convert.ToString(reader["USERID"]); data.USERNAME = Convert.ToString(reader["USERNAME"]); data.THIRDPARTY = Convert.ToString(reader["THIRDPARTY"]); data.ACCOUNT = Convert.ToString(reader["ACCOUNT"]); data.ACCOUNTPSW = Convert.ToString(reader["ACCOUNTPSW"]); data.CREATEUSER = Convert.ToString(reader["CREATEUSER"]); data.MODIFIEDUSER = Convert.ToString(reader["MODIFIEDUSER"]); data.CREATETIME = Convert.ToString(reader["CREATETIME"]); data.MODIFIEDTIME = Convert.ToString(reader["MODIFIEDTIME"]); data.CUSTOMERNAME= Convert.ToString(reader["CUSTOMERNAME"]); #endregion headList.Add(data); } reader.Close(); } return headList; } public static DBResult SaveDetail(List bodyList, string userid) { var result = new DBResult(); Database db = DatabaseFactory.CreateDatabase(); using (var conn = db.CreateConnection()) { conn.Open(); var tran = conn.BeginTransaction(); try { var cmdInsert = db.GetSqlStringCommand( @"insert into sys_thidparty_account (GID,USERID,CUSTOMERNAME,THIRDPARTY,ACCOUNT,ACCOUNTPSW,CREATEUSER,CREATETIME,MODIFIEDUSER,MODIFIEDTIME) values (@GID,@USERID,@CUSTOMERNAME,@THIRDPARTY,@ACCOUNT,@ACCOUNTPSW,@CREATEUSER,@CREATETIME,@MODIFIEDUSER,@MODIFIEDTIME) "); var cmdUpdate = db.GetSqlStringCommand( @"update sys_thidparty_account set USERID=@USERID,THIRDPARTY=@THIRDPARTY,ACCOUNT=@ACCOUNT,ACCOUNTPSW=@ACCOUNTPSW,MODIFIEDUSER=@MODIFIEDUSER,MODIFIEDTIME=@MODIFIEDTIME,CUSTOMERNAME=@CUSTOMERNAME where GID=@GID "); if (bodyList != null) { foreach (var enumValue in bodyList) { if (enumValue.GID == "*" || enumValue.GID == "") { cmdInsert.Parameters.Clear(); db.AddInParameter(cmdInsert, "@GID", DbType.String, Guid.NewGuid().ToString()); db.AddInParameter(cmdInsert, "@USERID", DbType.String, enumValue.USERID); db.AddInParameter(cmdInsert, "@THIRDPARTY", DbType.String, enumValue.THIRDPARTY); db.AddInParameter(cmdInsert, "@ACCOUNT", DbType.String, enumValue.ACCOUNT); db.AddInParameter(cmdInsert, "@ACCOUNTPSW", DbType.String, enumValue.ACCOUNTPSW); db.AddInParameter(cmdInsert, "@CREATEUSER", DbType.String, userid); db.AddInParameter(cmdInsert, "@CREATETIME", DbType.String,DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); db.AddInParameter(cmdInsert, "@MODIFIEDUSER", DbType.String, userid); db.AddInParameter(cmdInsert, "@MODIFIEDTIME", DbType.String, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); db.AddInParameter(cmdInsert, "@CUSTOMERNAME", DbType.String, enumValue.CUSTOMERNAME); db.ExecuteNonQuery(cmdInsert, tran); } else { cmdUpdate.Parameters.Clear(); db.AddInParameter(cmdUpdate, "@GID", DbType.String, enumValue.GID); db.AddInParameter(cmdUpdate, "@USERID", DbType.String, enumValue.USERID); db.AddInParameter(cmdUpdate, "@THIRDPARTY", DbType.String, enumValue.THIRDPARTY); db.AddInParameter(cmdUpdate, "@ACCOUNT", DbType.String, enumValue.ACCOUNT); db.AddInParameter(cmdUpdate, "@ACCOUNTPSW", DbType.String, enumValue.ACCOUNTPSW); db.AddInParameter(cmdUpdate, "@MODIFIEDUSER", DbType.String, userid); db.AddInParameter(cmdUpdate, "@MODIFIEDTIME", DbType.String, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); db.AddInParameter(cmdUpdate, "@CUSTOMERNAME", DbType.String, enumValue.CUSTOMERNAME); db.ExecuteNonQuery(cmdUpdate, tran); } } } tran.Commit(); } catch (Exception) { tran.Rollback(); result.Success = false; result.Message = "保存出现错误,请重试或联系系统管理员"; return result; } } result.Success = true; result.Message = "保存成功" + result.Message; return result; } public static DBResult DeleteDetail(List headData) { var result = new DBResult(); Database db = DatabaseFactory.CreateDatabase(); using (var conn = db.CreateConnection()) { conn.Open(); var tran = conn.BeginTransaction(); try { if (headData != null) { foreach (var enumValue in headData) { var cmdDelete = db.GetSqlStringCommand("delete from sys_thidparty_account where GID='" + enumValue.GID + "'"); db.ExecuteNonQuery(cmdDelete, tran); } } tran.Commit(); } catch (Exception) { tran.Rollback(); result.Success = false; result.Message = "删除出现错误,请重试或联系系统管理员"; return result; } } result.Success = true; result.Message = "删除成功"; return result; } #endregion } }