package com.yeshi.fanli.job.goods;
|
|
import java.util.Calendar;
|
import java.util.List;
|
|
import javax.annotation.Resource;
|
|
import org.springframework.core.task.TaskExecutor;
|
import org.springframework.stereotype.Component;
|
|
import com.xxl.job.core.biz.model.ReturnT;
|
import com.xxl.job.core.handler.annotation.XxlJob;
|
import com.yeshi.goods.facade.dto.taobao.haodanku.HDKGoodsListResultDTO;
|
import com.yeshi.goods.facade.entity.taobao.haodanku.HDKGoodsDetail;
|
import com.yeshi.fanli.service.inter.goods.PullNewGoodsService;
|
import com.yeshi.goods.facade.service.HDKGoodsDetailService;
|
import com.yeshi.goods.facade.utils.taobao.HaoDanKuApiUtil;
|
|
@Component
|
public class HDKGoodsJob {
|
|
@Resource
|
private HDKGoodsDetailService hdkGoodsDetailService;
|
|
@Resource
|
private PullNewGoodsService pullNewGoodsService;
|
|
@Resource(name = "taskExecutor")
|
private TaskExecutor executor;
|
|
/**
|
* 同步所有商品
|
* @Title: syncAll
|
* @Description:
|
* @param param
|
* @return
|
* @throws Exception
|
* ReturnT<String> 返回类型
|
* @throws
|
*/
|
@XxlJob("goods-taobao-hdk-syncall")
|
public ReturnT<String> syncAll(String param) throws Exception {
|
Integer minId = 1;
|
while (minId != null) {
|
HDKGoodsListResultDTO dto = HaoDanKuApiUtil.getInstance().listGoods(minId, null, 200);
|
if (dto != null) {
|
minId = dto.getMinId();
|
List<HDKGoodsDetail> goodsList = dto.getGoodsList();
|
if (goodsList != null)
|
for (HDKGoodsDetail goods : goodsList) {
|
try {
|
hdkGoodsDetailService.addGoods(goods);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
} else {
|
minId = null;
|
}
|
}
|
|
return ReturnT.SUCCESS;
|
}
|
|
/**
|
* 新增
|
* @Title: syncNewAdd
|
* @Description:
|
* @param param
|
* @return
|
* @throws Exception
|
* ReturnT<String> 返回类型
|
* @throws
|
*/
|
@XxlJob("goods-taobao-hdk-newadd")
|
public ReturnT<String> newAdd(String param) throws Exception {
|
// 更新最近2小时的数据
|
int endHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
|
int startHour = endHour - 2;
|
if (startHour < 0)
|
startHour = 0;
|
|
Integer minId = 1;
|
while (minId != null) {
|
HDKGoodsListResultDTO dto = HaoDanKuApiUtil.getInstance().listAddGoods(minId, startHour, endHour, 200);
|
if (dto != null) {
|
minId = dto.getMinId();
|
List<HDKGoodsDetail> goodsList = dto.getGoodsList();
|
if (goodsList != null)
|
for (HDKGoodsDetail goods : goodsList) {
|
try {
|
hdkGoodsDetailService.addGoods(goods);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
} else {
|
minId = null;
|
}
|
}
|
return ReturnT.SUCCESS;
|
}
|
|
/**
|
* 更新最新的数据
|
* @Title: update
|
* @Description:
|
* @param param
|
* @return
|
* @throws Exception
|
* ReturnT<String> 返回类型
|
* @throws
|
*/
|
@XxlJob("goods-taobao-hdk-update")
|
public ReturnT<String> update(String param) throws Exception {
|
Integer minId = 1;
|
while (minId != null) {
|
HDKGoodsListResultDTO dto = HaoDanKuApiUtil.getInstance().listUpdateGoods(minId, 200);
|
if (dto != null) {
|
minId = dto.getMinId();
|
List<HDKGoodsDetail> goodsList = dto.getGoodsList();
|
if (goodsList != null)
|
for (HDKGoodsDetail goods : goodsList) {
|
try {
|
hdkGoodsDetailService.updateGoods(goods);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
executor.execute(new Runnable() {
|
@Override
|
public void run() {
|
try {
|
pullNewGoodsService.updateGoods(goods);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
});
|
}
|
} else {
|
minId = null;
|
}
|
}
|
return ReturnT.SUCCESS;
|
}
|
|
/**
|
* 下架失效的
|
* @Title: offlineInvalid
|
* @Description:
|
* @param param
|
* @return
|
* @throws Exception
|
* ReturnT<String> 返回类型
|
* @throws
|
*/
|
@XxlJob("goods-taobao-hdk-offline-invalid")
|
public ReturnT<String> offlineInvalid(String param) throws Exception {
|
int endHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
|
int startHour = endHour - 2;
|
if (startHour < 0)
|
startHour = 0;
|
List<Long> ids = HaoDanKuApiUtil.getInstance().listInvalidGoods(startHour, endHour);
|
// 分页删除
|
int pageSize = 100;
|
int page = ids.size() % pageSize == 0 ? ids.size() / pageSize : ids.size() / pageSize + 1;
|
for (int i = 0; i < page; i++) {
|
int start = i * pageSize;
|
List<Long> tempList = ids.subList(start, start + pageSize > ids.size() ? ids.size() : start + pageSize);
|
hdkGoodsDetailService.deleteByItemIds(tempList);
|
}
|
|
executor.execute(new Runnable() {
|
@Override
|
public void run() {
|
try {
|
pullNewGoodsService.deleteGoods(ids);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
});
|
|
return ReturnT.SUCCESS;
|
}
|
|
}
|