package com.platform.modules.plm.thread;

import org.apache.commons.lang3.StringUtils;

import com.platform.common.utils.FileUtils;
import com.platform.common.utils.HttpClientHelper;
import com.platform.common.utils.SpringContextUtils;
import com.platform.modules.plm.entity.PlmVideoNewsEntity;
import com.platform.modules.plm.service.PlmVideoNewsService;
import com.platform.modules.plm.util.MaterialUtils;
import com.platform.modules.video.service.RealPathResolver;

public class QkSynthesisStopThread  extends Thread {
	
	private Long id;
	private String mediaPath;
	private String materiaPath;
	
	public QkSynthesisStopThread(Long id,String mediaPath,String materiaPath){
		this.id = id;
		this.mediaPath = mediaPath;
		this.materiaPath = materiaPath;
	}

	@Override
	public void run() {
		if(id==null){
			return;
		}
		
		PlmVideoNewsService plmVideoNewsService = (PlmVideoNewsService) SpringContextUtils.getBean("plmVideoNewsService");
		RealPathResolver realPathResolver = (RealPathResolver) SpringContextUtils.getBean("realPathResolver");
		
		PlmVideoNewsEntity videoNewsEntity = plmVideoNewsService.selectById(id);
		if(videoNewsEntity==null || videoNewsEntity.getId()==null || StringUtils.isBlank(videoNewsEntity.getMaterialUrl()) || StringUtils.isNoneBlank(videoNewsEntity.getVideoUrl())){
			return;
		}
		
		String coverUrl = (videoNewsEntity.getCoverUrl().indexOf("?")>0 ? videoNewsEntity.getCoverUrl().substring(0, videoNewsEntity.getCoverUrl().lastIndexOf("?")) : videoNewsEntity.getCoverUrl());
		String newCoverName = FileUtils.createFileName() + "." + FileUtils.getSuffix(coverUrl);
        String serverCoverPath = mediaPath + "/" + MaterialUtils.getDateDir() +"/" + newCoverName;
        String newCoverPath = materiaPath + serverCoverPath;
    	HttpClientHelper.sendGetAndSaveFile(coverUrl, null, realPathResolver.get(newCoverPath),false);
    	videoNewsEntity.setTitleImg(serverCoverPath);
    	
    	String materialUrl = (videoNewsEntity.getMaterialUrl().indexOf("?")>0 ? videoNewsEntity.getMaterialUrl().substring(0, videoNewsEntity.getMaterialUrl().lastIndexOf("?")) : videoNewsEntity.getMaterialUrl());
    	String newMaterialName = FileUtils.createFileName() + "." + FileUtils.getSuffix(materialUrl);
    	String serverVideoPath = mediaPath + "/" + MaterialUtils.getDateDir() +"/" + newMaterialName; // 存放在服务器地址
        String newFilePath = materiaPath + "/" + serverVideoPath;
    	HttpClientHelper.sendGetAndSaveFile(materialUrl, null, realPathResolver.get(newFilePath),false);
    	videoNewsEntity.setVideoUrl(serverVideoPath);
    	videoNewsEntity.setStatus(2);
    	plmVideoNewsService.updateById(videoNewsEntity);
	}
}
