package company_approval_message

import (
	"encoding/json"
	"hongze/hongze_yb/models/tables/company_approval_message"
	"time"
)

//消息来源类型，1:客户，2：合同，3：用印，4：指标替换，5：问答社区，6：问答评论，7：视频评论，8：线上路演评论
const (
	CompanyApprovalMessageSourceTypeByCompany          = 1
	CompanyApprovalMessageSourceTypeByContract         = 2
	CompanyApprovalMessageSourceTypeBySeal             = 3
	CompanyApprovalMessageSourceTypeByEdbInfo          = 4
	CompanyApprovalMessageSourceTypeByQuestion         = 5
	CompanyApprovalMessageSourceTypeByQuestionComment  = 6
	CompanyApprovalMessageSourceTypeByVideoComment     = 7
	CompanyApprovalMessageSourceTypeByRoadVideoComment = 8
)

// 1:申请消息，2:审批结果，3：消息通知
const (
	CompanyApprovalMessageMessageTypeByApply          = 1
	CompanyApprovalMessageMessageTypeByApprovalResult = 2
	CompanyApprovalMessageMessageTypeByMessage        = 3
)

//审批状态,1:待审批，2:已审批，3:已驳回
const (
	CompanyApprovalMessageApprovalStatusByPending  = 1
	CompanyApprovalMessageApprovalStatusByApproved = 2
	CompanyApprovalMessageApprovalStatusByRejected = 3
)

// MessageInfo 消息主要内容
type MessageInfo struct {
	CompanyName          string    `json:"company_name"`
	ProductId            int       `json:"product_id"`
	CompanyProductStatus string    `json:"company_product_status"`
	Title                string    `json:"title"`
	Content              string    `json:"content"`
	UserId               uint64    `json:"user_id"`
	UserName             string    `json:"user_name"`
	CreateTime           time.Time `json:"create_time"`
	Extra                string    `json:"extra" description:"附加字段"`
	MessageSource        string    `json:"message_source" description:"消息来源：问答社区/视频社区/线上路演"`
}

// AddCompanyApprovalMessage 添加系统消息
// @params createUserId int "消息创建人id"
// @params receiveUserId int "消息接收人id"
// @params companyApprovalId int "消息对应的id"
func AddCompanyApprovalMessage(createUserId, receiveUserId, companyId, companyApprovalId int, msgType, sourceType, approvalStatus int8, companyName, remark, content string, messageInfo MessageInfo) (err error) {
	messageInfoStr, err := json.Marshal(messageInfo)
	if err != nil {
		return
	}
	msgItem := &company_approval_message.CompanyApprovalMessage{
		//ID:                0,
		CreateUserID:      createUserId,
		ReceiveUserID:     receiveUserId,
		MessageStatus:     0, //消息状态:0未读，1:已读，2：作废
		Remark:            remark,
		Content:           content,
		CompanyID:         companyId,
		CompanyName:       companyName,
		CreateTime:        time.Now(),
		ModifyTime:        time.Now(),
		CompanyApprovalID: companyApprovalId,
		ApprovalStatus:    approvalStatus,         //审批状态,1:待审批，2:已审批，3:已驳回
		OperationStatus:   1,                      //消息状态:1:待审批，2:已审批
		MessageType:       msgType,                //1:申请消息，2:审批结果，3：消息通知
		SourceType:        sourceType,             //消息来源，1:客户，2：合同，3：用印
		MessageInfo:       string(messageInfoStr), //消息主要内容，json数据
	}
	err = msgItem.Create()
	return
}

// CancelCompanyApprovalMessage 消息作废
func CancelCompanyApprovalMessage(companyApprovalId int, sourceType int8) (err error) {
	err = company_approval_message.Cancel(companyApprovalId, sourceType)
	return
}
