您的位置:首页 > 其它

插件开发技术说明(6)---分页查询示例

2015-06-01 23:43 323 查看
以下是7052-Request的实现代码。

从本示例可以学习到:

.如何利用CPagizerHelper简化分页处理

.如何利用CDBHandle直接把查询转换为消息包的CRowset.

.如何实现热门排序(SQL语句编写)

.分页处理是跨数据库平台的

.如何处理多个条件

 
#include "wiser.h"
#include "PagizeHelper.h"
#include "DBHandle.h"
 
int CWiser::OnQueryMySupplierGoods(CWrappedMsg<> *in, vector<CWrappedMsg<> *>& out, DISPATCH_RESULT& or)
{
or.err_code_ = CE_DATABASE;
CMsg* pMsgIN = in->msg;
long lEid,lUserid,lCoEid=0,lBrandid=0,lMinOrderQty=0;
short flag = 0;
char* pGoodsName = NULL;
if (pMsgIN->GetParam("EId",lEid)||pMsgIN->GetParam("UserId",lUserid)||pMsgIN->GetParam("Flag",flag)) {
or.err_code_ = CE_PARAM_ERROR;
return -1;
}
if (flag!=1&&flag!=2) { ///< 范围标志 1-已合作 2-未合作
or.err_code_ = CE_PARAM_ERROR;
return -1;
}
if (pMsgIN->GetParam("CoEId",lCoEid)==0) ///< 3-指定机构
flag = 3;
lBrandid = 0;
pMsgIN->GetParam("BrandID",lBrandid);
pMsgIN->GetParam("MinOrderQty",lMinOrderQty);
string goods_info;
pMsgIN->GetParam("GoodsInfo",goods_info);
short goods_info_flag = 0; ///< 商品信息标志 0-无商品信息 1-条码 2-商品名称
if (!ISEMPTY_STR(goods_info.c_str())) {
goods_info_flag = IsInteger(goods_info.c_str()) ? 1 : 2;
}

string cond;
if (lBrandid) {
cond += LogMsg("a.brandid=%d and ",lBrandid);
}
if (lMinOrderQty) {
cond += LogMsg("a.minorderqty<%d and ",lMinOrderQty);
}
if (goods_info_flag) {
cond += LogMsg("a.%s like '%%%s%%' and ",goods_info_flag==1 ? "barcode":"goodsname",goods_info.c_str());
}
string cond_expr;
if (!cond.empty()) {
cond.erase(cond.length()-5,5); ///< 删除结尾的" and "
cond_expr = string(" and ")+cond;
}
string fld_list = "a.eid,ename,goodsid,goodsname,a.brandid,brandname,stype,spec,saleprice,wholeprice,minorderqty,uname,pkname,pkspec,pkqty,saleflag,a.dataversion,a.picversion";
string  sql;
switch(flag) {
case 1:
sql = LogMsg("select a.* from (select %s from t_bas_mygoods a,"
"(select coeid as eid,ename from t_sys_coorg a,t_sys_org b where a.coeid=b.eid and a.eid=%lu"
" union"
" select a.eid,ename from t_sys_coorg a,t_sys_org b where a.eid=b.eid and a.coeid=%lu) b,t_bas_brand c"
" where a.eid= b.eid and a.brandid=c.brandid %s) a left join t_bid_vendergoodsrank b on a.eid=b.eid order by rank",
fld_list.c_str(),lEid,lEid,cond_expr.c_str());
break;
case 2:
sql = LogMsg("select a.* from ("
" select %s from t_bas_mygoods a,"
" (select eid,ename from t_sys_org where orgtype=20 and eid<>%lu and eid not in ("
" select * from ("
" select coeid as eid from t_sys_coorg where eid=%lu"
" union "
" select eid from t_sys_coorg  where coeid=%lu) a ) ) b,t_bas_brand c"
" where a.eid= b.eid and a.brandid=c.brandid %s) a left join t_bid_vendergoodsrank b on a.eid=b.eid order by rank",
fld_list.c_str(),lEid,lEid,lEid,cond_expr.c_str());
break;
case 3:
sql = LogMsg("select a.* from ("
" select %s from t_bas_mygoods a,t_sys_org b,t_bas_brand c"
" where a.eid= b.eid and a.brandid=c.brandid and b.eid=%lu %s) a left join t_bid_vendergoodsrank b on a.eid=b.eid order by rank",
fld_list.c_str(),lCoEid,cond_expr.c_str());
break;
}

CPagizeHelper PageHelper;
PageHelper.dbc_name_ = local_dbc_;
CPageCond Cond;
PageHelper.GetPageCond(pMsgIN,&Cond);
CMsg *pAnsMsg = new CMsg;
pAnsMsg->SetMsgType(MT_CONFIRMATION);
CRowset *prsRow = new CRowset;
pAnsMsg->AddRowset(prsRow);

GETDBC(pdbor,local_dbc_.c_str());
CDBHandle DBHandle(pdbor);
int result = 0;
try {
if(Cond.page_flag_){
if (Cond.count_flag_) {
string CountSql = LogMsg("select count(*) from (%s) a",sql.c_str());
int ret = PageHelper.GetCount(CountSql.c_str(),&Cond);
if (ret!=1) {///< 检查是否有数据
pAnsMsg->Release();
return ret;
}
}
string PageSql = pdbor->GetDBExt()->PageQuery(sql,Cond.begin_pos_,Cond.page_size_);
DBHandle.QueryToGenRowset(PageSql,prsRow,SOURCEINFO);
PageHelper.PutPageResult(pAnsMsg,&Cond);
}
else
DBHandle.QueryToGenRowset(sql,prsRow,SOURCEINFO);
} catch(...) {
pAnsMsg->Release();
return -1;
}

DO_RESP(pAnsMsg,out);

return 0;
}


协议定义:

查询供应商经营的商品(7052)(*)

要求:

l 支持分页

l 热门排序

使用场景:

l 供应商页面:商品展示区域内容

协议名称

查询供应商经营的商品

协议编号

7052

通信模式

请求-确认

请求内容

参数

参数

名称

说明

企业ID

EId

用户ID

UserId

范围标志

Flag

1-已合作 2-未合作

供应商ID

CoEId

可选

品牌ID

BrandID

可选有品牌必有供应商

起订量

MinOrderQty

可选

商品信息

GoodsInfo

可选商品名称或条码,模糊匹配

返回内容

参数

参数

名称

说明

商品列表



名称

说明

供应商企业ID

EId

企业名称

EName

商品ID

GoodsID

商品名称

GoodsName

品牌ID

BrandID

品牌名称

BrandName

型号

SType

规格

Spec

散装价格

SalePrice

批发价格

WholePrice

最低订货量

MinOrderQty

单位

UName

如件/盒

件装单位

PKName

件装规格

PKSpec

件装数

PKQty

销售方式

SaleFlag

数据版本

DataVersion

图片版本

PicVersion

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: