From 5e7b0ed4a154ad067cbcf4aa1a1c7cce32f9864c Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期五, 26 四月 2024 18:02:17 +0800 Subject: [PATCH] 唯品会链接解析升级 --- fanli/src/main/java/com/yeshi/fanli/service/impl/push/PushGoodsServiceImpl.java | 1038 ++++++++++++++++++++++++++++---------------------------- 1 files changed, 519 insertions(+), 519 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/push/PushGoodsServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/push/PushGoodsServiceImpl.java index a491a89..a512c52 100644 --- a/fanli/src/main/java/com/yeshi/fanli/service/impl/push/PushGoodsServiceImpl.java +++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/push/PushGoodsServiceImpl.java @@ -1,519 +1,519 @@ -package com.yeshi.fanli.service.impl.push; - -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.List; - -import javax.annotation.Resource; - -import com.yeshi.fanli.entity.SystemEnum; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import com.yeshi.fanli.dao.mybatis.push.PushGoodsMapper; -import com.yeshi.fanli.entity.dynamic.GoodsEvaluate; -import com.yeshi.fanli.entity.dynamic.GoodsEvaluate.EvaluateEnum; -import com.yeshi.fanli.entity.dynamic.ImgInfo; -import com.yeshi.fanli.entity.dynamic.ImgInfo.ImgEnum; -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.system.ConfigKeyEnum; -import com.yeshi.goods.facade.entity.taobao.TaoBaoGoodsBrief; -import com.yeshi.fanli.exception.push.PushException; -import com.yeshi.fanli.exception.push.PushGoodsException; -import com.yeshi.fanli.service.inter.config.ConfigService; -import com.yeshi.fanli.service.inter.dynamic.GoodsEvaluateService; -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; - -import net.sf.json.JSONObject; - -@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; - - @Resource - private GoodsEvaluateService goodsEvaluateService; - - - - @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) { - 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(rollbackFor=Exception.class) - public void save(PushGoods record, List<Long> goodsIds) throws PushGoodsException, Exception { - // 瀹氭椂鏃堕棿 - if (StringUtil.isNullOrEmpty(record.getControlTime_str())) { - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); - record.setControlTime(format.parse(record.getControlTime_str().replaceAll("T", " "))); - } - - record.setIsPush(PushGoods.STATE_INIT); - record.setType(PushGoods.TYPE_GOODS); - - boolean isAdd = false; - // 鎺ㄩ�乮d - Long pushId = record.getId(); - if (pushId == null) { - // 鏂板 - isAdd = true; - - record.setCreateTime(new Date()); - record.setUpdateTime(new Date()); - pushGoodsMapper.insert(record); - - pushId = record.getId(); - - } else { - // 淇敼 - PushGoods current = pushGoodsMapper.selectByPrimaryKey(pushId); - if (current == null) { - throw new PushGoodsException(1, "璇ヨ褰曞凡涓嶅瓨鍦�"); - } - - record.setCreateTime(current.getCreateTime()); - record.setUpdateTime(new Date()); - pushGoodsMapper.updateByPrimaryKey(record); - } - - // 澶勭悊鍟嗗搧淇℃伅 - saveGoodsInfo(isAdd, pushId, goodsIds); - } - - @Transactional(rollbackFor=Exception.class) - 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) { - // 鑾峰彇鍟嗗搧璇︽儏 - try { - 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); - } - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - /* 淇敼鏃跺垹闄� 閮ㄥ垎 */ - 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(rollbackFor=Exception.class) - public void saveInfo(PushGoods record) throws PushGoodsException, Exception { - if (StringUtil.isNullOrEmpty(record.getOtherId())) { - throw new PushGoodsException(1, "鐩稿叧鍐呭ID涓嶈兘涓虹┖"); - } - - if (StringUtil.isNullOrEmpty(record.getTitle())) { - throw new PushGoodsException(1, "鏍囬涓嶈兘涓虹┖"); - } - - if (StringUtil.isNullOrEmpty(record.getContent())) { - throw new PushGoodsException(1, "鍐呭涓嶈兘涓虹┖"); - } - - if (record.getType() == null) { - throw new PushGoodsException(1, "鎺ㄩ�佺被鍨嬩笉鑳戒负绌�"); - } - - if (record.getType() == PushGoods.TYPE_ACTIVITY) { - GoodsEvaluate goodsEvaluate = goodsEvaluateService.getById(record.getOtherId()); - if (goodsEvaluate == null) { - throw new PushGoodsException(1, "鍙戝湀娲诲姩淇℃伅涓嶅瓨鍦�"); - } - - if (goodsEvaluate.getType() != EvaluateEnum.activity) { - throw new PushGoodsException(1, "璇ュ唴瀹归潪鍙戝湀娲诲姩淇℃伅"); - } - - String picUrl = null; - List<ImgInfo> imgList = goodsEvaluate.getImgList(); - if (imgList != null && imgList.size() > 0) { - for (ImgInfo imgInfo: imgList) { - if (!StringUtil.isNullOrEmpty(imgInfo.getUrl()) && StringUtil.isNullOrEmpty(picUrl) - && (imgInfo.getType() == ImgEnum.img || imgInfo.getType() == ImgEnum.activity)) { - picUrl = imgInfo.getUrl(); - break; - } - } - } - if (StringUtil.isNullOrEmpty(picUrl)) { - throw new PushGoodsException(1, "璇ラ潪鍙戝湀娲诲姩鏃犲浘鐗囦俊鎭�"); - } - record.setPicture(picUrl); - } - - - // 瀹氭椂鏃堕棿 - if (!StringUtil.isNullOrEmpty(record.getControlTime_str())) { - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); - record.setControlTime(format.parse(record.getControlTime_str().replaceAll("T", " "))); - } - // 娴忚鏁伴噺闅忔満 - if (record.getScanNum() == null || record.getScanNum() <= 0) - record.setScanNum((int) (Math.random() * 5000) + 1000); - - record.setIsPush(PushGoods.STATE_INIT); - record.setAlertTitle(record.getTitle()); - record.setAlertContent(record.getContent()); - - Long id = record.getId(); - if (id == null) { - record.setCreateTime(new Date()); - pushGoodsMapper.insert(record); - } else { - PushGoods current = pushGoodsMapper.selectByPrimaryKey(id); - if (current == null) { - throw new PushGoodsException(1, "璇ヨ褰曞凡涓嶅瓨鍦�"); - } - record.setCreateTime(current.getCreateTime()); - record.setScanNum(current.getScanNum()); - record.setUpdateTime(new Date()); - pushGoodsMapper.updateByPrimaryKey(record); - } - } - - - - @Override - @Transactional(rollbackFor=Exception.class) - 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, SystemEnum system) { - return pushGoodsMapper.listQuery(start, count, key, state,system); - } - - @Override - public long countQuery(String key, Integer state, SystemEnum system) { - return pushGoodsMapper.countQuery(key, state,system); - } - - @Override - public List<PushGoods> listHistoryByPushTime(long start, int count, Long uid, Date pushTime, SystemEnum system) { - return pushGoodsMapper.listHistoryByPushTime(start, count, uid, pushTime,system); - } - - @Override - public long countHistoryByPushTime(Long uid, Date pushTime, SystemEnum system) { - return pushGoodsMapper.countHistoryByPushTime(uid, pushTime,system); - } - - - @Override - public List<PushGoods> listTask( SystemEnum system) { - return pushGoodsMapper.listTask(system); - } - - - @Override - public void taskPush(PushGoods record) { - String msg = null; - int state = PushGoods.STATE_FAIL; - try { - // 鎵ц鎺ㄩ�� - executePush(record); - state = PushGoods.STATE_SUCCESS; - } catch (PushGoodsException e) { - msg = e.getMsg(); - } catch (PushException e) { - msg = e.getMsg(); - } catch (Exception e) { - msg = "绯荤粺鎺ㄩ�佸け璐�"; - } - record.setIsPush(state); - record.setPushTime(new Date()); - record.setRemark(msg); - record.setUpdateTime(new Date()); - pushGoodsMapper.updateByPrimaryKey(record); - } - - - @Override - public void handPush(Long id) throws Exception, PushGoodsException, PushException { - PushGoods record = selectByPrimaryKey(id); - if (record == null) { - throw new PushGoodsException(1, "鎺ㄩ�佷俊鎭凡涓嶅瓨鍦�"); - } - // 鎵ц鎺ㄩ�� - executePush(record); - - record.setIsPush(PushGoods.STATE_SUCCESS); - record.setPushTime(new Date()); - record.setUpdateTime(new Date()); - pushGoodsMapper.updateByPrimaryKey(record); - } - - - - @Override - public void executePush(PushGoods pushGoods) throws Exception, PushGoodsException, PushException { - if (StringUtil.isNullOrEmpty(pushGoods.getTitle()) || StringUtil.isNullOrEmpty(pushGoods.getContent())) { - throw new PushGoodsException(1, "鎺ㄩ�佹爣棰樺強鍐呭涓嶈兘涓虹┖"); - } - - if (StringUtil.isNullOrEmpty(pushGoods.getVersions())) { - throw new PushGoodsException(1, "鎺ㄩ�佺増鏈笉鑳戒负绌�"); - } - - if (pushGoods.getType() == null || pushGoods.getType() == PushGoods.TYPE_GOODS) { - executePushGoods(pushGoods); // 鎺ㄩ�佸晢鍝� - } else if (pushGoods.getType() == PushGoods.TYPE_ACTIVITY) { - executePushActivity(pushGoods); // 鎺ㄩ�佹椿鍔� - } - } - - - /** - * 鎺ㄩ�佹椿鍔� - * @param pushGoods - * @throws Exception - * @throws PushGoodsException - * @throws PushException - */ - private void executePushActivity(PushGoods pushGoods) throws Exception, PushGoodsException, PushException { - String otherId = pushGoods.getOtherId(); - if (StringUtil.isNullOrEmpty(otherId)) { - throw new PushGoodsException(1, "娲诲姩id涓嶈兘涓虹┖"); - } - - GoodsEvaluate goodsEvaluate = goodsEvaluateService.getById(otherId); - if (goodsEvaluate == null) { - throw new PushGoodsException(1, "鍙戝湀娲诲姩淇℃伅涓嶅瓨鍦�"); - } - - if (goodsEvaluate.getType() != EvaluateEnum.activity) { - throw new PushGoodsException(1, "璇ュ唴瀹归潪鍙戝湀娲诲姩淇℃伅"); - } - - String picUrl = null; - List<ImgInfo> imgList = goodsEvaluate.getImgList(); - if (imgList != null && imgList.size() > 0) { - for (ImgInfo imgInfo: imgList) { - if (imgInfo.getType() == ImgEnum.img && !StringUtil.isNullOrEmpty(imgInfo.getUrl())) { - picUrl = imgInfo.getUrl(); - } - } - } - - if (StringUtil.isNullOrEmpty(picUrl)) { - throw new PushGoodsException(1, "璇ラ潪鍙戝湀娲诲姩鏃犲浘鐗囦俊鎭�"); - } - - - String versions = pushGoods.getVersions(); - JSONObject json = JSONObject.fromObject(versions); - - String versionsIOS = json.getString("IOS"); - List<String> listIOS = new ArrayList<String>(); - if (versionsIOS != null && versionsIOS.trim().length() > 0) { - if (versionsIOS.contains("鍏ㄦ帹")) { - listIOS = null; - } else { - listIOS = Arrays.asList(versionsIOS.split(",")); - } - } - - List<String> listAndroid = new ArrayList<String>(); - String versionsAndroid = json.getString("Android"); - if (versionsAndroid != null && versionsAndroid.trim().length() > 0) { - if (versionsAndroid.contains("鍏ㄦ帹")) { - listAndroid = null; - } else { - listAndroid = Arrays.asList(versionsAndroid.split(",")); - } - } - - String url = configService.getValue(ConfigKeyEnum.pushActivityLink.getKey(),pushGoods.getSystem()); - if (StringUtil.isNullOrEmpty(url)) { - throw new PushGoodsException(1, "鎺ㄩ�侀〉闈㈤摼鎺ヤ笉瀛樺湪"); - } - url = url + "?id=" + pushGoods.getId(); - // h娲诲姩鍏ㄦ帹 - pushService.pushUrl(pushGoods.getUid(), pushGoods.getTitle(), pushGoods.getContent(), url, listIOS, listAndroid,pushGoods.getSystem()); - } - - - /** - * 鎺ㄩ�佸晢鍝� - * @param pushGoods - * @throws Exception - * @throws PushGoodsException - * @throws PushException - */ - private void executePushGoods(PushGoods pushGoods) throws Exception, PushGoodsException, PushException { - Long id = pushGoods.getId(); - List<PushGoodsGroup> goodsList = pushGoodsGroupService.getAllInfoByPushId(id); - if (goodsList == null || goodsList.size() == 0) { - throw new PushGoodsException(1, "鎺ㄩ�佹棤鍟嗗搧,璇峰畬鍠勬暟鎹�"); - } - - String versions = pushGoods.getVersions(); - if (versions == null || versions.trim().length() == 0) { - throw new PushGoodsException(1, "鎺ㄩ�佺増鏈笉鑳戒负绌�"); - } - - List<String> listIOS = new ArrayList<String>(); - JSONObject json = JSONObject.fromObject(versions); - String versionsIOS = json.getString("IOS"); - if (versionsIOS != null && versionsIOS.trim().length() > 0) { - if (versionsIOS.contains("鍏ㄦ帹")) { - listIOS = null; - } else { - listIOS = Arrays.asList(versionsIOS.split(",")); - } - } - - List<String> listAndroid = new ArrayList<String>(); - String versionsAndroid = json.getString("Android"); - if (versionsAndroid != null && versionsAndroid.trim().length() > 0) { - if (versionsAndroid.contains("鍏ㄦ帹")) { - listAndroid = null; - } else { - listAndroid = Arrays.asList(versionsAndroid.split(",")); - } - } - - if (goodsList.size() == 1) { - /* 鍗曚釜鍟嗗搧鎺ㄩ�侊細 鐩存帴澶勭悊涓哄晢鍝佷俊鎭� */ - PushGoodsGroup pushGoodsGroup = goodsList.get(0); - CommonGoods commonGoods = pushGoodsGroup.getCommonGoods(); - if (commonGoods == null) { - throw new PushGoodsException(1, "鍟嗗搧璇︾粏淇℃伅宸蹭笉瀛樺湪"); - } - - Long goodsId = commonGoods.getGoodsId(); - String url = "https://item.taobao.com/item.htm?id=" + goodsId; - pushService.pushGoods(pushGoods.getUid(), pushGoods.getTitle(), pushGoods.getContent(), url,listIOS, listAndroid,pushGoods.getSystem()); - } else { - /* 澶氫釜鍟嗗搧鎺ㄩ�� */ - String url = configService.getValue(ConfigKeyEnum.pushGoodsDetails.getKey(),pushGoods.getSystem()); - if (StringUtil.isNullOrEmpty(url)) { - throw new PushGoodsException(1, "鎺ㄩ�侀〉闈㈤摼鎺ヤ笉瀛樺湪"); - } - // 鐢熸垚閾炬帴 - url = url + "?id=" + id; - pushService.pushUrl(pushGoods.getUid(), pushGoods.getTitle(), pushGoods.getContent(), url, listIOS, listAndroid,pushGoods.getSystem()); - } - } - - -} +package com.yeshi.fanli.service.impl.push; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +import javax.annotation.Resource; + +import com.yeshi.fanli.entity.SystemEnum; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import com.yeshi.fanli.dao.mybatis.push.PushGoodsMapper; +import com.yeshi.fanli.entity.dynamic.GoodsEvaluate; +import com.yeshi.fanli.entity.dynamic.GoodsEvaluate.EvaluateEnum; +import com.yeshi.fanli.entity.dynamic.ImgInfo; +import com.yeshi.fanli.entity.dynamic.ImgInfo.ImgEnum; +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.system.ConfigKeyEnum; +import com.yeshi.goods.facade.entity.taobao.TaoBaoGoodsBrief; +import com.yeshi.fanli.exception.push.PushException; +import com.yeshi.fanli.exception.push.PushGoodsException; +import com.yeshi.fanli.service.inter.config.ConfigService; +import com.yeshi.fanli.service.inter.dynamic.GoodsEvaluateService; +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; + +import net.sf.json.JSONObject; + +@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; + + @Resource + private GoodsEvaluateService goodsEvaluateService; + + + + @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) { + 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(rollbackFor=Exception.class) + public void save(PushGoods record, List<String> goodsIds) throws PushGoodsException, Exception { + // 瀹氭椂鏃堕棿 + if (StringUtil.isNullOrEmpty(record.getControlTime_str())) { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + record.setControlTime(format.parse(record.getControlTime_str().replaceAll("T", " "))); + } + + record.setIsPush(PushGoods.STATE_INIT); + record.setType(PushGoods.TYPE_GOODS); + + boolean isAdd = false; + // 鎺ㄩ�乮d + Long pushId = record.getId(); + if (pushId == null) { + // 鏂板 + isAdd = true; + + record.setCreateTime(new Date()); + record.setUpdateTime(new Date()); + pushGoodsMapper.insert(record); + + pushId = record.getId(); + + } else { + // 淇敼 + PushGoods current = pushGoodsMapper.selectByPrimaryKey(pushId); + if (current == null) { + throw new PushGoodsException(1, "璇ヨ褰曞凡涓嶅瓨鍦�"); + } + + record.setCreateTime(current.getCreateTime()); + record.setUpdateTime(new Date()); + pushGoodsMapper.updateByPrimaryKey(record); + } + + // 澶勭悊鍟嗗搧淇℃伅 + saveGoodsInfo(isAdd, pushId, goodsIds); + } + + @Transactional(rollbackFor=Exception.class) + public void saveGoodsInfo(boolean isAdd, Long pushId, List<String> goodsIds) throws Exception { + if (goodsIds == null || goodsIds.size() == 0) { + if (!isAdd) { + pushGoodsGroupService.deleteByPushId(pushId); + } + return; + } + + // 鍟嗗搧淇℃伅鑾峰彇涓庝繚瀛� + List<Long> listCommonId = new ArrayList<>(); + for (String auctionId : goodsIds) { + // 鑾峰彇鍟嗗搧璇︽儏 + try { + 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); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /* 淇敼鏃跺垹闄� 閮ㄥ垎 */ + 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(rollbackFor=Exception.class) + public void saveInfo(PushGoods record) throws PushGoodsException, Exception { + if (StringUtil.isNullOrEmpty(record.getOtherId())) { + throw new PushGoodsException(1, "鐩稿叧鍐呭ID涓嶈兘涓虹┖"); + } + + if (StringUtil.isNullOrEmpty(record.getTitle())) { + throw new PushGoodsException(1, "鏍囬涓嶈兘涓虹┖"); + } + + if (StringUtil.isNullOrEmpty(record.getContent())) { + throw new PushGoodsException(1, "鍐呭涓嶈兘涓虹┖"); + } + + if (record.getType() == null) { + throw new PushGoodsException(1, "鎺ㄩ�佺被鍨嬩笉鑳戒负绌�"); + } + + if (record.getType() == PushGoods.TYPE_ACTIVITY) { + GoodsEvaluate goodsEvaluate = goodsEvaluateService.getById(record.getOtherId()); + if (goodsEvaluate == null) { + throw new PushGoodsException(1, "鍙戝湀娲诲姩淇℃伅涓嶅瓨鍦�"); + } + + if (goodsEvaluate.getType() != EvaluateEnum.activity) { + throw new PushGoodsException(1, "璇ュ唴瀹归潪鍙戝湀娲诲姩淇℃伅"); + } + + String picUrl = null; + List<ImgInfo> imgList = goodsEvaluate.getImgList(); + if (imgList != null && imgList.size() > 0) { + for (ImgInfo imgInfo: imgList) { + if (!StringUtil.isNullOrEmpty(imgInfo.getUrl()) && StringUtil.isNullOrEmpty(picUrl) + && (imgInfo.getType() == ImgEnum.img || imgInfo.getType() == ImgEnum.activity)) { + picUrl = imgInfo.getUrl(); + break; + } + } + } + if (StringUtil.isNullOrEmpty(picUrl)) { + throw new PushGoodsException(1, "璇ラ潪鍙戝湀娲诲姩鏃犲浘鐗囦俊鎭�"); + } + record.setPicture(picUrl); + } + + + // 瀹氭椂鏃堕棿 + if (!StringUtil.isNullOrEmpty(record.getControlTime_str())) { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + record.setControlTime(format.parse(record.getControlTime_str().replaceAll("T", " "))); + } + // 娴忚鏁伴噺闅忔満 + if (record.getScanNum() == null || record.getScanNum() <= 0) + record.setScanNum((int) (Math.random() * 5000) + 1000); + + record.setIsPush(PushGoods.STATE_INIT); + record.setAlertTitle(record.getTitle()); + record.setAlertContent(record.getContent()); + + Long id = record.getId(); + if (id == null) { + record.setCreateTime(new Date()); + pushGoodsMapper.insert(record); + } else { + PushGoods current = pushGoodsMapper.selectByPrimaryKey(id); + if (current == null) { + throw new PushGoodsException(1, "璇ヨ褰曞凡涓嶅瓨鍦�"); + } + record.setCreateTime(current.getCreateTime()); + record.setScanNum(current.getScanNum()); + record.setUpdateTime(new Date()); + pushGoodsMapper.updateByPrimaryKey(record); + } + } + + + + @Override + @Transactional(rollbackFor=Exception.class) + 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, SystemEnum system) { + return pushGoodsMapper.listQuery(start, count, key, state,system); + } + + @Override + public long countQuery(String key, Integer state, SystemEnum system) { + return pushGoodsMapper.countQuery(key, state,system); + } + + @Override + public List<PushGoods> listHistoryByPushTime(long start, int count, Long uid, Date pushTime, SystemEnum system) { + return pushGoodsMapper.listHistoryByPushTime(start, count, uid, pushTime,system); + } + + @Override + public long countHistoryByPushTime(Long uid, Date pushTime, SystemEnum system) { + return pushGoodsMapper.countHistoryByPushTime(uid, pushTime,system); + } + + + @Override + public List<PushGoods> listTask( SystemEnum system) { + return pushGoodsMapper.listTask(system); + } + + + @Override + public void taskPush(PushGoods record) { + String msg = null; + int state = PushGoods.STATE_FAIL; + try { + // 鎵ц鎺ㄩ�� + executePush(record); + state = PushGoods.STATE_SUCCESS; + } catch (PushGoodsException e) { + msg = e.getMsg(); + } catch (PushException e) { + msg = e.getMsg(); + } catch (Exception e) { + msg = "绯荤粺鎺ㄩ�佸け璐�"; + } + record.setIsPush(state); + record.setPushTime(new Date()); + record.setRemark(msg); + record.setUpdateTime(new Date()); + pushGoodsMapper.updateByPrimaryKey(record); + } + + + @Override + public void handPush(Long id) throws Exception, PushGoodsException, PushException { + PushGoods record = selectByPrimaryKey(id); + if (record == null) { + throw new PushGoodsException(1, "鎺ㄩ�佷俊鎭凡涓嶅瓨鍦�"); + } + // 鎵ц鎺ㄩ�� + executePush(record); + + record.setIsPush(PushGoods.STATE_SUCCESS); + record.setPushTime(new Date()); + record.setUpdateTime(new Date()); + pushGoodsMapper.updateByPrimaryKey(record); + } + + + + @Override + public void executePush(PushGoods pushGoods) throws Exception, PushGoodsException, PushException { + if (StringUtil.isNullOrEmpty(pushGoods.getTitle()) || StringUtil.isNullOrEmpty(pushGoods.getContent())) { + throw new PushGoodsException(1, "鎺ㄩ�佹爣棰樺強鍐呭涓嶈兘涓虹┖"); + } + + if (StringUtil.isNullOrEmpty(pushGoods.getVersions())) { + throw new PushGoodsException(1, "鎺ㄩ�佺増鏈笉鑳戒负绌�"); + } + + if (pushGoods.getType() == null || pushGoods.getType() == PushGoods.TYPE_GOODS) { + executePushGoods(pushGoods); // 鎺ㄩ�佸晢鍝� + } else if (pushGoods.getType() == PushGoods.TYPE_ACTIVITY) { + executePushActivity(pushGoods); // 鎺ㄩ�佹椿鍔� + } + } + + + /** + * 鎺ㄩ�佹椿鍔� + * @param pushGoods + * @throws Exception + * @throws PushGoodsException + * @throws PushException + */ + private void executePushActivity(PushGoods pushGoods) throws Exception, PushGoodsException, PushException { + String otherId = pushGoods.getOtherId(); + if (StringUtil.isNullOrEmpty(otherId)) { + throw new PushGoodsException(1, "娲诲姩id涓嶈兘涓虹┖"); + } + + GoodsEvaluate goodsEvaluate = goodsEvaluateService.getById(otherId); + if (goodsEvaluate == null) { + throw new PushGoodsException(1, "鍙戝湀娲诲姩淇℃伅涓嶅瓨鍦�"); + } + + if (goodsEvaluate.getType() != EvaluateEnum.activity) { + throw new PushGoodsException(1, "璇ュ唴瀹归潪鍙戝湀娲诲姩淇℃伅"); + } + + String picUrl = null; + List<ImgInfo> imgList = goodsEvaluate.getImgList(); + if (imgList != null && imgList.size() > 0) { + for (ImgInfo imgInfo: imgList) { + if (imgInfo.getType() == ImgEnum.img && !StringUtil.isNullOrEmpty(imgInfo.getUrl())) { + picUrl = imgInfo.getUrl(); + } + } + } + + if (StringUtil.isNullOrEmpty(picUrl)) { + throw new PushGoodsException(1, "璇ラ潪鍙戝湀娲诲姩鏃犲浘鐗囦俊鎭�"); + } + + + String versions = pushGoods.getVersions(); + JSONObject json = JSONObject.fromObject(versions); + + String versionsIOS = json.getString("IOS"); + List<String> listIOS = new ArrayList<String>(); + if (versionsIOS != null && versionsIOS.trim().length() > 0) { + if (versionsIOS.contains("鍏ㄦ帹")) { + listIOS = null; + } else { + listIOS = Arrays.asList(versionsIOS.split(",")); + } + } + + List<String> listAndroid = new ArrayList<String>(); + String versionsAndroid = json.getString("Android"); + if (versionsAndroid != null && versionsAndroid.trim().length() > 0) { + if (versionsAndroid.contains("鍏ㄦ帹")) { + listAndroid = null; + } else { + listAndroid = Arrays.asList(versionsAndroid.split(",")); + } + } + + String url = configService.getValue(ConfigKeyEnum.pushActivityLink.getKey(),pushGoods.getSystem()); + if (StringUtil.isNullOrEmpty(url)) { + throw new PushGoodsException(1, "鎺ㄩ�侀〉闈㈤摼鎺ヤ笉瀛樺湪"); + } + url = url + "?id=" + pushGoods.getId(); + // h娲诲姩鍏ㄦ帹 + pushService.pushUrl(pushGoods.getUid(), pushGoods.getTitle(), pushGoods.getContent(), url, listIOS, listAndroid,pushGoods.getSystem()); + } + + + /** + * 鎺ㄩ�佸晢鍝� + * @param pushGoods + * @throws Exception + * @throws PushGoodsException + * @throws PushException + */ + private void executePushGoods(PushGoods pushGoods) throws Exception, PushGoodsException, PushException { + Long id = pushGoods.getId(); + List<PushGoodsGroup> goodsList = pushGoodsGroupService.getAllInfoByPushId(id); + if (goodsList == null || goodsList.size() == 0) { + throw new PushGoodsException(1, "鎺ㄩ�佹棤鍟嗗搧,璇峰畬鍠勬暟鎹�"); + } + + String versions = pushGoods.getVersions(); + if (versions == null || versions.trim().length() == 0) { + throw new PushGoodsException(1, "鎺ㄩ�佺増鏈笉鑳戒负绌�"); + } + + List<String> listIOS = new ArrayList<String>(); + JSONObject json = JSONObject.fromObject(versions); + String versionsIOS = json.getString("IOS"); + if (versionsIOS != null && versionsIOS.trim().length() > 0) { + if (versionsIOS.contains("鍏ㄦ帹")) { + listIOS = null; + } else { + listIOS = Arrays.asList(versionsIOS.split(",")); + } + } + + List<String> listAndroid = new ArrayList<String>(); + String versionsAndroid = json.getString("Android"); + if (versionsAndroid != null && versionsAndroid.trim().length() > 0) { + if (versionsAndroid.contains("鍏ㄦ帹")) { + listAndroid = null; + } else { + listAndroid = Arrays.asList(versionsAndroid.split(",")); + } + } + + if (goodsList.size() == 1) { + /* 鍗曚釜鍟嗗搧鎺ㄩ�侊細 鐩存帴澶勭悊涓哄晢鍝佷俊鎭� */ + PushGoodsGroup pushGoodsGroup = goodsList.get(0); + CommonGoods commonGoods = pushGoodsGroup.getCommonGoods(); + if (commonGoods == null) { + throw new PushGoodsException(1, "鍟嗗搧璇︾粏淇℃伅宸蹭笉瀛樺湪"); + } + + String goodsId = commonGoods.getGoodsId(); + String url = "https://item.taobao.com/item.htm?id=" + goodsId; + pushService.pushGoods(pushGoods.getUid(), pushGoods.getTitle(), pushGoods.getContent(), url,listIOS, listAndroid,pushGoods.getSystem()); + } else { + /* 澶氫釜鍟嗗搧鎺ㄩ�� */ + String url = configService.getValue(ConfigKeyEnum.pushGoodsDetails.getKey(),pushGoods.getSystem()); + if (StringUtil.isNullOrEmpty(url)) { + throw new PushGoodsException(1, "鎺ㄩ�侀〉闈㈤摼鎺ヤ笉瀛樺湪"); + } + // 鐢熸垚閾炬帴 + url = url + "?id=" + id; + pushService.pushUrl(pushGoods.getUid(), pushGoods.getTitle(), pushGoods.getContent(), url, listIOS, listAndroid,pushGoods.getSystem()); + } + } + + +} -- Gitblit v1.8.0