admin
2019-07-30 573c491b4a1ba60e12a5678a01c1546c0077c1ee
fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserGoodsStorageServiceImpl.java
@@ -4,35 +4,40 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
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.yeshi.utils.JsonUtil;
import org.yeshi.utils.taobao.TbImgUtil;
import org.yeshi.utils.entity.FileUploadResult;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yeshi.fanli.dao.mybatis.user.UserGoodsStorageMapper;
import com.yeshi.fanli.dto.share.ShareGoodsRecordDTO;
import com.yeshi.fanli.entity.bus.user.UserGoodsStorage;
import com.yeshi.fanli.entity.goods.CommonGoods;
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.user.UserGoodsStorageService;
import com.yeshi.fanli.service.inter.user.UserShareGoodsRecordService;
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.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 {
@@ -44,6 +49,9 @@
   private RedisManager redisManager;
   
   @Resource
   private ConfigService configService;
   @Resource
   private CommonGoodsService commonGoodsService;
   
   @Resource
@@ -51,6 +59,11 @@
   @Resource
   private UserGoodsStorageMapper userGoodsStorageMapper;
   @Resource
   private UserShareGoodsRecordService userShareGoodsRecordService;
   @Resource
   private HongBaoManageService hongBaoManageService;
   @Override
   public int deleteByPrimaryKey(Long id) {
@@ -117,7 +130,7 @@
   public long countQueryByUid(Long uid) {
      return userGoodsStorageMapper.countQueryByUid(uid);
   }
   @Override
   public void save(Long uid, Set<Long> auctionIds) throws UserGoodsStorageException {
@@ -189,17 +202,31 @@
         return array;
      }
      
      Date nowDate = new Date();
      long nh = 1000 * 60 * 60;
      long nd = 1000 * 24 * 60 * 60;
      List<CommonGoods> listUpadteCommonGoods = new ArrayList<CommonGoods>();
      List<Long> listGid = new ArrayList<Long>();
      for (UserGoodsStorage userGoodsStorage : listStorage) {
         CommonGoods commonGoods = userGoodsStorage.getCommonGoods();
         if (commonGoods == null) {
            continue;
         }
         listGid.add(commonGoods.getGoodsId());
      }
      
      // API网络接口验证是否在售
      List<TaoBaoGoodsBrief> listTaoKeGoods = null;
      try {
         listTaoKeGoods = TaoKeApiUtil.getBatchGoodsInfo(listGid);
      } catch (TaoKeApiException e) {
         e.printStackTrace();
      } catch (TaobaoGoodsDownException e) {
         e.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      }
      BigDecimal  rate = manageService.getFanLiRate();
      Gson gson = JsonUtil.getConvertBigDecimalToStringSubZeroBuilder(new GsonBuilder())
            .excludeFieldsWithoutExposeAnnotation().setDateFormat("yyyy-MM-dd").create();
      Map<String, String> map = manageService.convertMap();
      BigDecimal  rate = manageService.getFanLiRate();
      
      for (UserGoodsStorage userGoodsStorage : listStorage) {
         CommonGoods commonGoods = userGoodsStorage.getCommonGoods();
@@ -207,79 +234,36 @@
            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 hour = diff % nd / nh;
                if (hour > 1 ) {
                   try {
                      if (commonGoods.getGoodsType() == CommonGoods.GOODS_TYPE_TB) {
                         goodsBrief = redisManager.getTaoBaoGoodsBrief(commonGoods.getGoodsId());
                         goodsBrief.setState(0);
                         goodsState = 0;
                      }
                  } catch (TaobaoGoodsDownException e) {
                     // 已下架
                     goodsState = 1;
                     commonGoods.setState(1);
                     e.printStackTrace();
                  }
                }
         if (listTaoKeGoods != null && listTaoKeGoods.size() > 0 ) {
            int state = 1;  // 默认停售
            Long goodsId = commonGoods.getGoodsId();
            for (TaoBaoGoodsBrief taoKeGoods: listTaoKeGoods) {
               Long auctionId = taoKeGoods.getAuctionId();
               if (goodsId == auctionId || goodsId.equals(auctionId)) {
                  state = 0; // 在售
                  break;
               }
            }
            commonGoods.setState(state);
         }
         
         if (goodsBrief == null) {
            goodsBrief = TaoBaoUtil.convert(commonGoods);
         } else {
            CommonGoods upadeteCommonGoods = CommonGoodsFactory.create(goodsBrief);
            upadeteCommonGoods.setState(goodsState);
            listUpadteCommonGoods.add(upadeteCommonGoods);
         }
         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 = commonGoods.getPicture();
         if (!StringUtil.isNullOrEmpty(pictUrl) && !pictUrl.contains("320x320")) {
            commonGoods.setPicture(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;
@@ -297,4 +281,53 @@
      }
      return state;
   }
   @Override
   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>();
      BigDecimal rate = hongBaoManageService.getShareRate();
      for (UserGoodsStorage userGoodsStorage: listResult) {
         CommonGoods commonGoods = userGoodsStorage.getCommonGoods();
         TaoBaoGoodsBrief goodsBrief = TaoBaoUtil.convert(commonGoods);
         listGoodsBrief.add(goodsBrief);
         BigDecimal money = TaoBaoUtil.getGoodsHongBaoMoney(goodsBrief, rate);
         totalMoney = MoneyBigDecimalUtil.add(totalMoney, money);
      }
      ShareGoodsRecordDTO shareRecord = userShareGoodsRecordService.addRecordGoodsStorage(uid, listGoodsBrief, listStorageID);
      FileUploadResult uploadResult = new FileUploadResult();
      uploadResult.setUrl(shareRecord.getSharePictureUrl());
      JSONObject data = new JSONObject();
      data.put("revenue", listGoodsBrief.size() + "个商品预估分享奖金:¥"+totalMoney);
      data.put("shareId", shareRecord.getRedisKey());
      data.put("shareImg", uploadResult);
      data.put("notifyDesc", configService.get("goods_share_multiple_notify"));
      return data;
   }
}