package com.platform.service;

import com.platform.dao.ApiBrandMapper;
import com.platform.entity.BrandVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;


@Service
public class ApiBrandService {
	@Autowired
	private ApiBrandMapper brandDao;
	
	
	public BrandVo queryObject(Integer id){
		return brandDao.queryObject(id);
	}

	public List<Integer> getBrandTypeList(){
		return brandDao.getBrandTypeList();
	}

	public Integer queryIdByScene(String sceneNum){
		if(StringUtils.isBlank(sceneNum)){
			return null;
		}
		return  brandDao.queryIdByScene(sceneNum);
	}

	public BrandVo queryByScene(String sceneNum){
		if(StringUtils.isBlank(sceneNum)){
			return null;
		}
		return  brandDao.queryByScene(sceneNum);
	}
	
	public List<BrandVo> queryList(Map<String, Object> map){
		return brandDao.queryList(map);
	}
	
	
	public int queryTotal(Map<String, Object> map){
		return brandDao.queryTotal(map);
	}
	
	
	public void save(BrandVo brand){
		brandDao.save(brand);
	}
	
	
	public void update(BrandVo brand){
		brandDao.update(brand);
	}
	
	
	public void delete(Integer id){
		brandDao.delete(id);
	}
	
	
	public void deleteBatch(Integer[] ids){
		brandDao.deleteBatch(ids);
	}

	public List<BrandVo> getRecommondDeptBrand() {
		List<BrandVo> brandVos = brandDao.getRecommondDeptBrand();
		// 根据企业id获取该企业下访问量最多的场景
		brandVos.stream().forEach(brandVo -> brandVo.setId(getTopPvNumByDeptId(brandVo.getId())));
		return brandVos;
	}

	private Integer getTopPvNumByDeptId(Integer deptId) {
		if(ObjectUtils.isEmpty(deptId)){
			return deptId;
		}
		return brandDao.getTopPvNumByDeptId(deptId);
	}

	public Long increaseViewCount(String brandId) {
		if(ObjectUtils.isEmpty(brandId)){
			return 0L;
		}
		// 查找数据
		brandDao.increaseViewCount(brandId);
		BrandVo brandVo = brandDao.queryObject(brandId);
		if(!ObjectUtils.isEmpty(brandVo) && !ObjectUtils.isEmpty(brandVo.getPvTotalNum())){
			return brandVo.getPvTotalNum();
		}
		return 0L;
	}
}
