package seal import ( "encoding/json" "errors" "fmt" "hongze/hongze_mobile_admin/models/custom" "hongze/hongze_mobile_admin/models/tables/admin" "hongze/hongze_mobile_admin/models/tables/approval_flow_node" "hongze/hongze_mobile_admin/models/tables/company_approval_message" "hongze/hongze_mobile_admin/models/tables/contract_approval" "hongze/hongze_mobile_admin/models/tables/contract_approval_record" "hongze/hongze_mobile_admin/models/tables/seal" "hongze/hongze_mobile_admin/services" "hongze/hongze_mobile_admin/services/alarm_msg" "hongze/hongze_mobile_admin/services/flow" "hongze/hongze_mobile_admin/utils" "strconv" "strings" "time" ) //消息来源 var sourceType = 3 //提交审批 func Apply(sealInfo *seal.Seal) (err error) { //发起审批 approval, contractApprovalRecordList, err := apply(sealInfo) if err != nil { return } //待办通知 { //发送消息下级审批人 go messageToNodeUser(contractApprovalRecordList[0].NodeId, approval.ApplyUserId, approval.ContractApprovalId, 1, 1, sealInfo.CompanyName, sealInfo.Use) //go messageToAllCcUser(approval.ApplyUserId, approval.ContractApprovalId, 1, 1, sealInfo.CompanyName, sealInfo.Use) } return } // GetFlowTypeBySealType 根据盖章类型(多选,逗号拼接)获取审批流的类型 func GetFlowTypeBySealType(sealType string) (flowType int) { flowType = 5 // 合同章 longFlow := "公章,法人章" // 盖章类型包含其中之一则审批流变成6 sealTypeArr := strings.Split(sealType, ",") for _, itemType := range sealTypeArr { if strings.Contains(longFlow, itemType) { flowType = 6 } } return } //审批人修改后重新提交审批单 func CheckApply(sealInfo *seal.Seal, opUser *custom.AdminWx) (err error) { //发起审批 approvalInfo, contractApprovalRecordList, err := apply(sealInfo) if err != nil { return } //校验一级审批人是否与当前提交人一致,如果一致的话,那么审批通过且不发送消息给一级审批人 var contractApprovalRecord *contract_approval_record.ContractApprovalRecord for _, tmpContractApprovalRecord := range contractApprovalRecordList { if tmpContractApprovalRecord.NodeId == approvalInfo.CurrNodeId && tmpContractApprovalRecord.ApproveUserId == opUser.AdminId { contractApprovalRecord = tmpContractApprovalRecord } } //如果一级审批人和当前提交人一致,那么审批通过,且不发送消息给一级审批人 if contractApprovalRecord != nil { sealInfo, approvalInfo, approvalRecord, tmpErr := CheckApproveAuth(sealInfo.SealId, opUser) if tmpErr != nil { err = tmpErr return } err = Approved(sealInfo, approvalInfo, approvalRecord, opUser, "") } else { //如果一级审批人和当前提交人不一致,那么发送消息给一级审批人,重走审批流程 //待办通知 { //发送消息下级审批人 go messageToNodeUser(contractApprovalRecordList[0].NodeId, approvalInfo.ApplyUserId, approvalInfo.ContractApprovalId, 1, 1, sealInfo.CompanyName, sealInfo.Use) //发送消息给所有的抄送人 //go messageToAllCcUser(approval.ApplyUserId, approval.ContractApprovalId, 1, 1, sealInfo.CompanyName, sealInfo.Use) } } return } //发起审批 func apply(sealInfo *seal.Seal) (approval *contract_approval.ContractApproval, contractApprovalRecordList []*contract_approval_record.ContractApprovalRecord, err error) { //用印状态判断 ignoreStatus := []string{"待提交", "已撤回", "已驳回"} if !strings.Contains(strings.Join(ignoreStatus, ","), sealInfo.Status) { err = errors.New("用印单状态异常,不允许提交审批,当前用印单状态:" + sealInfo.Status) return } //将用印数据存为快照 sealDetailByte, err := json.Marshal(sealInfo) if err != nil { err = errors.New(fmt.Sprint("用印单异常,当前用印单数据格式化失败:", err)) return } //获取审批流 flowId := GetFlowTypeBySealType(sealInfo.SealType) flowItemInfo, err := flow.GetApprovalFlow(flowId) if err != nil { return } if len(flowItemInfo.NodeList) <= 0 { err = errors.New("审批流程未配置") return } //校验是否存在待审批的审批单(其实没有多大意义,只是为了 异常数据校验) approval = &contract_approval.ContractApproval{ ContractId: sealInfo.SealId, ApprovalType: "seal", Status: "待审批", ApplyContent: sealInfo.Use, ContractDetail: string(sealDetailByte), ApplyUserId: sealInfo.UserId, ApplyUserName: sealInfo.UserName, FlowId: flowItemInfo.FlowId, FlowVersion: flowItemInfo.CurrVersion, CurrNodeId: flowItemInfo.NodeList[0].NodeId, StartNodeId: flowItemInfo.NodeList[0].NodeId, ModifyTime: time.Now(), CreateTime: time.Now(), } has, err := approval.CheckPendingByContractId(sealInfo.SealId, "seal") if err != nil { return } if has { err = errors.New("用印异常,不允许提交审批,存在待审核的审批单") return } ApproveUserMap := make(map[int]approval_flow_node.User) for _, nodeItem := range flowItemInfo.NodeList { //审批流记录 if len(nodeItem.UserList) <= 0 { err = errors.New(fmt.Sprint("审批流程异常,没有可审批的人员,Err:", err)) return } for _, userItem := range nodeItem.UserList { contractApprovalRecord := &contract_approval_record.ContractApprovalRecord{ Status: "待审批", ApproveRemark: "", ApproveUserId: userItem.AdminId, ApproveUserName: userItem.Name, ApproveRoleTypeCode: userItem.RoleTypeCode, NodeId: nodeItem.NodeId, NodeType: nodeItem.NodeType, NextNodeId: nodeItem.NextNodeId, PrevNodeId: nodeItem.PrevNodeId, CreateTime: time.Now(), ModifyTime: time.Now(), } contractApprovalRecordList = append(contractApprovalRecordList, contractApprovalRecord) ApproveUserMap[userItem.AdminId] = userItem } } err = approval.Apply(approval, contractApprovalRecordList) if err != nil { return } return } //撤销合同 func CancelApply(sealInfo *seal.Seal, opUserId int, opUserName string) (err error) { if sealInfo == nil { err = errors.New("审批流异常,没有审批流信息") return } //用印状态判断 if sealInfo.Status != "待审批" { err = errors.New("用印状态异常,不允许撤回申请,当前用印状态:" + sealInfo.Status) return } //校验操作人与合同申请人是否同一人 if sealInfo.UserId != opUserId { err = errors.New("操作人异常,不允许撤回非本人提交的申请:" + strconv.Itoa(sealInfo.UserId)) return } //获取最近一次待审批的审批单信息 approvalInfo, err := contract_approval.GetLastContractApprovalByContractId(sealInfo.SealId, "seal") if err != nil { return } if approvalInfo.Status != "待审批" { err = errors.New("审批单状态异常,审批单状态:" + sealInfo.Status) return } //获取当前审批单的审批流信息 approvalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractApprovalIdAndNodeId(approvalInfo.ContractApprovalId, approvalInfo.CurrNodeId) if err != nil { err = errors.New("获取审批失败,Err:" + err.Error()) return } if len(approvalRecordList) <= 0 { err = errors.New("找不到对应的审批流") } for _, tmpApprovalRecord := range approvalRecordList { //判断当前节点是否存在上级节点,如果存在,那么说明 if tmpApprovalRecord.PrevNodeId > 0 { err = errors.New("合同已存在审批操作,不允许撤回申请") return } } //获取当前审批单中所有待审批的流程流 contractApprovalRecordList, err := contract_approval_record.GetPendingContractApprovalRecordListByContractId(approvalInfo.ContractApprovalId, approvalInfo.CurrNodeId) if err != nil { return } //撤回审批流 err = approvalInfo.Cancel(approvalInfo, contractApprovalRecordList) if err != nil { return } // 操作日志 err = seal.AddSealOperationRecord(sealInfo.SealId, opUserId, 0, "cancel", opUserName, "撤回用印申请", "") if err != nil { err = errors.New("新增用印日志失败") return } ////添加操作日志 //remark := "撤回申请" //_ = contract_operation_record.AddContractOperationRecord(contractInfo.ContractId, contractInfo.SellerId, contractApprovalRecord.ContractApprovalRecordId, "cancel_apply", contractInfo.SellerName, remark) //作废原有消息 for _, contractApprovalRecord := range contractApprovalRecordList { go company_approval_message.CancelCompanyApprovalMessage(contractApprovalRecord.ContractApprovalRecordId, 2) } return } //校验审批操作权限 func CheckApproveAuth(sealId int, opUser *custom.AdminWx) (sealInfo *seal.Seal, approvalInfo *contract_approval.ContractApproval, approvalRecord *contract_approval_record.ContractApprovalRecord, err error) { //用印详情 sealInfo, err = seal.GetSealInfoById(sealId) if err != nil { err = errors.New("获取用印失败,Err:" + err.Error()) return } //合同状态判断 if sealInfo.Status != "待审批" { err = errors.New("用印状态异常,不允许驳回申请,当前合同状态:" + sealInfo.Status) return } //获取该最近一条审批单详情 approvalInfo, err = contract_approval.GetLastContractApprovalByContractId(sealInfo.SealId, "seal") if err != nil { err = errors.New("获取审批单失败,Err:" + err.Error()) return } if approvalInfo.Status != "待审批" { err = errors.New("当前审批单状态异常,不允许审批,审批单状态:" + approvalInfo.Status) return } //审批流 approvalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractApprovalIdAndNodeId(approvalInfo.ContractApprovalId, approvalInfo.CurrNodeId) if err != nil { err = errors.New("获取审批失败,Err:" + err.Error()) return } for _, tmpApprovalRecord := range approvalRecordList { if tmpApprovalRecord.NodeType == "check" && tmpApprovalRecord.ApproveUserId == opUser.AdminId { approvalRecord = tmpApprovalRecord } } if approvalRecord == nil { err = errors.New("当前账号没有审批权限") return } return } //驳回审批 func Reject(sealInfo *seal.Seal, approvalInfo *contract_approval.ContractApproval, approvalRecord *contract_approval_record.ContractApprovalRecord, opUser *custom.AdminWx, rejectRemark string) (err error) { err = reject(sealInfo, approvalInfo, approvalRecord, opUser, rejectRemark) if err != nil { return } //添加操作日志 //remark := "驳回申请" //_ = contract_operation_record.AddContractOperationRecord(contractInfo.ContractId, opUserId, approvalRecord.ContractApprovalRecordId, "reject", opUserName, remark) //待办通知 { content := sealInfo.CompanyName + " 用印已驳回" approvalSysUser, _ := admin.GetAdminById(sealInfo.UserId) go services.AddCompanyApprovalMessage(opUser.AdminId, sealInfo.UserId, 0, approvalRecord.ContractApprovalRecordId, 2, sourceType, 3, sealInfo.CompanyName, content, content, approvalSysUser.Mobile, "") //发送消息给所有的抄送人 //go messageToAllCcUser(opUserId, approvalRecord.ContractApprovalId, 2, 3, sealInfo.CompanyName, content) } return } //审批人修改后驳回 func CheckReject(sealInfo *seal.Seal, approvalInfo *contract_approval.ContractApproval, approvalRecord *contract_approval_record.ContractApprovalRecord, opUser *custom.AdminWx, rejectRemark string) (err error) { err = reject(sealInfo, approvalInfo, approvalRecord, opUser, rejectRemark) if err != nil { return } return } //实际驳回 func reject(sealInfo *seal.Seal, approvalInfo *contract_approval.ContractApproval, approvalRecord *contract_approval_record.ContractApprovalRecord, opUser *custom.AdminWx, rejectRemark string) (err error) { if approvalRecord == nil { err = errors.New("审批流异常,没有审批流信息") return } //审批流状态判断 if approvalRecord.Status != "待审批" { err = errors.New("审批流状态异常,不允许驳回申请,当前审批流状态:" + approvalRecord.Status) return } //判断是否审批类型,如果不是审批类型,那么就没有审批权限 if approvalRecord.NodeType != "check" { err = errors.New("当前账号没有审批权限") return } //操作人 opUserId := opUser.AdminId opUserName := opUser.RealName //操作权限校验 if opUser.RoleTypeCode != approvalRecord.ApproveRoleTypeCode { err = errors.New("当前账号没有审批权限") return } if approvalRecord.ApproveUserId > 0 && approvalRecord.ApproveUserId != opUserId { err = errors.New("当前账号没有审批权限,需要指定人操作") return } //审批单状态判断 if approvalInfo.Status != "待审批" { err = errors.New("审批单状态异常,不允许驳回申请,当前审批单状态:" + approvalInfo.Status) return } //用印状态判断 if sealInfo.Status != "待审批" { err = errors.New("用印状态异常,不允许驳回申请,当前用印状态:" + sealInfo.Status) return } err = approvalInfo.Reject(approvalInfo, approvalRecord, opUserId, opUserName, rejectRemark) if err != nil { return } // 操作日志 err = seal.AddSealOperationRecord(sealInfo.SealId, opUser.AdminId, approvalRecord.ContractApprovalRecordId, "reject", opUser.RealName, "驳回审批", rejectRemark) if err != nil { err = errors.New("新增用印日志失败") return } return } //审批通过 func Approved(sealInfo *seal.Seal, approvalInfo *contract_approval.ContractApproval, approvalRecord *contract_approval_record.ContractApprovalRecord, opUser *custom.AdminWx, approvedRemark string) (err error) { if approvalRecord == nil { err = errors.New("审批流异常,没有审批流信息") return } //审批流状态判断 if approvalRecord.Status != "待审批" { err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + approvalRecord.Status) return } //判断是否审批类型,如果不是审批类型,那么就没有审批权限 if approvalRecord.NodeType != "check" { err = errors.New("当前账号没有审批权限") return } //操作人 opUserId := opUser.AdminId //opUserName := opUser.RealName //操作权限校验 if opUser.RoleTypeCode != approvalRecord.ApproveRoleTypeCode { err = errors.New("当前账号没有审批权限") return } if approvalRecord.ApproveUserId > 0 && approvalRecord.ApproveUserId != opUserId { err = errors.New("当前账号没有审批权限,需要指定人操作") return } //审批单状态判断 if approvalInfo.Status != "待审批" { err = errors.New("审批单状态异常,不允许审批,当前审批单状态:" + approvalInfo.Status) return } //用印状态判断 if sealInfo.Status != "待审批" { err = errors.New("用印状态异常,不允许审批,当前用印状态:" + sealInfo.Status) return } err = approvalInfo.Approved(approvalInfo, approvalRecord, approvedRemark) if err != nil { return } // 操作日志 err = seal.AddSealOperationRecord(sealInfo.SealId, opUser.AdminId, approvalRecord.ContractApprovalRecordId, "approved", opUser.RealName, "通过审批", "") if err != nil { err = errors.New("新增用印日志失败") return } //添加操作日志 //remark := "审批通过" //_ = contract_operation_record.AddContractOperationRecord(contractInfo.ContractId, opUserId, approvalRecord.ContractApprovalRecordId, "approval", opUserName, remark) //发送信息给所有抄送人 content := sealInfo.CompanyName + " 用印已审核" //go messageToAllCcUser(opUserId, approvalRecord.ContractApprovalId, 2, 2, sealInfo.CompanyName, content) //如果下一个节点属于结束节点,那么通知对应的销售 if approvalRecord.NextNodeId == 0 { //待办通知(通知销售已经审核通过了) { content := sealInfo.CompanyName + " 用印已审核" approvalSysUser, _ := admin.GetAdminById(sealInfo.UserId) go services.AddCompanyApprovalMessage(opUserId, sealInfo.UserId, 0, approvalRecord.ContractApprovalRecordId, 2, sourceType, 2, sealInfo.CompanyName, content, content, approvalSysUser.Mobile, "") } } else { //获取下级节点信息 flowNodeInfo, tmpErr := approval_flow_node.GetByNodeId(approvalRecord.NextNodeId) if tmpErr != nil { err = tmpErr return } //如果该级节点是抄送类型,那么需要将该节点给处理掉 if flowNodeInfo.NodeType == "cc" { go approvedByCc(approvedRemark, approvalRecord) } else { //发送消息下级审批人 go messageToNodeUser(approvalRecord.NextNodeId, sealInfo.UserId, approvalInfo.ContractApprovalId, 1, 1, sealInfo.CompanyName, content) } } return } //审批通过(抄送节点) func approvedByCc(approvedRemark string, sourceApprovalRecord *contract_approval_record.ContractApprovalRecord) (err error) { //下个流程节点id nextNodeId := 0 //获取审批单中抄送节点的所有数据列表 approvalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractIdAndNode(sourceApprovalRecord.ContractApprovalId, sourceApprovalRecord.NextNodeId) if err != nil { return } //遍历所有的抄送单 for _, approvalRecord := range approvalRecordList { nextNodeId = approvalRecord.NextNodeId //审批流状态判断 if approvalRecord.Status != "待审批" { err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + approvalRecord.Status) return } //判断是否审批类型,如果不是审批类型,那么就没有审批权限 if approvalRecord.NodeType != "cc" { err = errors.New("当前账号不是抄送权限") return } } //获取审批单详情 approvalInfo, err := contract_approval.GetContractApprovalById(sourceApprovalRecord.ContractApprovalId) if err != nil { return } //获取用印信息 sealInfo, err := seal.GetSealInfoById(approvalInfo.ContractId) if err != nil { return } //if contractApprovalRecord err = approvalInfo.ApprovedByCc(approvalInfo, approvalRecordList, approvedRemark, nextNodeId) if err != nil { return } content := sealInfo.CompanyName + " 用印已审核" //发送消息给抄送人 go messageToNodeUser(approvalRecordList[0].NodeId, sealInfo.UserId, approvalInfo.ContractApprovalId, 2, 2, sealInfo.CompanyName, content) //如果下一个节点属于结束节点,那么通知对应的销售 if nextNodeId == 0 { //待办通知 { content = sealInfo.CompanyName + " 用印已审核" approvalSysUser, _ := admin.GetAdminById(sealInfo.UserId) go services.AddCompanyApprovalMessage(sourceApprovalRecord.ApproveUserId, sealInfo.UserId, 0, sourceApprovalRecord.ContractApprovalRecordId, 2, sourceType, 2, sealInfo.CompanyName, content, content, approvalSysUser.Mobile, "") } } else { //获取下级节点信息 flowNodeInfo, tmpErr := approval_flow_node.GetByNodeId(nextNodeId) if tmpErr != nil { err = tmpErr return } //如果下级节点是抄送类型,那么还是需要处理抄送节点逻辑 if flowNodeInfo.NodeType == "cc" { go approvedByCc(approvedRemark, sourceApprovalRecord) } else { //如果下级级节点是审批类型 //发送消息下级审批人 go messageToNodeUser(nextNodeId, sealInfo.UserId, approvalInfo.ContractApprovalId, 1, 1, sealInfo.CompanyName, approvalInfo.ApplyContent) } } return } //抄送信息给用户(根据节点来推送) func messageToNodeUser(flowNodeId, createUserId, contractApprovalId, messageType, approvalStatus int, companyName, content string) (err error) { defer func() { if err != nil { //go utils.SendEmail("抄送信息给用户失败"+time.Now().Format("2006-01-02 15:04:05"), ";Err:"+err.Error(), utils.EmailSendToUsers) go alarm_msg.SendAlarmMsg("抄送信息给用户失败:"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+err.Error(), 3) utils.FileLog.Info("抄送信息给用户失败,Err:%s,%s", err.Error()) } }() //获取审批单中抄送节点的所有数据列表 contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractIdAndNode(contractApprovalId, flowNodeId) if err != nil { return } err = messageToUser(contractApprovalRecordList, createUserId, messageType, approvalStatus, companyName, content) return } //发送信息给所有抄送人 // @param createUserId 消息发起人 // @param contractApprovalId 审批单id // @param messageType 消息类型:1:申请消息,2:审批结果 // @param approvalStatus 审批状态:1:待审批,2:已审批,3:已驳回" // @param companyName 企业客户名称 // @param content 消息内容 func messageToAllCcUser(createUserId, contractApprovalId, messageType, approvalStatus int, companyName, content string) (err error) { //获取审批单中抄送节点的所有数据列表 contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractApprovalId(contractApprovalId) if err != nil { return } err = messageToUser(contractApprovalRecordList, createUserId, messageType, approvalStatus, companyName, content) return } //抄送信息给用户(发送给所有抄送人) // @param createUserId 待审批、抄送记录 // @param createUserId 消息发起人 // @param contractApprovalId 审批单id // @param messageType 消息类型:1:申请消息,2:审批结果 // @param approvalStatus 审批状态:1:待审批,2:已审批,3:已驳回" // @param companyName 企业客户名称 // @param content 消息内容 func messageToUser(contractApprovalRecordList []*contract_approval_record.ContractApprovalRecord, createUserId, messageType, approvalStatus int, companyName, content string) (err error) { for _, contractApprovalRecord := range contractApprovalRecordList { sysUserMobile := "" sysUser, _ := admin.GetAdminById(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 }