using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.MvcShipping.Models.MsSysDeptArrears; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.Areas.CommMng.Models; using HcUtility.Comm; namespace DSWeb.MvcShipping.DAL.MsSysDeptArrears { public class MsSysDeptArrearsDAL { #region Inquery DataList static public List GetDataList(string strCondition, string companyid, string sort = null) { var rangstr = " COMPANYID='" + companyid + "'"; if (!string.IsNullOrEmpty(rangstr)) { if (!string.IsNullOrEmpty(strCondition)) { strCondition = strCondition + " and " + rangstr; } else { strCondition = rangstr; } } var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append("GID,DEPT,MAXARREARS,COMPANYID,CREATEUSER,CREATETIME,REMARKS"); strSql.Append(" from SYS_DEPT_ARREARS "); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" where " + strCondition); } var sortstring = DatasetSort.Getsortstring(sort); if (!string.IsNullOrEmpty(sortstring)) { strSql.Append(" order by " + sortstring); } else { strSql.Append(" order by DEPT"); } return SetData(strSql); } static public SysDeptArrears GetData(string condition, string companyid) { SysDeptArrears data = null; var list = GetDataList(condition,companyid); if (list.Count > 0) data = list[0]; if (data == null) { data = new SysDeptArrears(); data.COMPANYID = companyid; } return data; } 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()) { SysDeptArrears data = new SysDeptArrears(); #region Set DB data to Object data.GID = Convert.ToString(reader["GID"]); data.DEPT = Convert.ToString(reader["DEPT"]); data.MAXARREARS = Convert.ToString(reader["MAXARREARS"]); data.COMPANYID = Convert.ToString(reader["COMPANYID"]); data.CREATEUSER = Convert.ToString(reader["CREATEUSER"]); data.REMARKS = Convert.ToString(reader["REMARKS"]); 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 SaveDetail(List bodyList, string companyid) { 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_DEPT_ARREARS (GID,DEPT,MAXARREARS,COMPANYID,CREATEUSER,REMARKS,CREATETIME) values (@GID,@DEPT,@MAXARREARS,@COMPANYID,@CREATEUSER,@REMARKS,@CREATETIME) "); var cmdUpdate = db.GetSqlStringCommand( @"update SYS_DEPT_ARREARS set DEPT=@DEPT,MAXARREARS=@MAXARREARS,REMARKS=@REMARKS 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, "@COMPANYID", DbType.String, companyid); db.AddInParameter(cmdInsert, "@DEPT", DbType.String, enumValue.DEPT); db.AddInParameter(cmdInsert, "@MAXARREARS", DbType.String, enumValue.MAXARREARS); db.AddInParameter(cmdInsert, "@CREATEUSER", DbType.String, enumValue.CREATEUSER); db.AddInParameter(cmdInsert, "@REMARKS", DbType.String, enumValue.REMARKS); db.AddInParameter(cmdInsert, "@CREATETIME", DbType.String, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); db.ExecuteNonQuery(cmdInsert, tran); } else { cmdUpdate.Parameters.Clear(); db.AddInParameter(cmdUpdate, "@GID", DbType.String, enumValue.GID); db.AddInParameter(cmdUpdate, "@DEPT", DbType.String, enumValue.DEPT); db.AddInParameter(cmdUpdate, "@MAXARREARS", DbType.String, enumValue.MAXARREARS); db.AddInParameter(cmdUpdate, "@REMARKS", DbType.String, enumValue.REMARKS); 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_DEPT_ARREARS 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; } } }