Example #1
0
        /// <summary>
        /// 将聊天记录写进数据库
        /// </summary>
        /// <param name="session"></param>
        /// <param name="lstParams">
        /// lstParams[0] : 是否创建新cookie 0:创建 1:不创建
        /// lstParams[1] : 2:在线消息 3:离线消息(直接用此值拼表名)
        /// </param>
        /// <returns></returns>
        public static OperationReturn WriteChatMsg(SessionInfo session, ChatMessage chatMsg, List <string> lstParams)
        {
            OperationReturn optReturn = new OperationReturn();

            try
            {
                string strSql    = string.Empty;
                string strToken  = session.RentInfo.Token;
                long   lCookieID = 0;
                long   now       = 0;
                CommonFuncs.DateTimeToNumber(DateTime.Now, ref now);
                long nowUtc = 0;
                CommonFuncs.DateTimeToNumber(DateTime.Now.ToUniversalTime(), ref nowUtc);
                List <string> lstSerialParam = new List <string>();
                if (lstParams[0] == "0")
                {
                    //创建新会话
                    lstSerialParam.Add("16");
                    lstSerialParam.Add("161");
                    optReturn = GetSerialID(session, lstSerialParam);
                    if (!optReturn.Result)
                    {
                        //LogOperation.WriteLog("WriteChatMsg GetSerialID error: " + optReturn.Message);
                        return(optReturn);
                    }
                    lCookieID = long.Parse(optReturn.Data.ToString());
                    // LogOperation.WriteLog("Create new cookie "+lCookieID.ToString());
                    switch (session.DBType)
                    {
                    case 2:
                        strSql    = "insert into t_16_001_{0}(C001,C002,C003,C006,C007,C008,C009) values({1},{2},{3},{4},'1',{5},'1')";
                        strSql    = string.Format(strSql, strToken, lCookieID, now, nowUtc, chatMsg.SenderID, chatMsg.ResourceID);
                        optReturn = MssqlOperation.ExecuteSql(session.DBConnectionString, strSql);
                        break;

                    case 3:
                        strSql    = "insert into t_16_001_{0}(C001,C002,C003,C006,C007,C008,C009) values({1},{2},{3},{4},'1',{5},'1')";
                        strSql    = string.Format(strSql, strToken, lCookieID, now, nowUtc, chatMsg.SenderID, chatMsg.ResourceID);
                        optReturn = OracleOperation.ExecuteSql(session.DBConnectionString, strSql);
                        break;
                    }
                    if (!optReturn.Result)
                    {
                        // LogOperation.WriteLog("Create cookie error:" + optReturn.Message);
                        return(optReturn);
                    }
                }
                else
                {
                    // LogOperation.WriteLog("Cookie exists");
                    lCookieID = chatMsg.CookieID;
                }
                //写消息详细记录
                // LogOperation.WriteLog("Add chat message,database type = " + session.DatabaseInfo.TypeName);
                string strTableName = lstParams[1];
                if (strTableName == "3")
                {
                    //如果是离线消息 需要获得流水号 流水号用来在发送完离线消息后删除该消息
                    lstSerialParam.Clear();
                    lstSerialParam.Add("16");
                    lstSerialParam.Add("162");
                    optReturn = GetSerialID(session, lstSerialParam);
                    if (!optReturn.Result)
                    {
                        // LogOperation.WriteLog("Create offline serialID error : " + optReturn.Message);
                        return(optReturn);
                    }
                    // LogOperation.WriteLog("create offline msgID : " + optReturn.Data.ToString());
                    long lMsgID = long.Parse(optReturn.Data.ToString());
                    switch (session.DBType)
                    {
                    case 2:
                        strSql = "insert into t_16_00{0}_{1} values({10},{2},{3},{4},{5},'{6}',{7},'{8}','{9}')";
                        strSql = string.Format(strSql, lstParams[1], strToken, lCookieID, now, nowUtc,
                                               chatMsg.SenderID, chatMsg.SenderName, chatMsg.ReceiverID, chatMsg.ReceiverName, chatMsg.MsgContent, lMsgID);
                        optReturn = MssqlOperation.ExecuteSql(session.DBConnectionString, strSql);

                        break;

                    case 3:
                        strSql = "insert into t_16_00{0}_{1} values({10},{2},{3},{4},{5},'{6}',{7},'{8}','{9}')";
                        strSql = string.Format(strSql, lstParams[1], strToken, lCookieID, now, nowUtc,
                                               chatMsg.SenderID, chatMsg.SenderName, chatMsg.ReceiverID, chatMsg.ReceiverName, chatMsg.MsgContent, lMsgID);
                        optReturn = OracleOperation.ExecuteSql(session.DBConnectionString, strSql);
                        break;
                    }
                }
                else
                {
                    switch (session.DBType)
                    {
                    case 2:
                        strSql = "insert into t_16_00{0}_{1} values({2},{3},{4},{5},'{6}',{7},'{8}','{9}')";
                        strSql = string.Format(strSql, lstParams[1], strToken, lCookieID, now, nowUtc,
                                               chatMsg.SenderID, chatMsg.SenderName, chatMsg.ReceiverID, chatMsg.ReceiverName, chatMsg.MsgContent);
                        optReturn = MssqlOperation.ExecuteSql(session.DBConnectionString, strSql);

                        break;

                    case 3:
                        strSql = "insert into t_16_00{0}_{1} values({2},{3},{4},{5},'{6}',{7},'{8}','{9}')";
                        strSql = string.Format(strSql, lstParams[1], strToken, lCookieID, now, nowUtc,
                                               chatMsg.SenderID, chatMsg.SenderName, chatMsg.ReceiverID, chatMsg.ReceiverName, chatMsg.MsgContent);
                        optReturn = OracleOperation.ExecuteSql(session.DBConnectionString, strSql);
                        break;
                    }
                }

                //LogOperation.WriteLog("Add chat message done");
                optReturn.Data = lCookieID;
            }
            catch (Exception ex)
            {
                optReturn.Result  = false;
                optReturn.Message = ex.Message;
                //LogOperation.WriteLog("WriteChatMsg exception ,message = " + ex.Message);
            }
            return(optReturn);
        }