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

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


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;

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.libmy.domain.TbLibBookPl;
import com.ssj.dao.weixin.library.dao.IBookPlDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.util.HttpRemote;
import com.ssj.service.weixin.download.service.DownloadFileService;
import com.ssj.service.weixin.library.service.IBookPlService;

@Service(version="1.0")
public class BookPlServiceImpl extends BaseServiceImpl<TbLibBookPl, String> implements IBookPlService {

	@Autowired
	private IBookPlDao dao;
	
	@Autowired
	private DownloadFileService downloadFileService;
	
	@Override
	public PagingAndSortingRepository<TbLibBookPl, String> getDao() {
		
		return dao;
	}

	@Override
	public TbLibBookPl saveAnswer( TbLibBookPl bookpl, String[] images_localId,
			String[] images_serverId, String[] recording_localId, String[] recording_serverId){
		
		if(bookpl.getPlLng() != null && bookpl.getPlLat() != null){
			String url = "http://apis.map.qq.com/ws/geocoder/v1/";
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("location", bookpl.getPlLat()+","+bookpl.getPlLng());
			data.put("key", "CA5BZ-KRF3J-3RXFX-FUAYV-EUIC7-ADBDK");
			JSONObject jsonObject;
			try {
				jsonObject = JSONObject.parseObject(HttpRemote.GETMethod(url, data));
				if(jsonObject.getInteger("status") == 0){  //正常
					bookpl.setPlAddress(jsonObject.getJSONObject("result").getString("address"));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
		if(images_localId!=null&&images_localId.length>0 && images_serverId !=null && images_serverId.length>0){
			DownloadFile file = new  DownloadFile();
			file.setCreateTime(new Date());
			file.setMediaId(images_serverId[0]);
			file.setStat(0);
			file.setType(1);//图片
			file.setLocalId(images_localId[0]);
			downloadFileService.save(file);
			//answers.setPhoto(file.getId());
			bookpl.setImgUrl(file.getId());
		}
		if(recording_localId!=null&&recording_localId.length>0 && recording_serverId !=null && recording_serverId.length>0){
			DownloadFile file = new  DownloadFile();
			file.setCreateTime(new Date());
			file.setMediaId(recording_serverId[0]);
			file.setStat(0);
			file.setType(2);//语音
			file.setLocalId(recording_localId[0]);
			downloadFileService.save(file);
			bookpl.setMp3Url(file.getId());
		}
		bookpl.setCreateTime(new Date());
		this.save(bookpl);
		return bookpl;
	}


	@Override
	public TbLibBookPl findByIdAndUserId(String id, String userId) {
		
		return dao.findByIdAndUserId(id,userId);
	}

	@Override
	public Integer countByUserIdAndVip(String userId, String vipId) {
		return dao.countByUserIdAndVip(userId,vipId);
	}

}
