using System; using System.Data; using System.Collections.Generic; using System.Text; using DSWeb.MvcShipping.Models.MsUserMsgSet; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.Areas.CommMng.Models; using HcUtility.Comm; namespace DSWeb.MvcShipping.DAL.MsUserMsgSet { public class MsUserMsgSetDAL { #region Inquery DataList static public List GetUseMsgSetList(string strCondition, string userid,string companyid,string sort = null) { var strSql = new StringBuilder(); strSql.Append("SELECT * FROM (SELECT "); strSql.Append("[GID],[USERID],[WORKFLOWTYPE_NAME],[WORKFLOWTYPE_DESCRIPTION],'否' ISRECEIVE"); strSql.Append(" from user_msg_set where USERID='" + userid + "' "); strSql.Append(" union "); strSql.Append(" select NEWID() GID,'' USERID,[NAME] WORKFLOWTYPE_NAME,[DESCRIPTION] WORKFLOWTYPE_DESCRIPTION,'是' ISRECEIVE"); strSql.Append(" from workflow_type where typeno in (select distinct typeno from workflow) AND NAME NOT IN (SELECT WORKFLOWTYPE_NAME from user_msg_set) ) AS A "); if (!string.IsNullOrEmpty(strCondition)) { strSql.Append(" and " + strCondition); } var sortstring = DatasetSort.Getsortstring(sort); if (!string.IsNullOrEmpty(sortstring) && sortstring.Trim()!="") { strSql.Append(" order by " + sortstring); } else { strSql.Append(" order by WORKFLOWTYPE_NAME"); } return SetData(strSql); } 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()) { UserMsgSet data = new UserMsgSet(); #region Set DB data to Object data.GID = Convert.ToString(reader["GID"]); data.USERID = Convert.ToString(reader["USERID"]); data.WORKFLOWTYPE_NAME = Convert.ToString(reader["WORKFLOWTYPE_NAME"]); data.WORKFLOWTYPE_DESCRIPTION = Convert.ToString(reader["WORKFLOWTYPE_DESCRIPTION"]); data.ISRECEIVE = Convert.ToString(reader["ISRECEIVE"]); #endregion headList.Add(data); } reader.Close(); } return headList; } #endregion public static DBResult SetUserMsgSet(List boday,string userid) { var result = new DBResult(); Database db = DatabaseFactory.CreateDatabase(); using (var conn = db.CreateConnection()) { conn.Open(); var tran = conn.BeginTransaction(); try { if (boday != null) { var cmdDelete = db.GetSqlStringCommand("delete from user_msg_set where USERID=@USERID "); cmdDelete.Parameters.Clear(); db.AddInParameter(cmdDelete, "@USERID", DbType.String, userid); db.ExecuteNonQuery(cmdDelete, tran); foreach (var enumValue in boday) { if (enumValue.ISRECEIVE == "否") { var cmdInsert = db.GetSqlStringCommand("insert into user_msg_set(GID,USERID,WORKFLOWTYPE_NAME,WORKFLOWTYPE_DESCRIPTION,NOTRECEIVE,CREATEUSER,CREATETIME) values(NEWID(),'" + userid + "','" + enumValue.WORKFLOWTYPE_NAME + "','" + enumValue.WORKFLOWTYPE_DESCRIPTION+ "',1,'" +userid+ "','"+DateTime.Now.ToString("yyyy-MM-dd")+"')"); db.ExecuteNonQuery(cmdInsert, tran); } } } result = new DBResult(); result.Success = true; result.Message = "设置完成"; tran.Commit(); } catch (Exception) { tran.Rollback(); result.Success = false; result.Message = "设置完成出现错误,请重试或联系系统管理员"; return result; } } result.Success = true; result.Message = "设置完成"; return result; } } }