using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.MvcShipping.Models.MsCodeOpMustField; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.Areas.CommMng.Models; using HcUtility.Comm; namespace DSWeb.MvcShipping.DAL.MsCodeOpMustField { public class MsCodeOpMustFieldDAL { #region Inquery DataList static public List GetDataList(string strCondition, string companyid, string sort = null) { var rangstr =" CORPID='"+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,CORPID,BSTYPE,FIELDNAME,ISMUST,ISCOLOR,ISREADONLY"); strSql.Append(" from CODE_OPMUSTFIELD "); 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 BSTYPE,FIELDNAME"); } return SetData(strSql); } static public CodeOpMustField GetData(string condition, string companyid) { CodeOpMustField data = null; var list = GetDataList(condition,companyid); if (list.Count > 0) data = list[0]; if (data == null) { data = new CodeOpMustField(); data.CORPID = 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()) { CodeOpMustField data = new CodeOpMustField(); #region Set DB data to Object data.GID = Convert.ToString(reader["GID"]); data.BSTYPE = Convert.ToString(reader["BSTYPE"]); data.FIELDNAME = Convert.ToString(reader["FIELDNAME"]); data.CORPID = Convert.ToString(reader["CORPID"]); data.ISMUST = Convert.ToString(reader["ISMUST"]); data.ISCOLOR = Convert.ToString(reader["ISCOLOR"]); data.ISREADONLY = Convert.ToString(reader["ISREADONLY"]); #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 code_opmustfield (GID,CORPID,BSTYPE,FIELDNAME,ISMUST,ISCOLOR,ISREADONLY) values (@GID,@CORPID,@BSTYPE,@FIELDNAME,@ISMUST,@ISCOLOR,@ISREADONLY) "); var cmdUpdate = db.GetSqlStringCommand( @"update code_opmustfield set BSTYPE=@BSTYPE,FIELDNAME=@FIELDNAME,ISMUST=@ISMUST,ISCOLOR=@ISCOLOR,ISREADONLY=@ISREADONLY 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, "@CORPID", DbType.String, companyid); db.AddInParameter(cmdInsert, "@BSTYPE", DbType.String, enumValue.BSTYPE); db.AddInParameter(cmdInsert, "@FIELDNAME", DbType.String, enumValue.FIELDNAME); db.AddInParameter(cmdInsert, "@ISMUST", DbType.String, enumValue.ISMUST); db.AddInParameter(cmdInsert, "@ISCOLOR", DbType.String, enumValue.ISCOLOR); db.AddInParameter(cmdInsert, "@ISREADONLY", DbType.String, enumValue.ISREADONLY); db.ExecuteNonQuery(cmdInsert, tran); } else { cmdUpdate.Parameters.Clear(); db.AddInParameter(cmdUpdate, "@GID", DbType.String, enumValue.GID); db.AddInParameter(cmdUpdate, "@BSTYPE", DbType.String, enumValue.BSTYPE); db.AddInParameter(cmdUpdate, "@FIELDNAME", DbType.String, enumValue.FIELDNAME); db.AddInParameter(cmdUpdate, "@ISMUST", DbType.String, enumValue.ISMUST); db.AddInParameter(cmdUpdate, "@ISCOLOR", DbType.String, enumValue.ISCOLOR); db.AddInParameter(cmdUpdate, "@ISREADONLY", DbType.String, enumValue.ISREADONLY); 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 code_opmustfield 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; } } }