package com.qunzhixinxi.hnqz.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qunzhixinxi.hnqz.admin.entity.SysWxReplyMessage;
import com.qunzhixinxi.hnqz.admin.entity.SysWxReplyMessageMedia;
import com.qunzhixinxi.hnqz.admin.entity.SysWxReplyMessageRelation;
import com.qunzhixinxi.hnqz.admin.mapper.SysWxReplyMessageMapper;
import com.qunzhixinxi.hnqz.admin.service.SysWxReplyMessageMediaService;
import com.qunzhixinxi.hnqz.admin.service.SysWxReplyMessageRelationService;
import com.qunzhixinxi.hnqz.admin.service.SysWxReplyMessageService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 微信消息自动回复
 *
 * @author lixuesong
 * @date 2021年04月28日 17:18
 */
@Service
@AllArgsConstructor
public class SysWxReplyMessageServiceImpl extends ServiceImpl<SysWxReplyMessageMapper, SysWxReplyMessage> implements SysWxReplyMessageService {

	private final SysWxReplyMessageMapper sysWxReplyMessageMapper;

	private final SysWxReplyMessageRelationService sysWxReplyMessageRelationService;

	private final SysWxReplyMessageMediaService sysWxReplyMessageMediaService;

	/**
	 * 查询消息列表分页
	 *
	 * @param page
	 * @param sysWxReplyMessage
	 * @return
	 */
	@Override
	public IPage<List<SysWxReplyMessage>> listMessagePage(Page page, SysWxReplyMessage sysWxReplyMessage) {
		return sysWxReplyMessageMapper.listMessagePage(page, sysWxReplyMessage);
	}

	/**
	 * 根据id查询消息
	 *
	 * @param id
	 * @return
	 */
	@Override
	public SysWxReplyMessage getMessageById(Integer id) {
		return sysWxReplyMessageMapper.getMessageById(id);
	}

	/**
	 * 保存消息
	 *
	 * @param sysWxReplyMessage
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveMessage(SysWxReplyMessage sysWxReplyMessage) {
		// 保存主消息表
		LocalDateTime now = LocalDateTime.now();
		sysWxReplyMessage.setCreateTime(now);
		sysWxReplyMessage.setUpdateTime(now);
		save(sysWxReplyMessage);
		// 保存消息素材
		List<SysWxReplyMessageRelation> relationList = new ArrayList<>();
		for (int i = 0; i < sysWxReplyMessage.getMediaList().size(); i++) {
			// media子对象
			SysWxReplyMessageMedia media = sysWxReplyMessage.getMediaList().get(i);
			media.setSort(i + 1);
			media.setCreateTime(now);
			media.setUpdateTime(now);
			sysWxReplyMessageMediaService.save(media);
			// 中间表对象
			SysWxReplyMessageRelation relation = new SysWxReplyMessageRelation();
			relation.setMessageId(sysWxReplyMessage.getId());
			relation.setMessageMediaId(media.getId());
			relationList.add(relation);
		}
		// 中间表对象批量保存
		return sysWxReplyMessageRelationService.saveBatch(relationList);
	}

	/**
	 * 更新消息
	 *
	 * @param sysWxReplyMessage 消息实体
	 * @return 是否成功标志
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateMessageById(SysWxReplyMessage sysWxReplyMessage) {
		// 更新主消息表
		LocalDateTime now = LocalDateTime.now();
		sysWxReplyMessage.setUpdateTime(now);
		this.updateById(sysWxReplyMessage);
		// 删除旧数据
		List<SysWxReplyMessageRelation> relationList = sysWxReplyMessageRelationService.list(Wrappers.<SysWxReplyMessageRelation>lambdaQuery()
				.eq(SysWxReplyMessageRelation::getMessageId, sysWxReplyMessage.getId()));
		if (CollectionUtil.isNotEmpty(relationList)) {
			List<Integer> relationIdList = relationList.stream().map(SysWxReplyMessageRelation::getId).collect(Collectors.toList());
			List<Integer> mediaIdList = relationList.stream().map(SysWxReplyMessageRelation::getMessageMediaId).collect(Collectors.toList());
			sysWxReplyMessageRelationService.removeByIds(relationIdList);
			sysWxReplyMessageMediaService.removeByIds(mediaIdList);
		}
		// 保存消息素材
		List<SysWxReplyMessageRelation> newRelationList = new ArrayList<>();
		for (int i = 0; i < sysWxReplyMessage.getMediaList().size(); i++) {
			// media子对象
			SysWxReplyMessageMedia media = sysWxReplyMessage.getMediaList().get(i);
			media.setSort(i + 1);
			media.setCreateTime(now);
			media.setUpdateTime(now);
			media.setId(null);
			sysWxReplyMessageMediaService.save(media);
			// 中间表对象
			SysWxReplyMessageRelation relation = new SysWxReplyMessageRelation();
			relation.setMessageId(sysWxReplyMessage.getId());
			relation.setMessageMediaId(media.getId());
			newRelationList.add(relation);
		}
		// 中间表对象批量保存
		return sysWxReplyMessageRelationService.saveBatch(newRelationList);
	}

	/**
	 * 删除消息
	 *
	 * @param id 主键id
	 * @return 是否成功标志
	 */
	@Override
	public boolean deleteMessageById(Integer id) {
		this.removeById(id);
		List<SysWxReplyMessageRelation> relationList = sysWxReplyMessageRelationService.list(Wrappers.<SysWxReplyMessageRelation>lambdaQuery()
				.eq(SysWxReplyMessageRelation::getMessageId, id));
		if (CollectionUtil.isNotEmpty(relationList)) {
			List<Integer> relationIdList = relationList.stream().map(SysWxReplyMessageRelation::getId).collect(Collectors.toList());
			List<Integer> MediaIdList = relationList.stream().map(SysWxReplyMessageRelation::getMessageMediaId).collect(Collectors.toList());
			sysWxReplyMessageRelationService.removeByIds(relationIdList);
			sysWxReplyMessageMediaService.removeByIds(MediaIdList);
			return true;
		}
		return false;
	}
}
