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

import com.ssj.bean.weixin.story.bean.StoryTask;
import com.ssj.bean.weixin.story.bean.StoryTaskPractice;
import com.ssj.dao.weixin.story.dao.StoryTaskPracticeDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.security.manager.TokenManager;
import com.ssj.service.weixin.story.service.StoryTaskPracticeService;
import com.ssj.service.weixin.story.service.StoryTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/** 
 * @author  ZhangCaibao 
 * @version 2018年3月21日 下午2:09:31 
 * 类说明 
 */
@Service
@Transactional
public class StoryTaskPracticeServiceImpl extends BaseServiceImpl<StoryTaskPractice, String> implements StoryTaskPracticeService {
	@Autowired
	private StoryTaskPracticeDao  storyTaskPracticeDao;
	@Autowired
	private StoryTaskService  storyTaskService;
    @Autowired
    private TokenManager tokenManager;
	@Override
	public PagingAndSortingRepository<StoryTaskPractice, String> getDao() {
		return storyTaskPracticeDao;
	}
	@Override
	public void saveStoryTaskPractice(String storyTaskId,String userId) {
		//已经学习古诗
		StoryTaskPractice  storyTaskPractice =storyTaskPracticeDao.findByStoryTaskIdAndUserId(storyTaskId,userId);
		StoryTask storyTask=	storyTaskService.getById(storyTaskId);
		if(storyTaskPractice==null){
			storyTaskPractice = new  StoryTaskPractice();
			storyTaskPractice.setCreateTime(new Date());
			storyTaskPractice.setStoryTaskId(storyTaskId);
			storyTaskPractice.setUserId(userId);
			storyTaskPractice.setStoryId(storyTask.getStoryId());
			storyTaskPractice.setDaysNum(storyTaskPractice.getDaysNum());
			this.save(storyTaskPractice);
		}
		Integer countPractice=this.countPractice(storyTask.getStoryId(), userId);
		if(countPractice==21){
			String key="GS_"+userId;
			tokenManager.delString(key);
		}
	}
	@Override
	public Integer countPractice(String storyId, String userId) {
		return storyTaskPracticeDao.countPractice(storyId, userId);
	}
	@Override
	public Integer countPractice(String userId) {
		return storyTaskPracticeDao.countPractice( userId);
	}
	@Override
	public List<Integer> getStoryPracticeDayNum(String storyId, String userId) {
		return storyTaskPracticeDao.getStoryPracticeDayNum(storyId, userId);
	}

}
