package com.cirle.scientific.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.cirle.scientific.dao.QkjArticleDao;
import com.cirle.scientific.po.QkjArticle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.cirle.scientific.dao.QkjPositonsDao;
import com.cirle.scientific.dao.QkjRecommendDao;
import com.cirle.scientific.dto.QkjPositionsDTO;
import com.cirle.scientific.dto.QkjRecommendDTO;
import com.cirle.scientific.po.QkjPositions;
import com.cirle.scientific.po.QkjRecommend;
import com.cirle.scientific.service.RecommendService;
import com.cirle.scientific.utils.CopyUtil;

import lombok.extern.slf4j.Slf4j;

@Component
@Transactional
@Slf4j
public class RecommendServiceImpl implements RecommendService {

    @Autowired
    private QkjRecommendDao qkjRecommendDao;

	@Autowired
	private QkjArticleDao articleDao;

    @Autowired
    private QkjPositonsDao qkjPositonsDao;

	@Override
	public List<QkjRecommendDTO> findByPosIdLimit(Long posId, Integer start, Integer size) {
		return findByPosIdLimit(posId, null, start, size);
	}

	@Override
	public List<QkjRecommendDTO> findByPosIdLimit(Long posId, Integer status, Integer start, Integer size) {
		List<QkjRecommend> rcmds = null;
		if (status != null) {
			rcmds = qkjRecommendDao.findByPosId(posId.intValue(), status, (start-1)*size, size);
		} else {
			rcmds = qkjRecommendDao.findByPosId(posId.intValue(), (start-1)*size, size);
		}
//		List<QkjRecommend> rcmds = qkjRecommendDao.findByPosId(posId.intValue(), status, (start-1)*size, size);
		List<QkjRecommendDTO> dtos = new ArrayList<QkjRecommendDTO>();
		for(QkjRecommend r : rcmds){
			if(r.getRcmdType()==3){//资讯
				QkjArticle article = articleDao.findById(Long.valueOf(r.getRcmdObjId())).get();
				r.setAuthor(article.getAuthor());
				r.setClick(article.getClick());
			}
			dtos.add(buildRecommendDTO(r));
		}
		return dtos;
	}

    @Override
	public List<QkjRecommendDTO> findByPostTypeAndObjId(Integer PostType, Integer ObjId, Integer start, Integer size) {
		List<QkjRecommend> rcmds = qkjRecommendDao.findByPostTypeAndObjId(PostType, ObjId, (start-1)*size, size);
		List<QkjRecommendDTO> dtos = new ArrayList<QkjRecommendDTO>();
		for(QkjRecommend r : rcmds){
			dtos.add(buildRecommendDTO(r));
		}
		return dtos;
	}

	@Override
	public QkjPositionsDTO findByPosId(Long posId) {
		return findByPosId(posId, null);
	}

	@Override
	public QkjPositionsDTO findByPosId(Long posId, Integer status) {
    		QkjPositions postInfo = qkjPositonsDao.findById(posId).get();
    		QkjPositionsDTO postDto = new QkjPositionsDTO();
    		postDto.setPosName(postInfo.getPosName());
    		postDto.setPosType(postInfo.getPosType());
    		postDto.setPPosid(postInfo.getPPosid());
    		if(postInfo.getPosName().contains("窄条")){
    			postDto.setAdFlag(1);
    		}
    		if(postInfo.getPosType() == 1 || postInfo.getPosType() == 3){
    			//普通版位或者子版位
    			postDto.setRcmdData(findByPosIdLimit(posId, status, 1, 20));
    		}else if(postInfo.getPosType() == 2){
    			//系列版位
    			List<QkjPositions> postList = qkjPositonsDao.findBypPosId(posId.intValue());
    			List<QkjPositionsDTO> sonDto = new ArrayList<QkjPositionsDTO>();
    			for(QkjPositions pos : postList){
    				sonDto.add(findByPosId(pos.getId(), status));
    			}
    			postDto.setChildren(sonDto);
    		}
		return postDto;
	}

	private QkjRecommendDTO buildRecommendDTO(QkjRecommend recommend) {
		QkjRecommendDTO dto = CopyUtil.copy(recommend, QkjRecommendDTO.class);
    		if(dto.getRcmdImgpath() != null
    				&& dto.getRcmdImgpath().indexOf("http") == -1){
    			dto.setRcmdImgpath("http://files.qkyxzx.com/"+dto.getRcmdImgpath());
    		}
		return dto;
	}
}
