首页 > 其他分享 >分页批量请求数据封装

分页批量请求数据封装

时间:2023-08-01 12:33:55浏览次数:35  
标签:return 分页 批量 param protected import 封装 null public


关于淘宝接口每次最多取100个,需要分页获取的封装

总体思路采取迭代器的方式来多次发送请求.

 

TOPCollection<TOPArticleOrder> orders = session.getArticleOrdersByNick(articleCode, start, end,nick);
size = saveOrders(orders.toList());

 

最终调用TOPCollection中的toList()方法,采用迭代器的方式分页获取数据

迭代器的实现为 TOPCollectionIterator 调用hasnext方法,实际调用writer.hasNext()

write的实现类 CollectionWriter 最终通过 session.execute 来执行分页发送请求的处理.

 

----------------------

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.bstek.common.bean.PropertiesMapping;
import com.bstek.common.lang.AssertUtils;
import com.bstek.common.lang.convert.ConverterUtils;
import com.hupun.crmbatch.service.top.TOPSession;
import com.hupun.crmbatch.service.top.bean.article.TOPArticleOrder;
import com.hupun.crmbatch.service.top.client.AbstractTOPSession;
import com.hupun.crmbatch.service.top.exception.TOPException;
import com.hupun.crmbatch.service.top.exception.TOPExceptionConstants;
import com.taobao.api.TaobaoRequest;
import com.taobao.api.domain.ArticleBizOrder;
import com.taobao.api.request.VasOrderSearchRequest;
import com.taobao.api.response.VasOrderSearchResponse;
 
/**
 * TOP 应用订购订单获取器
 */
public class TOPArticleOrdersGetter extends TOPCollectionHandler<ArticleBizOrder, TOPArticleOrder, VasOrderSearchResponse> {
 
private String code;
 
private Date start;
private Date end;
 
protected int pageNo;
protected int pageSize;
/**
* 订购用户昵称
*/
private String nick;
 
 
/**
* 构造函数
* @param session TOP 会话
*/
public TOPArticleOrdersGetter(AbstractTOPSession session) {
super(session);
 
pageNo = 1;
pageSize = 200;
}
 
public String getCode() {
return code;
}
 
public Date getEnd() {
return end;
}
 
public String getRequesInfo() {
return TOPHandleMessages.top_article_orders_getter.format(code, start, end, pageNo);
}
 
public TaobaoRequest<VasOrderSearchResponse> getRequest() throws TOPException {
VasOrderSearchRequest req = new VasOrderSearchRequest();
if (AssertUtils.UNFORCE.empty(code))
throw new TOPException(TOPExceptionConstants.Exp_param_missing, TOPHandleMessages.param_article_code);
req.setArticleCode(code);
 
if (start != null && end != null && start.before(end)) {
req.setStartCreated(start);
req.setEndCreated(end);
}
 
if (!StringUtils.isEmpty(nick)){
req.setNick(nick);
}
 
req.setPageNo(Long.valueOf(pageNo));
req.setPageSize(Long.valueOf(pageSize));
return req;
}
 
public Date getStart() {
return start;
}
 
public boolean isPrivate() {
return true;
}
 
public TOPException parseError(String code, String msg, TOPSession session) {
return null;
}
 
public void setCode(String code) {
this.code = code;
}
 
public void setEnd(Date end) {
this.end = end;
}
 
public void setStart(Date start) {
this.start = start;
}
 
protected Long collectTotal(VasOrderSearchResponse response) {
return response.getTotalItem();
}
 
protected TOPArticleOrder createData() {
return new TOPArticleOrder();
}
 
protected PropertiesMapping createMapping() {
PropertiesMapping mapping = new PropertiesMapping(10);
mapping.append("articleCode", "articleCode");
mapping.append("orderId", "orderID");
mapping.append("articleName", "name");
mapping.append("create", "created");
mapping.append("fee", "fee");
mapping.append("itemCode", "itemCode");
mapping.append("orderCycleStart", "cycleStart");
mapping.append("orderCycleEnd", "cycleEnd");
mapping.append("nick", "nick");
mapping.append("totalPayFee", "payment");
return mapping;
}
 
protected TOPArticleOrder doTransform(ArticleBizOrder data) {
return getTransformer().transform(data);
}
 
protected List<ArticleBizOrder> getList(VasOrderSearchResponse response) {
return response.getArticleBizOrders();
}
 
protected boolean hasNext() {
return (getTotal() + (pageSize - 1)) / pageSize > pageNo;
}
 
protected void moveNext() {
pageNo++;
}
 
protected Object transform(String source, String target, Object value) throws RuntimeException {
if ("fee".equalsIgnoreCase(source)) {
return ConverterUtils.NORMAL.convert(value, BigDecimal.class).divide(BigDecimal.valueOf(100));
} else if ("totalPayFee".equalsIgnoreCase(source)) {
return ConverterUtils.NORMAL.convert(value, BigDecimal.class).divide(BigDecimal.valueOf(100));
}
return super.transform(source, target, value);
}
 
public String getNick() {
return nick;
}
 
public void setNick(String nick) {
this.nick = nick;
}
 
}

 

-----------------------------------------

 

import java.util.Collection;
import java.util.Iterator;
 
import com.bstek.common.bean.PropertiesMapping;
import com.bstek.common.lang.proxy.ProxyUtils;
import com.hupun.crmbatch.service.top.bean.TOPCollection;
import com.hupun.crmbatch.service.top.bean.TOPCollection.TOPCollectionWriter;
import com.hupun.crmbatch.service.top.client.AbstractTOPSession;
import com.taobao.api.TaobaoResponse;
 
/**
 * TOP 平台集合数据调用处理器
 */
public abstract class TOPCollectionHandler<S, R, T extends TaobaoResponse> implements TOPHandler<TOPCollection<R>, T> {
 
/**
* 集合元素转换器
*/
protected class CollectionElementTransformer extends TOPDataTransformer<S, R> {
 
public Object transform(String source, String target, Object value) throws RuntimeException {
return TOPCollectionHandler.this.transform(source, target, value);
}
 
protected R createData() {
return TOPCollectionHandler.this.createData();
}
 
protected PropertiesMapping createMapping() {
return TOPCollectionHandler.this.createMapping();
}
}
 
/**
* 集合写入器
*/
protected class CollectionWriter extends TOPCollectionWriter<R> {
 
public void setTotal(int size) {
super.setTotal(size);
TOPCollectionHandler.this.setTotal(size);
}
 
protected boolean hasNext() {
return TOPCollectionHandler.this.hasNext();
}
 
protected void nextBatch() {
moveNext();
for (int i = 0;;) {
try {
session.execute(TOPCollectionHandler.this, isLog());
break;
} catch (Exception e) {
if (i++ >= 5) throw ProxyUtils.wrapThrowable(e, RuntimeException.class);
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// ignore
}
}
}
}
 
protected final AbstractTOPSession session;
private int total;
private CollectionWriter writer;
 
private TOPDataTransformer<S, R> transformer;
 
/**
* 构造函数
* @param session TOP 会话
*/
protected TOPCollectionHandler(AbstractTOPSession session) {
this.session = session;
}
 
public TOPCollection<R> getResult(T response) {
if (writer == null) writer = new CollectionWriter();
Long total = collectTotal(response);
if (total != null) writer.setTotal(total.intValue());
Collection<? extends S> list = getList(response);
if (list != null) doTransform(list, writer);
return writer.getCollection();
}
 
/**
* 提取总数量 请求不返回总数量,返回 <code>null</code>
* @param response 请求返回结果
* @return 总数量
*/
protected abstract Long collectTotal(T response);
 
/**
* 创建元素对象
* @return 元素对象实例
*/
protected R createData() {
return null;
}
 
/**
* 创建属性映射关系
* @return 属性映射关系
*/
protected PropertiesMapping createMapping() {
return null;
}
 
/**
* 执行列表数据转换
* @param list 源数据集
* @param writer 集合写入器
*/
protected void doTransform(Collection<? extends S> list, CollectionWriter writer) {
for (Iterator<? extends S> it = list.iterator(); it.hasNext(); it.remove()) {
writer.add(doTransform(it.next()));
}
}
 
/**
* 执行数据转换
* @param data 源数据
* @return 结果数据
*/
protected abstract R doTransform(S data);
 
/**
* 获取结果数据列表
* @param response 请求返回结果
* @return 数据列表
*/
protected abstract Collection<? extends S> getList(T response);
 
/**
* 获取总数量
* @return 总数量
*/
protected int getTotal() {
return total;
}
 
/**
* 获取数据转换器
* @return 转换器
*/
protected TOPDataTransformer<S, R> getTransformer() {
if (transformer == null) transformer = new CollectionElementTransformer();
return transformer;
}
 
/**
* 是否存在下一批
* @return 是、否
*/
protected abstract boolean hasNext();
 
/**
* 是否记录日志
* @return 是、否
*/
protected boolean isLog() {
return true;
}
 
/**
* 移动条件至下一批
*/
protected abstract void moveNext();
 
/**
* 设置总数量
* @param size 总数量
*/
protected void setTotal(int size) {
this.total = size;
}
 
/**
* 转换属性值
* @param source 源属性名
* @param target 目标属性名
* @param value 属性值
* @return 目标值
* @throws RuntimeException
*/
protected Object transform(String source, String target, Object value) throws RuntimeException {
return value;
}
}

 

---------------------------------

 

package com.hupun.crmbatch.service.top.handlers;
 
import com.hupun.crmbatch.service.top.exception.TOPException;
import com.taobao.api.TaobaoRequest;
import com.taobao.api.TaobaoResponse;
 
/**
 * TOP 平台调用处理器
 */
public interface TOPHandler<R, S extends TaobaoResponse> {
 
/**
* 获取请求描述信息
* @return 描述信息
*/
public String getRequesInfo();
 
/**
* 获取请求信息
* @return TOP 请求
* @throws TOPException
*/
public TaobaoRequest<S> getRequest() throws TOPException;
 
/**
* 获取处理结果
* @param response TOP 返回结果
* @return 处理结果
*/
public R getResult(S response);
}
 
---------------------------------------
 
 
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
 
/**
 * TOP 数据集合体
 */
public class TOPCollection<E> implements Serializable, Iterable<E> {
 
/**
* TOP 数据集合体写入器
*/
public static abstract class TOPCollectionWriter<E> {
 
private TOPCollection<E> collection;
 
/**
* 构造函数
*/
protected TOPCollectionWriter() {
}
 
/**
* 添加 TOP 数据
* @param item TOP 数据
*/
public void add(E item) {
if (collection == null) collection = new TOPCollection(this, 10, false);
collection.add(item);
}
 
/**
* 获取数据集合体
* @return 集合体
*/
public TOPCollection<E> getCollection() {
if (collection == null) collection = new TOPCollection(this, 10, false);
return collection;
}
 
/**
* 设置总数量
* @param size 总数量
*/
public void setTotal(int size) {
if (collection == null) collection = new TOPCollection(this, size, true);
}
 
/**
* 是否有下一批
* @return 是、否
*/
protected abstract boolean hasNext();
 
/**
* 获取下一批数据
*/
protected abstract void nextBatch();
}
 
/**
* TOP 数据集合体迭代器
*/
protected class TOPCollectionIterator implements Iterator<E> {
 
private Entry current;
private long last;
 
protected TOPCollectionIterator() {
current = head;
last = System.currentTimeMillis();
}
 
@Override
public boolean hasNext() {
if (current.next == head) {
synchronized (head) {
if (current.next == head && writer.hasNext()) {
long l = System.currentTimeMillis();
if (l == last) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
}
writer.nextBatch();
last = System.currentTimeMillis();
return current.next != head;
} else {
return false;
}
}
} else if (current.next == null) {
return false;
}
return true;
}
 
@Override
public E next() {
current = current.next;
return current == null ? null : current.value;
}
 
@Override
public void remove() {
if (current == head) return;
Entry tmp = current.previous;
current.remove();
current = tmp;
}
}
 
class Entry implements Serializable {
private static final long serialVersionUID = 57961385403762668L;
 
private Entry next;
private Entry previous;
private final E value;
 
/**
* 构造函数
* @param value 元素值
*/
protected Entry(E value) {
this.value = value;
}
 
/**
* 移除当前元素
*/
public void remove() {
if (this == head) throw new NoSuchElementException();
synchronized (head) {
if (previous != null) previous.next = next;
if (next != null) next.previous = previous;
previous = null;
next = null;
size--;
}
}
 
/**
* 设置上一个元素项
* @param ele 元素项
* @param append 是否增加
*/
public void setPrevious(Entry ele, boolean append) {
if (ele == null) return;
synchronized (head) {
if (previous != null) previous.next = ele;
ele.previous = previous;
ele.next = this;
previous = ele;
if (append) size++;
}
}
}
 
private static final long serialVersionUID = -6163119051025582037L;
 
private transient int size;
private transient Entry head;
 
private final TOPCollectionWriter writer;
private final boolean isTotal;
protected int total;
 
/**
* 构造函数
*/
protected TOPCollection() {
this(null, 0, true);
}
 
/**
* 构造函数
* @param writer 集合体写入器
* @param initSize 初始数量
* @param isTotal 是否总计
*/
protected TOPCollection(TOPCollectionWriter<E> writer, int initSize, boolean isTotal) {
size = 0;
head = new Entry(null);
head.setPrevious(head, false);
 
this.isTotal = isTotal;
total = initSize;
this.writer = writer;
}
 
/**
* 首个元素
* @return 元素值 集合为空返回 <code>null</code>
*/
public E first() {
Iterator<E> it = iterator();
return it.hasNext() ? null : it.next();
}
 
/**
* 导入到目标列表中
* @param list 目标列表
*/
public void importTo(Collection<E> list) {
for (Iterator<E> it = iterator(); it.hasNext();) {
list.add(it.next());
}
}
 
/**
* 是否为空
* @return 是、否
*/
public boolean isEmpty() {
return !iterator().hasNext();
}
 
@Override
public Iterator<E> iterator() {
return new TOPCollectionIterator();
}
 
/**
* 获取数量
* @return 数量
*/
public int size() {
if (isTotal) {
return total;
} else {
throw new UnsupportedOperationException();
}
}
 
/**
* 转换为列表
* @return 列表
*/
public List<E> toList() {
List<E> list = new ArrayList(size);
importTo(list);
return list;
}
 
/**
* 添加元素
* @param o 元素
*/
protected void add(E o) {
Entry ele = new Entry(o);
head.setPrevious(ele, true);
}
 
/**
* 序列化读取
* @param s 序列化输入流
* @throws java.io.IOException
* @throws ClassNotFoundException
*/
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
 
int size = s.readInt();
 
head = new Entry(null);
head.setPrevious(head, false);
 
for (int i = 0; i < size; i++)
head.setPrevious(new Entry((E) s.readObject()), true);
}
 
/**
* 序列化写入
* @param s 序列化输出流
* @throws java.io.IOException
*/
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
s.defaultWriteObject();
 
s.writeInt(size);
 
for (Entry e = head.next; e != head; e = e.next)
s.writeObject(e.value);
}
}

标签:return,分页,批量,param,protected,import,封装,null,public
From: https://blog.51cto.com/u_16161240/6919574

相关文章

  • mybatis-plus数据批量插入
       为了提高数据处理效率,大量数据需要插入数据时可以采用批量数据插入的策略提高数据插入的效率。如下是实现方法1、代码结构 2、实体类packagelittle.tiger.one.application.testblockt.domain;importcom.baomidou.mybatisplus.annotation.TableId;importcom.ba......
  • 手动封装call
     目录前言导语代码封装部分验证总结前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从头再来歌谣的意志是永恒的放弃很容易但是坚持一定很酷导语歌谣歌谣......
  • android网络通讯数据封装之 json
    Demo程序包括客户端和服务端客户端按json数据格式封装数据传至服务端。服务端为简单的servlet程序,负责接收客户端传到json数据,然后按原数据返回客户端.实例代码如下:publicstaticStringcmdLogIn(){StringurlString="http://192.168.8.75:89/webroot/jsontest";HttpPo......
  • 【11.0】DRF之过滤排序分页
    【准备数据】模型fromdjango.dbimportmodels#Createyourmodelshere.classBook(models.Model):name=models.CharField(max_length=32)price=models.IntegerField()序列化类#-*-coding:Utf-8-*-#@File:book_serializer.py#author:Chi......
  • Mybatis中的Mapper.xml映射文件sql查询接收多个参数 分页查询 sql语句动态生成的时候,
    Mybatis中的Mapper.xml映射文件sql查询接收多个参数分页查询sql语句动态生成的时候,使用${},sql语句中某个参数进行占位的时候用#{}原文链接:https://blog.csdn.net/Ep_Little_prince/article/details/100687848​我们都知道,在Mybatis中的Mapper.xml映射文件可以定制动态SQ......
  • 批量导入书签pdf
    FreePic2Pdf快速为pdf文档添加带有超链接的书签目录ZhanweiLiu于2020-05-0613:53:40发布3137收藏14分类专栏:书签PDF版权书签同时被2个专栏收录1篇文章0订阅订阅专栏PDF1篇文章0订阅订阅专栏FreePic2Pdf快速为pdf文档添加带有超链接的书签目录下载FreePic2Pdf提......
  • python + 批量提取文件中exce数据
    importpandasaspdimportos#创建一个空的数据框列表data_frames=[]#获取当前脚本的目录路径current_directory=os.getcwd()folder_path=os.path.join(current_directory,'excel')#相对路径:excel文件夹所在的位置#遍历文件夹中的所有Excel文件forfile......
  • 基于面向对象的数据封装
    基于面向对象的数据封装目录基于面向对象的数据封装1基于面向对象的数据封装优点2示例1基于面向对象的数据封装优点将属性和方法放到一起做为一个整体,然后通过实例化对象来处理;隐藏内部实现细节,只需要和对象及其属性和方法交互就可以了;对类的属性和方法增加访问权限控制......
  • 【个人模板封装】树套树、高维数据结构
    前言这是我个人使用的一些模板封装,限于个人能力,可能存在诸多不足与漏洞,在未加测试直接使用前请务必小心谨慎。更新可能会滞后于我本地的文档,如有疑问或者催更之类的可以在评论区留言。全文模板测试均基于以下版本信息,请留意版本兼容问题。Windows,64bitG++(ISOC++20)stack......
  • Vue3中使用TypeScript封装axios遇到的问题(AxiosRequestConfig)
    如果您有更好的解决方法,欢迎评论区评论。版本"dependencies":{"axios":"^1.0.0","vant":"^4.6.3","vue":"^3.3.4","vue-router":"^4.2.4","vuex":&quo......