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

import java.util.Date;
import java.util.List;

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

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.dao.weixin.download.dao.DownloadFileDao;
import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.security.manager.TokenManager;
import com.ssj.framework.weixin.util.AmrToMp3Util;
import com.ssj.framework.weixin.util.MUDload;
import com.ssj.service.weixin.download.service.DownloadFileService;
import com.ssj.bean.weixin.download.domain.DownloadFile;

@Service(version="1.0")
@Transactional
public class DownloadFileServiceImpl extends BaseServiceImpl<DownloadFile, String> implements DownloadFileService {
	@Autowired
	private  DownloadFileDao  downloadFileDao;
	
    @Autowired
	private TokenManager tokenManager;

	@Override
	public PagingAndSortingRepository<DownloadFile, String> getDao() {
		return downloadFileDao;
	}

	@Override
	public void downloadPhoto( String id) {
		download(id);
	}

	@Override
	public void download() {
		try {
			
			 Thread t1 = new Thread(new Runnable() {
		            public void run() {
		            	try {
		            		DownloadFile();
						} catch (Exception e) {
							e.printStackTrace();
						}
		            }
		        });
		        t1.start();
		        
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private void download(String id) {
		DownloadFile downloadFile=this.getById(id);
		if(downloadFile !=null){
			String fPath = DateHelper.formatDateByFormat(new Date(), "yyyy/MM/dd");
				String result=MUDload.downloadMedia(tokenManager.getSSJAccessToken(), downloadFile.getMediaId(), fPath);
				if(result !=null){
					downloadFile.setStat(1);
					downloadFile.setUrl(result);
				}else{
					downloadFile.setStat(2);
				}
				this.save(downloadFile);
		}
	}
	@Override
	public void DownloadFile() {
		List<DownloadFile> list=downloadFileDao.findByStat(0);
		if(list.size()>0){
			String fPath = DateHelper.formatDateByFormat(new Date(), "yyyy/MM/dd");
			for (DownloadFile downloadFile : list) {
				String result=MUDload.downloadMedia(tokenManager.getSSJAccessToken(), downloadFile.getMediaId(), fPath);
				if(result !=null){
					downloadFile.setStat(1);
					downloadFile.setUrl(result);
					if(downloadFile.getType()==2){
						String outPath=AmrToMp3Util.changeToMp3(result);//amr 转换为mp3
						downloadFile.setMp3url(outPath);
						downloadFile.setDuration(AmrToMp3Util.getMp3duration(outPath));
					}
				}else{
					downloadFile.setStat(2);
				}
				this.save(downloadFile);
			}
		}
	}

	@Override
	public DownloadFile downloadVoice(String id) {
		DownloadFile downloadFile=this.getById(id);
		if(downloadFile !=null){
			String fPath = DateHelper.formatDateByFormat(new Date(), "yyyy/MM/dd");
				String result=MUDload.downloadMedia(tokenManager.getSSJAccessToken(), downloadFile.getMediaId(), fPath);
				if(result !=null){
					downloadFile.setStat(1);
					downloadFile.setUrl(result);
					if(downloadFile.getType()==2){
						String outPath=AmrToMp3Util.changeToMp3(result);//amr 转换为mp3
						downloadFile.setMp3url(outPath);
						downloadFile.setDuration(AmrToMp3Util.getMp3duration(outPath));
					}
				}else{
					downloadFile.setStat(2);
				}
				this.save(downloadFile);
		}
		return downloadFile;
	}

	

}
