package com.sqx.modules.message.service.impl;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Query;
import com.sqx.common.utils.Result;
import com.sqx.ffmpeg.FFmpeg;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.artificer.entity.Artificer;
import com.sqx.modules.artificer.service.ArtificerService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.message.dao.MessageInfoAudioDao;
import com.sqx.modules.message.dao.MessageInfoDao;
import com.sqx.modules.message.entity.MessageInfoAudio;
import com.sqx.modules.message.entity.MessageInfo;
import com.sqx.modules.message.entity.MessageInfoAudio;
import com.sqx.modules.message.service.MessageAudioService;
import com.sqx.modules.message.service.MessageService;
import com.sqx.modules.risk.entity.Risk;
import com.sqx.modules.risk.service.RiskService;
import com.sqx.modules.utils.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息
 */
@Service
public class MessageAudioServiceImpl extends ServiceImpl<MessageInfoAudioDao, MessageInfoAudio> implements MessageAudioService {

	@Autowired
	private FFmpeg fFmpeg;

	@Autowired
	private MessageService messageService;

	@Autowired
	private CommonInfoService commonRepository;

	@Override
	public String mergeAllAudio(String batch) {
		String http = commonRepository.findOne(19).getValue();
		String[] split = http.split("://");
		// 路径
		String baseAudioUploadPath = "/www/wwwroot/"+split[1]+"/file/uploadPath";

		QueryWrapper<MessageInfo> iqw = new QueryWrapper<>();
		iqw.eq("batch",batch);
		iqw.last("limit 1");
		MessageInfo oldInfo = messageService.getOne(iqw);
		if(oldInfo == null){
			return "合并失败，未找到数据！";
		}
		String userId = oldInfo.getUserId();
		String dateTime = oldInfo.getCreateAt();
		String date = null;

		String[] dateTime2 = StringUtils.split(dateTime," ");
		date = dateTime2[0];

		String baseFilePath = baseAudioUploadPath + "/" + "audio" + "/" + userId + "/" + date + "/" + batch + "/";

		String filePath = baseFilePath + "all.mp3";
		FileUtil.touch(filePath);
		String url = StringUtils.replace(filePath,"/www/wwwroot/"+split[1],http);
		//执行合并
		//查询出已经合并后的文件列表
		QueryWrapper<MessageInfoAudio> qw = new QueryWrapper<>();
		qw.eq("batch",batch);
		List<MessageInfoAudio> audioList = list(qw);
		List<String> mergeList = Lists.newArrayList();
		for (MessageInfoAudio audio: audioList){
			//转换成物理地址
			String fileUrl = audio.getAuditContent();
			String path = StringUtils.replace(fileUrl,http, "/www/wwwroot/"+split[1]);
			mergeList.add(path);
		}
		fFmpeg.mergeAllAudio(mergeList,filePath);
		UpdateWrapper<MessageInfo> uw = new UpdateWrapper<>();
		uw.set("image",url);
		uw.eq("batch",batch);
		messageService.update(uw);
		return "合并成功";
	}

//    @Autowired
//    private MessageInfoAudioDao messageInfoAudioDao;
//
//    @Override
//    public List<MessageInfoAudio> findAll() {
//        return messageInfoAudioDao.selectList(null);
//    }
//
//    @Override
//    public int saveBody(MessageInfoAudio messageInfo) {
//        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        //Date now = new Date();
//        //messageInfo.setCreateAt(sdf.format(now));
//        return messageInfoAudioDao.insert(messageInfo);
//    }
//
//    @Override
//    public MessageInfoAudio findOne(long id) {
//        return messageInfoAudioDao.selectById(id);
//    }
//
//    @Override
//    public MessageInfoAudio selectById(long id) {
//        return messageInfoAudioDao.selectById(id);
//    }
//
//    @Override
//    public int delete(long id) {
//        messageInfoAudioDao.deleteById(id);
//        return 1;
//    }
//
//    @Override
//    public PageUtils find(String state, int page,int limit) {
//        Page<MessageInfoAudio> pages = new Page<>(page, limit);
//        return new PageUtils(messageInfoAudioDao.find(pages,state));
//    }
//
//    @Override
//    @Transactional
//    public int updateBody(MessageInfoAudio userInfo) {
//        messageInfoAudioDao.updateById(userInfo);
//        return 1;
//    }
//
//    @Override
//    public PageUtils findType(Integer type, int page,int limit) {
//        Page<MessageInfoAudio> pages = new Page<>(page, limit);
//        return new PageUtils(messageInfoAudioDao.findType(pages,type));
//    }
//
//    @Override
//    @Transactional
//    public int updateState(String state, Long id) {
//        return messageInfoAudioDao.updateState(state,id);
//    }
//
//    @Override
//    @Transactional
//    public int updateSendState(String state, Long id) {
//        return messageInfoAudioDao.updateSendState(state,id);
//    }
//
//    @Override
//    public PageUtils findTypeByUserId( String type,String userId, int page,int limit) {
//        Page<MessageInfoAudio> pages = new Page<>(page, limit);
//        return new PageUtils(messageInfoAudioDao.findTypeByUserId(pages,type,userId));
//    }

}
