package com.hotent.portal.controller;

import com.hotent.base.controller.BaseController;
import com.hotent.base.controller.BaseController2;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.PageBean;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.query.QueryOP;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.StringUtil;
import com.hotent.portal.model.MessageRead;
import com.hotent.portal.model.SysMessage;
import com.hotent.portal.params.MessaboxVo;
import com.hotent.portal.persistence.manager.MessageReadManager;
import com.hotent.portal.persistence.manager.SysMessageManager;
import com.hotent.uc.api.impl.util.ContextUtil;
import com.hotent.uc.api.model.IUser;
import com.hotent.uc.api.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 内部消息
 * 
 * @company 广州宏天软件股份有限公司
 * @author zhangxianwen
 * @email zhangxw@jee-soft.cn
 * @date 2018年7月17日
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@RestController
@RequestMapping("/innermsg/messageReceiver/v2")
@Api(tags="内部消息接收2")
public class MessageReceiverController2 extends BaseController2 {
	@Resource
	MessageReadManager messageReadManager;
	@Resource
	SysMessageManager sysMessageManager;
	@Resource
	IUserService iUserService;

	@RequestMapping(value="list", method=RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "消息列表", httpMethod = "POST", notes = "消息列表")
	public CommonResult<PageList<SysMessage>> listJson(@ApiParam(name="queryFilter",value="通用查询对象")@RequestBody QueryFilter queryFilter) throws Exception {
		queryFilter.addFilter("receiverId", ContextUtil.getCurrentUserId(), QueryOP.EQUAL);
		PageList<SysMessage> sysMessageList = sysMessageManager.getMsgByUserId(queryFilter);
		List<SysMessage> list = sysMessageList.getRows();
		PageList<SysMessage> newSysMessageList = new PageList<SysMessage>();
		//消息类型值得转换
//		for (int i = 0; i < list.size(); i++) {
//			SysMessage sysMessage = list.get(i);
//			String messageType= MessageTypeUtil.getValue(sysMessage.getMessageType());
//			sysMessage.setMessageType(messageType);
//			newSysMessageList.getRows().add(sysMessage);
//		}
		newSysMessageList.setRows(list);
		newSysMessageList.setPage(sysMessageList.getPage());
		newSysMessageList.setPageSize(sysMessageList.getPageSize());
		newSysMessageList.setTotal(sysMessageList.getTotal());
		return CommonResult.<PageList<SysMessage>>ok().value(newSysMessageList);
	}

	@RequestMapping(value="edit", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "消息编辑页面信息", httpMethod = "GET", notes = "消息编辑页面信息")
	public CommonResult<MessageRead> edit(@ApiParam(name="id", value="主键", required = false)@RequestParam String id) throws Exception {
		MessageRead messageRead = null;
		if(StringUtil.isNotEmpty(id)){
			messageRead = messageReadManager.get(id);
		}
		return CommonResult.<MessageRead>ok().value(messageRead);
	}

	@RequestMapping(value="get", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "消息明细页面", httpMethod = "GET", notes = "消息明细页面")
	public CommonResult<SysMessage> get(@ApiParam(name="id", value="主键", required = false)@RequestParam String id) throws Exception {
		SysMessage sysMessage = sysMessageManager.get(id);
		IUser user = ContextUtil.getCurrentUser();
		//保存到已读表
		messageReadManager.addMessageRead(id, user);
		sysMessageManager.update(sysMessage);
		return CommonResult.<SysMessage>ok().value(sysMessage);
	}
	
	@RequestMapping(value="remove",method=RequestMethod.DELETE, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "删除消息记录", httpMethod = "DELETE", notes = "删除消息记录")
	public CommonResult<String> remove(@ApiParam(name="ids",value="任务记录ID，多个用“,”号分隔", required = true) @RequestParam String ids) throws Exception{
		String[] aryIds = null;
		if(!StringUtil.isEmpty(ids)){
			aryIds = ids.split(",");
		}
		sysMessageManager.removeByIds(Arrays.asList(aryIds));
		return CommonResult.<String>ok().message("删除消息成功");
	}
	
	@RequestMapping(value="getMessBoxInfo", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取用户已读未读消息", httpMethod = "GET", notes = "获取用户已读未读消息")
	public CommonResult<MessaboxVo> getMessBoxInfo(@ApiParam(name="account", value="用户账号", required = true)@RequestParam String account) throws Exception {
		MessaboxVo messBoxInfo = sysMessageManager.getMessBoxInfo(account);
		return CommonResult.<MessaboxVo>ok().value(messBoxInfo);
	}
	
	@RequestMapping(value="mark",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "标记为已读", httpMethod = "GET", notes = "标记为已读")
	public CommonResult<String> mark(@ApiParam(name="ids",value="任务记录ID，多个用“,”号分隔", required = true) @RequestParam String ids) throws Exception{
		String[] aryIds = null;
		if(!StringUtil.isEmpty(ids)){
			aryIds = ids.split(",");
		}
		IUser user = ContextUtil.getCurrentUser();
		//保存到已读表
		for (String string : aryIds) {
			messageReadManager.addMessageRead(string, user);
		}
		return CommonResult.<String>ok().message("标记成功");
	}
	
	@RequestMapping(value="getMsgbadge",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取当前用户未读消息数量", httpMethod = "GET", notes = "获取当前用户未读消息数量")
	public CommonResult<Integer> getMsgbadge(@ApiParam(name="isPublic", value="是否系统公告", required = false)@RequestParam(required = false) String isPublic) throws Exception{
		if(StringUtil.isEmpty(isPublic)) {
			isPublic = "0"; //是否公告 0:个人 1:系统公告
		}
		int badge = sysMessageManager.getNotReadMsgNum(ContextUtil.getCurrentUserId(), isPublic);
		return CommonResult.<Integer>ok().message("获取成功").value(badge);
	}
}
