package com.cirle.scientific.service.impl;

import com.cirle.scientific.dao.QkjArticleDao;
import com.cirle.scientific.dao.QkjTagsDao;
import com.cirle.scientific.po.QkjArticle;
import com.cirle.scientific.po.QkjTags;
import com.cirle.scientific.pojo.BizCodeEnum;
import com.cirle.scientific.pojo.Result;
import com.cirle.scientific.service.ArticleService;
import com.cirle.scientific.service.CommonService;
import com.cirle.scientific.service.RecommendService;
import com.cirle.scientific.utils.DateFormater;
import com.cirle.scientific.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
@Transactional
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private QkjArticleDao articleDao;

    @Autowired
	private CommonService commonService;

    @Autowired
	private RecommendService recommendService;

    @Autowired
	QkjTagsDao qkjTagsDao;


	@Override
	public Result getArtListData(String memCard, Integer cataId, Integer page, Integer size,String sortBy,boolean isPC) {
		Integer[] mOrPc= new Integer[2];//电脑
		if(isPC){ //手机
			mOrPc[0]=0;
			mOrPc[1]=2;
		}else{
			mOrPc[0]=1;
			mOrPc[1]=2;
		}
		List<QkjArticle> articleList = null;
		//默认获取最新数据，如果 sortBy = hottest (热门数据，根据点击量排序)
		if(cataId == -1){
			if ("hottest".equals(sortBy)){
				articleList = articleDao.findListByHottest((page-1)*size, size, mOrPc);
			}else {
				articleList = articleDao.findList((page-1)*size, size,mOrPc);
			}
		}else{
			if ("hottest".equals(sortBy)){
				articleList = articleDao.ByCataIdHottest(cataId, (page-1)*size, size,mOrPc);
			}else {
				articleList = articleDao.findByCataId(cataId, (page-1)*size, size,mOrPc);
			}
		}

		List<Map<String, Object>> productList = articleList.stream().map(item ->{
			return makeArticleMap(item);
		}).collect(Collectors.toList());
		return Result.success(productList);
	}

    @Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public Result getDetailData(Long artId, String memCard) {
		QkjArticle article = articleDao.findById(artId).get();
		if(article != null){
			//增加点击量
			int random=(int)(Math.random()*5+1);
			article.setClick(article.getClick()+random);
			article.setVclick(article.getVclick()+1);
			article = articleDao.save(article);

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("info", makeArticleMap(article));
			map.put("ads", recommendService.findByPostTypeAndObjId(3, article.getId().intValue(), 1, 100));
			return Result.success(map);
		}
		return Result.failed("获取文章详情失败");
    }

    @Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public Result updataClick(Long artId) {
		Optional<QkjArticle> byId = articleDao.findById(artId);
		if (byId != null && byId.isPresent()) {
			QkjArticle qkjArticle = byId.get();
			Integer vclick = qkjArticle.getVclick();
			qkjArticle.setVclick(vclick+1);
			Integer click = qkjArticle.getClick();
			qkjArticle.setClick(click+1);
			articleDao.save(qkjArticle);
		}
		return Result.success(BizCodeEnum.SUCCESS);
    }

    private Map<String, Object> makeArticleMap(QkjArticle item){
		Map<String, Object> product = new HashMap<String, Object>();
		product.put("id", item.getId());
		product.put("title", item.getTitle());
		product.put("coverImg", commonService.buildImgUrl(item.getCoverImg()));
		product.put("author", item.getAuthor());
		product.put("publishTime", DateFormater.formatDate(item.getPublishTime(),"yyyy年MM月dd日"));
		product.put("type", item.getType());
		product.put("click", StringUtil.getTenThousandOfANumber(item.getClick())+"次阅读");
		product.put("content", item.getContent());
		//保存标签
		List<QkjTags> tags = qkjTagsDao.findTagsByObjIdAndObjType(item.getId(), 4);
		if (tags != null && !tags.isEmpty()) {
			product.put("tags", tags);
		}
		return product;
	}

}
