using System; using System.Data; using System.Configuration; using System.Data.SqlClient; using System.Collections; using System.Collections.Generic; using DSWeb.Models; using WebSqlHelper; namespace DSWeb.EntityDA { public class MessageDA { private const string PARM_MESSAGE_GID = "@gid"; private const string PARM_MESSAGE_NAME = "@name"; private const string PARM_MESSAGE_DESCRIPTION = "@description"; private const string PARM_MESSAGE_CREATE_TIME = "@create_time"; private const string PARM_MESSAGE_IS_READ = "@is_read"; private const string PARM_MESSAGE_READ_COUNT = "@read_count"; private const string PARM_MESSAGE_MESSAGE_CONTENT = "@message_content"; private const string PARM_MESSAGE_LAST_READ_TIME = "@last_readtime"; private const string PARM_MESSAGE_RECEIVER = "@receiver"; private const string PARM_MESSAGE_TYPE = "@type"; private const string PARM_MESSAGE_TASK_URL = "@task_url"; private const string PARM_MESSAGE_TASK_ID = "@task_id"; private const string PARM_MESSAGE_IS_DELETE = "@is_delete"; private const string PARM_MESSAGE_DELETE_USER = "@delete_user"; private const string PARM_MESSAGE_DELETE_TIME = "@delete_time"; private const string PARM_MESSAGE_RECV_TYPE = "@recv_type"; private const string SQL_SELECT_MESSAGE_BY_RECEIVER_ALL = " SELECT GID,NAME,DESCRIPTION,CREATETIME,ISREAD,READCOUNT,MESSAGECONTENT,LASTREADTIME," + " RECEIVER,TYPE,TASKURL,TASKID,ISDELETE,DELETEUSER,DELETETIME FROM sys_message WHERE RECEIVER = @receiver AND ISNULL(ISDELETE,0) <> 1"; private const string SQL_SELECT_MESSAGE_BY_GID = " SELECT GID,NAME,DESCRIPTION,CREATETIME,ISREAD,READCOUNT,MESSAGECONTENT,LASTREADTIME," + " RECEIVER,TYPE,TASKURL,TASKID,ISDELETE,DELETEUSER,DELETETIME FROM sys_message WHERE GID = @gid AND ISNULL(ISDELETE,0) <> 1"; private const string SQL_SELECT_MESSAGE_BY_RECEIVER_UNREAD = " SELECT GID,NAME,DESCRIPTION,CREATETIME,ISREAD,READCOUNT,MESSAGECONTENT,LASTREADTIME," + " RECEIVER,TYPE,TASKURL,TASKID FROM sys_message WHERE RECEIVER = @receiver AND ISREAD = 0 AND READCOUNT = 0 AND ISNULL(ISDELETE,0) <> 1"; private const string SQL_INSERT_MESSAGE = " INSERT INTO sys_message(GID,[NAME],DESCRIPTION,CREATETIME,ISREAD,READCOUNT,MESSAGECONTENT,RECEIVER,[TYPE],TASKURL,TASKID,RECVTYPE) " + " VALUES(@gid,@name,@description,GETDATE(),@is_read,@read_count,@message_content,@receiver,@type,@task_url,@task_id,@recv_type) "; //获取未阅读的和阅读过的个人消息 private const string SQL_SELECT_READ_TOTAL = " SELECT SUM(CASE WHEN ISNULL(ISREAD,0) = 1 THEN 1 ELSE 0 END) AS READTOTAL,SUM(CASE WHEN ISNULL(ISREAD,0) = 0 THEN 1 ELSE 0 END) AS UNREADTOTAL,COUNT(GID) as TOTAL FROM sys_message WITH(NOLOCK)" + " WHERE RECEIVER = @receiver AND ISNULL(ISDELETE,0) <> 1 "; private const string SQL_UPDATE_MESSAGE_SET_READ = " UPDATE sys_message SET ISREAD = 1,READCOUNT = READCOUNT + 1,LASTREADTIME = GETDATE() WHERE GID = @gid "; private const string SQL_UPDATE_MESSAGE_DELETE = " UPDATE sys_message SET ISDELETE = 1,DELETEUSER = @delete_user,DELETETIME = GETDATE() WHERE GID = @gid "; #region 删除消息 /// /// 删除消息 /// /// 消息GID /// 删除操作人GID /// 值1表示删除成功 值不等1表示删除失败 public int DeleteMessage(string tempMessageGID,string tempUserID) { int iResult = 0; SqlParameter[] parms = new SqlParameter[] { new SqlParameter(PARM_MESSAGE_GID,SqlDbType.VarChar,36), new SqlParameter(PARM_MESSAGE_DELETE_USER,SqlDbType.VarChar,36) }; parms[0].Value = tempMessageGID; parms[1].Value = tempUserID; using (SqlConnection sqlConnection = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction)) { try { SqlHelper.ExecuteNonQuery(sqlConnection, CommandType.Text, SQL_UPDATE_MESSAGE_DELETE, parms); iResult = 1; } catch { iResult = -1;//执行异常失败 } } return iResult; } #endregion #region 消息执行阅读 /// /// 消息执行阅读 /// /// 消息GID /// 值1消息置阅读状态成功 值不等于1表示消息置阅读状态失败 public int DoMessageRead(string tempMessageGID) { int iResult = 0; SqlParameter parm = new SqlParameter(PARM_MESSAGE_GID, SqlDbType.VarChar, 36); parm.Value = tempMessageGID; using (SqlConnection sqlConnection = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction)) { try { SqlHelper.ExecuteNonQuery(sqlConnection, CommandType.Text, SQL_UPDATE_MESSAGE_SET_READ, parm); iResult = 1; } catch { iResult = -1;//执行异常失败 } } return iResult; } #endregion #region 根据消息GID获取消息信息 /// /// 根据消息GID获取消息信息 /// /// 消息GID /// 返回实体类消息 public MessageEntity GetMessageByGID(string tempMessageGID) { MessageEntity messageEntity = null; SqlParameter parm = new SqlParameter(PARM_MESSAGE_GID, SqlDbType.VarChar, 36); parm.Value = tempMessageGID; using (SqlDataReader sqlRead = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_MESSAGE_BY_GID, parm)) { try { while (sqlRead.Read()) { messageEntity = new MessageEntity(); if (!sqlRead.IsDBNull(0)) { messageEntity.GID = sqlRead.GetString(0); } if (!sqlRead.IsDBNull(1)) { messageEntity.Name = sqlRead.GetString(1); } if (!sqlRead.IsDBNull(2)) { messageEntity.Description = sqlRead.GetString(2); } if (!sqlRead.IsDBNull(3)) { messageEntity.CreateTime = sqlRead.GetDateTime(3); } if (!sqlRead.IsDBNull(4)) { messageEntity.IsRead = sqlRead.GetBoolean(4); } if (!sqlRead.IsDBNull(5)) { messageEntity.ReadCount = sqlRead.GetInt32(5); } if (!sqlRead.IsDBNull(6)) { messageEntity.MessageContent = sqlRead.GetString(6); } if (!sqlRead.IsDBNull(7)) { messageEntity.LastReadTime = sqlRead.GetDateTime(7); } if (!sqlRead.IsDBNull(8)) { messageEntity.Receiver = sqlRead.GetString(8); } if (!sqlRead.IsDBNull(9)) { messageEntity.Type = sqlRead.GetInt32(9); } if (!sqlRead.IsDBNull(10)) { messageEntity.TaskUrl = sqlRead.GetString(10); } if (!sqlRead.IsDBNull(11)) { messageEntity.TaskID = sqlRead.GetString(11); } if (!sqlRead.IsDBNull(12)) { messageEntity.IsDelete = sqlRead.GetBoolean(12); } if (!sqlRead.IsDBNull(13)) { messageEntity.DeleteUser = sqlRead.GetString(13); } if (!sqlRead.IsDBNull(14)) { messageEntity.DeleteTime = sqlRead.GetDateTime(14); } } } catch (Exception execError) { throw execError; } } return messageEntity; } #endregion public MessageTotalEntity GetMessageTotalByReceiver(string tempReceiverGID) { MessageTotalEntity messageTotalEntity = null; SqlParameter parm = new SqlParameter(PARM_MESSAGE_RECEIVER, SqlDbType.VarChar, 36); parm.Value = tempReceiverGID; using (SqlDataReader sqlRead = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_READ_TOTAL, parm)) { try { while (sqlRead.Read()) { messageTotalEntity = new MessageTotalEntity(); if (!sqlRead.IsDBNull(0)) { messageTotalEntity.ReadTotal = sqlRead.GetInt32(0); } if (!sqlRead.IsDBNull(1)) { messageTotalEntity.ReadTotal = sqlRead.GetInt32(1); } if (!sqlRead.IsDBNull(2)) { messageTotalEntity.Total = sqlRead.GetInt32(2); } messageTotalEntity.Receiver = tempReceiverGID; } } catch (Exception execError) { throw execError; } } return messageTotalEntity; } #region 创建消息 /// /// 创建消息 /// /// 消息类型 /// 消息内容(可以为多条消息同时创建) /// 值等于1表示创建成功 值不等1表示创建失败 public int CreateMessage(MessageType messageType,IList messageEntities) { int iResult = 0; using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction)) { try { SqlParameter[] insertParms = new SqlParameter[] { new SqlParameter(PARM_MESSAGE_GID,SqlDbType.VarChar,36), new SqlParameter(PARM_MESSAGE_NAME,SqlDbType.VarChar,50), new SqlParameter(PARM_MESSAGE_DESCRIPTION,SqlDbType.VarChar,50), new SqlParameter(PARM_MESSAGE_IS_READ,SqlDbType.Bit), new SqlParameter(PARM_MESSAGE_READ_COUNT,SqlDbType.Int), new SqlParameter(PARM_MESSAGE_MESSAGE_CONTENT,SqlDbType.VarChar,500), new SqlParameter(PARM_MESSAGE_RECEIVER,SqlDbType.VarChar,36), new SqlParameter(PARM_MESSAGE_TYPE,SqlDbType.Int), new SqlParameter(PARM_MESSAGE_TASK_URL,SqlDbType.VarChar,300), new SqlParameter(PARM_MESSAGE_TASK_ID,SqlDbType.VarChar,100), new SqlParameter(PARM_MESSAGE_RECV_TYPE,SqlDbType.Int) }; foreach (MessageEntity messageEntity in messageEntities) { insertParms[0].Value = messageEntity.GID; insertParms[1].Value = messageEntity.Name; insertParms[2].Value = messageEntity.Description; insertParms[3].Value = messageEntity.IsRead; insertParms[4].Value = messageEntity.ReadCount; insertParms[5].Value = messageEntity.MessageContent; insertParms[6].Value = messageEntity.Receiver; insertParms[7].Value = messageEntity.Type; insertParms[8].Value = messageEntity.TaskUrl; insertParms[9].Value = messageEntity.TaskID; insertParms[10].Value = messageEntity.RecvType; SqlHelper.ExecuteNonQuery(sqlTran, CommandType.Text, SQL_INSERT_MESSAGE, insertParms); } iResult = 1;//状态为1表示删除成功 sqlTran.Commit(); } catch (Exception execError) { iResult = -1;//有异常,插入失败 sqlTran.Rollback(); iResult = -2;//插入异常,事务已回滚成功 throw execError; } finally { SqlHelper.CloseConnection(); } } return iResult; } #endregion #region 根据消息接收人获取所有消息信息 /// /// 根据消息接收人获取所有消息信息 /// /// 消息接收人GID /// 返回所有未阅读的消息 public IList GetMessageUnReceiver(string tempReceiverGID) { IList messageEntities = new List(); SqlParameter parm = new SqlParameter(PARM_MESSAGE_RECEIVER, SqlDbType.VarChar, 36); parm.Value = tempReceiverGID; using (SqlDataReader sqlRead = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_MESSAGE_BY_RECEIVER_ALL, parm)) { try { while (sqlRead.Read()) { MessageEntity messageEntity = new MessageEntity(); if (!sqlRead.IsDBNull(0)) { messageEntity.GID = sqlRead.GetString(0); } if (!sqlRead.IsDBNull(1)) { messageEntity.Name = sqlRead.GetString(1); } if (!sqlRead.IsDBNull(2)) { messageEntity.Description = sqlRead.GetString(2); } if (!sqlRead.IsDBNull(3)) { messageEntity.CreateTime = sqlRead.GetDateTime(3); } if (!sqlRead.IsDBNull(4)) { messageEntity.IsRead = sqlRead.GetBoolean(4); } if (!sqlRead.IsDBNull(5)) { messageEntity.ReadCount = sqlRead.GetInt32(5); } if (!sqlRead.IsDBNull(6)) { messageEntity.MessageContent = sqlRead.GetString(6); } if (!sqlRead.IsDBNull(7)) { messageEntity.LastReadTime = sqlRead.GetDateTime(7); } if (!sqlRead.IsDBNull(8)) { messageEntity.Receiver = sqlRead.GetString(8); } if (!sqlRead.IsDBNull(9)) { messageEntity.Type = sqlRead.GetInt32(9); } if (!sqlRead.IsDBNull(10)) { messageEntity.TaskUrl = sqlRead.GetString(10); } if (!sqlRead.IsDBNull(11)) { messageEntity.TaskID = sqlRead.GetString(11); } if (!sqlRead.IsDBNull(12)) { messageEntity.IsDelete = sqlRead.GetBoolean(12); } if (!sqlRead.IsDBNull(13)) { messageEntity.DeleteUser = sqlRead.GetString(13); } if (!sqlRead.IsDBNull(14)) { messageEntity.DeleteTime = sqlRead.GetDateTime(14); } messageEntities.Add(messageEntity); } } catch (Exception execError) { throw execError; } } return messageEntities; } #endregion #region 获取SQL语句查询数据集 /// /// 获取SQL语句查询数据集 /// /// /// public DataSet GetExcuteSql(string strSql) { DataSet tempSet = new DataSet(); tempSet = SqlHelper.ExecuteDataset(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql); return tempSet; } #endregion public int GetMessageType(MessageType messageType) { int iResult = 0; switch (messageType) { case MessageType.FEEAUDIT: iResult = 1; break; case MessageType.FEEPAY: iResult = 2; break; case MessageType.FEEPAYAPP: iResult = 3; break; case MessageType.INVOICE: iResult = 4; break; case MessageType.INVOICEAPP: iResult = 5; break; case MessageType.BLISSUESUR: iResult = 6; break; case MessageType.FEEAUDITPASS: iResult = 7; break; case MessageType.FEEAUDITREJECT: iResult = 8; break; case MessageType.FEEMODIFY: iResult = 9; break; case MessageType.SEAORDERAUDIT: iResult = 10; break; case MessageType.BLISSUEPUT: iResult = 11; break; case MessageType.CRMInfoFaqInfo: iResult = 21; break; default: break; } return iResult; } public MessageType GetMessageTypeNum(int iType) { MessageType messageType = MessageType.NULL; switch (iType) { case 1: messageType = MessageType.FEEAUDIT; break; case 2: messageType = MessageType.FEEPAY; break; case 3: messageType = MessageType.FEEPAYAPP; break; case 4: messageType = MessageType.INVOICE; break; case 5: messageType = MessageType.INVOICEAPP; break; case 6: messageType = MessageType.BLISSUESUR; break; case 7: messageType = MessageType.FEEAUDITPASS; break; case 8: messageType = MessageType.FEEAUDITREJECT; break; case 9: messageType = MessageType.FEEMODIFY; break; case 10: messageType = MessageType.SEAORDERAUDIT; break; case 11: messageType = MessageType.BLISSUEPUT; break; case 21: messageType = MessageType.CRMInfoFaqInfo; break; default: break; } return messageType; } } public enum MessageType { NULL = 0, FEEAUDIT = 1,//海运进口应收应付待审核通知 FEEPAY = 2, FEEPAYAPP = 3,//付费申请审核 INVOICE = 4, INVOICEAPP = 5, BLISSUESUR = 6, FEEAUDITPASS = 7, FEEAUDITREJECT = 8, FEEMODIFY = 9, SEAORDERAUDIT = 10, BLISSUEPUT = 11, CRMInfoFaqInfo = 21//询价报价申请 } }