package org.springblade.flow.message.controller;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.springblade.flow.base.ActionResult;
import org.springblade.flow.base.vo.ListVO;
import org.springblade.flow.message.model.ImReplyListModel;
import org.springblade.flow.message.model.ImReplyListVo;
import org.springblade.flow.message.service.ImContentService;
import org.springblade.flow.message.service.ImReplyService;
import org.springblade.flow.permission.entity.UserEntity;
import org.springblade.flow.permission.service.User4FlowService;
import org.springblade.flow.util.JsonUtil;
import org.springblade.flow.util.UploaderUtil;
import org.springblade.flow.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 消息会话接口
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @date 2021-05-29
 */
@Api(tags = "消息会话接口", value = "imreply")
@RestController
@RequestMapping("/paas/message/imreply")
public class ImReplyController {
	@Autowired
	private ImReplyService imReplyService;
	@Autowired
	private ImContentService imContentService;
	@Autowired
	private User4FlowService userService;
	@Autowired
	private UserProvider userProvider;

	/**
	 * 获取消息会话列表
	 *
	 * @return
	 */
	@ApiOperation("获取消息会话列表")
	@GetMapping
	public ActionResult getList() {
		List<ImReplyListModel> imReplyList = imReplyService.getImReplyList();
		for (ImReplyListModel vo : imReplyList) {
			// 头像路径拼接
			vo.setHeadIcon(UploaderUtil.uploaderImg(vo.getHeadIcon()));
			// 获取未读消息
			vo.setUnreadMessage(imContentService.getUnreadCount(vo.getId(), userProvider.getUserId()));
			UserEntity entity = userService.getInfo(vo.getId());
			if (entity != null) {
				// 拼接账号和名称
				vo.setRealName(entity.getRealName());
				vo.setAccount(entity.getAccount());
			}
		}
		// 排序
		imReplyList = imReplyList.stream().sorted(Comparator.comparing(ImReplyListModel::getLatestDate).reversed())
				.collect(Collectors.toList());
		List<ImReplyListVo> imReplyListVoList = JsonUtil.getJsonToList(imReplyList, ImReplyListVo.class);
		ListVO listVO = new ListVO();
		listVO.setList(imReplyListVoList);
		return ActionResult.success(listVO);
	}

}
