package controllers

import (
	"eta/eta_mini_crm_ht/models"
	"eta/eta_mini_crm_ht/models/response"
	"eta/eta_mini_crm_ht/utils"
	"github.com/rdlucklib/rdluck_tools/paging"
)

type SysMessageReportController struct {
	BaseAuthController
}

// @Title 阅读消息
// @Description 阅读消息
// @Param	request	body system.SysRoleDeleteReq true "type json string"
// @Success 200 查看成功
// @router /read [post]
func (this *SysMessageReportController) Read() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	err := models.UpdateReadSysMessageReportByUserId(this.SysUser.SysUserId)
	if err != nil {
		br.Msg = "更新消息状态失败"
		br.ErrMsg = "更新消息状态失败，系统错误，Err:" + err.Error()
		return
	}

	br.Msg = "查看成功"
	br.Success = true
	br.Ret = 200
}

// List
// @Title 系统消息列表
// @Description 系统消息列表
// @Param   PageSize   query   int  true       "每页数据条数"
// @Param   CurrentIndex   query   int  true       "当前页页码，从1开始"
// @Success 200 {object} response.SysRoleListResp
// @router /list [get]
func (this *SysMessageReportController) List() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")

	var startSize int
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = utils.StartIndex(currentIndex, pageSize)

	total, err := models.GetSysMessageReportCountBySysUserId(this.SysUser.SysUserId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	list, err := models.GetSysMessageReportBySysUserId(this.SysUser.SysUserId, startSize, pageSize)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	messageList := make([]*models.SysMessageReportView, 0)
	for _, v := range list {
		messageList = append(messageList, &models.SysMessageReportView{
			SysMessageReportId: v.SysMessageReportId,
			UserId:             v.UserId,
			ReceiveSysUserId:   v.ReceiveSysUserId,
			MessageType:        v.MessageType,
			Content:            v.Content,
			Remark:             v.Remark,
			IsRead:             v.IsRead,
			CreateTime:         v.CreateTime.Format(utils.FormatDateTime),
			ModifyTime:         v.ModifyTime.Format(utils.FormatDateTime),
		})

	}
	page := paging.GetPaging(currentIndex, pageSize, total)
	resp := new(response.SysMessageListResp)
	resp.List = messageList
	resp.Paging = page
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}
