package message

import (
	"errors"
	"fmt"
	"hongze/hongze_yb/global"
	"hongze/hongze_yb/models/request/message"
	"hongze/hongze_yb/models/response"
	"hongze/hongze_yb/models/tables/yb_message"
	"hongze/hongze_yb/services/user"
	"hongze/hongze_yb/utils"
	"time"
)

// GetList 查询消息列表
func GetList(user user.UserInfo, msgType, pageIndex, pageSize int) (ret response.RespMessageList, err error) {
	var errMsg string
	defer func() {
		if err != nil {
			global.LOG.Critical(fmt.Sprintf("message GetList: userId=%d, err:%s， errMsg:%s", user.UserID, err.Error(), errMsg))
		}
	}()

	//校验入参
	//查询消息列表
	offset := (pageIndex - 1) * pageSize
	msgList, err := yb_message.GetListByType(user.UserID, msgType, offset, pageSize)
	if err != nil {
		errMsg = err.Error()
		err = errors.New("查询消息列表出错")
		return
	}

	total, err := yb_message.GetListTotalByType(user.UserID, msgType)
	if err != nil {
		errMsg = err.Error()
		err = errors.New("查询消息列表总数出错")
		return
	}

	//处理列表返回值
	var list []*response.RespMessageItem
	for _, v := range msgList {
		tmp := new(response.RespMessageItem)
		tmp.MsgId = v.MsgId
		tmp.IsRead = v.IsRead
		tmp.UserId = v.UserId
		tmp.Type = v.Type
		tmp.CreateTime = v.CreateTime
		tmp.ReportChapterId = v.ReportChapterId
		tmp.ReportId = v.ReportId
		tmp.OldReportId = v.OldReportId
		tmp.OldReportChapterId = v.OldReportChapterId
		tmp.ContentFirst = v.ContentFirst
		tmp.ContentSecond = v.ContentSecond
		tmp.Img = utils.DEFAULT_HONGZE_SYS_LOGO
		list = append(list, tmp)
	}
	
	ret.List = list
	ret.Paging = response.GetPaging(pageIndex, pageSize, int(total))
	return
}

// BatchRead 一键已读
func BatchRead(user user.UserInfo, req message.ReqMessageBatch) (err error) {
	var errMsg string
	defer func() {
		if err != nil {
			global.LOG.Critical(fmt.Sprintf("message BatchRead: userId=%d, err:%s， errMsg:%s", user.UserID, err.Error(), errMsg))
		}
	}()

	//根据类型设置一键已读
    err = yb_message.SetBatchRead(user.UserID, req.Type)
	if err != nil {
		errMsg = err.Error()
		err = errors.New("一键已读设置失败")
		return
	}
	return
}

// SingleRead 单条消息设置已读
func SingleRead(user user.UserInfo, req message.ReqMessage) (err error) {
	var errMsg string
	defer func() {
		if err != nil {
			global.LOG.Critical(fmt.Sprintf("message SingleRead: userId=%d, err:%s， errMsg:%s", user.UserID, err.Error(), errMsg))
		}
	}()
	if req.MsgId <=0 {
		err = errors.New("请输入消息序号")
		return
	}
	//查询当前消息
	msgInfo, err := yb_message.GetMsgByMsgId(user.UserID, req.MsgId)
	if err != nil {
		errMsg = err.Error()
		err = errors.New("查询消息出错")
		return
	}
	if msgInfo.MsgId <= 0 {
		err = errors.New("消息不存在")
		return
	}
	if msgInfo.IsRead == 1 {
		return
	}
	msgInfo.IsRead = 1
	msgInfo.ModifyTime = time.Now()
	//根据类型设置一键已读
	err = msgInfo.Update([]string{"is_read","modify_time"})
	if err != nil {
		errMsg = err.Error()
		err = errors.New("已读设置失败")
		return
	}
	return
}


// SingleDelete 单条消息删除
func SingleDelete(user user.UserInfo, req message.ReqMessage) (err error) {
	var errMsg string
	defer func() {
		if err != nil {
			global.LOG.Critical(fmt.Sprintf("message SingleDelete: userId=%d, err:%s， errMsg:%s", user.UserID, err.Error(), errMsg))
		}
	}()
	if req.MsgId <=0 {
		err = errors.New("请输入消息序号")
		return
	}
	//查询当前消息
	msgInfo, err := yb_message.GetMsgByMsgId(user.UserID, req.MsgId)
	if err != nil {
		errMsg = err.Error()
		err = errors.New("查询消息出错")
		return
	}
	if msgInfo.MsgId <= 0 {
		err = errors.New("消息不存在")
		return
	}
	if msgInfo.Enabled == 0 {
		return
	}
	msgInfo.Enabled = 0
	msgInfo.ModifyTime = time.Now()
	//根据类型设置一键已读
	err = msgInfo.Update([]string{"enabled","modify_time"})
	if err != nil {
		errMsg = err.Error()
		err = errors.New("删除消息失败")
		return
	}
	return
}
