package com.yeshi.fanli.service.impl.push;
|
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.Date;
|
import java.util.List;
|
|
import javax.annotation.Resource;
|
import javax.transaction.Transactional;
|
|
import org.springframework.stereotype.Service;
|
|
import com.yeshi.fanli.dao.mybatis.push.PushGoodsMapper;
|
import com.yeshi.fanli.entity.goods.CommonGoods;
|
import com.yeshi.fanli.entity.push.PushGoods;
|
import com.yeshi.fanli.entity.push.PushGoodsGroup;
|
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
|
import com.yeshi.fanli.exception.PushException;
|
import com.yeshi.fanli.exception.push.PushGoodsException;
|
import com.yeshi.fanli.service.inter.config.ConfigService;
|
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
|
import com.yeshi.fanli.service.inter.push.PushGoodsGroupService;
|
import com.yeshi.fanli.service.inter.push.PushGoodsService;
|
import com.yeshi.fanli.service.inter.push.PushService;
|
import com.yeshi.fanli.util.StringUtil;
|
import com.yeshi.fanli.util.factory.CommonGoodsFactory;
|
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
|
|
@Service
|
public class PushGoodsServiceImpl implements PushGoodsService {
|
|
@Resource
|
private PushService pushService;
|
|
@Resource
|
private ConfigService configService;
|
|
@Resource
|
private PushGoodsMapper pushGoodsMapper;
|
|
@Resource
|
private CommonGoodsService commonGoodsService;
|
|
@Resource
|
private PushGoodsGroupService pushGoodsGroupService;
|
|
@Override
|
public int deleteByPrimaryKey(Long id) throws PushGoodsException {
|
return pushGoodsMapper.deleteByPrimaryKey(id);
|
}
|
|
@Override
|
public int insert(PushGoods record) throws PushGoodsException {
|
return pushGoodsMapper.insert(record);
|
}
|
|
@Override
|
public int insertSelective(PushGoods record) throws PushGoodsException {
|
return pushGoodsMapper.insertSelective(record);
|
}
|
|
@Override
|
public PushGoods selectByPrimaryKey(Long id) throws PushGoodsException {
|
return pushGoodsMapper.selectByPrimaryKey(id);
|
}
|
|
@Override
|
public int updateByPrimaryKeySelective(PushGoods record) throws PushGoodsException {
|
return pushGoodsMapper.updateByPrimaryKeySelective(record);
|
}
|
|
@Override
|
public int updateByPrimaryKey(PushGoods record) throws PushGoodsException {
|
return pushGoodsMapper.updateByPrimaryKey(record);
|
}
|
|
@Override
|
@Transactional
|
public void save(PushGoods record, List<Long> goodsIds) throws Exception {
|
|
boolean isAdd = false;
|
// 推送id
|
Long pushId = record.getId();
|
if (pushId == null) {
|
// 新增
|
isAdd = true;
|
|
record.setCreateTime(new Date());
|
record.setUpdateTime(new Date());
|
pushGoodsMapper.insertSelective(record);
|
|
pushId = record.getId();
|
|
} else {
|
// 修改
|
record.setUpdateTime(new Date());
|
pushGoodsMapper.updateByPrimaryKeySelective(record);
|
}
|
|
// 处理商品信息
|
saveGoodsInfo(isAdd, pushId, goodsIds);
|
}
|
|
@Transactional
|
public void saveGoodsInfo(boolean isAdd, Long pushId, List<Long> goodsIds) throws Exception {
|
|
if (goodsIds == null || goodsIds.size() == 0) {
|
if (!isAdd) {
|
pushGoodsGroupService.deleteByPushId(pushId);
|
}
|
return;
|
}
|
|
// 商品信息获取与保存
|
List<Long> listCommonId = new ArrayList<Long>();
|
for (Long auctionId : goodsIds) {
|
// 获取商品详情
|
TaoBaoGoodsBrief goodsBrief = TaoKeApiUtil.searchGoodsDetail(auctionId);
|
|
// 转换简版商品信息
|
CommonGoods commonGoods = CommonGoodsFactory.create(goodsBrief);
|
if (commonGoods != null) {
|
commonGoodsService.addOrUpdateCommonGoods(commonGoods);
|
Long cid = commonGoods.getId();
|
if (cid != null) {
|
listCommonId.add(cid);
|
}
|
}
|
}
|
|
/* 修改时删除 部分 */
|
if (!isAdd) {
|
|
List<Long> listdelete = new ArrayList<Long>();
|
|
List<PushGoodsGroup> listGroup = pushGoodsGroupService.selectByPushId(pushId);
|
|
if (listGroup != null && listGroup.size() > 0) {
|
for (PushGoodsGroup group : listGroup) {
|
Long groupId = group.getId();
|
|
CommonGoods commonGoods = group.getCommonGoods();
|
if (commonGoods != null) {
|
Long commonId = commonGoods.getId();
|
if (commonId != null && listCommonId.contains(groupId)) {
|
// 已经存在
|
listCommonId.remove(commonId);
|
} else {
|
// 已经被删除
|
listdelete.add(groupId);
|
}
|
}
|
}
|
}
|
|
// 删除已被前端清除商品
|
if (listdelete.size() > 0) {
|
pushGoodsGroupService.deleteBatchByPrimaryKey(listdelete);
|
}
|
}
|
|
// 插入商品列表
|
if (listCommonId.size() > 0 && !listCommonId.isEmpty()) {
|
List<PushGoodsGroup> listAdd = new ArrayList<PushGoodsGroup>();
|
for (Long cid : listCommonId) {
|
PushGoodsGroup pushGoodsGroup = new PushGoodsGroup();
|
pushGoodsGroup.setPushId(pushId);
|
pushGoodsGroup.setCommonGoods(new CommonGoods(cid));
|
listAdd.add(pushGoodsGroup);
|
}
|
|
pushGoodsGroupService.insertBatch(listAdd);
|
}
|
|
}
|
|
@Override
|
@Transactional
|
public int deleteBatchByPrimaryKey(List<Long> list) throws Exception {
|
|
/*List<PushGoods> lisState = pushGoodsMapper.listByPushState(list);
|
if (lisState != null && lisState.size() > 0) {
|
for (PushGoods pushGoods : lisState) {
|
// 已推送的消息不能删除
|
list.remove(pushGoods.getId());
|
}
|
}*/
|
|
// 删除对应商品
|
pushGoodsGroupService.deleteBatchByPushId(list);
|
|
return pushGoodsMapper.deleteBatchByPrimaryKey(list);
|
}
|
|
@Override
|
public List<PushGoods> listQuery(long start, int count, String key, Integer state) {
|
return pushGoodsMapper.listQuery(start, count, key, state);
|
}
|
|
@Override
|
public long countQuery(String key, Integer state) {
|
return pushGoodsMapper.countQuery(key, state);
|
}
|
|
@Override
|
public List<PushGoods> listHistoryByPushTime(long start, int count, Long uid, Date pushTime) {
|
return pushGoodsMapper.listHistoryByPushTime(start, count, uid, pushTime);
|
}
|
|
@Override
|
public long countHistoryByPushTime(Long uid, Date pushTime) {
|
return pushGoodsMapper.countHistoryByPushTime(uid, pushTime);
|
}
|
|
@Override
|
public void executePush(Long id) throws Exception, PushGoodsException, PushException {
|
|
PushGoods pushGoods = selectByPrimaryKey(id);
|
if (pushGoods == null) {
|
throw new PushException(1, "推送信息已不存在");
|
}
|
|
String alertTitle = pushGoods.getAlertTitle();
|
String alertContent = pushGoods.getAlertContent();
|
if (StringUtil.isNullOrEmpty(alertTitle) || StringUtil.isNullOrEmpty(alertContent)) {
|
throw new PushException(1, "推送标题及内容不能为空");
|
}
|
|
List<PushGoodsGroup> goodsList = pushGoodsGroupService.getAllInfoByPushId(id);
|
if (goodsList == null || goodsList.size() == 0) {
|
throw new PushException(1, "推送无商品,请完善数据");
|
}
|
|
List<String> listVersion = null;
|
String versions = pushGoods.getVersions();
|
if (versions != null && versions.trim().length() > 0) {
|
listVersion = Arrays.asList(versions.split(","));
|
}
|
|
if (goodsList.size() == 1) {
|
/* 单个商品推送: 直接处理为商品信息 */
|
PushGoodsGroup pushGoodsGroup = goodsList.get(0);
|
CommonGoods commonGoods = pushGoodsGroup.getCommonGoods();
|
if (commonGoods == null) {
|
throw new PushException(1, "商品详细信息已不存在");
|
}
|
|
Long goodsId = commonGoods.getGoodsId();
|
String url = "https://item.taobao.com/item.htm?id=" + goodsId;
|
|
pushService.pushGoods(pushGoods.getUid(), url, alertTitle, alertContent, listVersion);
|
|
} else {
|
/* 多个商品推送 */
|
String url = configService.get("push_goods_details");
|
if (StringUtil.isNullOrEmpty(url)) {
|
throw new PushException(1, "推送页面链接不存在");
|
}
|
// 生成链接
|
url = url + "?id=" + id;
|
|
pushService.pushUrl(pushGoods.getUid(), url, alertTitle, alertContent, listVersion);
|
}
|
|
// 已推送
|
pushGoods.setPush(true);
|
// 推送时间
|
pushGoods.setPushTime(new Date());
|
// 更新时间
|
pushGoods.setUpdateTime(new Date());
|
|
updateByPrimaryKeySelective(pushGoods);
|
|
}
|
|
}
|