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

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.alibaba.fastjson.JSONObject;
import com.ssj.bean.weixin.download.domain.DownloadFile;
import com.ssj.bean.weixin.story.bean.StoryCard;
import com.ssj.bean.weixin.story.bean.StoryTask;
import com.ssj.dao.weixin.story.dao.StoryCardDao;
import com.ssj.dao.weixin.story.dao.StoryQueryDao;
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.util.HttpRemote;
import com.ssj.service.weixin.download.service.DownloadFileService;
import com.ssj.service.weixin.story.service.StoryCardService;
import com.ssj.service.weixin.story.service.StoryTaskService;
import com.ssj.service.weixin.user.service.QrCodeService;
@Service(version="1.0")
@Transactional
public class StoryCardServiceImpl  extends BaseServiceImpl<StoryCard, String> implements StoryCardService{
	 private   Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private StoryCardDao storyCardDao;
	@Autowired
	private StoryQueryDao storyQueryDao;
	
	@Autowired
	private DownloadFileService downloadFileService;
	
   @Autowired
   private StoryTaskService storyTaskService;
   @Autowired
   private QrCodeService qrCodeService;
	
	@Override
	public PagingAndSortingRepository<StoryCard, String> getDao() {
		return storyCardDao;
	}
	@Override
	public Page<StoryCard> findByPage(Map<String, Object> params, Pageable sp) {
		Map<String, SearchFilter> filters = SearchFilter.parse(params);
		Specification<StoryCard> specification = DynamicSpecifications.bySearchFilter(filters.values(), StoryCard.class);    
		return  storyCardDao.findAll(specification, sp);
	}
	@Override
	public Integer getUserCount(String userId, String storyId) {
		return storyCardDao.getUserCount(userId, storyId);
	}
	@Override
	public Integer punchCardDays(String userId) {
		return storyCardDao.punchCardDays(userId);
	}
	@Override
	public Integer getUserCard() {
		return storyCardDao.getUserCard();
	}
	@Override
	public Integer getStoryIdCount(String storyId) {
		return storyCardDao.getStoryIdCount(storyId);
	}
	@Override
	public List<Integer> getStoryCardDayNum(String storyId,String userId) {
		return storyCardDao.getStoryCardDayNum(storyId,userId);
	}
	@Override
	public StoryCard saveStoryCard(StoryCard storyCard,
			String recording_localId, String recording_serverId) throws Exception {
		logger.info("recording_localId--"+recording_localId);
		logger.info("recording_serverId--"+recording_serverId);
		if(recording_localId!=null && recording_serverId !=null ){
			DownloadFile file = new  DownloadFile();
			file.setCreateTime(new Date());
			file.setMediaId(recording_serverId);
			file.setStat(0);
			file.setType(2);//语音
			file.setLocalId(recording_localId);
			downloadFileService.save(file);
			storyCard.setVoice(file.getId());
		}
		if(storyCard.getCoorx() != null && storyCard.getCoory()  != null){
			String url = "http://apis.map.qq.com/ws/geocoder/v1/";
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("location",storyCard.getCoorx()+","+storyCard.getCoory());
			data.put("key", "CA5BZ-KRF3J-3RXFX-FUAYV-EUIC7-ADBDK");
			JSONObject jsonObject;
			try {
				jsonObject = JSONObject.parseObject(HttpRemote.GETMethod(url, data));
				if(jsonObject.getInteger("status") == 0){  //正常
					storyCard.setCoorName(jsonObject.getJSONObject("result").getString("address"));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		StoryTask  storyTask =storyTaskService.getById(storyCard.getStoryTaskId());
   	storyCard.setDaysNum(storyTask.getDaysNum());
   	this.save(storyCard);
		return storyCard;
	}
	@Override
	public void updateStoryCardVoice(StoryCard storyCard) {
		if(storyCard !=null && storyCard.getVoice()!=null){
			DownloadFile  downloadFile =downloadFileService.downloadVoice(storyCard.getVoice());
			storyCard.setVoice(downloadFile.getMp3url());
			this.save(storyCard);
		}
		
	}
	@Override
	public List<Map<String, Object>> findByStoryTaskId(String storyTaskId) {
		return storyQueryDao.findByStoryTaskId(storyTaskId);
	}
	@Override
	public List<Map<String, Object>> findByStoryIdAndNum(
			String storyId, String num,String userId) {
		return storyQueryDao.findByStoryIdAndNum(storyId, num,userId);
	}
	@Override
	public List<Map<String, Object>> findByUserIdList(String userId,
			String storyTaskId) {
		return storyQueryDao.findByUserIdList(userId, storyTaskId);
	}

}
