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

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.platform.common.utils.ConverterUtils;
import com.platform.common.utils.PageUtils;
import com.platform.modules.foreign.entity.CommentForeignEntity;
import com.platform.modules.live.dao.LiveCommentDao;
import com.platform.modules.live.entity.LiveCommentEntity;
import com.platform.modules.live.service.LiveCommentService;
import com.platform.modules.live.utils.CommentSourceEnum;
import com.platform.modules.live.utils.ReportReleaseEnum;
import com.platform.modules.live.vo.LiveCommentAppVo;
import com.platform.modules.sensitiveword.service.SensitiveWordService;
import org.springframework.transaction.annotation.Transactional;

@Service("liveCommentService")
@Transactional
public class LiveCommentServiceImpl extends ServiceImpl<LiveCommentDao, LiveCommentEntity>
		implements
			LiveCommentService {

	@Autowired
	private SensitiveWordService sensitiveWordService;

	@Override
	@Transactional(readOnly = true)
	public PageUtils queryPageByParams(Map<String, Object> params) {
		// 检索记录
		int current = ConverterUtils.parseInt(params.get("page"));
		int limit = ConverterUtils.parseInt(params.get("limit"));
		Page<Map<String, Object>> page = new Page<Map<String, Object>>(current, limit);
		List<LiveCommentEntity> pageList = this.baseMapper.queryPageMap(page, params);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (pageList != null && pageList.size() > 0) {
			for (int i = 0; i < pageList.size(); i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				LiveCommentEntity bean = (LiveCommentEntity) pageList.get(i);
				if (bean == null) {
					continue;
				}
				map.put("id", bean.getId());
				map.put("text", bean.getText());
				map.put("userName", bean.getUserName());
				map.put("createDate", bean.getCreateDate());
				map.put("source", bean.getSource());
				map.put("sourceShow", null != bean.getSource() ? CommentSourceEnum.getName(bean.getSource()) : null);
				map.put("isRelease", bean.getIsRelease());
				map.put("isReleaseShow",
						null != bean.getIsRelease() ? ReportReleaseEnum.getName(bean.getIsRelease()) : null);
				// 取消发布更新置空
				if (null != bean.getIsRelease() && bean.getIsRelease().equals(ReportReleaseEnum.RELEASED.getKey())) {
					map.put("releaseUserName", bean.getReleaseUserName());
					map.put("releaseDate", bean.getReleaseDate());
				} else {
					map.put("releaseUserName", null);
					map.put("releaseDate", null);
				}
				list.add(map);
			}
		}
		page.setRecords(list);
		return new PageUtils(page);
	}

	@Override
	@Transactional(readOnly = true)
	public List<CommentForeignEntity> queryForeignList(Integer sceneId, Integer page, Integer limit) {
		page = ConverterUtils.parseInt(page);
		limit = ConverterUtils.parseInt(limit);

		LiveCommentEntity comment = new LiveCommentEntity();
		// 未删除
		comment.setIsDelete(0);
		// 已发布
		comment.setIsRelease(1);
		comment.setSceneId(sceneId);
		Page<LiveCommentEntity> dbPage = new Page<>(page, limit);

		List<CommentForeignEntity> foreignList = new ArrayList<>();
		EntityWrapper<LiveCommentEntity> ew = new EntityWrapper<>(comment);
		ew.orderBy("id", false);
		dbPage = this.selectPage(dbPage, ew);
		if (null != dbPage && null != dbPage.getRecords() && dbPage.getRecords().size() > 0) {
			for (LiveCommentEntity entity : dbPage.getRecords()) {
				foreignList.add(new CommentForeignEntity(entity));
			}
		}
		return foreignList;
	}

	@Override
	@Transactional(readOnly = true)
	public Map<String, Object> info(Integer id) {
		if (null == id) {
			return null;
		}
		LiveCommentEntity bean = this.baseMapper.selectById(id);
		if (null == bean) {
			return null;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", bean.getId());
		map.put("text", bean.getText());
		map.put("userName", bean.getUserName());
		map.put("createDate", bean.getCreateDate());
		map.put("source", bean.getSource());
		map.put("sourceShow", null != bean.getSource() ? CommentSourceEnum.getName(bean.getSource()) : null);
		map.put("isRelease", bean.getIsRelease());
		map.put("isReleaseShow", null != bean.getIsRelease() ? ReportReleaseEnum.getName(bean.getIsRelease()) : null);
		// 取消发布更新置空
		if (null != bean.getIsRelease() && bean.getIsRelease().equals(ReportReleaseEnum.RELEASED.getKey())) {
			map.put("releaseUserName", bean.getReleaseUserName());
			map.put("releaseDate", bean.getReleaseDate());
		} else {
			map.put("releaseUserName", null);
			map.put("releaseDate", null);
		}
		return map;
	}

	@Override
	public void save(LiveCommentEntity liveReport, Long userId) {
		Date date = new Date();
		liveReport.setCreateDate(date);
		if (null != liveReport.getIsRelease()
				&& liveReport.getIsRelease().equals(ReportReleaseEnum.RELEASED.getKey())) {
			liveReport.setReleaseUser(userId);
			liveReport.setReleaseDate(new Date());
		} else {
			liveReport.setIsRelease(0);
		}
		liveReport.setCreateId(userId);
		String text = liveReport.getText();
		text = sensitiveWordService.replace(text);
		liveReport.setText(text);
		this.baseMapper.insert(liveReport);
	}

	@Override
	public void update(LiveCommentEntity liveReport, Long userId) {
		Date date = new Date();

		if (null != liveReport.getIsRelease()
				&& liveReport.getIsRelease().equals(ReportReleaseEnum.RELEASED.getKey())) {
			liveReport.setReleaseUser(userId);
			liveReport.setReleaseDate(new Date());
		} else {
			liveReport.setIsRelease(0);
		}

		liveReport.setLastUpdateDate(date);
		liveReport.setLastUpdateUser(userId);
		String text = liveReport.getText();
		text = sensitiveWordService.replace(text);
		liveReport.setText(text);
		this.baseMapper.updateById(liveReport);
	}

	@Override
	public void delete(Integer id, Long userId) {
		if (id == null) {
			return;
		}
		LiveCommentEntity entity = new LiveCommentEntity();
		entity.setIsDelete(1);
		entity.setId(id);
		entity.setLastUpdateDate(new Date());
		entity.setLastUpdateUser(userId);
		this.baseMapper.updateById(entity);
	}

	@Override
	public void deleteBatch(Integer[] ids, Long userId) {
		if (ids == null || ids.length == 0) {
			return;
		}
		for (Integer id : ids) {
			if (id == null) {
				continue;
			}
			LiveCommentEntity entity = new LiveCommentEntity();
			entity.setIsDelete(1);
			entity.setId(id);
			entity.setLastUpdateDate(new Date());
			entity.setLastUpdateUser(userId);
			this.baseMapper.updateById(entity);
		}
	}

	@Override
	public void releaseBatch(Integer[] ids, Long userId) {
		if (ids == null || ids.length == 0) {
			return;
		}
		for (Integer id : ids) {
			if (id == null) {
				continue;
			}
			LiveCommentEntity entity = new LiveCommentEntity();
			entity.setIsRelease(1);
			entity.setId(id);
			entity.setLastUpdateDate(new Date());
			entity.setLastUpdateUser(userId);
			entity.setReleaseDate(new Date());
			entity.setReleaseUser(userId);
			this.baseMapper.updateById(entity);
		}
	}

	@Override
	public void cancelReleaseBatch(Integer[] ids, Long userId) {
		if (ids == null || ids.length == 0) {
			return;
		}
		for (Integer id : ids) {
			if (id == null) {
				continue;
			}
			LiveCommentEntity entity = new LiveCommentEntity();
			entity.setIsRelease(0);
			entity.setId(id);
			entity.setLastUpdateDate(new Date());
			entity.setLastUpdateUser(userId);
			entity.setReleaseDate(null);
			entity.setReleaseUser(null);
			this.baseMapper.updateById(entity);
		}
	}

	@Override
	@Transactional(readOnly = true)
	public Page<LiveCommentAppVo> queryCommentCheckPageByParams(Integer page, Integer limit, Integer sceneId, Integer source, String text) {

		Page<LiveCommentAppVo> commentAppVoPage = new Page<>(page, limit);

		return commentAppVoPage.setRecords(this.baseMapper.queryCommentCheckPageByParams(sceneId,source,text,commentAppVoPage));
	}

	@Override
	public List<LiveCommentEntity> getBySceneId(Integer sceneId) {

		EntityWrapper<LiveCommentEntity> wrapper = new EntityWrapper<>();
		wrapper.eq("scene_id", sceneId);
		wrapper.eq("is_release", 1);
		return this.selectList(wrapper);
	}
}
