package controllers

import (
	"eta/eta_api/models"
	"eta/eta_api/models/data_manage/data_manage_permission"
	"eta/eta_api/models/report_approve"
	"fmt"
)

// MessageController
// @Description: 消息控制器
type MessageController struct {
	BaseAuthController
}

// UnReadMessageNum
// @Title 获取未读取的消息数量
// @Description 获取未读取的消息数量
// @Success 200 {object} data_manage_permission.MessageDetailListResp
// @router /unread/num [get]
func (c *MessageController) UnReadMessageNum() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		c.Data["json"] = br
		c.ServeJSON()
	}()
	sysUser := c.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}

	var unReadReportNum, unReadDataPermissionNum int

	// 获取报告审批消息
	{
		cond := fmt.Sprintf(` AND %s = ?`, report_approve.ReportApproveMessageCols.ReceiveUserId)
		pars := make([]interface{}, 0)
		pars = append(pars, sysUser.AdminId)

		messageOb := new(report_approve.ReportApproveMessage)

		// 未读消息数
		cond += fmt.Sprintf(` AND %s = ?`, report_approve.ReportApproveMessageCols.IsRead)
		pars = append(pars, 0)
		unreadTotal, e := messageOb.GetCountByCondition(cond, pars)
		if e != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取审批消息列表总数失败, Err: " + e.Error()
			return
		}
		unReadReportNum = unreadTotal
	}

	// 获取资产消息
	{
		cond := fmt.Sprintf(` AND %s = ?`, data_manage_permission.DataPermissionMessageCols.ReceiveUserId)
		pars := make([]interface{}, 0)
		pars = append(pars, sysUser.AdminId)

		messageOb := new(data_manage_permission.DataPermissionMessage)
		// 未读消息数
		cond += fmt.Sprintf(` AND %s = ?`, data_manage_permission.DataPermissionMessageCols.IsRead)
		pars = append(pars, 0)
		unreadTotal, e := messageOb.GetCountByCondition(cond, pars)
		if e != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取资产消息列表总数失败, Err: " + e.Error()
			return
		}
		unReadDataPermissionNum = unreadTotal
	}

	// 汇总数
	num := unReadReportNum + unReadDataPermissionNum

	br.Data = num
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
}
