package seal

import (
	"hongze/hz_crm_api/models/contract"
	"hongze/hz_crm_api/models/system"
	"hongze/hz_crm_api/services"
)

// 消息来源-用印
var sourceType = 3

func MessageToNodeUser(flowNodeId, createUserId, contractApprovalId, messageType, approvalStatus int, companyName, content string) (err error) {
	//获取审批单中抄送节点的所有数据列表
	contractApprovalRecordList, err := contract.GetContractApprovalRecordListByContractIdAndNode(contractApprovalId, flowNodeId)
	if err != nil {
		return
	}
	err = messageToUser(contractApprovalRecordList, createUserId, messageType, approvalStatus, companyName, content)
	return
}

func messageToUser(contractApprovalRecordList []*contract.ContractApprovalRecord, createUserId, messageType, approvalStatus int, companyName, content string) (err error) {
	for _, contractApprovalRecord := range contractApprovalRecordList {
		sysUserMobile := ""
		sysUser, _ := system.GetSysAdminById(contractApprovalRecord.ApproveUserId)
		if sysUser != nil {
			sysUserMobile = sysUser.Mobile
		}
		//接收人，就是审批记录里面的待审核人（抄送人）
		receiveUserId := contractApprovalRecord.ApproveUserId
		go services.AddCompanyApprovalMessage(createUserId, receiveUserId, 0, contractApprovalRecord.ContractApprovalRecordId, messageType, sourceType, approvalStatus, companyName, content, content, sysUserMobile)
	}
	return
}
