package com.ssj.service.weixin.story.service.impl;

import com.ssj.bean.weixin.story.bean.StoryTaskIntegral;
import com.ssj.dao.weixin.story.dao.StoryQueryDao;
import com.ssj.dao.weixin.story.dao.StoryTaskIntegralDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.weixin.story.service.StoryTaskIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/** 
 * @author  ZhangCaibao 
 * @version 2018年3月21日 上午11:09:38 
 * 类说明 
 */
@Service
@Transactional
public class StoryTaskIntegralServiceImpl extends BaseServiceImpl<StoryTaskIntegral, String> implements StoryTaskIntegralService{
    
	@Autowired
	private StoryTaskIntegralDao storyTaskIntegralDao;
	@Autowired
	private StoryQueryDao storyQueryDao;
	
	@Override
	public PagingAndSortingRepository<StoryTaskIntegral, String> getDao() {
		return storyTaskIntegralDao;
	}

	@Override
	public void saveIntegral(String userId, Integer type) {
		StoryTaskIntegral  storyTaskIntegral =storyTaskIntegralDao.findByUserIdAndType(userId, type);
		if(storyTaskIntegral==null){
			storyTaskIntegral = new StoryTaskIntegral();
			storyTaskIntegral.setType(type);
			storyTaskIntegral.setUserId(userId);
			storyTaskIntegral.setNum(1);
		}else{
			Integer num = storyTaskIntegral.getNum();
			storyTaskIntegral.setNum(num+1);
		}
		this.save(storyTaskIntegral);
	}

	@Override
	public List<StoryTaskIntegral> findByUserId(String userId) {
		return storyTaskIntegralDao.findByUserId(userId);
	}

	@Override
	public Integer countIntegral(String userId) {
		return storyTaskIntegralDao.countIntegral(userId);
	}

	@Override
	public List<String> getTitle(String userId) {
		List<String> str =new ArrayList<String>();
		List<String> list=storyTaskIntegralDao.findByType(1);
		if(list!=null && list.size()>0&&list.contains(userId)){
			str.add("记忆大师");
		}
		 list=storyTaskIntegralDao.findByType(2);
		if(list!=null && list.size()>0&&list.contains(userId)){
			str.add("文字侦探");
		}
		 list=storyTaskIntegralDao.findByType(3);
		if(list!=null && list.size()>0&&list.contains(userId)){
			str.add("鉴赏达人");
		}
		 list=storyTaskIntegralDao.findByType(4);
		if(list!=null && list.size()>0&&list.contains(userId)){
			str.add("运用能手");
		}
		return str;
	}
	@Override
	public String getUserTitle(String userId) {
		String str ="";
		List<String> list=storyTaskIntegralDao.findByType(1);
		if(list!=null && list.size()>0&&list.contains(userId)){
			return "记忆大师";
		}
		list=storyTaskIntegralDao.findByType(2);
		if(list!=null && list.size()>0&&list.contains(userId)){
			return "文字侦探";
		}
		list=storyTaskIntegralDao.findByType(3);
		if(list!=null && list.size()>0&&list.contains(userId)){
			return "鉴赏达人";
		}
		list=storyTaskIntegralDao.findByType(4);
		if(list!=null && list.size()>0&&list.contains(userId)){
			return "运用能手";
		}
		return str;
	}

	@Override
	public Page<Map<String, Object>> integralRank(
			Map<String, Object> searchParams, Pageable pageable) {
		return storyQueryDao.integralRank(searchParams, pageable);
	}

}
