package com.platform.modules.live.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.platform.autoconfigure.live.LiveProperties;
import com.platform.modules.live.entity.LiveMultimediaEntity;
import com.platform.modules.live.service.LiveMultimediaService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.platform.common.utils.Constant;
import com.platform.common.utils.ConverterUtils;
import com.platform.common.utils.FileUtils;
import com.platform.common.utils.PageUtils;
import com.platform.common.utils.R;
import com.platform.common.utils.ShiroUtils;
import com.platform.modules.live.constants.SceneConstants;
import com.platform.modules.live.dao.LiveReportDao;
import com.platform.modules.live.entity.LiveReportEntity;
import com.platform.modules.live.service.LiveReportService;
import com.platform.modules.live.service.LuceneService;
import com.platform.modules.live.utils.ArraysCustomeUtils;
import com.platform.modules.live.utils.ForeignProperties;
import com.platform.modules.live.utils.LuceneTypeEnum;
import com.platform.modules.live.utils.ReportReleaseEnum;
import com.platform.modules.live.utils.ReportTypeEnum;
import com.platform.modules.live.utils.TopEnum;
import com.platform.modules.live.vo.LiveReportVo;
import com.platform.modules.plm.util.MateriaFileProperties;
import com.platform.modules.plm.util.MaterialUtils;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.modules.sys.service.SysUserService;
import com.platform.modules.video.service.RealPathResolver;
import org.springframework.web.util.HtmlUtils;

@Service("liveReportService")
@Transactional
public class LiveReportServiceImpl extends ServiceImpl<LiveReportDao, LiveReportEntity> implements LiveReportService {

	private final Log log = LogFactory.getLog(LiveReportServiceImpl.class);
	
	@Autowired
	private MateriaFileProperties materiaFileProperties;
	@Autowired
	private RealPathResolver realPathResolver;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private LuceneService luceneService;
	@Autowired
	private ForeignProperties foreignProperties;
	@Autowired
	private LiveProperties liveProperties;

	@Autowired
	private LiveMultimediaService liveMultimediaService;

	@Override
	@Transactional(readOnly = true)
	public PageUtils queryPageByParams(Map<String, Object> params, boolean showPics) {
		//检索记录
		int current = ConverterUtils.parseInt(params.get("page"));
	    int limit = ConverterUtils.parseInt(params.get("limit"));
	    Page<Map<String, Object>> page = new Page<>(current, limit);
	    List<LiveReportEntity> pageList = this.baseMapper.queryPageMap(page, params);
	    List<Map<String, Object>> list = new ArrayList<>();
		if(pageList!=null && pageList.size()>0){
			for(int i=0;i<pageList.size();i++){
				Map<String, Object> map = new HashMap<>();
				LiveReportEntity bean = pageList.get(i);
				if(bean==null){
					continue;
				}
				map.put("id", bean.getId());
				map.put("reporterName", bean.getReporterName());
				if (null != bean.getReportType()) {
					map.put("reportType", bean.getReportType());
					map.put("reportTypeShow", ReportTypeEnum.getName(bean.getReportType()));
				}
				map.put("isTop", bean.getIsTop());
				map.put("isTopShow", null!=bean.getIsTop()? TopEnum.getName(bean.getIsTop()) : null);
				map.put("priority", bean.getPriority());
				
				map.put("isRelease", bean.getIsRelease());
				map.put("reportDate", bean.getReportDate());
				map.put("createDate", bean.getCreateDate());
				map.put("releaseDate", bean.getReleaseDate());
				map.put("content", bean.getContent());
				map.put("mediaUrl", MaterialUtils.getMaterialUrl(bean.getMediaUrl(), foreignProperties.getMediaurl()));
				map.put("fileUrl", bean.getMediaUrl());
				if (showPics) {
					map.put("imgList", handlerPicsList(bean.getImgList(), true));
					map.put("imgFileList", bean.getImgList()==null ? null : bean.getImgList().split(","));
				}
				map.put("fileSize", bean.getFileSize());
				map.put("fileDuration",bean.getFileDuration());
				// 20200430_Mxy 相关报道
				map.put("relatedReport", StringUtils.isNotBlank(bean.getRelatedReport()) ? bean.getRelatedReport() : null);
				list.add(map);
			}
		}
	    page.setRecords(list);
		return new PageUtils(page);
	}

	@Override
	@Transactional(readOnly = true)
	public Map<String, Object> info(Integer id) {
		if (null == id) {
			return null;
		}
		LiveReportEntity report = this.baseMapper.queryInfoById(id);
		if (null == report) {
			return null;
		}
		Map<String, Object> returnEntity = new HashMap<>();
		returnEntity.put("id", report.getId());
		returnEntity.put("sceneId", report.getSceneId());
		returnEntity.put("reportType", report.getReportType());
		returnEntity.put("reportTypeShow", null!=report.getReportType() ? ReportTypeEnum.getName(report.getReportType()) : "");
		returnEntity.put("reporterId", report.getUserId());
		returnEntity.put("reporterName", report.getReporterName());
		returnEntity.put("reportDate", report.getReportDate());
		// 20200316_Mxy 图片压缩
		String[] imgList = handlerPicsList(report.getImgList(), true);
		String[] list = null;
		if (null != imgList){
			list = new String[imgList.length];
			for (int i = 0; i < imgList.length; i++){
				list[i] = compressImage(imgList[i]);
			}
		}
		returnEntity.put("imgList", list);
		// 20200430_Mxy 相关报道
		returnEntity.put("relatedReport", StringUtils.isNotBlank(report.getRelatedReport()) ? report.getRelatedReport() : null);
		// returnEntity.put("imgList", handlerPicsList(report.getImgList(), true));
		returnEntity.put("imgFileList", report.getImgList()==null ? null : report.getImgList().split(","));
		returnEntity.put("fileUrl", report.getMediaUrl());
		returnEntity.put("mediaUrl", StringUtils.isNotBlank(report.getMediaUrl()) ? MaterialUtils.getMaterialUrl(report.getMediaUrl(), foreignProperties.getMediaurl()) : "");
		returnEntity.put("content", report.getContent());
		if (StringUtils.isNotBlank(report.getContentHref())) {
			returnEntity.put("contentHref", HtmlUtils.htmlUnescape(report.getContentHref()));
		}
		returnEntity.put("createDate", report.getCreateDate());
		returnEntity.put("isRelease", report.getIsRelease());
		returnEntity.put("isReleaseShow", null!=report.getIsRelease()? ReportReleaseEnum.getName(report.getIsRelease()) : null);
		
		returnEntity.put("isTop", report.getIsTop());
		returnEntity.put("isTopShow", null!=report.getIsTop()? TopEnum.getName(report.getIsTop()) : null);
		returnEntity.put("priority", report.getPriority());
		
		returnEntity.put("releaseUser", report.getReleaseUser());
		returnEntity.put("releaseUserName", report.getReleaseUserName());
		returnEntity.put("releaseDate", report.getReleaseDate());

		returnEntity.put("lastUpdateUser", report.getLastUpdateUser());
		returnEntity.put("lastUpdateUserName", report.getLastUpdateUserName());
		returnEntity.put("fileSize", report.getFileSize() );
		returnEntity.put("fileDuration",report.getFileDuration());
		returnEntity.put("lastUpdateDate", report.getLastUpdateDate());
		return returnEntity;
	}

	@Override
	public void save(LiveReportEntity liveReport, Long userId){
		boolean release = false;
		Date date = new Date();

        if(liveReport.getIsRelease() != null && (liveReport.getIsRelease()==1 || liveReport.getIsRelease().equals(1))){
            release = true;
            liveReport.setReleaseUser(userId);
            liveReport.setReleaseDate(date);
        }
        if (null == liveReport.getReportDate()) {
            liveReport.setReportDate(date);
        }
        if (null != userId) {
            SysUserEntity user = sysUserService.selectBaseById(Long.valueOf(userId));
            if (null != user) {
                liveReport.setReporterName(StringUtils.isNotBlank(user.getRealname())?user.getRealname():user.getUsername());
            }
        }
        liveReport.setUserId(userId);
        liveReport.setCreateDate(date);
        liveReport.setCreateId(userId);

        this.baseMapper.insert(liveReport);

        // 增加列表索引
		createReportIndex(release, liveReport, null);

	}

	@Override
	public void save(Integer reportType, MultipartFile materialFile, MultipartFile[] imgFiles, Long userId, LiveReportEntity liveReportEntity, Long userId1) {
		reportMediasUpload(reportType, materialFile, imgFiles, userId, liveReportEntity, null);

		this.save(liveReportEntity, userId);
	}

	private void reportMediasUpload(Integer reportType, MultipartFile multipartFile, MultipartFile[] imgFiles,
									Long userId, LiveReportEntity liveReportEntity, Integer[] imgFilesIndex) {
		if ((1 == reportType || 2 == reportType) && multipartFile!=null) {

			// 多媒体素材上传
			LiveMultimediaEntity multimediaEntity = new LiveMultimediaEntity();
			multimediaEntity.setMediaType(reportType);
			multimediaEntity.setTitle(multipartFile.getOriginalFilename());
			Map<String, Object> mediaMap = liveMultimediaService.save(multimediaEntity, multipartFile, userId);
			String fileUrl = (String) mediaMap.get("fileUrl");
			liveReportEntity.setMediaUrl(fileUrl);
//			return;
		}

//		if (3 == reportType) {
		StringBuilder stringBuilder = new StringBuilder();
		StringBuilder newSb = new StringBuilder();
		String newString = "";
		if (imgFiles != null && imgFiles.length > 0) {
			for (MultipartFile img : imgFiles) {
				if(img==null){
					continue;
				}
				LiveMultimediaEntity multimediaEntity = new LiveMultimediaEntity();
				multimediaEntity.setMediaType(SceneConstants.MediaType.IMAGE.getValue());
				multimediaEntity.setTitle(img.getOriginalFilename());
				Map<String, Object> mediaMap = liveMultimediaService.save(multimediaEntity, img, userId);
				String fileUrl = (String) mediaMap.get("fileUrl");

				newSb.append(fileUrl).append(",");
			}
			// 新增部分
			newString = newSb.substring(0, newSb.lastIndexOf(","));
		}
		Integer liveReportId = liveReportEntity.getId();
		if (liveReportId != null) {
			// 修改
			if (imgFilesIndex != null && imgFilesIndex.length > 0) {
				// 需要原图片
				LiveReportEntity oriEntity = this.selectById(liveReportEntity);
				String oldImgs = (oriEntity.getImgList().lastIndexOf(",")==oriEntity.getImgList().length()-1 ? oriEntity.getImgList()+newString : oriEntity.getImgList()+","+newString) ;
				if (StringUtils.isNotBlank(oldImgs)) {
					String[] imgs = oldImgs.split(",");

					for (Integer index : imgFilesIndex) {
						stringBuilder.append(imgs[index-1]).append(",");
					}
//					stringBuilder.append(newString);
					newString = stringBuilder.toString();
				}
			}
		}
		liveReportEntity.setImgList(newString);
//		}
	}

	@Override
	public void update(Integer reportType, MultipartFile materialFile, MultipartFile[] imgFiles, Long userId, LiveReportEntity liveReportEntity, Integer[] imgFilesIndex, Long userId1) {
		reportMediasUpload(reportType, materialFile, imgFiles, userId, liveReportEntity, imgFilesIndex);

		this.update(liveReportEntity, userId);
	}

	@Override
	public void update(LiveReportEntity liveReport, Long userId) {
		if (null == liveReport || null == liveReport.getId()) {
			throw new NullPointerException();
		}
		LiveReportEntity dbReport = this.baseMapper.selectById(liveReport.getId());
		if (null == dbReport) {
			throw new NullPointerException();
		}
//		liveReport.setImgList(dbReport.getImgList());
//		try {
//			handlerReportFiles(liveReport, materialFile, coverFiles, coverFilesIndex);
//		} catch (IOException e) {
//			throw new IOException();
//		}
		Date date = new Date();
		boolean release = false;
		if(liveReport.getIsRelease()==1 || liveReport.getIsRelease().equals(1)){
			release = true;
			liveReport.setReleaseUser(userId);
			liveReport.setReleaseDate(new Date());
		}
		// 置空操作兼容
		if (StringUtils.isBlank(liveReport.getContentHref())) {
			liveReport.setContentHref(" ");
		}
		liveReport.setLastUpdateDate(date);
		liveReport.setLastUpdateUser(userId);
		// 20200506_Mxy 相关报道修改
		if (StringUtils.isBlank(liveReport.getRelatedReport())){
			liveReport.setRelatedReport(" ");
		}
		this.baseMapper.updateById(liveReport);

		// 增加列表索引
		createReportIndex(release, liveReport, dbReport);
		if ((dbReport.getIsRelease() == 1 || liveReport.getIsRelease().equals(1)) && !release) {
			deleteReportIndexBatch(new Integer[]{liveReport.getId()});
		}
	}

	/**
	 * 处理报道中资源文件
	 * 用于保存或更新报道资源
	 * @param liveReport 报道信息
	 * @param materialFile 媒体资源
	 * @param coverFiles 图片资源，封面图或多图资源
	 * @param coverFilesIndex 图片资源索引
	 * @throws IOException
	 */
	private void handlerReportFiles (LiveReportEntity liveReport, MultipartFile materialFile, MultipartFile[]
			coverFiles, Integer[] coverFilesIndex) throws IOException {
		// 更新报道时，媒体资源更新后才会传入文件，否则无需更新
		if(materialFile!=null && !materialFile.isEmpty()){
			String newMaterialName = FileUtils.createFileName() + "." + FileUtils.getSuffix(materialFile.getOriginalFilename());
			String serverMaterialPath = materiaFileProperties.getMediaPath() + "/" + MaterialUtils.getDateDir() + "/" + newMaterialName;
			String newMaterialPath = realPathResolver.get(materiaFileProperties.getMateriaPath() + serverMaterialPath);
			FileUtils.copy(materialFile.getInputStream(), newMaterialPath);
			liveReport.setMediaUrl(serverMaterialPath);
		}
		boolean forCondition = (null!=coverFiles && coverFiles.length>0) || (coverFilesIndex!=null && coverFilesIndex
				.length>0);
		if(forCondition){
			String[] oldImgsArr = handlerPicsList(liveReport.getImgList(), false);
			// 需要校验文件
			boolean isCheck = false;
			Map<Integer, Object> oldImgsMap = new HashMap<>();
			if (null!=coverFilesIndex && (coverFilesIndex.length >= coverFiles.length)
					&& null!=oldImgsArr && oldImgsArr.length>0) {
				isCheck = true;
				ArraysCustomeUtils.forEach(oldImgsArr,(index, str) -> {
					oldImgsMap.put(++index, str);
				});
			}
			String imgs="";
			int oldNum = 0;
			for(int i=0; i<(null==coverFilesIndex?coverFiles.length:coverFilesIndex.length); i++){
				if (null!=coverFilesIndex) {
					Integer fileIndex = coverFilesIndex[i];
					if (isCheck && fileIndex<=oldImgsArr.length) {
						oldNum ++ ;
						if (null!=fileIndex && oldImgsMap.containsKey(fileIndex)) {
							if(i>0){
								imgs += ",";
							}
							imgs += oldImgsMap.get(fileIndex);
						}
						continue;
					}
				}
				if (null!=coverFiles && coverFiles.length>(i-oldNum)) {
					MultipartFile file = coverFiles[i-oldNum];
					if(file!=null && !file.isEmpty()){
						String newMaterialName = FileUtils.createFileName() + "." + FileUtils.getSuffix(file.getOriginalFilename());
						String serverMaterialPath = materiaFileProperties.getMediaPath() + "/" + MaterialUtils.getDateDir() + "/" + newMaterialName;
						String newMaterialPath = realPathResolver.get(materiaFileProperties.getMateriaPath() + serverMaterialPath);
						FileUtils.copy(file.getInputStream(), newMaterialPath);
						if(i>0){
							imgs += ",";
						}
						imgs += serverMaterialPath;
					}
				}
			}
			liveReport.setImgList(imgs);
		}
	}

	/**
	 * 处理拼接图片资源路径
	 * @param imgsStr
	 * @param isShow
	 * @return
	 */
	public String[] handlerPicsList(String imgsStr, boolean isShow) {
		String[] imgList = null;
		if(StringUtils.isNotBlank(imgsStr)){
			String[] imgsArr = imgsStr.split(",");
			if(imgsArr.length>0){
				// 若不需要全路径，直接返回
				if (!isShow) {
					return imgsArr;
				}
				imgList = new String[imgsArr.length];
				for(int j=0;j<imgsArr.length;j++){
					imgList[j] = MaterialUtils.getMaterialUrl(imgsArr[j], foreignProperties.getMediaurl());
				}
			}
		}
		return imgList;
	}
	
	@Override
	public void delete(Integer id, Long userId){
		if(id==null){
			return;
		}
		LiveReportEntity entity = new LiveReportEntity();
		entity.setIsDelete(1);
		entity.setId(id);
		entity.setLastUpdateDate(new Date());
		entity.setLastUpdateUser(userId);
		this.baseMapper.updateById(entity);

		deleteReportIndexBatch(new Integer[]{id});

	}
	
	@Override
	public void deleteBatch(Integer[] ids,Long userId){
		if(ids==null || ids.length==0){
			return;
		}
		// 20190221 TROY 调整逻辑
		LiveReportEntity entity = new LiveReportEntity();
		entity.setIsDelete(1);
		entity.setLastUpdateDate(new Date());
		entity.setLastUpdateUser(userId);
		EntityWrapper wrapper = new EntityWrapper<>(new LiveReportEntity());
		wrapper.in("id", ids);
		this.baseMapper.update(entity, wrapper);

		deleteReportIndexBatch(ids);
	}
	
	@Override
	public void releaseBatch(Integer[] ids,Long userId){
		if(ids==null || ids.length==0){
			return;
		}
		// 20190221 TROY 调整逻辑
		LiveReportEntity entity = new LiveReportEntity();
		entity.setIsRelease(ReportReleaseEnum.RELEASED.getKey());
		entity.setReleaseDate(new Date());
		entity.setReleaseUser(userId);

		EntityWrapper wrapper = new EntityWrapper<>(new LiveReportEntity());
		wrapper.in("id", ids);
		this.baseMapper.update(entity, wrapper);

		for(Integer id : ids){
			if(id == null){
				continue;
			}
			LiveReportEntity liveReport = this.baseMapper.selectById(id);
			if(liveReport == null){
				continue;
			}
			// 增加列表索引
			createReportIndex(true, liveReport, null);
		}
	}
	
	@Override
	public void cancelReleaseBatch(Integer[] ids, Long userId){
		if(ids==null || ids.length==0){
			return;
		}
		LiveReportEntity entity = new LiveReportEntity();
		entity.setIsRelease(ReportReleaseEnum.UNRELEASED.getKey());
		entity.setLastUpdateDate(new Date());
		entity.setLastUpdateUser(userId);
		entity.setReleaseDate(null);
		entity.setReleaseUser(null);

		EntityWrapper wrapper = new EntityWrapper<>(new LiveReportEntity());
		wrapper.in("id", ids);
		this.baseMapper.update(entity, wrapper);

		// 删除列表索引
		deleteReportIndexBatch(ids);
	}

	@Override
	public void topBatch(Integer[] ids,Long userId){
		if(ids==null || ids.length==0){
			return;
		}
		// 20190221 TROY 调整逻辑
		LiveReportEntity entity = new LiveReportEntity();
		entity.setIsTop(TopEnum.TOP.getKey());
		entity.setLastUpdateDate(new Date());
		entity.setLastUpdateUser(userId);

		EntityWrapper wrapper = new EntityWrapper<>(new LiveReportEntity());
		wrapper.in("id", ids);
		this.baseMapper.update(entity, wrapper);
		
		// 20191009 weiyan 新增索引
		for(Integer id : ids){
			if(id == null){
				continue;
			}
			LiveReportEntity liveReport = this.baseMapper.selectById(id);
			if(liveReport == null || (liveReport != null && liveReport.getIsRelease() != 1 && !liveReport.getIsRelease().equals(1))){
				continue;
			}
			// 增加列表索引
			createReportIndex(true, liveReport, null);
		}
	}
	
	@Override
	public void cancelTopBatch(Integer[] ids,Long userId){
		if(ids==null || ids.length==0){
			return;
		}
		LiveReportEntity entity = new LiveReportEntity();
		entity.setIsTop(TopEnum.UNTOP.getKey());
		entity.setLastUpdateDate(new Date());
		entity.setLastUpdateUser(userId);

		EntityWrapper wrapper = new EntityWrapper<>(new LiveReportEntity());
		wrapper.in("id", ids);
		this.baseMapper.update(entity, wrapper);
		
		// 20191009 weiyan 新增索引
		for(Integer id : ids){
			if(id == null){
				continue;
			}
			LiveReportEntity liveReport = this.baseMapper.selectById(id);
			if(liveReport == null || (liveReport != null && liveReport.getIsRelease() != 1 && !liveReport.getIsRelease().equals(1))){
				continue;
			}
			createReportIndex(true, liveReport, null);
		}
	}
	
	@Override
	public void priority(Integer[] ids,Integer[] prioritys,Long userId){
		if(ids==null || ids.length==0 || prioritys==null || prioritys.length==0){
			return;
		}
		for(int i=0;i<ids.length;i++){
			if(ids[i]!=null && prioritys[i]!=null){
				LiveReportEntity entity = new LiveReportEntity();
				entity.setPriority(prioritys[i]);
				entity.setLastUpdateDate(new Date());
				entity.setLastUpdateUser(userId);
				entity.setId(ids[i]);
				this.baseMapper.updateById(entity);
			}
		}
		// 20191009 weiyan 新增索引
		for(Integer id : ids){
			if(id == null){
				continue;
			}
			LiveReportEntity liveReport = this.baseMapper.selectById(id);
			if(liveReport == null || (liveReport != null && liveReport.getIsRelease() != 1 && !liveReport.getIsRelease().equals(1))){
				continue;
			}
			// 增加列表索引
			createReportIndex(true, liveReport, null);
		}
	}

	/**
	 * 报道索引重建
	 * @return
	 */
	@Override
	public R indexRebuild(Integer begin, Integer end) {
		if(ShiroUtils.getUserId() != Constant.SUPER_ADMIN) {
			return R.error("没有权限，请联系管理员授权");
		}

		int pageNo = 1;
		int pageSize = 500;
		Integer release = 1;

		EntityWrapper<LiveReportEntity> wrapper = new EntityWrapper<>(new LiveReportEntity());
		wrapper.eq("is_release", release);
		wrapper.eq("is_delete", SceneConstants.NOT_DELETE);
		if (begin != null && end != null && begin < end) {
			wrapper.between("id", begin, end);
		}

		Page<LiveReportEntity> page = new Page<>(pageNo, pageSize);

		while (pageNo == 1 || page.hasNext()){
			page = new Page<>(pageNo, pageSize);
			page = this.selectPage(page,wrapper);
			if (page.getRecords().size()>0) {
				for (LiveReportEntity report : page.getRecords()) {
					createReportIndex(report.getIsRelease().equals(release), report, null);
					log.info("报道 ["+ report.getId() + "] 索引创建成功");
				}
			}
			pageNo++;
		}

		return R.ok("更新索引成功");
	}
	
	private void createReportIndex(boolean release, LiveReportEntity oriReport, LiveReportEntity dbReport) {
		if (release) {
			LiveReportEntity tmpReport = new LiveReportEntity();
			tmpReport.setId(oriReport.getId());
			tmpReport.setSceneId(null==oriReport.getSceneId()&&null!=dbReport ? dbReport.getSceneId() : oriReport
					.getSceneId());
			tmpReport.setReportType(oriReport.getReportType());
			tmpReport.setReportDate(oriReport.getReportDate());
			tmpReport.setCreateDate(null==oriReport.getCreateDate()&&null!=dbReport? dbReport.getCreateDate() : oriReport
					.getCreateDate());
			tmpReport.setContent(oriReport.getContent());
			tmpReport.setContentHref(oriReport.getContentHref());
			tmpReport.setMediaUrl(StringUtils.isBlank(oriReport.getMediaUrl())&&null!=dbReport ? dbReport.getMediaUrl()
					:oriReport.getMediaUrl());
			tmpReport.setImgList(StringUtils.isBlank(oriReport.getImgList())&&null!=dbReport ? dbReport.getImgList() :
					oriReport.getImgList());
			tmpReport.setReporterName(StringUtils.isBlank(oriReport.getReporterName())&&null!=dbReport ? dbReport
					.getReporterName() : oriReport.getReporterName());
			tmpReport.setIsTop(oriReport.getIsTop());
			tmpReport.setPriority(oriReport.getPriority());
			// 20200430_Mxy 相关报道
			tmpReport.setRelatedReport(StringUtils.isNotBlank(oriReport.getRelatedReport()) ? oriReport.getRelatedReport() : null);
			try {
				luceneService.createIndex(tmpReport, false);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void deleteReportIndexBatch(Integer[] ids) {
		if(ids==null || ids.length==0){
			return;
		}
		for(Integer id : ids){
			if(id == null){
				continue;
			}
			try {
				luceneService.deleteIndex(LuceneTypeEnum.LUCENE_REPORT.getType(), "id", String.valueOf(id));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public List<LiveReportVo> recentLiveSceneReport(Integer sceneId) {
		if (sceneId == null) return null;
		List<LiveReportEntity> reportList = selectList(new EntityWrapper<LiveReportEntity>()
				.eq("scene_id", sceneId).eq("is_delete", SceneConstants.NOT_DELETE).eq("is_release", 1).orderBy("is_top", false).orderBy("priority", false).orderBy("id", false));

		if (reportList == null || reportList.size() <= 0)
			return null;
		List<LiveReportVo> voList = new ArrayList<>();
		for (LiveReportEntity reportEntity : reportList) {
			LiveReportVo vo = new LiveReportVo(reportEntity);
			vo.setMediaUrl(StringUtils.isNotBlank(reportEntity.getMediaUrl())?MaterialUtils.getMaterialUrl(reportEntity
					.getMediaUrl(), foreignProperties.getMediaurl()) : "");
			vo.setImgList(handlerPicsList(reportEntity.getImgList(), true));
			voList.add(vo);
		}
		return voList;
	}

	/**
	 * 20200316_Mxy 图片压缩
	 *
	 * @param sourceUrl 图片地址
	 * */
	private String compressImage(String sourceUrl){
		// 返回拼接压缩后地址
		return sourceUrl + liveProperties.getCompressImage().getGeometricFormat();
	}
}
