当查询条件很多时条件构造器可以让你不用写那么多Service查询条件
根据条件构造器查询总条数
@Override
public Integer getFundFlowCount(FundFlowReq req) throws KeynesException {
if (null == req) {
throw new KeynesException(KeynesExceptionEnum.MISSING_PARAMS);
}
Wrapper<FundFlow> wrapper = this.buildConditions(req);
Integer count;
try {
count = fundFlowMapper.selectCount(wrapper);
} catch (Exception e){
throw new KeynesException(KeynesExceptionEnum.QUERY_FAILED);
}
return count;
}
条件构造器
private Wrapper<FundFlow> buildConditions(FundFlowReq req) {
Wrapper<FundFlow> wrapper = new EntityWrapper<>();
if (StringUtils.isNotBlank(req.getFlowCode())) {
wrapper.eq("flow_code", req.getFlowCode());
}
if (req.getStatus()!=null) {
wrapper.eq("status", req.getStatus());
}
if (StringUtils.isNotBlank(req.getFundProCode())) {
wrapper.eq("fund_pro_code", req.getFundProCode());
}
if (StringUtils.isNotBlank(req.getOidFundno())) {
wrapper.eq("oid_fundno", req.getOidFundno());
}
if (StringUtils.isNotBlank(req.getUserType())) {
wrapper.eq("user_type", req.getUserType());
}
return wrapper;
}
根据总条数和条件构造器分页查询
@Override
public List<FundFlowVO> getFundFlowList(FundFlowReq req) throws KeynesException {
if (null == req) {
throw new KeynesException(KeynesExceptionEnum.MISSING_PARAMS);
}
//构造分页对象
Page page = new Page(req.getCurrPage(), req.getPageSize());
Wrapper<FundFlow> wrapper = this.buildConditions(req);
List<FundFlowVO> list;
try {
List<FundFlow> fundList = fundFlowMapper.selectPage(page, wrapper);
list = zDozerBeanUtil.convertList(fundList, FundFlowVO.class);
} catch (Exception e) {
//数据查询失败
throw new KeynesException(KeynesExceptionEnum.QUERY_FAILED);
}
return list;
}
}