📄 workthread.cpp
字号:
// WorkThread.cpp : implementation file
//
#include "stdafx.h"
#include "WorkThread.h"
#include "0000Thread.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWorkThread
IMPLEMENT_DYNCREATE(CWorkThread, CWinThread)
CWorkThread::CWorkThread()
{
}
CWorkThread::~CWorkThread()
{
}
BOOL CWorkThread::InitInstance()
{
// TODO: perform and per-thread initialization here
return TRUE;
}
int CWorkThread::ExitInstance()
{
// TODO: perform any per-thread cleanup here
return CWinThread::ExitInstance();
}
BEGIN_MESSAGE_MAP(CWorkThread, CWinThread)
//{{AFX_MSG_MAP(CWorkThread)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWorkThread message handlers
int CWorkThread::Run()
{
IOD_Info info;
m_myDB.OpenDB();
while (!theData.m_bServiceExit)
{
if (theData.GetOneRecMsg(&info))
{
if (Work(&info))
{
}
}
else
Sleep(1);
}
m_myDB.CloseDB();
return CWinThread::Run();
}
BOOL CWorkThread::Work(LPIOD_Info pIOD)
{
m_Info.szMobile=pIOD->szDestMobile;
m_myDB.GetCommonInfo(&m_Info);
m_szSubID=pIOD->szSubID;
m_szSrcTermID=pIOD->szSrcTermID;
m_szContent=pIOD->szContent;
m_szContent.TrimLeft();
m_szContent.TrimRight();
//天津的QX0相当于00000全部退订
if (m_Info.nProvince=22 && m_szSubID.CompareNoCase("QX0")==0)
strcpy(pIOD->szSubID,"00000");
// 0000处理
if (C0000Thread::Is0000Service(pIOD) )
{
theData.Add0000Msg(pIOD);
return TRUE;
}
// 二次定制
int nSedFlag=1;
if (m_Info.nProvince=791 && m_szSubID.CompareNoCase("Y")==0)
{//江西如果发Y到1818默认为发Y到1818000
strcpy(pIOD->szContent,"Y");
strcpy(pIOD->szSubID,"000");
m_szContent=pIOD->szContent;
m_szContent.TrimLeft();
m_szContent.TrimRight();
}
if (_stricmp(pIOD->szSubID, "000")==0 )
{
if (0!=Custom2(pIOD))
return TRUE;
else
nSedFlag=0;
}
if (m_Info.nProvince=22 && m_szSubID.Left(2).CompareNoCase("QR")==0)
{//天津的二次定制确认
if (0!=Custom2(pIOD))
return TRUE;
else
nSedFlag=0;
}
// 指令解析
if (_stricmp(pIOD->szSubID, "IUNKNOW2")==0 ||
_stricmp(pIOD->szSubID, "IUNKNOW")==0)
{
int nIndex=m_szContent.Find(' ');
if (nIndex==-1)
{
m_szSubID=m_szContent;
m_szContent.Empty();
}
else
{
m_szSubID=m_szContent.Left(nIndex);
m_szContent.Delete(0, nIndex+1);
m_szContent.FreeExtra();
m_szContent.TrimLeft();
}
}
// 源终端号码解析
if (_stricmp(pIOD->szSubID, "IUNKNOW2")!=0)
m_szSrcTermID="";
// 取得产品信息
m_pi.nType=-1;
while (!m_mmlist.empty())
m_mmlist.pop_front();
while (!m_msglist.empty())
m_msglist.pop_front();
m_si.nPort=0;
int nRet=m_myDB.GetProductInfo(pIOD->szDestMobile, m_szSubID, m_szSrcTermID, &m_pi, &m_mmlist, &m_msglist, &m_si);
if (nRet==-1)//param error
{
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_sys_error", NULL, &m_Info);
return TRUE;
}
else if (nRet==-3)
{
if (m_si.nPort!=0)//transfer
{
lstrcpy(pIOD->szSubID, m_si.szNum);
lstrcpy(pIOD->szSrcTermID, m_szSrcTermID.Mid(m_si.szNum.GetLength()));
lstrcpy(pIOD->szServiceIP, m_si.szIP);
pIOD->nServicePort=m_si.nPort;
theData.AddTransferMsg(pIOD);
return TRUE;
}
else//db cfg error
{
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_sys_error", NULL, &m_Info);
return TRUE;
}
}
else if (nRet==-4)//cmd error
{
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_cmd_error", NULL, &m_Info);
return TRUE;
}
else if (nRet<0)//db error
{
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_db_error", NULL, &m_Info);
return TRUE;
}
if (nSedFlag==0)
m_pi.nSedFlag=0;
// 参数解析
m_ParamList.RemoveAll();
m_szReceiver.Empty();
m_nOperate=1;
m_szUserID.Empty();
CString szTmp(m_szContent);
CString szParam;
int nIndex(0);
do
{
nIndex=szTmp.Find(' ');
if (nIndex==-1)
szParam=szTmp;
else
{
szParam=szTmp.Left(nIndex);
szTmp.Delete(nIndex+1);
szTmp.FreeExtra();
szTmp.TrimLeft();
}
if (szParam.CompareNoCase("Q")==0)
m_nOperate=2;
else if (JudgeNumber(szParam))
m_szReceiver=szParam;
else if (!szParam.IsEmpty())
m_ParamList.AddTail(szParam);
}while (nIndex!=-1);
if (m_pi.nType==1) //老产品
{
if (m_nOperate==1 && m_pi.nOperate==1)
Register1(pIOD);
else
Unregister1(pIOD);
}
else if (m_pi.nType==2) //新产品
{
if (m_nOperate==1 && m_pi.nOperate==1)
Register2(pIOD);
else
Unregister2(pIOD);
}
else
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_sys_error", NULL, &m_Info);
return TRUE;
}
int CWorkThread::Custom2(LPIOD_Info pIOD)
{
long nID(0);
CString szSourceSubID(pIOD->szSubID);
m_szSubID=szSourceSubID;
CString szContent, szName;
int nRet=m_myDB.GetSedHistory(pIOD->szDestMobile, m_szSubID, szContent, m_szSrcTermID, szName, nID);
if (nRet==-3) // 没有定制历史记录
{
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_2nd_timeout", NULL, &m_Info);
return -1;
}
else if (nRet!=0) // 数据库出错
{
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_2nd_db_error", NULL, &m_Info);
return -1;
}
if (m_szContent.Left(1).CompareNoCase("Y")==0) // 指令正确
{
m_myDB.SedHistoryDone(nID);
m_szContent=szContent;
return 0;
}
if (szSourceSubID.Left(2).CompareNoCase("QR")==0) // 指令正确
{
m_myDB.SedHistoryDone(nID);
m_szContent=szContent;
return 0;
}
pair<CString, CString> pa;
pa.first="{PNAME}";
pa.second=szName;
list<pair<CString, CString> > lp;
lp.push_back(pa);
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_2nd_cmd_error", &lp, &m_Info);
return -1;
}
BOOL CWorkThread::Register1(LPIOD_Info pIOD)
{
// 特殊产品(带参数产品判断)
if (0!=JudgeParam(pIOD))
return TRUE;
CString szOut;
if (m_szReceiver.IsEmpty())
m_szReceiver=pIOD->szDestMobile;
int nRet=m_myDB.QueryCustom(m_pi.szPushType, pIOD->szDestMobile, "", "", m_szReceiver, m_pi.szParam1, m_pi.szParam2, 0, szOut);
if (nRet<0)
{
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_db_error", NULL, &m_Info);
return TRUE;
}
if (!szOut.IsEmpty()) // 已定制
{
if (m_si.nPort!=0) // 需要转发
{
lstrcpy(pIOD->szSubID, m_szSubID);
lstrcpy(pIOD->szContent, "");
lstrcpy(pIOD->szSrcTermID, "1818");
pIOD->lMsgLen=0;
lstrcpy(pIOD->szServiceIP, m_si.szIP);
pIOD->nServicePort=m_si.nPort;
theData.AddTransferMsg(pIOD);
}
else
SetMsg(pIOD, MT_REG_EXISTS);
return TRUE;
}
if (m_pi.nSedFlag==1) // 二次定制
{
if (!m_myDB.AddOneSubConfirm(pIOD->szDestMobile, m_szSubID, m_szContent, m_szSrcTermID))
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_db_error", NULL, &m_Info);
else
SetMsg(pIOD, MT_REG_2ND);
return TRUE;
}
nRet=m_myDB.AddCustom(m_pi.szPushType, pIOD->szDestMobile, m_pi.szTime1, m_pi.szTime2, m_szReceiver, m_pi.szParam1, m_pi.szParam2, m_pi.nChannel);
if (nRet==-6)
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_blacklist", NULL, &m_Info);
else if (nRet<0)
SetMsg(pIOD, MT_REG_FAIL);
else
{
m_szUserID.Format("%d", nRet);
SetMsg(pIOD, MT_MISC_08);
SetMsg(pIOD, MT_REG_OK);
}
return TRUE;
}
BOOL CWorkThread::Register2(LPIOD_Info pIOD)
{
_ConnectionPtr pConn;
if (!m_myDB.OpenDB(m_pi.szSQL, pConn))
{
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_db_error", NULL, &m_Info);
return -2;
}
int nRet=m_myDB.QueryCustom(pConn, pIOD->szDestMobile, m_pi.szSQL);
if (nRet>=0) // 已定制
{
if (m_si.nPort!=0) // 需要转发
{
lstrcpy(pIOD->szSubID, m_szSubID);
lstrcpy(pIOD->szContent, "");
lstrcpy(pIOD->szSrcTermID, "1818");
pIOD->lMsgLen=0;
lstrcpy(pIOD->szServiceIP, m_si.szIP);
pIOD->nServicePort=m_si.nPort;
theData.AddTransferMsg(pIOD);
}
else
SetMsg(pIOD, MT_REG_EXISTS);
return TRUE;
}
else if (nRet!=-3&&nRet!=-4) // 非不存在
{
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_db_error", NULL, &m_Info);
return TRUE;
}
if (m_pi.nSedFlag==1) // 二次定制
{
if (!m_myDB.AddOneSubConfirm(pIOD->szDestMobile, m_szSubID, m_szContent, m_szSrcTermID))
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_db_error", NULL, &m_Info);
else
SetMsg(pIOD, MT_REG_2ND);
return TRUE;
}
nRet=m_myDB.AddCustom(pConn, pIOD->szDestMobile, m_pi.szSQL, m_pi.nChannel, m_szReceiver);
if (nRet==-6)
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_blacklist", NULL, &m_Info);
else if (nRet<0)
SetMsg(pIOD, MT_REG_FAIL);
else
{
m_szUserID.Format("%d", nRet);
SetMsg(pIOD, MT_MISC_08);
SetMsg(pIOD, MT_REG_OK);
}
m_myDB.CloseDB(pConn);
return TRUE;
}
BOOL CWorkThread::Unregister1(LPIOD_Info pIOD)
{
CString szOut;
if (m_szReceiver.IsEmpty())
m_szReceiver=pIOD->szDestMobile;
int nRet=m_myDB.QueryCustom(m_pi.szPushType, pIOD->szDestMobile, "", "", m_szReceiver, m_pi.szParam1, m_pi.szParam2, 1, szOut);
if (nRet<0)
{
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_db_error", NULL, &m_Info);
return TRUE;
}
if (szOut.IsEmpty()) // 无定制
{
SetMsg(pIOD, MT_UNREG_NOTEXISTS);
return TRUE;
}
nRet=m_myDB.DeleteCustom(m_pi.szPushType, pIOD->szDestMobile, /*m_pi.szTime1, m_pi.szTime2*/"","", m_szReceiver, m_pi.szParam1, m_pi.szParam2, 1, m_pi.nChannel);
if (nRet<0)
SetMsg(pIOD, MT_UNREG_FAIL);
else
{
SetMsg(pIOD, MT_UNREG_OK);
SetMsg(pIOD, MT_MISC_09);
}
return TRUE;
}
BOOL CWorkThread::Unregister2(LPIOD_Info pIOD)
{
_ConnectionPtr pConn;
if (!m_myDB.OpenDB(m_pi.szSQL, pConn))
{
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_db_error", NULL, &m_Info);
return -2;
}
int nRet=m_myDB.QueryCustom(pConn, pIOD->szDestMobile, m_pi.szSQL);
if (nRet==-3||nRet==-4) // 不存在
{
SetMsg(pIOD, MT_UNREG_NOTEXISTS);
return TRUE;
}
else if (nRet<0)
{
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_db_error", NULL, &m_Info);
return TRUE;
}
nRet=m_myDB.DeleteCustom(pConn, pIOD->szDestMobile, m_pi.szSQL, m_pi.nChannel);
if (nRet<0)
SetMsg(pIOD, MT_UNREG_FAIL);
else
{
SetMsg(pIOD, MT_UNREG_OK);
SetMsg(pIOD, MT_MISC_09);
}
m_myDB.CloseDB(pConn);
return TRUE;
}
int CWorkThread::JudgeParam(LPIOD_Info pIOD)
{
CString szParam1;
CString szParam2;
POSITION pos=m_ParamList.GetHeadPosition();
if (pos!=NULL)
szParam1=m_ParamList.GetNext(pos);
if (pos!=NULL)
szParam2=m_ParamList.GetNext(pos);
// 特殊产品(带参数产品判断)
if (m_pi.szPushType.CompareNoCase("tq")==0) // 天气
{
if (szParam1.IsEmpty())
{
theData.SetMsg2(&m_myDB, pIOD, "TQ_no_city", NULL, &m_Info);
return -1;
}
szParam1=szParam1.Left(20);
int nRet=m_myDB.IsValidCity(szParam1);
if (nRet==-2)
{
theData.SetMsg2(&m_myDB, pIOD, "TYDZ_db_error", NULL, &m_Info);
return -1;
}
else if (nRet!=0)
{
theData.SetMsg2(&m_myDB, pIOD, "TQ_invalid_city", NULL, &m_Info);
return -1;
}
m_pi.szParam1=szParam1;
}
else if (m_pi.szPushType.CompareNoCase("yc")==0) // 运程
{
if (szParam1.IsEmpty())
{
theData.SetMsg2(&m_myDB, pIOD, "YC_no_date", NULL, &m_Info);
return -1;
}
// 生日必须是数字,0101 — 1231
CString szTmp(szParam1);
if (szTmp.GetLength() == 3)
{
szTmp = _T("0");
szTmp += szParam1;
}
else if (szTmp.GetLength() == 2)
{
szTmp = _T("0");
szTmp += szParam1.Left(1);
szTmp += _T("1");
szTmp += szParam1.Right(1);
}
if (szTmp.GetLength() != 4 || !JudgeNumber(szTmp))
{
theData.SetMsg2(&m_myDB, pIOD, "YC_invalid_date", NULL, &m_Info);
return -1;
}
int month = atoi(szTmp.Left(2));
int day = atoi(szTmp.Right(2));
if (month > 12 || month < 1 || day > 31 || day < 1)
{
theData.SetMsg2(&m_myDB, pIOD, "YC_invalid_date", NULL, &m_Info);
return -1;
}
int value = atoi(szTmp);
if (value >= 1223 || value <= 120)
m_pi.szParam1 = _T("9"); // 魔羯座
else if (value <= 219)
m_pi.szParam1 = _T("10"); // 水瓶座
else if (value <= 320)
m_pi.szParam1 = _T("11"); // 双鱼座
else if (value <= 420)
m_pi.szParam1 = _T("0"); // 白羊座
else if (value <= 520)
m_pi.szParam1 = _T("1"); // 金牛座
else if (value <= 622)
m_pi.szParam1 = _T("2"); // 双子座
else if (value <= 722)
m_pi.szParam1 = _T("3"); // 巨蟹座
else if (value <= 822)
m_pi.szParam1 = _T("4"); // 狮子座
else if (value <= 922)
m_pi.szParam1 = _T("5"); // 处女座
else if (value <= 1022)
m_pi.szParam1 = _T("6"); // 天称座
else if (value <= 1122)
m_pi.szParam1 = _T("7"); // 天蝎座
else
m_pi.szParam1 = _T("8"); // 射手座
}
return 0;
}
void CWorkThread::SetMsg(LPIOD_Info pIOD, MSGTYPE eType)
{
/* if (eType==MT_MISC_08||eType==MT_MISC_09) // MISC msg
{
list<MISCMSG>::iterator mm;
for (mm=m_mmlist.begin(); mm!=m_mmlist.end(); ++mm)
{
if (eType==MT_MISC_08 && (*mm).szFeeType.CompareNoCase("08")==0)
theData.SetMsg(pIOD, (*mm).szContent, "1818", (*mm).szSubID, "0", "08", &m_Info);
else if (eType==MT_MISC_09 && (*mm).szFeeType.CompareNoCase("09")==0)
theData.SetMsg(pIOD, (*mm).szContent, "1818", (*mm).szSubID, "0", "09", &m_Info);
}
return;
}
*/
BOOL bDone(FALSE);
list<OUTPUTMSG>::iterator msg;
for (msg=m_msglist.begin(); msg!=m_msglist.end(); ++msg)
{
if ((*msg).nType==eType)
{
CString szContent=(*msg).szMsg;
if (szContent.Find("{PNAME}")!=-1)
szContent.Replace("{PNAME}", m_pi.szName);
if (szContent.Find("{MOBILE}")!=-1)
szContent.Replace("{MOBILE}", pIOD->szDestMobile);
if (szContent.Find("{RECEIVER}")!=-1)
szContent.Replace("{RECEIVER}", m_szReceiver);
if (szContent.Find("{USERID}")!=-1)
szContent.Replace("{USERID}", m_szUserID);
theData.SetMsg(pIOD, /*(*msg).szMsg*/szContent, (*msg).szSendNo, (*msg).szSubID, (*msg).szFeeCode, (*msg).szFeeType, (*msg).nMsgType, &m_Info);
bDone=TRUE;
}
}
}
BOOL CWorkThread::JudgeNumber(LPCTSTR pszIn)
{
int len = lstrlen(pszIn);
for (int i = 0; i < len; i++)
{
if (pszIn[i] < 0x30 || pszIn[i] > 0x39)
return FALSE;
}
return TRUE;
}
/*
BOOL CWorkThread::JudgeMobile(LPCTSTR pszIn)
{
int len = lstrlen(pszIn);
if (len != 11)
return FALSE;
if (pszIn[0] != '1' || (pszIn[1] != '3'&&pszIn[1]!='7'))
return FALSE;
for (int i = 0; i < len; i++)
{
if (pszIn[i] < 0x30 || pszIn[i] > 0x39)
return FALSE;
}
return TRUE;
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -