package com.yeshi.fanli.service.impl.taobao.dataoke;
|
|
import java.math.BigDecimal;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import javax.annotation.Resource;
|
|
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import com.yeshi.fanli.dao.mybatis.taobao.dataoke.DaTaoKeDetailMapper;
|
import com.yeshi.fanli.dto.dataoke.DaTaoKeApiResult;
|
import com.yeshi.fanli.dto.dataoke.DingDongQiangDTO;
|
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
|
import com.yeshi.fanli.entity.taobao.dataoke.DaTaoKeDetail;
|
import com.yeshi.fanli.log.LogHelper;
|
import com.yeshi.fanli.service.inter.taobao.dataoke.DaTaoKeGoodsDetailService;
|
import com.yeshi.fanli.util.RedisManager;
|
import com.yeshi.fanli.util.dataoke.DaTaoKeApiUtil;
|
import com.yeshi.fanli.util.taobao.DaTaoKeUtil;
|
|
@Service
|
public class DaTaoKeGoodsDetailServiceImpl implements DaTaoKeGoodsDetailService {
|
@Resource
|
private DaTaoKeDetailMapper daTaoKeDetailMapper;
|
|
@Resource
|
private RedisManager redisManager;
|
|
@Override
|
public void startSyncGoods() {
|
LogHelper.test("大淘客同步开始");
|
// 判断是否有正在进行的更新
|
long firstTime = System.currentTimeMillis() - 1000 * 60 * 5L;
|
int totalCount = 0;
|
for (int p = 1; p <= 2000; p++) {// 10W数据
|
try {
|
DaTaoKeApiResult result = DaTaoKeApiUtil.goodsList(p);
|
if (result.getDetailList().size() < 50)// 更新完成
|
{
|
break;
|
}
|
addGoodsList(result.getDetailList());
|
totalCount += result.getDetailList().size();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
LogHelper.test("大淘客总共更新数据数量:" + totalCount);
|
// 清除过期数据
|
if (totalCount > 50000)// 保证有5w数据再清除过期
|
{
|
daTaoKeDetailMapper.deleteByMinUpdateTime(firstTime);
|
LogHelper.test("删除大淘客过期数据:" + totalCount);
|
}
|
}
|
|
@Transactional
|
private void addGoodsList(List<DaTaoKeDetail> goodsList) {
|
List<Long> idList = new ArrayList<>();
|
for (DaTaoKeDetail goods : goodsList) {
|
goods.setUpdateTime(new Date());
|
idList.add(goods.getId());
|
}
|
// 批量删除
|
daTaoKeDetailMapper.deleteByIdList(idList);
|
// 批量插入
|
daTaoKeDetailMapper.insertByBatch(goodsList);
|
}
|
|
@Override
|
public List<DaTaoKeDetail> listByGoodsIds(List<Long> goodsIdList) {
|
if (goodsIdList == null || goodsIdList.size() == 0)
|
return null;
|
List<DaTaoKeDetail> goodsList = daTaoKeDetailMapper.listByGoodsIds(goodsIdList);
|
return goodsList;
|
}
|
|
@Override
|
public List<TaoBaoGoodsBrief> filterTaoBaoGoods(List<TaoBaoGoodsBrief> goodsList) {
|
List<Long> goodsIdList = new ArrayList<>();
|
for (TaoBaoGoodsBrief goods : goodsList)
|
goodsIdList.add(goods.getAuctionId());
|
|
List<DaTaoKeDetail> dtList = listByGoodsIds(goodsIdList);
|
|
Map<Long, DaTaoKeDetail> map = new HashMap<>();
|
if (dtList != null)
|
for (DaTaoKeDetail goods : dtList)
|
map.put(goods.getGoodsId(), goods);
|
for (TaoBaoGoodsBrief goods : goodsList) {
|
DaTaoKeDetail detail = map.get(goods.getAuctionId());
|
goods = DaTaoKeUtil.filterTaoBaoGoods(goods, detail);
|
}
|
return goodsList;
|
}
|
|
@Override
|
public TaoBaoGoodsBrief filterTaoBaoGoods(TaoBaoGoodsBrief goods) {
|
List<Long> goodsIdList = new ArrayList<>();
|
goodsIdList.add(goods.getAuctionId());
|
|
List<DaTaoKeDetail> daTaoKeList = daTaoKeDetailMapper.listByGoodsIds(goodsIdList);
|
if (daTaoKeList == null || daTaoKeList.size() == 0)
|
return goods;
|
|
// 重新设置标题与券价格
|
goods = DaTaoKeUtil.filterTaoBaoGoods(goods, daTaoKeList.get(0));
|
|
return goods;
|
}
|
|
@Override
|
public List<DaTaoKeDetail> listByIds(List<Long> idList) {
|
return daTaoKeDetailMapper.listByIds(idList);
|
}
|
|
@Override
|
public List<DaTaoKeDetail> listByDtitle(String dtitle) {
|
|
return daTaoKeDetailMapper.listByDtitle(dtitle.trim());
|
}
|
|
@Override
|
public List<DaTaoKeDetail> listSearchByTitleWithCid(String title, Long cid, int page, int pageSize) {
|
return daTaoKeDetailMapper.listSearchByTitleWithCid(title, (cid == null || cid == 0 ? null : cid),
|
(page - 1) * pageSize, pageSize);
|
}
|
|
@Override
|
public Long countSearchByTitleWithCid(String title, Long cid) {
|
return daTaoKeDetailMapper.countSearchByTitleWithCid(title, (cid == null || cid == 0 ? null : cid));
|
}
|
|
@Override
|
public List<DaTaoKeDetail> getGoodsNotInList(Long cid, List<Long> listId, int count) {
|
return daTaoKeDetailMapper.getGoodsNotInList(cid, listId, count);
|
}
|
|
@Override
|
public List<DaTaoKeDetail> listByCidAndMaxPrice(Integer cid, BigDecimal maxPrice, int page, int pageSize) {
|
return daTaoKeDetailMapper.listByCidAndMaxMinPrice(cid, maxPrice, null, (page - 1) * pageSize, pageSize);
|
}
|
|
@Override
|
public Long countByCidAndMaxPrice(Integer cid, BigDecimal maxPrice) {
|
|
return daTaoKeDetailMapper.countByCidAndMaxMinPrice(cid, maxPrice, null);
|
}
|
|
@Cacheable(value = "daTaoKeGoodsCache", key = "'getDingDongQiangData'")
|
@Override
|
public List<DingDongQiangDTO> getDingDongQiangData() {
|
List<DingDongQiangDTO> list = null;
|
int count = 0;
|
while ((list == null || list.size() == 0) && count < 3) {
|
count++;
|
list = DaTaoKeUtil.getDingDongQiang();
|
}
|
if (list == null)
|
return null;
|
return list;
|
}
|
|
@Cacheable(value = "daTaoKeGoodsCache", key = "'getDingDongQiangData-'+#time")
|
@Override
|
public List<DaTaoKeDetail> getDingDongQiangData(String time) {
|
List<DingDongQiangDTO> list = getDingDongQiangData();
|
if (list == null)
|
return null;
|
Map<String, DingDongQiangDTO> map = new HashMap<>();
|
for (DingDongQiangDTO dto : list)
|
|
map.put(dto.getTime(), dto);
|
|
DingDongQiangDTO dto = map.get(time);
|
if (dto != null) {
|
List<Long> idList = new ArrayList<>();
|
for (DaTaoKeDetail td : dto.getGoodsList())
|
idList.add(td.getId());
|
List<DaTaoKeDetail> finalList = listByIds(idList);
|
for (DaTaoKeDetail detail : finalList) {
|
detail.setQuanReceive(
|
detail.getQuanSurplus() + detail.getQuanReceive() - (int) (200 + Math.random() * 1000));
|
}
|
return finalList;
|
}
|
return null;
|
}
|
|
}
|