using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.MvcShipping.Models.MsCodeOpTemplate; using DSWeb.MvcShipping.Models.MsCodeDuiTemplate; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.Areas.CommMng.Models; using HcUtility.Comm; namespace DSWeb.MvcShipping.DAL.MsCodeOpTemplate { public class MsCodeOpTemplateDAL { #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,COMPANYID,BSTYPE,TEMPLATENAME,OP,[INPUTBY],[INPUTTIME],REMARK,ISPUBLIC"); strSql.Append(",(select ShowName from [user] where GID=CODE_OP_TEMPLATE.INPUTBY) as INPUTBYREF"); strSql.Append(",(select ShowName from [user] where GID=CODE_OP_TEMPLATE.OP) as OPREF"); strSql.Append(" from CODE_OP_TEMPLATE "); 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 TEMPLATENAME"); } return SetData(strSql); } static public CodeOpTemplate GetData(string condition, string companyid) { CodeOpTemplate data = null; var list = GetDataList(condition,companyid); if (list.Count > 0) data = list[0]; if (data == null) { data = new CodeOpTemplate(); 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()) { CodeOpTemplate data = new CodeOpTemplate(); #region Set DB data to Object data.GID = Convert.ToString(reader["GID"]); data.BSTYPE = Convert.ToString(reader["BSTYPE"]); data.INPUTBY = Convert.ToString(reader["INPUTBY"]); data.INPUTBYREF = Convert.ToString(reader["INPUTBYREF"]); data.OP = Convert.ToString(reader["OP"]); data.OPREF = Convert.ToString(reader["OPREF"]); data.TEMPLATENAME = Convert.ToString(reader["TEMPLATENAME"]); data.REMARK = Convert.ToString(reader["REMARK"]); data.ISPUBLIC = Convert.ToString(reader["ISPUBLIC"]); if (reader["INPUTTIME"] != DBNull.Value) data.INPUTTIME = Convert.ToDateTime(reader["INPUTTIME"]).ToString("yyyy-MM-dd HH:mm:ss"); #endregion headList.Add(data); } reader.Close(); } return headList; } #endregion static public List GetFieldList(string bstable) { var strSql = new StringBuilder(); strSql.Append("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='"+bstable+"'"); return SetFieldData(strSql); } private static List SetFieldData(StringBuilder strSql) { var bodyList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { CodeDuiFeeName data = new CodeDuiFeeName(); #region Set DB data to Object data.FieldName = Convert.ToString(reader["COLUMN_NAME"]); #endregion bodyList.Add(data); } reader.Close(); } return bodyList; } #region CodeOpTemplateDetail static public List GetCodeOpTemplateDetail(string strCondition, string sort = null) { var strSql = new StringBuilder(); strSql.Append("SELECT [GID],[LINKGID],FIELDNAME,[REMARK]"); strSql.Append(" from CODE_OP_TEMPLATE_DETAIL "); 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 FIELDNAME"); } return SetCodeOpTemplateDetailData(strSql); } private static List SetCodeOpTemplateDetailData(StringBuilder strSql) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { CodeOpTemplateDetail data = new CodeOpTemplateDetail(); #region Set DB data to Object data.GID = Convert.ToString(reader["GID"]); data.LINKGID = Convert.ToString(reader["LINKGID"]); //if (reader["ITEMSNO"] != DBNull.Value) // data.ITEMSNO = Convert.ToInt16(reader["ITEMSNO"]); data.FIELDNAME = Convert.ToString(reader["FIELDNAME"]); data.REMARK = Convert.ToString(reader["REMARK"]); #endregion headList.Add(data); } reader.Close(); } return headList; } public static DBResult SaveCodeOpTemplateDetail(List bodyList, string PID) { 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_OP_TEMPLATE_DETAIL (GID,LINKGID,FIELDNAME,REMARK) values (@GID,@LINKGID,@FIELDNAME,@REMARK) "); var cmdUpdate = db.GetSqlStringCommand( @"update CODE_OP_TEMPLATE_DETAIL set FIELDNAME=@FIELDNAME,REMARK=@REMARK where GID=@GID "); if (bodyList != null) { foreach (var enumValue in bodyList) { if (enumValue.LINKGID == "*" || enumValue.LINKGID == "") { cmdInsert.Parameters.Clear(); db.AddInParameter(cmdInsert, "@GID", DbType.String, Guid.NewGuid().ToString()); db.AddInParameter(cmdInsert, "@LINKGID", DbType.String, PID); db.AddInParameter(cmdInsert, "@FIELDNAME", DbType.String, enumValue.FIELDNAME); db.AddInParameter(cmdInsert, "@REMARK", DbType.String, enumValue.REMARK); db.ExecuteNonQuery(cmdInsert, tran); } else { cmdUpdate.Parameters.Clear(); db.AddInParameter(cmdUpdate, "@GID", DbType.String, enumValue.GID); db.AddInParameter(cmdUpdate, "@FIELDNAME", DbType.String, enumValue.FIELDNAME); db.AddInParameter(cmdUpdate, "@REMARK", DbType.String, enumValue.REMARK); 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 DeleteCodeOpTemplateDetail(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_OP_TEMPLATE_DETAIL 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 public static DBResult DeleteDetail(CodeOpTemplate headData) { var result = new DBResult(); Database db = DatabaseFactory.CreateDatabase(); using (var conn = db.CreateConnection()) { conn.Open(); var tran = conn.BeginTransaction(); try { if (headData != null) { var cmdDelete = db.GetSqlStringCommand("delete from CODE_OP_TEMPLATE_DETAIL where LINKGID='" + headData.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; } } }