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);
|
}
|
|
|
|
}
|