package com.yeshi.fanli.service.impl.lable;
|
|
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 javax.transaction.Transactional;
|
|
import org.springframework.stereotype.Service;
|
|
import com.yeshi.fanli.dao.mybatis.TaoBaoClassRelationMapper;
|
import com.yeshi.fanli.dao.mybatis.lable.LabelGoodsMapper;
|
import com.yeshi.fanli.dao.mybatis.lable.QualityFactoryMapper;
|
import com.yeshi.fanli.dao.mybatis.lable.QualityFlashSaleMapper;
|
import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoGoodsBriefMapper;
|
import com.yeshi.fanli.entity.bus.clazz.TaoBaoClassRelation;
|
import com.yeshi.fanli.entity.bus.lable.BoutiqueAutoRule;
|
import com.yeshi.fanli.entity.bus.lable.Label;
|
import com.yeshi.fanli.entity.bus.lable.LabelGoods;
|
import com.yeshi.fanli.entity.bus.lable.QualityFactory;
|
import com.yeshi.fanli.entity.bus.lable.QualityFlashSale;
|
import com.yeshi.fanli.entity.common.AdminUser;
|
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
|
import com.yeshi.fanli.exception.QualityFactoryException;
|
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
|
import com.yeshi.fanli.exception.taobao.TaobaoGoodsUpdateException;
|
import com.yeshi.fanli.log.LogHelper;
|
import com.yeshi.fanli.service.inter.goods.TaoBaoGoodsBriefService;
|
import com.yeshi.fanli.service.inter.lable.LabelService;
|
import com.yeshi.fanli.service.inter.lable.QualityFactoryService;
|
import com.yeshi.fanli.service.inter.lable.QualityFlashSaleService;
|
import com.yeshi.fanli.service.inter.taobao.TaoBaoGoodsUpdateService;
|
import com.yeshi.fanli.service.inter.taobao.TaoBaoUnionConfigService;
|
import com.yeshi.fanli.util.Constant;
|
import com.yeshi.fanli.util.RedisManager;
|
import com.yeshi.fanli.util.StringUtil;
|
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
|
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
|
import com.yeshi.fanli.vo.quality.QualityFactoryVO;
|
|
@Service
|
public class QualityFactoryServiceImpl implements QualityFactoryService {
|
|
@Resource
|
private RedisManager redisManager;
|
@Resource
|
private QualityFactoryMapper qualityFactoryMapper;
|
@Resource
|
private TaoBaoGoodsBriefService taoBaoGoodsBriefService;
|
@Resource
|
private LabelGoodsMapper labelGoodsMapper;
|
@Resource
|
private LabelService labelService;
|
@Resource
|
private TaoBaoGoodsBriefMapper taoBaoGoodsBriefMapper;
|
@Resource
|
private TaoBaoUnionConfigService taoBaoUnionConfigService;
|
@Resource
|
private TaoBaoClassRelationMapper taoBaoClassRelationMapper;
|
@Resource
|
private QualityFlashSaleService qualityFlashSaleService;
|
@Resource
|
private QualityFlashSaleMapper qualityFlashSaleMapper;
|
|
@Resource
|
private TaoBaoGoodsUpdateService taoBaoGoodsUpdateService;
|
|
@Override
|
public int updateByPrimaryKeySelective(QualityFactory record) throws QualityFactoryException {
|
return qualityFactoryMapper.updateByPrimaryKeySelective(record);
|
}
|
|
@Override
|
public void deleteByTbAuctionId(Long auctionId) {
|
|
if (auctionId == null) {
|
return;
|
}
|
|
List<TaoBaoGoodsBrief> tbgoods = taoBaoGoodsBriefMapper.queryByAuctionId(auctionId);
|
if (tbgoods != null && tbgoods.size() > 0) {
|
TaoBaoGoodsBrief taoBaoGoodsBrief = tbgoods.get(0);
|
try {
|
deleteByTaoBaoGoodsId(taoBaoGoodsBrief.getId());
|
} catch (QualityFactoryException e) {
|
e.printStackTrace();
|
}
|
}
|
|
}
|
|
@Override
|
public void deleteByTaoBaoGoodsId(Long gid) throws QualityFactoryException {
|
|
if (gid == null) {
|
return;
|
}
|
|
List<QualityFactory> list = qualityFactoryMapper.queryByGoodsId(gid);
|
if (list != null && list.size() > 0) {
|
|
// 删除限时抢购
|
for (QualityFactory qualityFactory : list) {
|
qualityFlashSaleMapper.deleteByQualityID(qualityFactory.getId());
|
}
|
|
// 移除精选库
|
qualityFactoryMapper.deleteByGoodsId(gid);
|
}
|
|
// 删除对应标签
|
labelGoodsMapper.deleteByGoodsId(gid);
|
}
|
|
@Override
|
public void deleteBatchByTaoBaoGoodsId(List<Long> listGid) throws QualityFactoryException {
|
|
if (listGid == null || listGid.size() == 0) {
|
return;
|
}
|
|
List<QualityFactory> list = qualityFactoryMapper.listQueryGoodsByGoodsId(listGid);
|
|
if (list != null && list.size() > 0) {
|
|
List<Long> listFid = new ArrayList<Long>();
|
for (QualityFactory qualityFactory : list) {
|
listFid.add(qualityFactory.getId());
|
}
|
|
if (listFid.size() > 0) {
|
// 批量删除精选库
|
qualityFactoryMapper.deleteBatchByPrimaryKey(listFid);
|
// 批量删除限时抢购
|
qualityFlashSaleMapper.deleteBatchByQualityID(listFid);
|
}
|
}
|
|
// 删除对应标签
|
labelGoodsMapper.deleteBatchByGoodsId(listGid);
|
}
|
|
@Override
|
@Transactional
|
public void deleteByGoodsId(List<String> ids) throws QualityFactoryException {
|
|
if (ids != null && ids.size() > 0) {
|
for (String goodsId : ids) {
|
qualityFactoryMapper.deleteByGoodsId(Long.parseLong(goodsId));
|
}
|
}
|
}
|
|
@Override
|
public QualityFactory selectByPrimaryKey(Long id) throws QualityFactoryException {
|
return qualityFactoryMapper.selectByPrimaryKey(id);
|
}
|
|
@Override
|
public List<Long> getAuctionIdbyClassId(Long systemCid, Integer goodsSource, String dateTime)
|
throws QualityFactoryException {
|
return qualityFactoryMapper.getAuctionIdbyClassId(systemCid, goodsSource, dateTime);
|
}
|
|
@Override
|
public List<QualityFactory> queryAll(long start, int count, Integer days) {
|
return qualityFactoryMapper.queryAll(start, count, days);
|
}
|
|
@Override
|
public List<QualityFactory> query(QualityFactoryVO qualityFactoryVO) throws QualityFactoryException {
|
return qualityFactoryMapper.query(qualityFactoryVO);
|
}
|
|
@Override
|
public long queryCount(QualityFactoryVO qualityFactoryVO) throws QualityFactoryException {
|
return qualityFactoryMapper.queryCount(qualityFactoryVO);
|
}
|
|
@Override
|
public Map<String, Object> getCountAll() {
|
return qualityFactoryMapper.getCountAll();
|
}
|
|
@Override
|
public Long queryCountByGoodsId(Long goodsId) {
|
return qualityFactoryMapper.queryCountByGoodsId(goodsId);
|
}
|
|
@Override
|
public List<QualityFactory> listQueryByAuctionId(List<Long> list) {
|
return qualityFactoryMapper.listQueryByAuctionId(list);
|
}
|
|
@Override
|
@Transactional
|
public void addBatchGoodsByTaoBaoId(List<Long> listTaoBaoId, String lableNames, AdminUser admin)
|
throws Exception {
|
|
List<TaoBaoGoodsBrief> listAddGoods = new ArrayList<TaoBaoGoodsBrief>();
|
if (listTaoBaoId != null && listTaoBaoId.size() > 0) {
|
for (Long auctionId: listTaoBaoId) {
|
/* 根据auctionId 获取淘宝商品 */
|
TaoBaoGoodsBrief goodsBrief = TaoKeApiUtil.searchGoodsDetail(auctionId);
|
if (goodsBrief != null) {
|
listAddGoods.add(goodsBrief);
|
}
|
}
|
}
|
}
|
|
|
@Override
|
@Transactional
|
public void addBatchTaoBaoGoods(List<TaoBaoGoodsBrief> listTaoBaoGoods, String lableNames, AdminUser admin) throws Exception {
|
|
if (listTaoBaoGoods == null || listTaoBaoGoods.size() == 0) {
|
throw new Exception("未找到匹配的商品");
|
}
|
|
// 创建标签
|
List<Label> listLabel = labelService.addBatchByNames(lableNames, admin);
|
|
List<QualityFactory> listQuality = new ArrayList<QualityFactory>();
|
List<LabelGoods> listLabelGoods = new ArrayList<LabelGoods>();
|
|
for (TaoBaoGoodsBrief goodsBrief: listTaoBaoGoods) {
|
|
// 本地是否已存在此商品
|
List<TaoBaoGoodsBrief> hasList = taoBaoGoodsBriefService.queryByAuctionId(goodsBrief.getAuctionId());
|
|
if (hasList != null && hasList.size() > 0) {
|
TaoBaoGoodsBrief currentGoodsBrief = hasList.get(0);
|
goodsBrief.setId(currentGoodsBrief.getId());
|
|
// 更新商品最新信息
|
taoBaoGoodsUpdateService.updateTaoBaoGoods(goodsBrief);
|
} else {
|
goodsBrief.setId(null);
|
goodsBrief.setState(0);
|
goodsBrief.setCreatetime(new Date());
|
goodsBrief.setUpdatetime(new Date());
|
// 非空项 设置默认值
|
setTaoBaoGoodsBriefDefault(goodsBrief);
|
// 保存至本地
|
taoBaoGoodsBriefService.insertSelective(goodsBrief);
|
}
|
|
// 是否已入库
|
List<QualityFactory> selectionList = qualityFactoryMapper.queryByGoodsId(goodsBrief.getId());
|
|
if (selectionList == null || selectionList.size() == 0) {
|
// 添加至精选商品库
|
QualityFactory selectionGoods = new QualityFactory();
|
|
selectionGoods.setCreateUser(admin);
|
selectionGoods.setCreatetime(new Date());
|
selectionGoods.setUpdateUser(admin);
|
selectionGoods.setUpdatetime(new Date());
|
|
selectionGoods.setWeight(50);// 默认50
|
selectionGoods.setTaoBaoGoodsBrief(goodsBrief);
|
selectionGoods.setEntryMode(QualityFactory.MODE_MANUAL);// 人工筛选
|
selectionGoods.setGoodsSource(QualityFactory.SOURCE_TAOBAO);// 来源淘宝
|
|
listQuality.add(selectionGoods);
|
} else {
|
// 更新商品
|
QualityFactory selectionGoods = selectionList.get(0);
|
selectionGoods.setUpdatetime(new Date());
|
qualityFactoryMapper.updateByPrimaryKeySelective(selectionGoods);
|
}
|
|
if (listLabel != null && listLabel.size() > 0) {
|
for (Label label : listLabel) {
|
/* 贴上标签 : 已贴不处理则 */
|
Long existence = labelGoodsMapper.isExistence(goodsBrief.getId(), label.getId());
|
|
// 该商品不存在此标签则添加, 反之不做处理
|
if (existence == null || existence == 0l) {
|
// 贴上新标签
|
LabelGoods lg = new LabelGoods();
|
lg.setLabel(label);
|
lg.setCreateUser(admin);
|
lg.setCreatetime(new Date());
|
lg.setTaoBaoGoodsBrief(goodsBrief);
|
listLabelGoods.add(lg);
|
}
|
}
|
}
|
|
}
|
|
if (listQuality.size() > 0) {
|
qualityFactoryMapper.insertBatch(listQuality);
|
}
|
|
if (listLabelGoods.size() > 0) {
|
labelGoodsMapper.insertBatch(listLabelGoods);
|
}
|
|
}
|
|
|
@Override
|
@Transactional
|
public void setWeightBatch(List<Long> idList, AdminUser admin, Integer weight, Integer weightSmall,
|
Integer weightLarge) throws Exception {
|
|
for (Long selectId : idList) {
|
|
QualityFactory selectionGoods = qualityFactoryMapper.selectByPrimaryKey(selectId);
|
|
if (selectionGoods == null) {
|
continue;
|
}
|
// 默认 50
|
int resultWeight = 50;
|
|
if (weight != null) {
|
resultWeight = weight;
|
} else {
|
if (weightSmall != null && weightLarge != null) {
|
// 随机权重
|
resultWeight = weightSmall + (int) (Math.random() * ((weightLarge - weightSmall) + 1));
|
}
|
}
|
|
// 修改权重
|
selectionGoods.setUpdateUser(admin);
|
selectionGoods.setWeight(resultWeight);
|
qualityFactoryMapper.updateByPrimaryKeySelective(selectionGoods);
|
}
|
|
}
|
|
@Override
|
public Long countTotalRows(Integer days) {
|
return qualityFactoryMapper.countTotalRows(days);
|
}
|
|
@Override
|
public void updateWeight(Integer weight, Integer time) {
|
qualityFactoryMapper.updateWeight(weight, time);
|
}
|
|
@Override
|
public long queryNeedUpdateCount() {
|
return qualityFactoryMapper.countQueryNeedUpdate();
|
}
|
|
@Override
|
public List<Long> queryNeedUpdate(long start, int count, int hour) {
|
return qualityFactoryMapper.queryNeedUpdate(start, count, hour);
|
}
|
|
/**
|
* 获取缓存
|
*
|
* @param key
|
* 键
|
* @param title
|
* 标签名称
|
* @return
|
*/
|
public String getLabelIdByRedis(String key, String title) throws Exception {
|
|
String value = redisManager.getCommonString(key);
|
|
if (StringUtil.isNullOrEmpty(value)) {
|
|
// id为空时系统进行查询
|
List<Label> list9K9 = labelService.selectByTitle(title);
|
|
if (list9K9 != null && list9K9.size() > 0) {
|
Long id = list9K9.get(0).getId();
|
if (id != null) {
|
value = id + "";
|
// 缓存20分钟
|
redisManager.cacheCommonString(key, value, 60 * 20);
|
}
|
}
|
}
|
|
return value;
|
}
|
|
@Override
|
@Transactional
|
public void updateGoodsFactory(List<Long> listId) {
|
|
if (listId == null || listId.size() == 0) {
|
return;
|
}
|
|
List<QualityFactory> listQuality = qualityFactoryMapper.listQueryGoodsByGoodsId(listId);
|
if (listQuality == null || listQuality.size() == 0) {
|
return;
|
}
|
|
// 删除精选库
|
List<Long> listDeleteQuality = new ArrayList<Long>();
|
// 删除标签
|
List<Long> listDeleteLabel = new ArrayList<Long>();
|
|
List<QualityFactory> listUpdateQuality = new ArrayList<QualityFactory>();
|
|
List<TaoBaoGoodsBrief> listUpdateGoodsBrief = new ArrayList<TaoBaoGoodsBrief>();
|
|
List<Long> list9ClassDelete = new ArrayList<Long>();
|
// 添加商品标签列表
|
List<LabelGoods> listLabelAdd = new ArrayList<LabelGoods>();
|
|
try {
|
|
List<Long> list9Class = new ArrayList<Long>();
|
Map<String, Label> map = new HashMap<String, Label>();
|
|
if (Constant.IS_OUTNET) {
|
|
String value9K9 = getLabelIdByRedis("lab-9K9", "9.9");
|
String value19K9 = getLabelIdByRedis("lab-19K9", "19.9");
|
String value29K9 = getLabelIdByRedis("lab-29K9", "29.9");
|
String value49K9 = getLabelIdByRedis("lab-49K9", "49.9");
|
|
if (!StringUtil.isNullOrEmpty(value9K9)) {
|
map.put("9.9", new Label(Long.parseLong(value9K9)));
|
list9Class.add(Long.parseLong(value9K9));
|
}
|
|
if (!StringUtil.isNullOrEmpty(value19K9)) {
|
map.put("19.9", new Label(Long.parseLong(value19K9)));
|
list9Class.add(Long.parseLong(value19K9));
|
}
|
|
if (!StringUtil.isNullOrEmpty(value29K9)) {
|
map.put("29.9", new Label(Long.parseLong(value29K9)));
|
list9Class.add(Long.parseLong(value29K9));
|
}
|
|
if (!StringUtil.isNullOrEmpty(value49K9)) {
|
map.put("49.9", new Label(Long.parseLong(value49K9)));
|
list9Class.add(Long.parseLong(value49K9));
|
}
|
|
}
|
|
listQuality.parallelStream().forEach(quality -> {
|
|
TaoBaoGoodsBrief goods = quality.getTaoBaoGoodsBrief();
|
|
if (goods == null) {
|
// 移除精选库
|
listDeleteQuality.add(quality.getId());
|
|
} else {
|
|
Long id = goods.getId();
|
|
TaoBaoGoodsBrief newGoodsBrief = null;
|
|
Long auctionId = goods.getAuctionId();
|
try {
|
// 获取详情
|
newGoodsBrief = TaoKeApiUtil.searchGoodsDetail(auctionId);
|
} catch (TaobaoGoodsDownException e1) {
|
// 商品下架 移除数据库
|
taoBaoGoodsUpdateService.deleteTaoBaoGoods(auctionId);
|
|
} catch (Exception e) {
|
try {
|
LogHelper.errorDetailInfo(e);
|
} catch (Exception e1) {
|
e1.printStackTrace();
|
}
|
}
|
|
if (newGoodsBrief == null) {
|
|
/* 无券信息 移除精选库 */
|
listDeleteQuality.add(quality.getId());
|
// 移除标签
|
listDeleteLabel.add(goods.getId());
|
|
} else {
|
/* 无券信息 移除精选库 */
|
if (StringUtil.isNullOrEmpty(newGoodsBrief.getCouponInfo())
|
&& StringUtil.isNullOrEmpty(newGoodsBrief.getCouponActivityId())) { // 无券信息
|
|
// 移除精选库
|
listDeleteQuality.add(quality.getId());
|
// 移除标签
|
listDeleteLabel.add(goods.getId());
|
|
} else {
|
/* 更新商品信息 */
|
TaoBaoGoodsBrief updateGoods = taoBaoGoodsUpdateService
|
.getUpdateTaoBaoGoodsBrief(newGoodsBrief);
|
|
updateGoods.setId(id);
|
taoBaoGoodsBriefService.setGoodsBriefDefault(updateGoods);
|
|
listUpdateGoodsBrief.add(updateGoods);
|
|
/* 更新精选库 信息时间 */
|
Integer biz30day = updateGoods.getBiz30day();
|
if (biz30day < 1000) {
|
// 销量小于1000 重新设置权重
|
int weight = 10 + (int) (Math.random() * 2000);
|
quality.setWeight(weight);
|
}
|
quality.setUpdatetime(new Date());
|
listUpdateQuality.add(quality);
|
|
/* 判断是否已经贴上标签 9k9标签 对应价格变化 */
|
if (list9Class.size() > 0) {
|
List<LabelGoods> listGlid = labelGoodsMapper.listQuery9k9Class(id, list9Class);
|
if (listGlid != null && listGlid.size() > 0) {
|
/* 删除原标签 9k9 */
|
for (LabelGoods labelGoods : listGlid) {
|
list9ClassDelete.add(labelGoods.getId());
|
}
|
|
/* 贴上新标签 9k9 */
|
LabelGoods labelGds = screen9K9(updateGoods, map);
|
if (labelGds != null) {
|
listLabelAdd.add(labelGds);
|
}
|
}
|
}
|
|
}
|
}
|
}
|
|
});
|
|
// 更新商品信息
|
if (listUpdateGoodsBrief.size() > 0) {
|
taoBaoGoodsUpdateService.updateTaoBaoGoods(listUpdateGoodsBrief);
|
}
|
|
// 更新精选库
|
if (listUpdateQuality.size() > 0) {
|
qualityFactoryMapper.updateBatchSelective(listUpdateQuality);
|
}
|
|
// 批量删除精选库
|
if (listDeleteQuality.size() > 0) {
|
qualityFactoryMapper.deleteBatchByPrimaryKey(listDeleteQuality);
|
// 批量删除限时抢购
|
qualityFlashSaleMapper.deleteBatchByQualityID(listDeleteQuality);
|
}
|
|
// 删除对应标签
|
if (listDeleteLabel.size() > 0) {
|
labelGoodsMapper.deleteBatchByGoodsId(listDeleteLabel);
|
}
|
|
// 添加标签
|
if (listLabelAdd.size() > 0) {
|
labelGoodsMapper.insertBatch(listLabelAdd);
|
}
|
|
// 删除标签
|
if (list9ClassDelete.size() > 0) {
|
labelGoodsMapper.deleteBatchByPK(list9ClassDelete);
|
}
|
|
} catch (Exception e) {
|
try {
|
LogHelper.errorDetailInfo(e);
|
} catch (Exception e1) {
|
e1.printStackTrace();
|
}
|
}
|
}
|
|
@Override
|
public void autoInsertOrUpadateStorage(List<TaoBaoGoodsBrief> goodsList, List<Label> listLabs,
|
BoutiqueAutoRule autoRule) throws Exception {
|
|
if (goodsList == null || goodsList.size() == 0) {
|
return;
|
}
|
|
/* 来源推荐商品需要获取详情 */
|
if (QualityFactory.SOURCE_TAOBAO_MATERIAL == autoRule.getGoodsSource()) {
|
// 插入商品集合
|
List<TaoBaoGoodsBrief> listGoodsBrief = new ArrayList<TaoBaoGoodsBrief>();
|
|
goodsList.parallelStream().forEach(goods -> {
|
// 获取详情
|
try {
|
TaoBaoGoodsBrief taoBaoGoodsBrief = TaoKeApiUtil.searchGoodsDetail(goods.getAuctionId());
|
if (taoBaoGoodsBrief != null) {
|
listGoodsBrief.add(taoBaoGoodsBrief);
|
}
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
});
|
|
goodsList.clear();
|
|
goodsList = listGoodsBrief;
|
|
if (goodsList == null || goodsList.size() == 0) {
|
return;
|
}
|
}
|
|
// 商品id集合
|
List<Long> listSystemTBid = new ArrayList<Long>();
|
|
// 添加商品标签列表
|
List<LabelGoods> listLabelAdd = new ArrayList<LabelGoods>();
|
|
// 插入商品集合
|
List<TaoBaoGoodsBrief> listGoodsInsert = new ArrayList<TaoBaoGoodsBrief>();
|
// 更新商品集合
|
List<TaoBaoGoodsBrief> listGoodsUpdate = new ArrayList<TaoBaoGoodsBrief>();
|
|
// 进入精选商品列表
|
List<QualityFactory> listAddInsert = new ArrayList<QualityFactory>();
|
// 进入精选商品列表
|
List<QualityFactory> listAddUpdate = new ArrayList<QualityFactory>();
|
|
// 插入商品淘宝ID集合
|
List<Long> listAuctionId = new ArrayList<Long>();
|
for (TaoBaoGoodsBrief goodsBrief : goodsList) {
|
listAuctionId.add(goodsBrief.getAuctionId());
|
}
|
|
List<TaoBaoGoodsBrief> listHas = taoBaoGoodsBriefService.listQueryByAuctionId(listAuctionId);
|
|
List<Long> list9Class = null;
|
List<Long> list9ClassDelete = new ArrayList<Long>();
|
Map<String, Label> map = new HashMap<String, Label>();
|
|
list9Class = new ArrayList<Long>();
|
|
List<Label> list9K9 = labelService.selectByTitleCache("labKey", "9.9");
|
if (list9K9 != null && list9K9.size() > 0) {
|
map.put("9.9", list9K9.get(0));
|
list9Class.add(list9K9.get(0).getId());
|
}
|
|
List<Label> list19k9 = labelService.selectByTitleCache("labKey", "19.9");
|
if (list19k9 != null && list19k9.size() > 0) {
|
map.put("19.9", list19k9.get(0));
|
list9Class.add(list19k9.get(0).getId());
|
}
|
|
List<Label> list29k9 = labelService.selectByTitleCache("labKey", "29.9");
|
if (list29k9 != null && list29k9.size() > 0) {
|
map.put("29.9", list29k9.get(0));
|
list9Class.add(list29k9.get(0).getId());
|
}
|
|
List<Label> list49k9 = labelService.selectByTitleCache("labKey", "49.9");
|
if (list49k9 != null && list49k9.size() > 0) {
|
map.put("49.9", list49k9.get(0));
|
list9Class.add(list49k9.get(0).getId());
|
}
|
|
for (TaoBaoGoodsBrief newGoodsBrief : goodsList) {
|
|
Long systemId = null;
|
boolean isExist = false;
|
Long auctionId = newGoodsBrief.getAuctionId();
|
|
/* 判断商品 是否已经存在数据库 */
|
if (listHas != null && listHas.size() > 0) {
|
for (TaoBaoGoodsBrief hasgoodsBrief : listHas) {
|
Long hasId = hasgoodsBrief.getAuctionId();
|
if (auctionId.equals(hasId)) {
|
systemId = hasgoodsBrief.getId();
|
// 存在数据库
|
isExist = true;
|
break;// 结束内部循环
|
}
|
}
|
}
|
|
/* 商品已存在数据库 */
|
if (isExist && systemId != null) {
|
|
// 用于确定是否存在精选库
|
listSystemTBid.add(systemId);
|
|
// 更新商品信息
|
TaoBaoGoodsBrief updateGoods = taoBaoGoodsUpdateService.getUpdateTaoBaoGoodsBrief(newGoodsBrief);
|
// 设置主键id
|
updateGoods.setId(systemId);
|
// 修改创建时间 - 换义新创建
|
Date date = new Date();
|
updateGoods.setCreatetime(date);
|
updateGoods.setUpdatetime(date);
|
taoBaoGoodsBriefService.setGoodsBriefDefault(updateGoods);
|
|
listGoodsUpdate.add(updateGoods);
|
|
Long has9k9ID = null;
|
|
/* 贴上标签 */
|
if (listLabs != null && listLabs.size() > 0) {
|
|
for (Label label : listLabs) {
|
|
// 包含 9.9/19.9/29.9/49.9 、同时来源淘宝推荐: 由于获取详情与推荐接口的券信息存在出入
|
// 以最后详情为准
|
if (list9Class.contains(label.getId()) && autoRule.isCalss9k9()
|
&& QualityFactory.SOURCE_TAOBAO_MATERIAL == autoRule.getGoodsSource()) {
|
|
/* 获取商品详情后计算是否属于 9k9系列 加入对应的标签 */
|
LabelGoods labelGds = screen9K9(updateGoods, map);
|
|
if (labelGds != null) {
|
|
Label label9k = labelGds.getLabel();
|
Long id = label9k.getId();
|
|
long existence = labelGoodsMapper.isExistence(systemId, id);
|
if (existence <= 0) {
|
// 不存在对应标签
|
listLabelAdd.add(labelGds);
|
} else {
|
// 已存在
|
has9k9ID = id;
|
}
|
|
}
|
|
} else {
|
|
long existence = labelGoodsMapper.isExistence(systemId, label.getId());
|
if (existence <= 0) {
|
// 贴上新标签
|
LabelGoods lg = new LabelGoods();
|
lg.setLabel(label);
|
lg.setCreateUser(autoRule.getAdminUser());
|
lg.setCreatetime(new Date());
|
lg.setTaoBaoGoodsBrief(updateGoods);
|
|
listLabelAdd.add(lg);
|
} else {
|
if (list9Class.contains(label.getId())) {
|
// 已存在
|
has9k9ID = label.getId();
|
}
|
}
|
}
|
}
|
}
|
|
/* 判断是否已经贴上标签 9k9标签 */
|
if (list9Class.size() > 0) {
|
|
List<LabelGoods> listGlid = labelGoodsMapper.listQuery9k9Class(systemId, list9Class);
|
|
if (listGlid != null && listGlid.size() > 0) {
|
|
if (has9k9ID == null && !autoRule.isCalss9k9()) {
|
// 非9k9入库规则 若商品信息变化 对应的标签根据价格 修改
|
LabelGoods labelGds = screen9K9(updateGoods, map);
|
if (labelGds != null) {
|
Label label9k = labelGds.getLabel();
|
Long id = label9k.getId();
|
|
long existence = labelGoodsMapper.isExistence(systemId, id);
|
if (existence <= 0) {
|
// 不存在对应标签
|
listLabelAdd.add(labelGds);
|
} else {
|
// 已存在
|
has9k9ID = id;
|
}
|
}
|
}
|
|
// 清空 9K9系列标签
|
for (LabelGoods labelGoods : listGlid) {
|
Label label = labelGoods.getLabel();
|
if (has9k9ID != null && has9k9ID.equals(label.getId())) {
|
continue;
|
}
|
|
list9ClassDelete.add(labelGoods.getId());
|
}
|
}
|
}
|
|
} else {
|
newGoodsBrief.setId(null);
|
newGoodsBrief.setState(0); // 状态启用
|
newGoodsBrief.setCreatetime(new Date());
|
newGoodsBrief.setUpdatetime(new Date());
|
// 设置默认值
|
taoBaoGoodsBriefService.setGoodsBriefDefault(newGoodsBrief);
|
// 插入数据库
|
listGoodsInsert.add(newGoodsBrief);
|
|
// 加入精选库
|
listAddInsert.add(getNewQualityFactory(newGoodsBrief, autoRule));
|
|
// 贴上标签
|
if (listLabs != null && listLabs.size() > 0) {
|
|
for (Label label : listLabs) {
|
|
// 包含 9.9/19.9/29.9/49.9 、同时来源淘宝推荐: 由于获取详情与推荐接口的券信息存在出入
|
// 以最后详情为准
|
if (list9Class.contains(label.getId()) && autoRule.isCalss9k9()
|
&& QualityFactory.SOURCE_TAOBAO_MATERIAL == autoRule.getGoodsSource()) {
|
/* 加入对应的9.9/19.9/29.9/49.9 标签 */
|
LabelGoods labelGds = screen9K9(newGoodsBrief, map);
|
|
if (labelGds != null) {
|
listLabelAdd.add(labelGds);
|
}
|
|
} else {
|
// 贴上新标签
|
LabelGoods lg = new LabelGoods();
|
lg.setLabel(label);
|
lg.setCreateUser(autoRule.getAdminUser());
|
lg.setCreatetime(new Date());
|
lg.setTaoBaoGoodsBrief(newGoodsBrief);
|
listLabelAdd.add(lg);
|
}
|
|
}
|
|
}
|
}
|
}
|
|
// 更新的商品信息 是否存在精选库中
|
List<QualityFactory> listExist = null;
|
if (listSystemTBid.size() > 0) {
|
listExist = qualityFactoryMapper.listQueryGoodsByGoodsId(listSystemTBid);
|
}
|
|
/* 更新淘宝商品信息 */
|
if (listGoodsUpdate.size() > 0) {
|
|
for (TaoBaoGoodsBrief goodsBrief : listGoodsUpdate) {
|
boolean isExist = false;
|
|
if (listExist == null || listExist.size() == 0) {
|
// 加入到精选库
|
listAddInsert.add(getNewQualityFactory(goodsBrief, autoRule));
|
continue;
|
}
|
|
QualityFactory selection = null;
|
Long id = goodsBrief.getId();
|
for (QualityFactory selectionGoods : listExist) {
|
TaoBaoGoodsBrief taoBaoGoodsBrief = selectionGoods.getTaoBaoGoodsBrief();
|
if (taoBaoGoodsBrief != null) {
|
Long sid = taoBaoGoodsBrief.getId();
|
if (id.equals(sid)) {
|
isExist = true;
|
selection = selectionGoods;
|
}
|
}
|
}
|
|
if (isExist) {
|
/* 更新精选库信息 */
|
|
// 随机权重
|
int weight = autoRule.getStartWeight()
|
+ (int) (Math.random() * ((autoRule.getEndWeight() - autoRule.getStartWeight()) + 1));
|
selection.setWeight(weight);
|
// 更新时间
|
selection.setUpdatetime(new Date());
|
// 更新来源规则
|
selection.setBoutiqueAutoRule(autoRule);
|
// 更新来源类别
|
selection.setSourceCalss(autoRule.getSourceCalss());
|
// 来源
|
selection.setGoodsSource(autoRule.getGoodsSource());
|
|
listAddUpdate.add(selection);
|
|
} else {
|
// 新增至精选库
|
listAddInsert.add(getNewQualityFactory(goodsBrief, autoRule));
|
}
|
|
}
|
try {
|
taoBaoGoodsUpdateService.updateTaoBaoGoods(listGoodsUpdate);
|
} catch (TaobaoGoodsUpdateException e) {
|
System.out.println(e.getMsg());
|
}
|
}
|
|
// 淘宝商品插入数据库
|
if (listGoodsInsert.size() > 0) {
|
taoBaoGoodsBriefService.insertBatch(listGoodsInsert);
|
}
|
|
// 更新精选库
|
if (listAddUpdate.size() > 0) {
|
qualityFactoryMapper.updateBatchSelective(listAddUpdate);
|
}
|
|
// 加入精选库
|
if (listAddInsert.size() > 0) {
|
qualityFactoryMapper.insertBatch(listAddInsert);
|
}
|
|
// 添加标签
|
if (listLabelAdd.size() > 0) {
|
labelGoodsMapper.insertBatch(listLabelAdd);
|
}
|
|
// 删除标签
|
if (list9ClassDelete.size() > 0) {
|
labelGoodsMapper.deleteBatchByPK(list9ClassDelete);
|
}
|
|
// 加入限时抢购
|
if (autoRule.isFlashSale()) {
|
updateQualityFlashSale(listAddInsert, listAddUpdate, autoRule);
|
}
|
|
}
|
|
public void updateQualityFlashSale(List<QualityFactory> listinsert, List<QualityFactory> listUpdate,
|
BoutiqueAutoRule autoRule) {
|
// 精选id集合
|
List<Long> listQFId = new ArrayList<Long>();
|
|
if (listinsert.size() > 0) {
|
for (QualityFactory qualityFactory : listinsert) {
|
listQFId.add(qualityFactory.getId());
|
}
|
}
|
|
if (listUpdate.size() > 0) {
|
for (QualityFactory qualityFactory : listUpdate) {
|
listQFId.add(qualityFactory.getId());
|
}
|
}
|
|
if (listQFId.size() == 0) {
|
return;
|
}
|
|
int startWeight = autoRule.getStartWeight();
|
int endWeight = autoRule.getEndWeight();
|
int goodsSource = autoRule.getGoodsSource();
|
|
// 加入入限时抢购
|
List<QualityFlashSale> listFlashSale = new ArrayList<QualityFlashSale>();
|
// 限时抢购更新: 时间、权重
|
List<QualityFlashSale> listFlashSaleUpdate = new ArrayList<QualityFlashSale>();
|
|
// 查询已存在显示限时抢购的商品
|
List<QualityFlashSale> listQuery = qualityFlashSaleService.listQueryByQualityID(listQFId);
|
|
if (listQuery == null || listQuery.size() == 0) {
|
// 全部新增入库
|
int i = 0;
|
for (Long qfId : listQFId) {
|
listFlashSale.add(getNewQualityFlashSale(qfId, i, startWeight, endWeight, goodsSource));
|
}
|
|
} else {
|
int i = 0;
|
boolean exist = false;
|
|
for (Long qfId : listQFId) {
|
|
for (QualityFlashSale flashSale : listQuery) {
|
QualityFactory qualityFactory = flashSale.getQualityFactory();
|
|
if (qfId.equals(qualityFactory.getId())) {
|
exist = true;
|
|
// 即将准备时间段时间
|
// int nowType = qualityFlashSaleService.getQueryType();
|
// flashSale.setType(nowType);
|
|
double weight = startWeight + (Math.random() * ((endWeight - startWeight) + 1));
|
flashSale.setWeight(weight);
|
flashSale.setUpdatetime(new Date());
|
listFlashSaleUpdate.add(flashSale);
|
|
break;
|
}
|
}
|
|
if (!exist) {
|
listFlashSale.add(getNewQualityFlashSale(qfId, i, startWeight, endWeight, goodsSource));
|
}
|
}
|
|
}
|
|
// 插入限时抢购
|
if (listFlashSale.size() > 0) {
|
qualityFlashSaleService.insertBatch(listFlashSale);
|
}
|
|
// 更新限时抢购
|
if (listFlashSaleUpdate.size() > 0) {
|
qualityFlashSaleService.updateBatchSelective(listFlashSaleUpdate);
|
}
|
|
}
|
|
/**
|
* 创建新对象QualityFlashSale
|
*
|
* @param qfId
|
* @param i
|
* @return
|
*/
|
public QualityFlashSale getNewQualityFlashSale(Long qfId, int i, int startWeight, int endWeight, int goodsSource) {
|
|
QualityFlashSale flashSale = new QualityFlashSale();
|
QualityFactory qualityFactory = new QualityFactory();
|
qualityFactory.setId(qfId);
|
flashSale.setQualityFactory(qualityFactory);
|
|
if (QualityFactory.SOURCE_TAOBAO_DATAOKE == goodsSource) {
|
// 大淘客优先展示
|
double weight = 20000 + (Math.random() * 2001);
|
flashSale.setWeight(weight);
|
} else {
|
double weight = startWeight + (Math.random() * ((endWeight - startWeight) + 1));
|
flashSale.setWeight(weight);
|
}
|
|
// int nowType = qualityFlashSaleService.getNowType();
|
// flashSale.setType(nowType);
|
|
flashSale.setCreatetime(new Date());
|
flashSale.setUpdatetime(new Date());
|
|
return flashSale;
|
}
|
|
/**
|
* 商品加入精选库-new QualityFactory()
|
*
|
* @param goodsBrief
|
* @param admin
|
* @return
|
*/
|
public QualityFactory getNewQualityFactory(TaoBaoGoodsBrief goodsBrief, BoutiqueAutoRule autoRule) {
|
|
/* 分类 */
|
Long classID = null;
|
|
Integer rootCatId = goodsBrief.getRootCatId();
|
if (rootCatId != null) {
|
List<TaoBaoClassRelation> listRelation = taoBaoClassRelationMapper.listByTaoBaoCid(rootCatId.longValue());
|
if (listRelation != null && listRelation.size() > 0) {
|
TaoBaoClassRelation taoBaoClassRelation = listRelation.get(0);
|
classID = taoBaoClassRelation.getCid();
|
}
|
}
|
|
// 添加至精选商品库
|
QualityFactory selectionGoods = new QualityFactory();
|
|
selectionGoods.setBoutiqueAutoRule(autoRule);
|
selectionGoods.setSourceCalss(autoRule.getSourceCalss());
|
selectionGoods.setSystemCid(classID);// 类别id
|
selectionGoods.setGoodsSource(autoRule.getGoodsSource());// 来源
|
selectionGoods.setTaoBaoGoodsBrief(goodsBrief);
|
|
selectionGoods.setCreateUser(autoRule.getAdminUser());
|
selectionGoods.setCreatetime(new Date());
|
selectionGoods.setUpdateUser(autoRule.getAdminUser());
|
selectionGoods.setUpdatetime(new Date());
|
selectionGoods.setEntryMode(QualityFactory.MODE_AUTO);// 系统筛选
|
|
// 随机权重
|
int resultWeight = autoRule.getStartWeight()
|
+ (int) (Math.random() * ((autoRule.getEndWeight() - autoRule.getStartWeight()) + 1));
|
|
selectionGoods.setWeight(resultWeight);
|
|
return selectionGoods;
|
|
}
|
|
public void setTaoBaoGoodsBriefDefault(TaoBaoGoodsBrief goodsBrief) {
|
BigDecimal defaultZero = new BigDecimal("0");
|
|
if (goodsBrief.getBiz30day() == null)
|
goodsBrief.setBiz30day(0);
|
|
if (goodsBrief.getCouponAmount() == null)
|
goodsBrief.setCouponAmount(defaultZero);
|
|
if (goodsBrief.getCouponLeftCount() == null)
|
goodsBrief.setCouponLeftCount(0);
|
|
if (goodsBrief.getCouponStartFee() == null)
|
goodsBrief.setCouponStartFee(defaultZero);
|
|
if (goodsBrief.getCouponTotalCount() == null)
|
goodsBrief.setCouponTotalCount(0);
|
|
if (goodsBrief.getDayLeft() == null)
|
goodsBrief.setDayLeft(0);
|
|
if (goodsBrief.getEventCreatorId() == null)
|
goodsBrief.setEventCreatorId(0);
|
|
if (goodsBrief.getHasRecommended() == null)
|
goodsBrief.setHasRecommended(0);
|
|
if (goodsBrief.getHasSame() == null)
|
goodsBrief.setHasSame(0);
|
|
if (goodsBrief.getIncludeDxjh() == null)
|
goodsBrief.setIncludeDxjh(0);
|
|
if (goodsBrief.getLeafCatId() == null)
|
goodsBrief.setIncludeDxjh(0);
|
|
if (goodsBrief.getReservePrice() == null)
|
goodsBrief.setReservePrice(defaultZero);
|
|
if (goodsBrief.getRfdRate() == null)
|
goodsBrief.setRfdRate(0);
|
|
if (goodsBrief.getRootCatId() == null)
|
goodsBrief.setRootCatId(0);
|
|
if (goodsBrief.getRootCatScore() == null)
|
goodsBrief.setRootCatScore(0);
|
|
if (goodsBrief.getSameItemPid() == null)
|
goodsBrief.setSameItemPid(0L);
|
|
if (goodsBrief.getSellerId() == null)
|
goodsBrief.setSellerId(0L);
|
|
if (goodsBrief.getTkCommFee() == null)
|
goodsBrief.setTkCommFee(defaultZero);
|
|
if (goodsBrief.getTkRate() == null)
|
goodsBrief.setTkRate(defaultZero);
|
|
if (goodsBrief.getTotalFee() == null)
|
goodsBrief.setTotalFee(defaultZero);
|
|
if (goodsBrief.getTotalNum() == null)
|
goodsBrief.setTotalNum(0);
|
|
if (goodsBrief.getUserType() == null)
|
goodsBrief.setUserType(0);
|
|
if (goodsBrief.getZkPrice() == null)
|
goodsBrief.setZkPrice(defaultZero);
|
|
}
|
|
/**
|
* 查询精选商品数据应用前端
|
*
|
* @param start
|
* @param count
|
* @param key
|
* @param classId
|
* 分类id
|
* @param labId
|
* 标签id
|
* @return
|
*/
|
@Override
|
public List<QualityFactory> listQuery(long start, int count, String key, Long classId, Long labId) {
|
return qualityFactoryMapper.listQuery(start, count, key, classId, labId, null, null);
|
}
|
|
/**
|
* 对应查询精选商品数据应用前端统计
|
*/
|
@Override
|
public long countQuery(String key, Integer goodsSource, Long classId, Long labId) {
|
return qualityFactoryMapper.countQuery(key, classId, labId, null, null);
|
}
|
|
public LabelGoods screen9K9(TaoBaoGoodsBrief goodsBrief, Map<String, Label> map) {
|
// 贴上新标签
|
LabelGoods lg = null;
|
|
/* 9k9 */
|
BigDecimal price9k9 = new BigDecimal(10);
|
/* 19k9 */
|
BigDecimal price19k9 = new BigDecimal(20);
|
/* 29.9 */
|
BigDecimal price29K9 = new BigDecimal(30);
|
/* 49.9 */
|
BigDecimal price49K9 = new BigDecimal(50);
|
/* 券后价--计算 */
|
BigDecimal couponPrice = TaoBaoUtil.getAfterUseCouplePrice(goodsBrief);
|
|
if (couponPrice.compareTo(price9k9) < 0) {
|
Label label = map.get("9.9");
|
if (label == null) {
|
return null;
|
}
|
|
lg = new LabelGoods();
|
lg.setLabel(label);
|
|
} else if (couponPrice.compareTo(price19k9) < 0) {
|
Label label = map.get("19.9");
|
if (label == null) {
|
return null;
|
}
|
|
lg = new LabelGoods();
|
lg.setLabel(label);
|
} else if (couponPrice.compareTo(price29K9) < 0) {
|
Label label = map.get("29.9");
|
if (label == null) {
|
return null;
|
}
|
|
lg = new LabelGoods();
|
lg.setLabel(label);
|
} else if (couponPrice.compareTo(price49K9) < 0) {
|
Label label = map.get("49.9");
|
if (label == null) {
|
return null;
|
}
|
|
lg = new LabelGoods();
|
lg.setLabel(label);
|
} else {
|
return null;
|
}
|
|
lg.setCreateUser(new AdminUser(2L));
|
lg.setCreatetime(new Date());
|
lg.setTaoBaoGoodsBrief(goodsBrief);
|
|
return lg;
|
}
|
|
@Override
|
public List<QualityFactory> listByMinSalesCountOrderByCreateTimeDesc(int salesCount, int page, int pageSize) {
|
return qualityFactoryMapper.listByMinSalesCountOrderByCreateTimeDesc(salesCount, (page - 1) * pageSize,
|
pageSize);
|
}
|
|
}
|