admin
2019-09-17 5eab2b71f0749e9f61fc83d9d7e03553e9fa75ef
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
package com.yeshi.fanli.service.impl.brand;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.brand.BrandShopCaheDao;
import com.yeshi.fanli.dao.mybatis.brand.BrandInfoMapper;
import com.yeshi.fanli.entity.brand.BrandInfo;
import com.yeshi.fanli.entity.brand.BrandShopCahe;
import com.yeshi.fanli.entity.taobao.SearchShopFilter;
import com.yeshi.fanli.entity.taobao.TaoBaoShop;
import com.yeshi.fanli.entity.taobao.TaoBaoShopInfo;
import com.yeshi.fanli.exception.brand.BrandInfoException;
import com.yeshi.fanli.service.inter.brand.BrandClassShopService;
import com.yeshi.fanli.service.inter.brand.BrandGoodsCaheService;
import com.yeshi.fanli.service.inter.brand.BrandInfoService;
import com.yeshi.fanli.service.inter.goods.TaoBaoGoodsBriefService;
import com.yeshi.fanli.service.inter.lable.QualityGoodsService;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoGoodsUpdateService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.goods.ShopInfoVOFactory;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
import com.yeshi.fanli.vo.brand.BrandInfoVO;
import com.yeshi.fanli.vo.brand.TaoBaoShopVO;
 
@Service
public class BrandInfoServiceImpl implements BrandInfoService {
    
    @Resource
    private BrandInfoMapper brandInfoMapper;
    
    @Resource
    private HongBaoManageService hongBaoManageService;
 
    @Resource
    private QualityGoodsService qualityGoodsService;
 
    @Resource
    private TaoBaoGoodsBriefService taoBaoGoodsBriefService;
 
    @Resource
    private TaoBaoGoodsUpdateService taoBaoGoodsUpdateService;
    
    @Resource
    private BrandClassShopService brandClassShopService;
 
    @Resource
    private BrandShopCaheDao brandShopCaheDao;
    
    @Resource
    private BrandGoodsCaheService brandGoodsCaheService;
    
    
    
    @Override
    public void saveObject(BrandInfo record) throws BrandInfoException{
        String name = record.getName();
        if (name == null || name.trim().length() == 0)
            throw new BrandInfoException(1, "名称不能为空");
        
        Integer state = record.getState();
        if (state == null)
            record.setState(0);
        
        Long id = record.getId();
        if (id == null) {
            record.setCreateTime(new Date());
            record.setUpdateTime(new Date());
            brandInfoMapper.insert(record);
        } else {
            BrandInfo resultObj = brandInfoMapper.selectByPrimaryKey(id);
            if (resultObj == null)
                throw new BrandInfoException(1, "修改内容已不存在");
            
            record.setCreateTime(resultObj.getCreateTime());
            record.setUpdateTime(new Date());
            brandInfoMapper.updateByPrimaryKey(record);
        }
    }
    
    
    @Override
    public int deleteBatchByPrimaryKey(List<Long> list) {
        return brandInfoMapper.deleteBatchByPrimaryKey(list);
    }
 
    
    @Override
    public List<BrandInfo> listQuery(long start, int count, String key, Integer state) {
        return null;
    }
 
 
    @Override
    public long countQuery(String key, Integer state) {
        return 0;
    }
    
    @Override
    @Cacheable(value = "brandCache", key = "'listValidBrandInfoCache-'+#cid")
    public List<BrandInfo> listValidBrandInfoCache(Long cid) {
        List<BrandInfo> listInfo = new ArrayList<BrandInfo>();
        
        List<TaoBaoShop> listShop = brandClassShopService.listEffectiveClassShop(cid);
        if (listShop == null || listShop.size() == 0)
            return listInfo;
        for (TaoBaoShop taoBaoShop : listShop) {
            BrandInfo brandInfo = new BrandInfo();
            brandInfo.setId(taoBaoShop.getId());
            brandInfo.setName(taoBaoShop.getShopName());
            brandInfo.setIcon(taoBaoShop.getShopIcon());
            listInfo.add(brandInfo);
        }
        return listInfo;
    }
    
    
    @Override
    @Cacheable(value = "brandCache", key = "'listBrandInfoCache-'+#start+'-'+#start +'-'+#cid")
    public List<BrandInfoVO> listBrandInfoCache(long start, int count, Long cid) {
        List<BrandInfoVO> listInfo = new ArrayList<BrandInfoVO>();
        
        List<TaoBaoShopVO> listShop = brandClassShopService.listEffectiveShop(start, count, cid);
        if (listShop == null || listShop.size() == 0)
            return listInfo;
        
        for (TaoBaoShopVO taoBaoShopVO : listShop) {
            String shopNameCustom = taoBaoShopVO.getShopNameCustom();
            if (!StringUtil.isNullOrEmpty(shopNameCustom)) {
                taoBaoShopVO.setShopName(shopNameCustom);
            }
            String shopIconCustom = taoBaoShopVO.getShopIconCustom();
            if (!StringUtil.isNullOrEmpty(shopIconCustom)) {
                taoBaoShopVO.setShopIcon(shopIconCustom);
            }
            
            BrandInfoVO brandInfoVO = new BrandInfoVO();
            brandInfoVO.setId(taoBaoShopVO.getId());
            brandInfoVO.setName(taoBaoShopVO.getShopName());
            brandInfoVO.setIcon(taoBaoShopVO.getShopIcon());
            brandInfoVO.setListGoods(taoBaoShopVO.getListGoodsVO());
            listInfo.add(brandInfoVO);
        }
        return listInfo;
    }
 
    
    @Override
    public long countBrandInfo(Long cid){
        return brandClassShopService.countBrandShopinfo(cid);
    }
    
    
    public void addShopAndGoods(long start, int count) {
        List<BrandInfo> list = brandInfoMapper.listValidAll(start, count);
        if(list == null || list.size() == 0) 
            return;
        
        for (BrandInfo brandInfo: list) {
            String key = brandInfo.getName();
            if(StringUtil.isNullOrEmpty(key))
                continue;
            
            // 添加店铺
            addBrandShop(brandInfo);
            
            // 添加店铺
            int goodsTotal = brandGoodsCaheService.addBrandGoods(brandInfo);
            
            brandInfo.setGoodsTotal(goodsTotal);
            brandInfoMapper.updateByPrimaryKeySelective(brandInfo);
        }
    }
    
    
    /**
     * 加入品牌店铺
     * @param brandInfo
     */
    private void addBrandShop(BrandInfo brandInfo) {
        SearchShopFilter filter = new SearchShopFilter();
        filter.setKey(brandInfo.getName());
        filter.setPageSize(100);
        filter.setTmall(true);
        filter.setSort("(total_auction_des");
        
        
        TaoBaoShopInfo taoBaoShop = null;
        for(int page = 1; page < 3; page++) {
            filter.setPage(page);
            List<TaoBaoShopInfo> listShop = TaoKeApiUtil.searchShop(filter);
            if(listShop == null || listShop.size() == 0)
                continue;
            
            for (TaoBaoShopInfo shop: listShop) {
                String shopTitle = shop.getShopTitle();
                if(StringUtil.isNullOrEmpty(shopTitle))
                    continue;
                
                if(!shopTitle.contains("旗舰店"))
                    continue;
                
                taoBaoShop = shop;
                break;
            }
            
            if (taoBaoShop != null) 
                break;
        }
        
        BrandShopCahe brandShop = new BrandShopCahe();
        brandShop.setBrandId(brandInfo.getId());
        brandShop.setShop(ShopInfoVOFactory.convertTaoBaoShop(taoBaoShop));
        brandShop.setCreateTime(new Date());
        brandShop.setUpdateTime(new Date());
        brandShopCaheDao.save(brandShop);
    }
    
    
    
}