package com.yonge.cooleshow.student.controller;

import com.dayaedu.cbs.openfeign.wrapper.message.CbsMessageWrapper;
import com.microsvc.toolkit.common.response.template.R;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.PageUtil;
import com.yonge.cooleshow.biz.dal.dto.search.SysMessageQueryInfo;
import com.yonge.cooleshow.biz.dal.entity.SysMessage;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.enums.MessageSendMode;
import com.yonge.cooleshow.biz.dal.enums.MessageTypeEnum;
import com.yonge.cooleshow.biz.dal.service.SysMessageService;
import com.yonge.cooleshow.biz.dal.wrapper.SysMessageWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.entity.Mapper;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.thirdparty.message.MessageSenderPluginContext;
import com.yonge.toolset.utils.validator.CommonValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "消息服务")
@RequestMapping("${app-config.url.student:}/sysMessage")
public class SysMessageController extends BaseController {

	@Autowired
	private SysMessageService sysMessageService;
	@Autowired
	private SysUserFeignService sysUserFeignService;

	@ApiOperation("获取所有消息列表")
	@PostMapping(value = "list")
	public HttpResponseResult<PageInfo<SysMessage>> list(@RequestBody SysMessageQueryInfo queryInfo){
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		queryInfo.setUserId(sysUser.getId());
		queryInfo.setType(MessageSendMode.PUSH.getCode());
		queryInfo.setClientId("STUDENT");
		return succeed(sysMessageService.queryListPage(queryInfo));
	}


	@ApiOperation("一键已读")
	@PostMapping("batchSetRead")
	public Object batchSetRead(@RequestParam(value = "messageType", required = false) MessageTypeEnum messageType) {
		int status = 1;
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		return succeed(sysMessageService.updateStatus(sysUser.getId(), status,"STUDENT",messageType) );
	}

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

	@ApiOperation(value = "查询用户未读消息条数")
	@GetMapping("/queryCountOfUnread")
	public HttpResponseResult<List<Mapper>> queryCountOfUnread() {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		List<Mapper>  map = sysMessageService.queryCountOfUnread(MessageSendMode.PUSH, sysUser.getId(), "STUDENT");

		return succeed(map);
	}
}
