yujian
2019-10-29 9b55262bb45c81c997ab04e55e8de13e14b8ca7d
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package com.yeshi.fanli.service.impl.taobao;
 
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
 
import javax.annotation.Resource;
 
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.tencentcloud.COSManager;
 
import com.yeshi.fanli.dao.mybatis.taobao.TaoBaoShopMapper;
import com.yeshi.fanli.dto.ConfigParamsDTO;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBriefExtra;
import com.yeshi.fanli.entity.taobao.TaoBaoShop;
import com.yeshi.fanli.entity.taobao.TaoBaoShopInfo;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoShopService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.db.MongoDBManager;
import com.yeshi.fanli.util.factory.goods.GoodsDetailVOFactory;
import com.yeshi.fanli.util.taobao.TaoBaoShopUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.vo.brand.TaoBaoShopVO;
import com.yeshi.fanli.vo.goods.GoodsDetailVO;
 
@Service
public class TaoBaoShopServiceImpl implements TaoBaoShopService {
 
    @Resource
    private MongoDBManager mongoDBManager;
 
    @Resource
    private TaoBaoShopMapper taoBaoShopMapper;
 
    @Resource
    private HongBaoManageService hongBaoManageService;
 
    @Override
    public TaoBaoShopInfo getTaoBaoShopInfo(TaoBaoGoodsBrief goodsInfo) {
        if (goodsInfo == null)
            return null;
        long startTime = System.currentTimeMillis();
        TaoBaoShopInfo shopInfo = null;
        try {
            shopInfo = mongoDBManager.getTBShopInfo(goodsInfo.getSellerId());
        } catch (Exception e) {
            LogHelper.error(e);
        }
 
        if (shopInfo == null) {
            shopInfo = TaoBaoShopUtil.getTaoBaoShopInfo(goodsInfo.getShopTitle(), goodsInfo.getSellerId(),
                    goodsInfo.getAuctionId());
            if (shopInfo != null)
                try {
                    mongoDBManager.saveTBShopInfo(shopInfo);
                } catch (Exception e) {
                }
        }
 
        LogHelper.test("获取店铺信息耗时:" + (System.currentTimeMillis() - startTime));
        return shopInfo;
    }
 
    @Override
    public TaoBaoShop selectByPrimaryKey(Long id) {
        return taoBaoShopMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public int insertSelective(TaoBaoShop record) {
        return taoBaoShopMapper.insertSelective(record);
    }
 
    @Override
    public int updateByPrimaryKeySelective(TaoBaoShop record) {
        return taoBaoShopMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public void changeInfo(MultipartFile file, Long id, String shopName, String key) {
        TaoBaoShop taoBaoShop = taoBaoShopMapper.selectByPrimaryKey(id);
        if (taoBaoShop == null) {
            return;
        }
 
        String fileLink = null;
        if (file != null) {
            try {
                fileLink = uploadPicture(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
 
        TaoBaoShop updateShop = new TaoBaoShop();
        if (!StringUtil.isNullOrEmpty(fileLink)) {
            updateShop.setShopIconCustom(fileLink);
 
            // 删除图片
            String shopIconCustom = taoBaoShop.getShopIconCustom();
            if (!StringUtil.isNullOrEmpty(shopIconCustom)) {
                COSManager.getInstance().deleteFile(shopIconCustom);
            }
        }
 
        if (StringUtil.isNullOrEmpty(shopName) || "null".equalsIgnoreCase(shopName)) {
            shopName = null;
        }
 
        updateShop.setId(id);
        updateShop.setShopNameCustom(shopName);
        updateShop.setKey(key);
        taoBaoShopMapper.updateByPrimaryKeySelective(updateShop);
    }
 
    /**
     * 上传图片
     * 
     * @param file
     * @return
     * @throws Exception
     */
    public String uploadPicture(MultipartFile file) throws Exception {
        // 文件解析
        InputStream inputStream = file.getInputStream();
        String contentType = file.getContentType();
        String type = contentType.substring(contentType.indexOf("/") + 1);
 
        // 文件路径
        String filePath = "/img/TaoBaoShop/" + UUID.randomUUID().toString().replace("-", "") + "." + type;
        // 执行上传
        String fileLink = COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
 
        return fileLink;
    }
 
    @Override
    public List<TaoBaoShopVO> listBrandShopinfo(long start, int count, Long cid) {
        return taoBaoShopMapper.listBrandShopinfo(start, count, cid);
    }
 
    @Override
    public long countBrandShopinfo(Long cid) {
        return taoBaoShopMapper.countBrandShopinfo(cid);
    }
 
    @Override
    public TaoBaoShop getTaoBaoShop(Long auctionId, Long sellerId) {
        TaoBaoShop shop = taoBaoShopMapper.selectByPrimaryKey(sellerId);
        if (shop != null)
            return shop;
        shop = TaoBaoUtil.getTaoBaoShopDetailByAuctionId(auctionId);
        try {
            if (shop != null)
                taoBaoShopMapper.insert(shop);
        } catch (Exception e) {
        }
        return shop;
    }
 
    @Override
    @Cacheable(value = "brandCache", key = "'getShopByKey-'+#key")
    public List<TaoBaoShopVO> getShopByKey(String key) {
        List<TaoBaoShopVO> list = taoBaoShopMapper.getShopByKey(key);
        if (list == null || list.size() == 0) {
            return null;
        }
 
        BigDecimal proportion = hongBaoManageService.getFanLiRate();
        for (TaoBaoShopVO taoBaoShopVO : list) {
 
            String shopLink = taoBaoShopVO.getShopLink();
            if (StringUtil.isNullOrEmpty(shopLink)) {
                taoBaoShopVO.setShopLink(TaoBaoUtil.getShopLink(taoBaoShopVO.getId()));
            }
 
            String shopNameCustom = taoBaoShopVO.getShopNameCustom();
            if (!StringUtil.isNullOrEmpty(shopNameCustom)) {
                taoBaoShopVO.setShopName(shopNameCustom);
            }
 
            String shopIconCustom = taoBaoShopVO.getShopIconCustom();
            if (!StringUtil.isNullOrEmpty(shopIconCustom)) {
                taoBaoShopVO.setShopIcon(shopIconCustom);
            }
 
            List<TaoBaoGoodsBriefExtra> listGoods = new ArrayList<TaoBaoGoodsBriefExtra>();
            List<TaoBaoGoodsBrief> listGoodsBrief = taoBaoShopVO.getListGoodsBrief();
            for (TaoBaoGoodsBrief taoBaoGoodsBrief : listGoodsBrief) {
                listGoods.add(TaoBaoUtil.getTaoBaoGoodsBriefExtra(taoBaoGoodsBrief, proportion.toString(), null));
            }
            taoBaoShopVO.setListGoods(listGoods);
        }
        return list;
    }
    
    
    @Override
    @Cacheable(value = "brandCache", key = "'getShopByKeyV2-'+#key")
    public List<TaoBaoShopVO> getShopByKeyV2(String key) {
        List<TaoBaoShopVO> list = taoBaoShopMapper.getShopByKey(key);
        if (list == null || list.size() == 0) {
            return null;
        }
 
        BigDecimal fanLiRate = hongBaoManageService.getFanLiRate();
        BigDecimal shareRate = hongBaoManageService.getShareRate();
        ConfigParamsDTO paramsDTO = new ConfigParamsDTO(fanLiRate, shareRate, Constant.MAX_REWARD_RATE);
 
        for (TaoBaoShopVO taoBaoShopVO : list) {
            
            Integer userType = taoBaoShopVO.getUserType();
            if (userType == null || userType == 0) {
                taoBaoShopVO.setUserType(10);
            } else {
                taoBaoShopVO.setUserType(11);
            }
 
            String shopLink = taoBaoShopVO.getShopLink();
            if (StringUtil.isNullOrEmpty(shopLink)) {
                taoBaoShopVO.setShopLink(TaoBaoUtil.getShopLink(taoBaoShopVO.getId()));
            }
 
            String shopNameCustom = taoBaoShopVO.getShopNameCustom();
            if (!StringUtil.isNullOrEmpty(shopNameCustom)) {
                taoBaoShopVO.setShopName(shopNameCustom);
            }
 
            String shopIconCustom = taoBaoShopVO.getShopIconCustom();
            if (!StringUtil.isNullOrEmpty(shopIconCustom)) {
                taoBaoShopVO.setShopIcon(shopIconCustom);
            }
 
            List<GoodsDetailVO> listGoods = new ArrayList<GoodsDetailVO>();
            List<TaoBaoGoodsBrief> listGoodsBrief = taoBaoShopVO.getListGoodsBrief();
            for (TaoBaoGoodsBrief taoBaoGoodsBrief : listGoodsBrief) {
                GoodsDetailVO goodsDetailVO = GoodsDetailVOFactory.convertTaoBao(taoBaoGoodsBrief, paramsDTO);
                listGoods.add(goodsDetailVO);
            }
            taoBaoShopVO.setListGoodsVO(listGoods);
        }
        return list;
    }
}