yujian
2020-05-09 7e7db2fa55a9a3af46d4fd8ede0dee147f101d64
fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserGoodsStorageServiceImpl.java
@@ -8,58 +8,67 @@
import javax.annotation.Resource;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.entity.FileUploadResult;
import org.yeshi.utils.taobao.TbImgUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yeshi.fanli.dao.mybatis.user.UserGoodsStorageMapper;
import com.yeshi.fanli.entity.bus.share.UserShareGoodsRecord.ShareSourceTypeEnum;
import com.yeshi.fanli.dto.pdd.PDDGoodsDetail;
import com.yeshi.fanli.dto.share.ShareGoodsRecordDTO;
import com.yeshi.fanli.entity.bus.user.UserGoodsStorage;
import com.yeshi.fanli.entity.bus.user.vip.UserLevelEnum;
import com.yeshi.fanli.entity.goods.CommonGoods;
import com.yeshi.fanli.entity.jd.JDGoods;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.exception.goods.CommonGoodsException;
import com.yeshi.fanli.exception.share.UserShareGoodsRecordException;
import com.yeshi.fanli.exception.taobao.TaoKeApiException;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
import com.yeshi.fanli.exception.user.UserGoodsStorageException;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.inter.user.UserGoodsStorageService;
import com.yeshi.fanli.service.inter.user.UserShareGoodsRecordService;
import com.yeshi.fanli.util.CommonGoodsUtil;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.CommonGoodsFactory;
import com.yeshi.fanli.util.jd.JDApiUtil;
import com.yeshi.fanli.util.pinduoduo.PinDuoDuoApiUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Service
public class UserGoodsStorageServiceImpl implements UserGoodsStorageService {
   @Resource(name = "taskExecutor")
   private TaskExecutor executor;
   @Resource
   private RedisManager redisManager;
   @Resource
   private ConfigService configService;
   @Resource
   private CommonGoodsService commonGoodsService;
   @Resource
   private HongBaoManageService manageService;
   @Resource
   private UserGoodsStorageMapper userGoodsStorageMapper;
   @Resource
   private UserShareGoodsRecordService userShareGoodsRecordService;
   @Resource
@@ -105,9 +114,10 @@
   public int deleteBatchByPrimaryKey(List<Long> list) {
      return userGoodsStorageMapper.deleteBatchByPrimaryKey(list);
   }
   /**
    * 根据主键 、uid 批量删除
    *
    * @param list
    * @return
    */
@@ -117,22 +127,22 @@
   }
   @Override
   public List<UserGoodsStorage> listQueryByUid(long start, int count, Long uid) {
      return userGoodsStorageMapper.listQueryByUid(start, count, uid);
   }
   @Override
   public UserGoodsStorage getByUidAndAuctionId(Long uid, Long auctionId) {
      return userGoodsStorageMapper.getByUidAndAuctionId(uid, auctionId);
   public List<UserGoodsStorage> listQueryByUid(long start, int count, Long uid, Integer source) {
      return userGoodsStorageMapper.listQueryByUid(start, count, uid, source);
   }
   @Override
   public long countQueryByUid(Long uid) {
      return userGoodsStorageMapper.countQueryByUid(uid);
   public UserGoodsStorage getByUidAndAuctionId(Long uid, Long auctionId, Integer goodsType) {
      return userGoodsStorageMapper.getByUidAndAuctionId(uid, auctionId, goodsType);
   }
   @Override
   public void save(Long uid, Set<Long> auctionIds) throws UserGoodsStorageException {
   public long countQueryByUid(Long uid, Integer source) {
      return userGoodsStorageMapper.countQueryByUid(uid, source);
   }
   @Override
   public void save(Long uid, Set<Long> auctionIds, Integer goodsType) throws UserGoodsStorageException {
      if (uid == null) {
         throw new UserGoodsStorageException(1, "用户尚未登录");
@@ -143,214 +153,307 @@
      }
      for (Long auctionId : auctionIds) {
         TaoBaoGoodsBrief goodsBrief = null;
         try {
            // 获取商品详情
            goodsBrief = redisManager.getTaoBaoGoodsBrief(auctionId);
         } catch (TaobaoGoodsDownException e) {
            // 更新简版 下架状态
            CommonGoods commonGoods = new CommonGoods();
            commonGoods.setGoodsId(auctionId);
            commonGoods.setGoodsType(CommonGoods.GOODS_TYPE_TB);
            commonGoods.setState(CommonGoods.STATE_OFFLINE);
            throw new UserGoodsStorageException(1, "该商品已下架");
         CommonGoods commonGoods = null;
         if (goodsType == null) {
            goodsType = Constant.SOURCE_TYPE_TAOBAO;
         }
         // 转换简版商品信息
         CommonGoods commonGoods = CommonGoodsFactory.create(goodsBrief);
         try {
            commonGoodsService.addOrUpdateCommonGoods(commonGoods);
         } catch (CommonGoodsException e) {
            e.printStackTrace();
            throw new UserGoodsStorageException(1, "商品信息更新失败");
         }
         if (goodsType == Constant.SOURCE_TYPE_JD) {
            // 京东
            JDGoods jdGoods = JDApiUtil.queryGoodsDetail(auctionId);
            if (jdGoods == null) {
               jdGoods = JDApiUtil.getGoodsDetail(auctionId);
            }
            if (jdGoods == null) {
               throw new UserGoodsStorageException(1, auctionId + "商品已下架");
            }
            commonGoods = CommonGoodsFactory.create(jdGoods);
         Long cid = commonGoods.getId();
         UserGoodsStorage goodsStorage = userGoodsStorageMapper.getByUidAndCid(uid, cid);
         if (goodsStorage != null) {
            // 初始话状态
            goodsStorage.setState(UserGoodsStorage.STATE_NORMAL);
            goodsStorage.setUpdateTime(new Date());
            userGoodsStorageMapper.updateByPrimaryKeySelective(goodsStorage);
         } else if (goodsType == Constant.SOURCE_TYPE_PDD) {
            // 拼多多
            PDDGoodsDetail pddGoods = PinDuoDuoApiUtil.getGoodsDetail(auctionId);
            if (pddGoods == null) {
               throw new UserGoodsStorageException(1, auctionId + "商品已下架");
            }
            commonGoods = CommonGoodsFactory.create(pddGoods);
         } else {
            goodsStorage = new UserGoodsStorage();
            goodsStorage.setUid(uid);;
            goodsStorage.setState(UserGoodsStorage.STATE_NORMAL);
            goodsStorage.setCommonGoods(commonGoods);
            goodsStorage.setCreateTime(new Date());
            goodsStorage.setUpdateTime(new Date());
            userGoodsStorageMapper.insertSelective(goodsStorage);
            // 淘宝
            try {
               // 获取商品详情
               TaoBaoGoodsBrief goodsBrief = redisManager.getTaoBaoGoodsBrief(auctionId);
               commonGoods = CommonGoodsFactory.create(goodsBrief);
            } catch (TaobaoGoodsDownException e) {
               throw new UserGoodsStorageException(1, auctionId + "商品已下架");
            }
         }
         if (commonGoods != null) {
            try {
               commonGoodsService.addOrUpdateCommonGoods(commonGoods);
            } catch (CommonGoodsException e) {
               e.printStackTrace();
               throw new UserGoodsStorageException(1, "商品信息更新失败");
            }
            Long cid = commonGoods.getId();
            UserGoodsStorage goodsStorage = userGoodsStorageMapper.getByUidAndCid(uid, cid);
            if (goodsStorage != null) {
               // 初始话状态
               goodsStorage.setState(UserGoodsStorage.STATE_NORMAL);
               goodsStorage.setUpdateTime(new Date());
               userGoodsStorageMapper.updateByPrimaryKeySelective(goodsStorage);
            } else {
               goodsStorage = new UserGoodsStorage();
               goodsStorage.setUid(uid);
               ;
               goodsStorage.setState(UserGoodsStorage.STATE_NORMAL);
               goodsStorage.setCommonGoods(commonGoods);
               goodsStorage.setCreateTime(new Date());
               goodsStorage.setUpdateTime(new Date());
               userGoodsStorageMapper.insertSelective(goodsStorage);
            }
         }
      }
   }
   @Override
   public JSONArray getMyStorage(int page, int pageSize, Long uid) throws UserGoodsStorageException {
      JSONArray  array = new JSONArray();
   @Transactional(rollbackFor=Exception.class)
   public void addCommonGoods(Long uid, Set<Long> ids) throws UserGoodsStorageException {
      List<UserGoodsStorage> listStorage = listQueryByUid(page , pageSize, uid);
      if (uid == null) {
         throw new UserGoodsStorageException(1, "用户尚未登录");
      }
      if (ids == null || ids.size() == 0) {
         throw new UserGoodsStorageException(1, "商品ID为空");
      }
      for (Long id : ids) {
         CommonGoods commonGoods = commonGoodsService.selectByPrimaryKey(id);
         if (commonGoods != null) {
            UserGoodsStorage goodsStorage = userGoodsStorageMapper.getByUidAndCid(uid, id);
            if (goodsStorage != null) {
               // 初始话状态
               goodsStorage.setState(UserGoodsStorage.STATE_NORMAL);
               goodsStorage.setUpdateTime(new Date());
               userGoodsStorageMapper.updateByPrimaryKeySelective(goodsStorage);
            } else {
               goodsStorage = new UserGoodsStorage();
               goodsStorage.setUid(uid);
               ;
               goodsStorage.setState(UserGoodsStorage.STATE_NORMAL);
               goodsStorage.setCommonGoods(commonGoods);
               goodsStorage.setCreateTime(new Date());
               goodsStorage.setUpdateTime(new Date());
               userGoodsStorageMapper.insertSelective(goodsStorage);
            }
         }
      }
   }
   @Override
   public JSONArray getMyStorage(int page, int pageSize, Long uid, Integer source) throws UserGoodsStorageException {
      JSONArray array = new JSONArray();
      List<UserGoodsStorage> listStorage = listQueryByUid(page, pageSize, uid, source);
      if (listStorage == null || listStorage.size() == 0) {
         return array;
      }
      Date nowDate = new Date();
      long nh = 1000 * 60 * 60;
      long nd = 1000 * 24 * 60 * 60;
      List<CommonGoods> listUpadteCommonGoods = new ArrayList<CommonGoods>();
      BigDecimal  rate = manageService.getFanLiRate();
      if (source == null) {
         source = Constant.SOURCE_TYPE_TAOBAO;
      }
      List<TaoBaoGoodsBrief> listTaoKeGoods = null;
      if (source == Constant.SOURCE_TYPE_TAOBAO) {
         List<Long> listGid = new ArrayList<Long>();
         for (UserGoodsStorage userGoodsStorage : listStorage) {
            CommonGoods commonGoods = userGoodsStorage.getCommonGoods();
            if (commonGoods == null) {
               continue;
            }
            listGid.add(commonGoods.getGoodsId());
         }
         // API网络接口验证是否在售
         try {
            listTaoKeGoods = TaoKeApiUtil.getBatchGoodsInfo(listGid);
         } catch (TaoKeApiException e) {
            e.printStackTrace();
         } catch (TaobaoGoodsDownException e) {
            e.printStackTrace();
         } catch (Exception e) {
            e.printStackTrace();
         }
      }
      BigDecimal rate = manageService.getFanLiRate(UserLevelEnum.daRen);
      Gson gson = JsonUtil.getConvertBigDecimalToStringSubZeroBuilder(new GsonBuilder())
            .excludeFieldsWithoutExposeAnnotation().setDateFormat("yyyy-MM-dd").create();
      for (UserGoodsStorage userGoodsStorage : listStorage) {
         CommonGoods commonGoods = userGoodsStorage.getCommonGoods();
         if (commonGoods == null) {
            continue;
         }
         TaoBaoGoodsBrief goodsBrief = null;
         JSONObject dataObject = new JSONObject();
         // 更新一个小时之内的商品不再更新
         Integer goodsState = commonGoods.getState();
         // 状态未下架时进行更新
         if (goodsState != null && (goodsState != 1 || !goodsState.equals(1))) {
            Date updateTime = commonGoods.getUpdateTime();
            if (updateTime != null) {
               // 计算差多少小时
               long diff = nowDate.getTime() - updateTime.getTime();
               long day = diff / nd;
                long hour = diff % nd / nh;
                if (hour > 1 || day > 0 ) {
                   try {
                      if (commonGoods.getGoodsType() == CommonGoods.GOODS_TYPE_TB) {
                         goodsBrief = redisManager.getTaoBaoGoodsBrief(commonGoods.getGoodsId());
                         goodsState = 0;
                         goodsBrief.setState(goodsState);
                         CommonGoods upadeteCommonGoods = CommonGoodsFactory.create(goodsBrief);
                         upadeteCommonGoods.setState(goodsState);
                        upadeteCommonGoods.setId(commonGoods.getId());
                        listUpadteCommonGoods.add(upadeteCommonGoods);
                      }
                  } catch (TaobaoGoodsDownException e) {
                     // 已下架
                     goodsState = 1;
                     commonGoods.setState(goodsState);
                     listUpadteCommonGoods.add(commonGoods);
                     e.printStackTrace();
                  }
                }
         int state = 0;
         Integer goodsType = commonGoods.getGoodsType();
         if (listTaoKeGoods != null && listTaoKeGoods.size() > 0 && goodsType != null
               && goodsType == Constant.SOURCE_TYPE_TAOBAO) {
            state = 1; // 默认停售
            Long goodsId = commonGoods.getGoodsId();
            for (TaoBaoGoodsBrief taoKeGoods : listTaoKeGoods) {
               Long auctionId = taoKeGoods.getAuctionId();
               if (goodsId == auctionId || goodsId.equals(auctionId)) {
                  state = 0; // 在售
                  break;
               }
            }
         }
         if (goodsBrief == null) {
            goodsBrief = TaoBaoUtil.convert(commonGoods);
         }
         commonGoods.setState(state);
         TaoBaoGoodsBrief goodsBrief = TaoBaoUtil.convert(commonGoods);
         // 判断是否已分享
         Integer state = userGoodsStorage.getState();
         int shared = UserGoodsStorage.STATE_SHARED;
         if (state != null && (state == shared || state.equals(shared))) {
            if (goodsState != 1 || !goodsState.equals(1)) {
               goodsBrief.setState(2); // 已分享状态
         Integer storageState = userGoodsStorage.getState();
         if (storageState != null && storageState == UserGoodsStorage.STATE_SHARED) {
            Integer goodsState = goodsBrief.getState();
            if (goodsState != null && goodsState != 1) {
               goodsBrief.setState(2); // 已分享状态 优先显示已下架
            }
         }
         // 改变图片尺寸
         String pictUrl = goodsBrief.getPictUrl();
         if (!StringUtil.isNullOrEmpty(pictUrl) && !pictUrl.contains("320x320")) {
            goodsBrief.setPictUrl(TbImgUtil.getTBSize320Img(pictUrl));
         }
         String json = gson.toJson(TaoBaoUtil.getTaoBaoGoodsBriefExtra(goodsBrief, rate.toString(), null));
         JSONObject dataObject = new JSONObject();
         dataObject.put("storageId", userGoodsStorage.getId());
         dataObject.put("goods", json);
         array.add(dataObject);
      }
      if (listUpadteCommonGoods.size() > 0) {
         executor.execute(new Runnable() {
            @Override
            public void run() {
               try {
                  // 更新简版信息
                  commonGoodsService.updateBatchCommonGoods(listUpadteCommonGoods);
               } catch (Exception e) {
                  e.printStackTrace();
               }
            }
         });
      }
      return array;
   }
   @Override
   public boolean isExistStorage(Long uid, Long auctionId) {
   public boolean isExistStorage(Long uid, Long auctionId, Integer goodType) {
      boolean state = false;
      if (uid != null && auctionId != null) {
         UserGoodsStorage userGoodsStorage = userGoodsStorageMapper.getByUidAndAuctionId(uid, auctionId);
         UserGoodsStorage userGoodsStorage = userGoodsStorageMapper.getByUidAndAuctionId(uid, auctionId, goodType);
         if (userGoodsStorage != null) {
            state = true;
         }
      }
      return state;
   }
   @Override
   public JSONObject shareGoods(Long uid, List<Long> listStorageID)
   public JSONObject shareGoods(Long uid, List<Long> listStorageID)
         throws UserGoodsStorageException, UserShareGoodsRecordException {
      List<UserGoodsStorage> listStorage = userGoodsStorageMapper.listQueryByIds(listStorageID);
      if (listStorage == null || listStorage.size() == 0) {
         throw new UserGoodsStorageException(1, "选品库商品不存在");
      }
      List<UserGoodsStorage> listResult = new ArrayList<UserGoodsStorage>();
      // 还原顺序 用于制图
      for (Long sid : listStorageID) {
         for (UserGoodsStorage torage : listStorage) {
            Long id = torage.getId();
            if (sid == id || sid.equals(id)) {
               listResult.add(torage);
               break;
            }
         }
      }
      BigDecimal totalMoney = new BigDecimal(0.00);
      List<TaoBaoGoodsBrief> listGoodsBrief = new ArrayList<TaoBaoGoodsBrief>();
      String rateStr = hongBaoManageService.get("hongbao_goods_proportion");
      for (UserGoodsStorage userGoodsStorage: listStorage) {
      BigDecimal rate = hongBaoManageService.getShareRate(UserLevelEnum.daRen);
      for (UserGoodsStorage userGoodsStorage : listResult) {
         CommonGoods commonGoods = userGoodsStorage.getCommonGoods();
         TaoBaoGoodsBrief goodsBrief = TaoBaoUtil.convert(commonGoods);
         listGoodsBrief.add(goodsBrief);
         // 已分享
         userGoodsStorage.setState(1);
         userGoodsStorage.setUpdateTime(new Date());
         BigDecimal money = TaoBaoUtil.getGoodsHongBaoMoney(goodsBrief, new BigDecimal(rateStr));
         BigDecimal money = TaoBaoUtil.getGoodsHongBaoMoney(goodsBrief, rate);
         totalMoney = MoneyBigDecimalUtil.add(totalMoney, money);
      }
      String revenue = listGoodsBrief.size() + "个商品预估分享奖金:¥"+totalMoney;
      FileUploadResult loadResult = userShareGoodsRecordService.save(uid, ShareSourceTypeEnum.storage,
            null, listGoodsBrief);
      // 更新为已分享
      userGoodsStorageMapper.updateBatchSelective(listStorage);
      ShareGoodsRecordDTO shareRecord = userShareGoodsRecordService.addRecordGoodsStorage(uid, listGoodsBrief,
            listStorageID);
      FileUploadResult uploadResult = new FileUploadResult();
      uploadResult.setUrl(shareRecord.getSharePictureUrl());
      JSONObject data = new JSONObject();
      data.put("shareImg", loadResult);
      data.put("revenue", revenue);
      data.put("notifyDesc", configService.get("goods_share_notify"));
      data.put("revenue", listGoodsBrief.size() + "个商品预估分享奖金:¥" + totalMoney);
      data.put("shareId", shareRecord.getRedisKey());
      data.put("shareImg", uploadResult);
      data.put("notifyDesc", configService.get(ConfigKeyEnum.goodsShareMultipleNotify.getKey()));
      return data;
   }
   @Override
   public JSONObject createShareV2(Long uid, List<Long> listStorageID, boolean needDrawPicture)
         throws UserGoodsStorageException, UserShareGoodsRecordException {
      List<UserGoodsStorage> listStorage = userGoodsStorageMapper.listQueryByIds(listStorageID);
      if (listStorage == null || listStorage.size() == 0) {
         throw new UserGoodsStorageException(1, "选品库商品不存在");
      }
      List<UserGoodsStorage> listResult = new ArrayList<UserGoodsStorage>();
      // 还原顺序 用于制图
      for (Long sid : listStorageID) {
         for (UserGoodsStorage torage : listStorage) {
            Long id = torage.getId();
            if (sid == id || sid.equals(id)) {
               listResult.add(torage);
               break;
            }
         }
      }
      BigDecimal totalMoney = new BigDecimal(0.00);
      List<CommonGoods> listCommonGoods = new ArrayList<CommonGoods>();
      Integer goodsType = null;
      BigDecimal rate = hongBaoManageService.getShareRate(UserLevelEnum.daRen);
      for (UserGoodsStorage userGoodsStorage : listResult) {
         CommonGoods commonGoods = userGoodsStorage.getCommonGoods();
         BigDecimal money = CommonGoodsUtil.getCommission(commonGoods, rate);
         totalMoney = MoneyBigDecimalUtil.add(totalMoney, money);
         listCommonGoods.add(commonGoods);
         if (goodsType == null) {
            goodsType = commonGoods.getGoodsType();
         }
      }
      ShareGoodsRecordDTO shareRecord = userShareGoodsRecordService.addRecordGoodsStorageV2(uid, listCommonGoods,
            listStorageID, needDrawPicture);
      FileUploadResult uploadResult = new FileUploadResult();
      uploadResult.setUrl(shareRecord.getSharePictureUrl());
      JSONObject data = new JSONObject();
      data.put("revenue", listCommonGoods.size() + "个商品预估分享奖金:¥" + totalMoney);
      data.put("shareId", shareRecord.getRedisKey());
      data.put("shareImg", uploadResult);
      // 提示语不同
      if (goodsType == Constant.SOURCE_TYPE_JD) {
         // 京东
         data.put("notifyDesc", configService.get(ConfigKeyEnum.goodsShareNotifyJD.getKey()));
      } else if (goodsType == Constant.SOURCE_TYPE_PDD) {
         // 拼多多
         data.put("notifyDesc", configService.get(ConfigKeyEnum.goodsShareNotifyPDD.getKey()));
      } else {
         data.put("notifyDesc", configService.get(ConfigKeyEnum.goodsShareMultipleNotify.getKey()));
      }
      return data;
   }
}