using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.MvcShipping.Models.MsCodeSgAuditRule; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.Areas.CommMng.Models; using HcUtility.Comm; namespace DSWeb.MvcShipping.DAL.MsCodeSgAuditRule { public class MsCodeSgAuditRuleDAL { #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,FIELDNAME,COMPARE,COMPARETYPE,VALUE"); strSql.Append(",(SELECT top 1 EnumValueName_2 FROM tSysEnumValue where ENUMTYPEID='96014' and EnumValueID=code_sgauditrule.FIELDNAME) BSTYPE"); strSql.Append(",(SELECT top 1 EnumValueName FROM tSysEnumValue where ENUMTYPEID='96014' and EnumValueID=code_sgauditrule.FIELDNAME) FIELDNAMEREF"); strSql.Append(",(CASE WHEN COMPARETYPE='字段' THEN (SELECT top 1 EnumValueName FROM tSysEnumValue where ENUMTYPEID='96014' and EnumValueID=code_sgauditrule.VALUE) ELSE VALUE END) VALUEREF"); strSql.Append(" from code_sgauditrule "); 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 CodeSgAuditRule GetData(string condition, string companyid) { CodeSgAuditRule data = null; var list = GetDataList(condition,companyid); if (list.Count > 0) data = list[0]; if (data == null) { data = new CodeSgAuditRule(); 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()) { CodeSgAuditRule data = new CodeSgAuditRule(); #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.FIELDNAMEREF = Convert.ToString(reader["FIELDNAMEREF"]); data.CORPID = Convert.ToString(reader["CORPID"]); data.COMPARE = Convert.ToString(reader["COMPARE"]); data.COMPARETYPE = Convert.ToString(reader["COMPARETYPE"]); data.VALUE = Convert.ToString(reader["VALUE"]); data.VALUEREF = Convert.ToString(reader["VALUEREF"]); #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_sgauditrule (GID,CORPID,BSTYPE,FIELDNAME,COMPARE,COMPARETYPE,VALUE) values (@GID,@CORPID,@BSTYPE,@FIELDNAME,@COMPARE,@COMPARETYPE,@VALUE) "); var cmdUpdate = db.GetSqlStringCommand( @"update code_sgauditrule set BSTYPE=@BSTYPE,FIELDNAME=@FIELDNAME,COMPARE=@COMPARE,COMPARETYPE=@COMPARETYPE,VALUE=@VALUE 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, "@COMPARE", DbType.String, enumValue.COMPARE); db.AddInParameter(cmdInsert, "@COMPARETYPE", DbType.String, enumValue.COMPARETYPE); db.AddInParameter(cmdInsert, "@VALUE", DbType.String, enumValue.VALUE); 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, "@COMPARE", DbType.String, enumValue.COMPARE); db.AddInParameter(cmdUpdate, "@COMPARETYPE", DbType.String, enumValue.COMPARETYPE); db.AddInParameter(cmdUpdate, "@VALUE", DbType.String, enumValue.VALUE); 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_sgauditrule 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; } } }