package com.yonge.cooleshow.bbs.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.bbs.dto.BbsPrivateMessageDto;
import com.yonge.cooleshow.bbs.dto.MessageReadDto;
import com.yonge.cooleshow.bbs.dto.PrivateMessagePersonVo;
import com.yonge.cooleshow.bbs.enums.YesOrNoEnum;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;

import com.yonge.toolset.base.util.StringUtil;
import com.yonge.toolset.mybatis.support.PageUtil;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import com.yonge.cooleshow.bbs.dto.search.BbsPrivateMessageSearch;
import com.yonge.cooleshow.bbs.entity.BbsPrivateMessage;
import com.yonge.cooleshow.bbs.service.BbsPrivateMessageService;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/bbsPrivateMessage")
@Api(value = "私信", tags = "私信")
public class BbsPrivateMessageController extends BaseController {

    @Autowired
    private BbsPrivateMessageService bbsPrivateMessageService;

	@Autowired
	private SysUserFeignService sysUserFeignService;
    

    @PostMapping("/page/person")
    @ApiOperation(value = "私信人员列表", notes = "传入bbsPrivateMessageSearch")
    public HttpResponseResult<PageInfo<PrivateMessagePersonVo>> pagePerson(@RequestBody BbsPrivateMessageSearch query) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || user.getId() == null) {
			throw  new BizException("请重新登录");
		}
		query.setUserId(user.getId());
		IPage<PrivateMessagePersonVo> pages = bbsPrivateMessageService.selectPersonPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
	}

	@PostMapping("/page/message")
	@ApiOperation(value = "私信人员消息列表", notes = "传入bbsPrivateMessageSearch")
	public HttpResponseResult<BbsPrivateMessageDto> pageMessage(@RequestBody @Valid BbsPrivateMessageSearch query) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || user.getId() == null) {
			throw  new BizException("请重新登录");
		}
		SysUser receiverUser = sysUserFeignService.queryUserById(query.getReceiverUserId());
		if (receiverUser == null || user.getId() == null) {
			throw  new BizException("私信接收方用户不存在");
		}
		BbsPrivateMessageDto bbsPrivateMessageDto = new BbsPrivateMessageDto();
		query.setUserId(user.getId());
		IPage<BbsPrivateMessage> pages = bbsPrivateMessageService.selectReadMessagePage(PageUtil.getPage(query), query);
		bbsPrivateMessageDto.setReadPage(PageUtil.pageInfo(pages));
		bbsPrivateMessageDto.setReceiverAvatar(receiverUser.getAvatar());
		bbsPrivateMessageDto.setReceiverName(receiverUser.getUsername());
		bbsPrivateMessageDto.setUserName(user.getUsername());
		bbsPrivateMessageDto.setUserAvatar(user.getAvatar());
		List<BbsPrivateMessage> unreadMessage  = bbsPrivateMessageService.selectUnreadMessage(query);
		bbsPrivateMessageDto.setUnreadList(unreadMessage);
		return succeed(bbsPrivateMessageDto);
	}


	@PostMapping("/save")
	@ApiOperation(value = "发送私信", notes = "传入bbsPrivateMessage")
	public HttpResponseResult save(@Valid @RequestBody BbsPrivateMessage bbsPrivateMessage) {

		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || user.getId() == null) {
			throw  new BizException("请重新登录");
		}

		SysUser receiverUser = sysUserFeignService.queryUserById(bbsPrivateMessage.getReceiverUserId());
		if (receiverUser == null || user.getId() == null) {
			throw  new BizException("私信接收方用户不存在");
		}
		bbsPrivateMessage.setSendUserId(user.getId());
		bbsPrivateMessage.setCreatedTime(new Date());
		bbsPrivateMessage.setReadFlag(YesOrNoEnum.NO);
		bbsPrivateMessageService.save(bbsPrivateMessage);
    	return succeed(bbsPrivateMessage);
	}


	@PostMapping("/read")
	@ApiOperation(value = "设置已读", notes = "传入bbsPrivateMessage")
	public HttpResponseResult read(@RequestBody @Valid MessageReadDto readDto) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || user.getId() == null) {
			throw  new BizException("请重新登录");
		}
		readDto.setUserId(user.getId());
		return status(bbsPrivateMessageService.read(readDto));
	}



	@PostMapping("/remove/{ids}")
	@ApiOperation(value = "测回消息", notes = "传入ids")
	public HttpResponseResult remove(@PathVariable String ids) {
        if (StringUtil.isEmpty(ids)) {
			return failed("参数不能为空");
		}
		return status(bbsPrivateMessageService.removeByIds(StringUtil.toLongList(ids)));
	}
}