yujian
2019-04-26 77602de9c796b5fd42b74003727d934df14152fa
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
package com.yeshi.fanli.service.impl.taobao;
 
import java.io.InputStream;
import java.util.UUID;
 
import javax.annotation.Resource;
 
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.entity.taobao.TaoBaoGoodsBrief;
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.taobao.TaoBaoShopService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.db.MongoDBManager;
import com.yeshi.fanli.util.taobao.TaoBaoShopUtil;
 
@Service
public class TaoBaoShopServiceImpl implements TaoBaoShopService {
 
    @Resource
    private MongoDBManager mongoDBManager;
    
    @Resource
    private TaoBaoShopMapper taoBaoShopMapper;
 
    @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) {
        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);
            }
        }
        
        updateShop.setId(id);
        updateShop.setShopName(shopName);
        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;
    }
 
    
 
}