package com.yeshi.fanli.service.impl.pdd;
|
|
import java.io.IOException;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import org.jsoup.Jsoup;
|
import org.jsoup.nodes.Document;
|
import org.jsoup.select.Elements;
|
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import com.yeshi.fanli.dto.pdd.PDDGoodsDetail;
|
import com.yeshi.fanli.dto.pdd.PDDGoodsResult;
|
import com.yeshi.fanli.dto.pdd.PDDSearchFilter;
|
import com.yeshi.fanli.entity.bus.clazz.GoodsClass;
|
import com.yeshi.fanli.exception.pdd.PDDOrderException;
|
import com.yeshi.fanli.service.inter.pdd.PDDGoodsService;
|
import com.yeshi.fanli.util.Constant;
|
import com.yeshi.fanli.util.StringUtil;
|
import com.yeshi.fanli.util.pinduoduo.PinDuoDuoApiUtil;
|
import com.yeshi.fanli.util.pinduoduo.PinDuoDuoUtil;
|
|
@Service
|
public class PDDGoodsServiceImpl implements PDDGoodsService {
|
|
private static final Map<Long, String> cidMap = new HashMap<Long, String>();
|
|
static {
|
cidMap.put(1L, "0");// 精选
|
cidMap.put(2L, "-11");// 清仓
|
cidMap.put(3L, "15");// 百货
|
cidMap.put(4L, "4");// 母婴
|
cidMap.put(5L, "1");// 食品
|
cidMap.put(6L, "14");// 女装
|
cidMap.put(7L, "18");// 电器
|
cidMap.put(8L, "1281");// 鞋包
|
cidMap.put(9L, "1282");// 内衣
|
cidMap.put(10L, "16");// 美妆
|
cidMap.put(11L, "743");// 男装
|
cidMap.put(12L, "2048");// 汽车
|
cidMap.put(13L, "13");// 水果
|
cidMap.put(14L, "818,1917,2974");// 家居:家纺、家装 家具
|
cidMap.put(15L, "2478");// 文具
|
cidMap.put(16L, "1451");// 运动
|
cidMap.put(17L, "590");// 虚拟
|
cidMap.put(18L, "3279");// 医药
|
}
|
|
@Cacheable(value = "pddCache", key = "'getDetailImageList'+#id")
|
@Override
|
public List<String> getDetailImageList(Long id) {
|
List<String> list = null;
|
int count = 0;
|
// 重试3次
|
while ((list == null || list.size() == 0) && count < 3) {
|
list = PinDuoDuoUtil.getDetailImages(id);
|
count++;
|
}
|
if (list == null || list.size() == 0) {
|
try {
|
Document doc = Jsoup
|
.connect("https://dk.gaoyong666.com/gylm/h5details/v1/details?classtype=1&goodsId=" + id)
|
.userAgent(
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36")
|
.get();
|
Elements els = doc.getElementsByTag("img");
|
for (int i = 0; i < els.size(); i++) {
|
String src = els.get(i).attr("src");
|
if (src.contains("yangkeduo")) {
|
list.add(src);
|
}
|
}
|
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
}
|
|
return list;
|
}
|
|
@Override
|
public List<GoodsClass> getSpecialClass() {
|
List<GoodsClass> list = new ArrayList<GoodsClass>();
|
list.add(new GoodsClass(1L, "精选"));
|
list.add(new GoodsClass(2L, "清仓"));
|
list.add(new GoodsClass(3L, "百货"));
|
list.add(new GoodsClass(4L, "母婴"));
|
list.add(new GoodsClass(5L, "食品"));
|
list.add(new GoodsClass(6L, "女装"));
|
list.add(new GoodsClass(7L, "电器"));
|
list.add(new GoodsClass(8L, "鞋包"));
|
list.add(new GoodsClass(9L, "内衣"));
|
list.add(new GoodsClass(10L, "美妆"));
|
list.add(new GoodsClass(11L, "男装"));
|
list.add(new GoodsClass(12L, "汽车"));
|
list.add(new GoodsClass(13L, "水果"));
|
list.add(new GoodsClass(14L, "家居"));
|
list.add(new GoodsClass(15L, "文具"));
|
list.add(new GoodsClass(16L, "运动"));
|
list.add(new GoodsClass(17L, "虚拟"));
|
list.add(new GoodsClass(18L, "医药"));
|
return list;
|
}
|
|
@Override
|
@Transactional
|
@Cacheable(value = "pddSpecialCache", key = "'specialSearch-'+#page+'-'+#cid")
|
public List<PDDGoodsDetail> specialSearch(Integer page, Long cid) throws PDDOrderException {
|
if (cid == null) {
|
throw new PDDOrderException(1, "分类id为空");
|
}
|
|
String pddcid = cidMap.get(cid);
|
if (StringUtil.isNullOrEmpty(pddcid)) {
|
throw new PDDOrderException(1, "分类id不存在");
|
}
|
|
PDDSearchFilter pddfilter = new PDDSearchFilter();
|
|
// 精选
|
if (cid == 1) {
|
pddfilter.setPage(page);
|
pddfilter.setPageSize(Constant.PAGE_SIZE);
|
PDDGoodsResult result = PinDuoDuoApiUtil.searchGoods(pddfilter);
|
if (result == null) {
|
return null;
|
} else {
|
return result.getGoodsList();
|
}
|
}
|
|
// 单个分类
|
if (!pddcid.contains(",")) {
|
pddfilter.setPage(page);
|
pddfilter.setPageSize(Constant.PAGE_SIZE);
|
pddfilter.setOptId(Long.parseLong(pddcid));
|
PDDGoodsResult result = PinDuoDuoApiUtil.searchGoods(pddfilter);
|
if (result == null) {
|
return null;
|
} else {
|
return result.getGoodsList();
|
}
|
}
|
|
// 多个分类id处理
|
List<PDDGoodsDetail> list = new ArrayList<PDDGoodsDetail>();
|
String[] arrayId = pddcid.split(",");
|
for (int i = 0; i < arrayId.length; i++) {
|
if (i >= 3) {
|
break;
|
}
|
pddfilter.setPage(page);
|
pddfilter.setPageSize(10);
|
pddfilter.setOptId(Long.parseLong(arrayId[i]));
|
PDDGoodsResult result = PinDuoDuoApiUtil.searchGoods(pddfilter);
|
if (result != null) {
|
List<PDDGoodsDetail> listGoods = result.getGoodsList();
|
if (listGoods != null && listGoods.size() > 0) {
|
list.addAll(listGoods);
|
}
|
}
|
}
|
return list;
|
}
|
|
@Override
|
@Cacheable(value = "pddSpecialCache", key = "'getTopGoodsList-' + #page + '-' + #sortType")
|
public PDDGoodsResult getTopGoodsList(int page, Integer sortType) {
|
return PinDuoDuoApiUtil.getTopList(PinDuoDuoApiUtil.PID_FANLI, page, Constant.PAGE_SIZE, sortType);
|
}
|
|
}
|