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

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

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.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.weixin.story.bean.Story;
import com.ssj.bean.weixin.story.bean.StoryDate;
import com.ssj.dao.weixin.story.dao.StoryDao;
import com.ssj.framework.basic.utils.DateHelper;
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.service.weixin.story.service.StoryService;
import com.ssj.service.weixin.story.service.StoryTaskPracticeService;
@Service(version="1.0")
@Transactional
public class StoryServiceImpl extends BaseServiceImpl<Story, String> implements StoryService {

	@Autowired
	private StoryDao stroyDao;
	
	 @Autowired
	 private StoryTaskPracticeService storyTaskPracticeService;
	private String [] arrayName={"春晓","夏莲","春草","春雪","冬雪","秋钓","回顾",
								 "敕勒","登楼","条山","小池","江南","鹿柴","回顾",
			                     "同情","断魂","喜庆","劝勉","感伤","念母","回顾"
			                     };
	
	@Override
	public PagingAndSortingRepository<Story, String> getDao() {
		return stroyDao;
	}
	@Override
	public Page<Story> findByPage(Map<String, Object> params, Pageable sp) {
		Map<String, SearchFilter> filters = SearchFilter.parse(params);
		Specification<Story> specification = DynamicSpecifications.bySearchFilter(filters.values(), Story.class);    
		return  stroyDao.findAll(specification, sp);
	}
	
	@Override
	public List<StoryDate> getNextDays(Date date, int days,Date today,String userId,String storyId) throws Exception {
	    //已学习的。
		List<Integer> cardDayNumList =storyTaskPracticeService.getStoryPracticeDayNum(storyId,userId);
		List<StoryDate> list = new  ArrayList<StoryDate> ();
		for (int i = 0; i < days; i++) {
			StoryDate   storyDate =new StoryDate();
			
			int d=DateHelper.daysOfTwo(date, today);
			storyDate.setNum(i+1);
			storyDate.setStrdate(DateHelper.format(date, "d"));
			
			//storyDate.setName(arrayName[i]);
			if(d==0){
				storyDate.setWeekDay("今天");
				storyDate.setStrstyle("no");
				if(cardDayNumList.contains(i+1)){
					storyDate.setStrstyle("yes");
				}
			}else{
				storyDate.setWeekDay(DateHelper.getWeekDays(date));
				if(d>0){
					storyDate.setStrstyle("no");
					if(cardDayNumList.contains(i+1)){
						storyDate.setStrstyle("yes");
					}
				}else{
					storyDate.setStrstyle("next");
				}
			}
			date =DateHelper.getNextDay(date, 1);
			list.add(storyDate);
		}
		return list;
	}
	@Override
	public Story getRecentStroy(int day,Date stroyDate) throws Exception {
		if(day == 0 ){
			Boolean boolDate=DateHelper.isInDate(new Date(), DateHelper.getNextDay(stroyDate, 1), DateHelper.getNextDay(stroyDate, 21));
			int numMinDay=DateHelper.daysOfTwo(stroyDate,new Date());
			if(boolDate){
			    day = numMinDay;
			}else{
				if(numMinDay <=0){
					day =1;
				}else{
					day = 21;
				}
			}
		}
		Story stroy = stroyDao.getEqStroy();
		return stroy;
	}
	@Override
	public Integer updateType(String id, Integer type) {
		return stroyDao.updateType(id, type);
	}
	@Override
	public Story getEqStroy() {
		return stroyDao.getEqStroy();
	}
	@Override
	public List<Story> punchTimeStory() {
		return stroyDao.punchTimeStory();
	}
	@Override
	public List<Story> findByType(Integer type) {
		return stroyDao.findByType(type);
	}

}
