admin
2019-07-18 8579b9ea8205a2bcea72d2c9a843fdad1ed2e665
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.yeshi.fanli.service.impl.taobao;
 
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 org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.taobao.TLJBuyGoodsDao;
import com.yeshi.fanli.dto.taobao.TaoLiJinDTO;
import com.yeshi.fanli.entity.taobao.TLJBuyGoods;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoKeAppInfo;
import com.yeshi.fanli.entity.taobao.dataoke.DaTaoKeDetailV2;
import com.yeshi.fanli.exception.taobao.TaoKeApiException;
import com.yeshi.fanli.service.inter.goods.TaoBaoGoodsBriefService;
import com.yeshi.fanli.service.inter.taobao.TLJBuyGoodsService;
import com.yeshi.fanli.service.inter.taobao.dataoke.DaTaoKeGoodsDetailService;
import com.yeshi.fanli.service.inter.taobao.dataoke.DaTaoKeGoodsDetailV2Service;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TaoBaoConstant;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 
@Service
public class TLJBuyGoodsServiceImpl implements TLJBuyGoodsService {
    @Resource
    private TLJBuyGoodsDao tljBuyGoodsDao;
 
    @Resource
    private DaTaoKeGoodsDetailV2Service daTaoKeGoodsDetailV2Service;
 
    @Resource
    private TaoBaoGoodsBriefService taoBaoGoodsBriefService;
 
    @Resource
    private DaTaoKeGoodsDetailService daTaoKeGoodsDetailService;
 
    @Resource
    private EhCacheCacheManager ehCacheCacheManager;
 
    @Override
    public void addTLJBuyGoods(TLJBuyGoods goods, TaoKeAppInfo taoKeAppInfo) {
        if (goods == null || StringUtil.isNullOrEmpty(goods.getDay()) || goods.getGoods() == null)
            return;
        goods.setId(StringUtil.Md5(goods.getDay() + "#" + goods.getGoods().getAuctionId()));
        if (goods.getUpdateTime() == null)
            goods.setUpdateTime(new Date());
        if (taoKeAppInfo != null)
            goods.setAppKey(taoKeAppInfo.getAppKey());
        tljBuyGoodsDao.save(goods);
    }
 
    @Cacheable(value = "commonContentCache", key = "'tljBuy-listByDay'+#day")
    @Override
    public List<TLJBuyGoods> listByDay(String day) {
        List<TLJBuyGoods> list = listByDay(day, 10);
        // if (list.size() <= 10)
        // return list;
        // else {
        // List<TLJBuyGoods> newList = new ArrayList<>();
        // for (int i = 0; i < 10; i++) {
        // newList.add(list.get(i));
        // }
        // return newList;
        // }
        return list;
    }
 
    @Override
    public List<TLJBuyGoods> listByDay(String day, int count) {
        List<TLJBuyGoods> list = tljBuyGoodsDao.listByDayOrderByUpdateTime(day, 1, count);
        List<TaoBaoGoodsBrief> goodsList = new ArrayList<>();
        if (list != null) {
            for (TLJBuyGoods shg : list) {
                // 计算推广红包
                BigDecimal spreadMoney = TaoBaoUtil.getGoodsHongBaoMoney(shg.getGoods(),
                        TaoBaoConstant.OWN_BUY_WITHOUT_FANLI_RATE);
                BigDecimal finalPrice = TaoBaoUtil.getAfterUseCouplePrice(shg.getGoods());
 
                // 推广红包 不能小于1,价格不能大于30
                if (spreadMoney.compareTo(new BigDecimal(1.0)) < 0 || finalPrice.compareTo(new BigDecimal(30)) >= 0) {
                    continue;
                }
 
                goodsList.add(shg.getGoods());
            }
        }
        // 过滤下线商品
        goodsList = taoBaoGoodsBriefService.filterOffLineGoods(goodsList);
        // 添加大淘客信息
        goodsList = daTaoKeGoodsDetailService.filterTaoBaoGoods(goodsList);
        Map<Long, TaoBaoGoodsBrief> goodsMap = new HashMap<>();
        if (goodsList != null)
            for (TaoBaoGoodsBrief g : goodsList)
                goodsMap.put(g.getAuctionId(), g);
 
        for (int j = 0; j < list.size(); j++) {
            if (goodsMap.get(list.get(j).getGoods().getAuctionId()) == null) {
                list.remove(j);
                j--;
            }
        }
        return list;
    }
 
    @Override
    public void deleteById(String id) {
        tljBuyGoodsDao.delete(id);
    }
 
    @Override
    public List<TaoBaoGoodsBrief> listPreGoods(int page) {
        List<DaTaoKeDetailV2> list = daTaoKeGoodsDetailV2Service.listPreTLJBuyGoods(page);
        List<TaoBaoGoodsBrief> goodsList = new ArrayList<>();
        if (list != null) {
            for (DaTaoKeDetailV2 v2 : list)
                goodsList.add(TaoBaoUtil.convert(v2));
        }
        return goodsList;
    }
 
    @Override
    public void deleteByGoodsId(Long goodsId) {
        tljBuyGoodsDao.deleteByGoodsId(goodsId);
    }
 
    @Override
    public boolean verifyCanCreateTLJ(Long auctionId) {
        TaoLiJinDTO taoLiJinDTO = null;
        try {
            taoLiJinDTO = TaoKeApiUtil.createTaoLiJin(auctionId, "淘礼金验证", new BigDecimal("1.00"), 1, new Date(),
                    new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 6), new Date(),
                    new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 6), TaoBaoConstant.ownBuyApp);
        } catch (TaoKeApiException e) {
            if (e.getCode() == TaoKeApiException.CODE_TLJ_FORBIDDEN) {
                // 该商品不支持创建淘礼金红包
                deleteByGoodsId(auctionId);
                return false;
            } else if (e.getCode() == TaoKeApiException.CODE_TLJ_NO_MONEY) {
                // 官方玩法钱包余额不足 TODO
            }
        }
        return true;
    }
 
    @Override
    public void updateGoods(TaoBaoGoodsBrief goods) {
        if (goods == null)
            return;
        List<TLJBuyGoods> list = tljBuyGoodsDao.listByAuctionId(goods.getAuctionId());
        // 查询自购立减是否小于1元
        BigDecimal money = TaoBaoUtil.getGoodsHongBaoMoney(goods, TaoBaoConstant.OWN_BUY_WITHOUT_FANLI_RATE);
        // if (money.compareTo(new BigDecimal(1)) < 0) {// 删除商品
        // if (list != null)
        // for (TLJBuyGoods tljGoods : list) {
        // tljBuyGoodsDao.delete(tljGoods.getId());
        // }
        // }
 
        if (list != null)
            for (TLJBuyGoods TLJBuyGoods : list) {
 
                if (goods.getCouponInfo() == null)
                    goods.setCouponInfo("");
                if (goods.getCouponAmount() == null)
                    goods.setCouponAmount(new BigDecimal(0));
 
                // 判断 券信息 价格信息 返利比例信息是否改变
                boolean change = false;
                if (goods.getZkPrice().compareTo(TLJBuyGoods.getGoods().getZkPrice()) != 0)
                    change = true;
                if (goods.getCouponAmount().compareTo(TLJBuyGoods.getGoods().getCouponAmount()) != 0)
                    change = true;
                if (!goods.getCouponInfo().equalsIgnoreCase(TLJBuyGoods.getGoods().getCouponInfo()))
                    change = true;
                if (goods.getTkRate().compareTo(TLJBuyGoods.getGoods().getTkRate()) != 0)
                    change = true;
 
                if (change) {
                    // 清除列表缓存
                    ehCacheCacheManager.getCache("commonContentCache").evict(
                            "tljBuy-listByDay" + TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd"));
                }
 
                // 更新券信息 ,价格信息,销量信息
                TLJBuyGoods.getGoods().setZkPrice(goods.getZkPrice());
                TLJBuyGoods.getGoods().setCouponAmount(goods.getCouponAmount());
                TLJBuyGoods.getGoods().setCouponInfo(goods.getCouponInfo());
                TLJBuyGoods.getGoods().setCouponEffectiveEndTime(goods.getCouponEffectiveEndTime());
                TLJBuyGoods.getGoods().setCouponEffectiveStartTime(goods.getCouponEffectiveStartTime());
                TLJBuyGoods.getGoods().setCouponLeftCount(goods.getCouponLeftCount());
                TLJBuyGoods.getGoods().setCouponStartFee(goods.getCouponStartFee());
                TLJBuyGoods.getGoods().setCouponTotalCount(goods.getCouponTotalCount());
                TLJBuyGoods.getGoods().setBiz30day(goods.getBiz30day());
                TLJBuyGoods.getGoods().setTkRate(goods.getTkRate());
                tljBuyGoodsDao.save(TLJBuyGoods);
            }
    }
 
    @Override
    public void subHongBaoLeftCount(Long auctionId, String day, int count) {
        TLJBuyGoods goods = tljBuyGoodsDao.selectByAuctionIdAndDay(auctionId, day);
        if (goods != null) {
            tljBuyGoodsDao.subHongBaoCount(goods.getId(), count);
        }
    }
 
    @Override
    public TLJBuyGoods selectByAuctionIdAndDay(Long auctionId, String day) {
        return tljBuyGoodsDao.selectByAuctionIdAndDay(auctionId, day);
    }
}