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

import com.ssj.bean.weixin.story.bean.StoryTask;
import com.ssj.dao.weixin.story.dao.StoryTaskDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.persistence.DynamicSpecifications;
import com.ssj.framework.core.persistence.SearchFilter;
import com.ssj.framework.core.security.manager.TokenManager;
import com.ssj.framework.idworker.IdWorker;
import com.ssj.service.weixin.story.service.StoryPraiseService;
import com.ssj.service.weixin.story.service.StoryService;
import com.ssj.service.weixin.story.service.StoryTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/** 
 * @author  ZhangCaibao 
 * @version 2017年6月12日 下午2:27:50 
 * 类说明 
 */
@Service
@Transactional
public class StoryTaskServiceImpl  extends BaseServiceImpl<StoryTask, String> implements StoryTaskService {

	@Autowired
	private StoryTaskDao storyTaskDao;
	@Autowired
	private StoryPraiseService storyPraiseService;
	
    @Autowired
	private TokenManager  tokenManager;
    @Autowired
    private StoryService  storyService;
	
	@Override
	public PagingAndSortingRepository<StoryTask, String> getDao() {
		return storyTaskDao;
	}

	@Override
	public Page<StoryTask> findByPage(Map<String, Object> params, Pageable sp) {
		Map<String, SearchFilter> filters = SearchFilter.parse(params);
		Specification<StoryTask> specification = DynamicSpecifications.bySearchFilter(filters.values(), StoryTask.class);    
		return  storyTaskDao.findAll(specification, sp);
	}

	@Override
	public StoryTask findByStoryIdAndDaysNum(String storyId, Integer daysNum) {
		return storyTaskDao.findByStoryIdAndDaysNum(storyId, daysNum);
	}

	@Override
	public void insertReadIn(String isbn13, String userId, String userImg) {
		Integer count=storyTaskDao.getObjectCount(isbn13, userId);
		if(count==0){
            String id = IdWorker.generateId();
			storyTaskDao.insertReadIn(isbn13, userId, userImg,id);
		}
		
	}

	

	@Override
	 public List<StoryTask> findByStoryTaskId() {
		return storyTaskDao.findByStoryTaskId();
	}

	@Override
	public StoryTask randStoryTask() {
		
		return storyTaskDao.randStoryTask();
	}


}
