package com.ym.mec.web.controller;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dto.SysMessageDto;
import com.ym.mec.biz.dal.enums.MessageSendMode;
import com.ym.mec.biz.dal.page.SysMessageQueryInfo;
import com.ym.mec.biz.service.SysMessageService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.page.PageInfo;

@RestController
@Api(tags = "消息服务")
@RequestMapping("sysMessage")
public class SysMessageController extends BaseController {

	@Autowired
	private SysMessageService sysMessageService;

	@Autowired
	private SysUserFeignService sysUserFeignService;

	@ApiOperation("获取所有消息列表")
	@GetMapping(value = "list")
	public Object list(SysMessageQueryInfo queryInfo){
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		if(!sysUser.getIsSuperAdmin()){
			queryInfo.setUserId(sysUser.getId());
		}
		queryInfo.setType(MessageSendMode.SEO.getCode());
		PageInfo<SysMessageDto> pageInfo = sysMessageService.queryListPage(queryInfo);
		return succeed(pageInfo);
	}

	@ApiOperation("设置已读")
	@PostMapping("setRead")
	public Object setRead(Long id) {
		int status = 1;
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		return sysMessageService.updateOneStatus(id, status) > 0 ? succeed() : failed();
	}

	@ApiOperation(value = "查询用户未读消息条数")
	@GetMapping("/queryCountOfUnread")
	public Object queryCountOfUnread() {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		return succeed(sysMessageService.queryCountOfUnread(MessageSendMode.SEO, sysUser.getId(),null));
	}
}
