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

import com.ssj.bean.weixin.story.bean.StoryTaskMedal;
import com.ssj.bean.weixin.story.bean.StoryTaskMedalRsp;
import com.ssj.dao.weixin.story.dao.StoryTaskMedalDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.weixin.story.service.StoryTaskMedalService;
import com.ssj.service.weixin.story.service.StoryTaskPracticeService;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/** 
 * @author  ZhangCaibao 
 * @version 2018年3月21日 上午11:06:56 
 * 类说明 
 */
@Service
@Transactional
public class StoryTaskMedalServiceImpl extends BaseServiceImpl<StoryTaskMedal, String> implements StoryTaskMedalService{
	@Autowired
	private StoryTaskMedalDao  storyTaskMedalDao ;
	
    @Autowired
    private StoryTaskPracticeService storyTaskPracticeService;
    
     String[] medals = {"古诗白丁", "古诗童生", "古诗学士", "古诗秀才", "古诗举人", "古诗贡士", "古诗进士","古诗榜眼",
    		 			"古诗探花", "古诗状元", "古诗大师", "古诗宗师", "古诗隐士", "古诗圣人", "古诗王者"
                        };
     String[] medalsImg = {"/gs/gu_shi_bai_ding.png", "/gs/gu_shi_tong_sheng.png", "/gs/gu_shi_xue_shi.png", 
    		               "/gs/gu_shi_xiu_cai.png", "/gs/gu_shi_ju_ren.png", "/gs/gu_shi_gong_shi.png", 
    		               "/gs/gu_shi_jin_shi.png","/gs/gu_shi_bang_yan.png","/gs/gu_shi_tan_hua.png", 
    		               "/gs/gu_shi_zhuang_yuan.png", "/gs/gu_shi_da_shi.png", "/gs/gu_shi_zong_shi.png", 
    		               "/gs/gu_shi_yin_shi.png", "/gs/gu_shi_sheng_ren.png", "/gs/gu_shi_wang_zhe.png"
     };
    
    
	@Override
	public PagingAndSortingRepository<StoryTaskMedal, String> getDao() {
		return storyTaskMedalDao;
	}
	@Override
	public Map<String,String> saveMedal(String storyId, String userId) {
		Map<String,String> map =new HashMap<String, String>();
		/**
		 * 先打分21首，才能打另一个主题。
		 */
		Integer countSum=storyTaskPracticeService.countPractice(userId);
		int d = countSum%21==0?countSum/21:countSum/21+1 ;
		Integer count=storyTaskPracticeService.countPractice(storyId,userId);
		int num =0;
		if(count==3){
			num = d*1;
		}else if(count==7){
			num = d*2;
		}else if(count==10){
			num = d*3;
		}else if(count==15){
			num = d*4;
		}else if(count==21){
			num = d*5;
		}
		if(num>15)num=15;
		if(count==3||count==7||count==10||count==15||count==21){
			StoryTaskMedal  storyTaskMedal=	storyTaskMedalDao.findByStoryIdAndDaysNum(storyId, count);
			if(storyTaskMedal==null){
				String name =medals[num];
				String img =medals[num];
				map.put("medal", name);
				map.put("img", img);
				storyTaskMedal = new StoryTaskMedal(storyId,userId,count,name,img);
				this.save(storyTaskMedal);
			}else{
				map.put("medal", storyTaskMedal.getName());
				map.put("img", storyTaskMedal.getImg());
			}
		}
		map.put("count", count.toString());
		return map;
	}
	@Override
	public Map<String, Object> getMedalByUserId(String userId) {
		List<String> list = storyTaskMedalDao.getMedalNameByUserId(userId);
		List<StoryTaskMedalRsp> gained =new ArrayList<StoryTaskMedalRsp>();
		List<StoryTaskMedalRsp> notgained =new ArrayList<StoryTaskMedalRsp>();
		for (int i = 0; i < medals.length; i++) {
			if(list.contains(medals[i])){
				gained.add(new StoryTaskMedalRsp(medals[i],medalsImg[i]));
			}else{
				notgained.add(new StoryTaskMedalRsp(medals[i],medalsImg[i]));
			}
		}
		Map<String, Object> map =new HashMap<String, Object>();
		map.put("notgained", notgained);
		map.put("gained", gained);
		//map.put("num", gained.size());
		return map;
	}
	@Override
	public int  getMedalNameByUserIdCount(String userId) {
		return storyTaskMedalDao.getMedalNameByUserIdCount(userId);
	}

}
