package seal import ( "errors" "fmt" "github.com/beego/beego/v2/client/orm" "hongze/hongze_mobile_admin/models/custom" sealResp "hongze/hongze_mobile_admin/models/response/seal" "hongze/hongze_mobile_admin/models/tables/admin" "hongze/hongze_mobile_admin/models/tables/contract" "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" contractService "hongze/hongze_mobile_admin/services/contract" "hongze/hongze_mobile_admin/utils" "mime/multipart" "reflect" "strings" "time" ) // AddSeal 添加用印 func AddSeal(userId, contractId, fileNum int, userName, use, useCompanyName, companyName, creditCode, serviceType, sealType, remark string, fileUrls []string) (sealInfo *seal.Seal, err error) { o := orm.NewOrm() to, err := o.Begin() if err != nil { return } defer func() { if err != nil { _ = to.Rollback() } else { _ = to.Commit() } }() if !strings.Contains(strings.Join(seal.EnumUse, ","), use) { err = errors.New("用印用途异常") return } if !strings.Contains(strings.Join(seal.EnumServiceType, ","), serviceType) { err = errors.New("业务类型异常") return } sealCode, err := seal.GetSealCode() if err != nil { return } fileUrl := "" now := time.Now() attachments := make([]*seal.Attachment,0) if len(fileUrls) == 1 { fileUrl = fileUrls[0] } sealInfo = &seal.Seal{ Code: sealCode, UserId: userId, UserName: userName, Use: use, CompanyName: companyName, UseCompanyName: useCompanyName, CreditCode: creditCode, ServiceType: serviceType, SealType: sealType, Status: "待提交", Remark: remark, FileUrl: fileUrl, FileNum: fileNum, ContractId: contractId, ModifyTime: now, CreateTime: now, } err = seal.AddSeal(sealInfo) if err != nil { err = errors.New("新增用印失败") return } if len(fileUrls) > 1 { for _, v := range fileUrls { tmp := &seal.Attachment{ SealId: sealInfo.SealId, FileUrl: v, ModifyTime: now, CreateTime: now, } attachments = append(attachments, tmp) } err = seal.AddAttachments(attachments) if err != nil { err = errors.New("添加用印附件失败") return } } // 操作日志 err = seal.AddSealOperationRecord(sealInfo.SealId, userId, 0, "apply", userName, "提交审批", "") if err != nil { err = errors.New("新增用印日志失败") return } return } // Edit 修改用印 func Edit(sealId, userId, contractId, fileNum int, use, companyName, userCompanyName, creditCode, serviceType, sealType, remark string, fileUrls []string, userName string) (sealInfo *seal.Seal, err error) { o := orm.NewOrm() to, err := o.Begin() if err != nil { return } defer func() { if err != nil { _ = to.Rollback() } else { _ = to.Commit() } }() if !strings.Contains(strings.Join(seal.EnumUse, ","), use) { err = errors.New("用印用途异常") return } if !strings.Contains(strings.Join(seal.EnumServiceType, ","), serviceType) { err = errors.New("业务类型异常") return } //查询当前合同信息 sealInfo, err = seal.GetSealInfoById(sealId) if err != nil { if err.Error() == utils.ErrNoRow() { err = errors.New(fmt.Sprint("根据用印编号:", sealId, " 找不到该用印")) } return } if sealInfo.UserId != userId { err = errors.New("当前账号无操作权限") return } ignoreStatus := []string{"待提交", "已撤回", "已驳回"} if !strings.Contains(strings.Join(ignoreStatus, ","), sealInfo.Status) { err = errors.New("用印状态异常,不允许修改,当前用印状态:" + sealInfo.Status) return } //sealId,userId int, userName, use, companyName, creditCode, serviceType, sealType, remark, fileUrl string fileUrl := "" now := time.Now() attachments := make([]*seal.Attachment,0) if len(fileUrls) == 1 { fileUrl = fileUrls[0] } sealInfo.Use = use sealInfo.CompanyName = companyName sealInfo.UseCompanyName = userCompanyName sealInfo.CreditCode = creditCode sealInfo.ServiceType = serviceType sealInfo.SealType = sealType sealInfo.Remark = remark sealInfo.FileUrl = fileUrl sealInfo.FileNum = fileNum sealInfo.ContractId = contractId sealInfo.CreateTime = now // 重提更新提交时间 sealInfo.ModifyTime = now sealInfo.Status = "待提交" //用印状态 err = sealInfo.Update([]string{"Use", "CompanyName", "UseCompanyName", "CreditCode", "ServiceType", "SealType", "Remark", "FileUrl", "FileNum", "ContractId", "CreateTime", "ModifyTime", "Status"}) if err != nil { err = errors.New("新增用印日志失败") return } if len(fileUrls) > 1 { for _, v := range fileUrls { tmp := &seal.Attachment{ SealId: sealInfo.SealId, FileUrl: v, ModifyTime: now, CreateTime: now, } attachments = append(attachments, tmp) } err = seal.AddAttachments(attachments) if err != nil { err = errors.New("添加用印附件失败") return } } // 操作日志 err = seal.AddSealOperationRecord(sealInfo.SealId, userId, 0, "edit", userName, "重提审批", "") if err != nil { err = errors.New("编辑用印日志失败") return } return } // CheckEdit 审批者修改用印 func CheckEdit(sealInfo *seal.Seal, approvalInfo *contract_approval.ContractApproval, approvalRecord *contract_approval_record.ContractApprovalRecord, fileNum int, fileUrls []string, use, sealType, remark string, opUser *custom.AdminWx) (err error) { if !strings.Contains(strings.Join(seal.EnumUse, ","), use) { err = errors.New("用印用途异常") return } //判断是否是合规账号 if opUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE { err = errors.New("当前账号无修改权限") return } //用印状态判断 if sealInfo.Status != "待审批" { err = errors.New("用印状态异常,不允许修改,当前用印状态:" + sealInfo.Status) return } //变更字段判断 updateCol := make([]string, 0) updateContent := make([]string, 0) if sealInfo.Use != use { updateCol = append(updateCol, "Use") updateContent = append(updateContent, "用印用途") } if sealInfo.SealType != sealType { updateCol = append(updateCol, "SealType") updateContent = append(updateContent, "加盖何种印章") } if sealInfo.Remark != remark { updateCol = append(updateCol, "Remark") updateContent = append(updateContent, "备注") } if sealInfo.FileNum != fileNum { updateCol = append(updateCol, "FileNum") updateContent = append(updateContent, "文件份数") } //判断附件地址是否相等: sealAttachements, err := seal.GetAttachmentBySealId(sealInfo.SealId) if err != nil { err = errors.New(fmt.Sprint("获取用印附件失败,Err:"+err.Error(), err)) return } oldFileUrls := make([]string, 0) if sealInfo.FileUrl != "" { oldFileUrls = append(oldFileUrls, sealInfo.FileUrl) } if len(sealAttachements) > 0 { for _, v := range sealAttachements{ oldFileUrls = append(oldFileUrls, v.FileUrl) } } attachmentFlag := false if !reflect.DeepEqual(oldFileUrls, fileUrls) { updateCol = append(updateCol, "FileUrl") updateContent = append(updateContent, "文件附件") attachmentFlag = true } fmt.Println(updateCol) if len(updateCol) <= 0 { err = errors.New("没有变更信息") return } content := "用印修改:" + opUser.RealName + "修改了" + strings.Join(updateContent, "、") // 通过盖章类型判断是否需要变更审批流程 originFlowType := GetFlowTypeBySealType(sealInfo.SealType) reqFlowType := GetFlowTypeBySealType(sealType) newFileUrl := "" if len(fileUrls) == 1 { newFileUrl = fileUrls[0] } //如果变更了盖章类型(涉及到审批流程变更),那么需要变更审批流 //变更审批流:先驳回用印申请,再修改用印,再发消息给申请人,再替申请人重新发起申请, //然后遍历审批流程,当前操作人节点之前的全部审核通过,最后通过当前操作人的节点(改成如果一级审批人如果是自己,那么直接通过这一级审批,否则不通过,让审批单重走流程) if originFlowType != reqFlowType { //先驳回 err = CheckReject(sealInfo, approvalInfo, approvalRecord, opUser, "") if err != nil { return } //修改用印申请,给申请人发消息 sealInfo.Use = use sealInfo.SealType = sealType sealInfo.Remark = remark sealInfo.FileNum = fileNum sealInfo.FileUrl = newFileUrl sealInfo.ModifyTime = time.Now() checkEdit(sealInfo, updateCol, approvalRecord.ContractApprovalRecordId, content, opUser, attachmentFlag, fileUrls) //重新获取最新的用印单 sealInfo, tmpErr := seal.GetSealInfoById(sealInfo.SealId) if tmpErr != nil { err = tmpErr return } //重新发起申请 err = CheckApply(sealInfo, opUser) if err != nil { return } } else { //修改用印基础信息 并 审批通过 //修改用印申请,给申请人发消息 sealInfo.Use = use sealInfo.SealType = sealType sealInfo.Remark = remark sealInfo.FileNum = fileNum sealInfo.FileUrl = newFileUrl sealInfo.ModifyTime = time.Now() checkEdit(sealInfo, updateCol, approvalRecord.ContractApprovalRecordId, content, opUser, attachmentFlag, fileUrls) //审核通过 err = Approved(sealInfo, approvalInfo, approvalRecord, opUser, "") } return } // checkEdit 审批人修改 func checkEdit(sealInfo *seal.Seal, updateCol []string, approvalRecordId int, content string, opUser *custom.AdminWx, attachmentFlag bool, fileUrls []string) { if attachmentFlag { // 同时更新用印申请和附件 _ = updateSealAndAttachment(sealInfo, updateCol, fileUrls) }else{ _ = sealInfo.Update(updateCol) } // 操作日志 _ = seal.AddSealOperationRecord(sealInfo.SealId, opUser.AdminId, approvalRecordId, "edit", opUser.RealName, "审批人修改信息", "") //给用印人,发送修改消息 sysUserMobile := "" sysUser, _ := admin.GetAdminById(opUser.AdminId) if sysUser != nil { sysUserMobile = sysUser.Mobile } go services.AddCompanyApprovalMessage(opUser.AdminId, sealInfo.UserId, 0, approvalRecordId, 3, sourceType, 2, sealInfo.CompanyName, content, content, sysUserMobile, "") return } // Invalid 作废用印 func Invalid(sealId int, opUser *custom.AdminWx, isInvalidContract bool) (err error) { //查询当前用印信息 sealInfo, err := seal.GetSealInfoById(sealId) if err != nil { if err.Error() == utils.ErrNoRow() { err = errors.New(fmt.Sprint("根据用印编号:", sealId, " 找不到初始用印")) } return } if sealInfo.UserId != opUser.AdminId && opUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE { err = errors.New("当前账号无操作权限") return } if sealInfo.Status != "已审批" { err = errors.New("用印状态异常,不允许作废,当前用印状态:" + sealInfo.Status) return } //如果删除状态 >0,那么代表已经被删除了 if sealInfo.IsDelete > 0 { err = errors.New("该用印已删除") return } //用印作废 err = seal.Invalid(sealInfo) if err != nil { return } // 操作日志 err = seal.AddSealOperationRecord(sealInfo.SealId, opUser.AdminId, 0, "invalid", opUser.RealName, "作废审批", "") if err != nil { err = errors.New("新增用印日志失败") return } //如果是系统合同,同时需要同步作废合同时,那么逻辑处理 if sealInfo.ContractId > 0 && isInvalidContract { err = contractService.InvalidContract(sealInfo.ContractId, opUser) if err != nil { utils.ApiLog.Println("作废用印时,同步作废合同失败,Err:", err.Error()) } } //获取最近一次提交的审批单信息 contractApproval, err := contract_approval.GetLastContractApprovalByContractId(sealInfo.SealId, "seal") if err != nil { err = nil return } //如果有提测信息,那么需要给对应的审批人发送消息 if contractApproval != nil { //作废后需要给审批者发送消息 contractApprovalRecordList, tmpErr := contract_approval_record.GetContractApprovalRecordListByContractApprovalId(contractApproval.ContractApprovalId) if tmpErr != nil { return } content := fmt.Sprint("作废", sealInfo.CompanyName, "用印申请") for _, contractApprovalRecord := range contractApprovalRecordList { if contractApprovalRecord.NodeType == "check" { //如果是审批人,那么需要发送消息给对方 //操作人信息 sysUserMobile := "" sysUser, _ := admin.GetAdminById(contractApprovalRecord.ApproveUserId) if sysUser != nil { sysUserMobile = sysUser.Mobile } go services.AddCompanyApprovalMessage(opUser.AdminId, contractApprovalRecord.ApproveUserId, 0, contractApprovalRecord.ContractApprovalRecordId, 3, sourceType, 2, sealInfo.CompanyName, content, content, sysUserMobile, "") } } } //添加操作日志 //remark := "作废合同模板" //_ = contract_operation_record.AddContractOperationRecord(nowContract.ContractId, opUser.AdminId, 0, "invalid", opUser.RealName, remark) return } // UploadCheckBackFileByFile 上传签回用印附件(实际操作) func UploadCheckBackFileByFile(sealId int, ext string, fileMulti multipart.File, opUser *custom.AdminWx) (sealInfo *seal.Seal, err error) { //查询当前用印信息 sealInfo, err = seal.GetSealInfoById(sealId) if err != nil { return } //用印状态判断 if sealInfo.Status != "已审批" && sealInfo.Status != "已签回" { err = errors.New("用印状态异常,不允许上传签回用印附件,当前用印状态:" + sealInfo.Status) return } fileName := `` //保存的文件名 fileName = sealInfo.CompanyName + "_" + sealInfo.Code + "(签回)" if sealInfo.ContractId > 0 { //获取合同信息 contractInfo, tmpErr := contract.GetContractById(sealInfo.ContractId) if tmpErr != nil { err = tmpErr return } //保存的文件名 fileName = contractInfo.CompanyName + "_" + contractInfo.ContractCode + "(签回)" } //非正式环境下,文件名上面还是加上随机数 if utils.RunMode != "release" { fileName += "_" + utils.GetRandStringNoSpecialChar(10) } fileName += ext //上传到阿里云 resourceUrl, err := services.UploadToOssAndFileName(fileMulti, fileName) if err != nil { err = errors.New("文件保存失败,Err:" + err.Error()) return } sealInfo.CheckBackFileUrl = resourceUrl sealInfo.Status = "已签回" sealInfo.ModifyTime = time.Now() sealInfo.CheckBackFileTime = time.Now() err = sealInfo.Update([]string{"CheckBackFileUrl", "Status", "ModifyTime", "CheckBackFileTime"}) if err != nil { return } // 发送模版消息 sellerInfo, tErr := admin.GetAdminById(sealInfo.UserId) if tErr == nil { if sellerInfo.Mobile != "" { go services.SendSealFinishedWxTemplateMsg(sellerInfo.Mobile, sealInfo.CompanyName, sealInfo.SealId) } } return } // UploadCheckBackFile 上传签回用印附件(实际操作) func UploadCheckBackFile(sealId int, fileUrl string, opUser *custom.AdminWx) (sealInfo *seal.Seal, err error) { //查询当前用印信息 sealInfo, err = seal.GetSealInfoByContractId(sealId) if err != nil { return } //用印状态判断 if sealInfo.Status != "已审批" && sealInfo.Status != "已签回" { err = errors.New("用印状态异常,不允许上传签回用印附件,当前用印状态:" + sealInfo.Status) return } sealInfo.CheckBackFileUrl = fileUrl sealInfo.Status = "已签回" sealInfo.ModifyTime = time.Now() sealInfo.CheckBackFileTime = time.Now() err = sealInfo.Update([]string{"CheckBackFileUrl", "Status", "ModifyTime", "CheckBackFileTime"}) if err != nil { return } // 发送模版消息 sellerInfo, tErr := admin.GetAdminById(sealInfo.UserId) if tErr == nil { if sellerInfo.Mobile != "" { go services.SendSealFinishedWxTemplateMsg(sellerInfo.Mobile, sealInfo.CompanyName, sealInfo.SealId) } } return } // GetOpButton 获取审批流和权限 func GetOpButton(sealInfo *seal.Seal, contractApprovalInfo *contract_approval.ContractApproval, opUser *custom.AdminWx) (sealOpButton sealResp.OpButton, flowNodeListResp [][]contract_approval_record.ContractApprovalRecord, err error) { //审批流 approvalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractApprovalId(contractApprovalInfo.ContractApprovalId) if err != nil { err = errors.New("获取审批失败,Err:" + err.Error()) return } //待审批状态下,如果是自己发起的审批单,同时已经经过了一轮审批,那么标记为处理中 if contractApprovalInfo.Status == "待审批" { if contractApprovalInfo.ApplyUserId == opUser.AdminId && contractApprovalInfo.CurrNodeId > contractApprovalInfo.StartNodeId { sealInfo.Status = "处理中" } } flowNodeMap := make(map[int][]contract_approval_record.ContractApprovalRecord, 0) keySort := make([]int, 0) for _, approvalRecord := range approvalRecordList { //如果当前节点正好是该节点,同时审批单状态是待审批状态,然后当前账号又有权限,该账号也正是审批人,那么允许审批操作 if contractApprovalInfo.CurrNodeId == approvalRecord.NodeId && contractApprovalInfo.Status == "待审批" { if opUser.AdminId == approvalRecord.ApproveUserId && approvalRecord.NodeType == "check" { sealOpButton.Approval = true } //合规在审批过程中具有 部分修改权限 if opUser.AdminId == approvalRecord.ApproveUserId && opUser.RoleTypeCode == utils.ROLE_TYPE_CODE_COMPLIANCE && approvalRecord.NodeType == "check" { sealOpButton.CheckEdit = true } } //如果当前节点正好是该节点,同时审批单状态又是第一层节点 if contractApprovalInfo.CurrNodeId == approvalRecord.NodeId && approvalRecord.PrevNodeId == 0 && contractApprovalInfo.Status == "待审批" { //发起人具有撤回审批单权限 if opUser.AdminId == sealInfo.UserId { sealOpButton.Cancel = true } } if tmpFlowNodeList, ok := flowNodeMap[approvalRecord.NodeId]; ok { flowNodeMap[approvalRecord.NodeId] = append(tmpFlowNodeList, *approvalRecord) } else { tmpFlowNodeList := make([]contract_approval_record.ContractApprovalRecord, 1) tmpFlowNodeList[0] = *approvalRecord flowNodeMap[approvalRecord.NodeId] = tmpFlowNodeList keySort = append(keySort, approvalRecord.NodeId) } //待审批状态下,如果当前审批节点就是操作人,审批节点超过当前审批节点,且节点类型是抄送人,,那么标记为处理中 if contractApprovalInfo.Status == "待审批" { if approvalRecord.ApproveUserId == opUser.AdminId && approvalRecord.NodeId < contractApprovalInfo.CurrNodeId && approvalRecord.NodeType != "cc" { sealInfo.Status = "处理中" } } } for _, key := range keySort { flowNodeListResp = append(flowNodeListResp, flowNodeMap[key]) } //作废权限 if opUser.AdminId == sealInfo.UserId && sealInfo.Status == "已审批" { sealOpButton.Invalid = true } //是否具有签回合同用印权限 uploadStatus := []string{"已审批", "已签回"} if opUser.RoleTypeCode == utils.ROLE_TYPE_CODE_COMPLIANCE && strings.Contains(strings.Join(uploadStatus, ","), sealInfo.Status) { sealOpButton.UploadFile = true } //编辑权限 editStatus := []string{"已驳回", "待提交", "已撤回"} if opUser.AdminId == sealInfo.UserId && strings.Contains(strings.Join(editStatus, ","), sealInfo.Status) { sealOpButton.Edit = true } return } //根据用印id获取用印详情 func GetSealDetailBySealId(sealId int, opUser *custom.AdminWx) (sealMore *sealResp.SealMoreResp, flowNodeListResp [][]contract_approval_record.ContractApprovalRecord, opButton sealResp.OpButton, err error) { sealInfo, err := seal.GetSealInfoById(sealId) if err != nil { err = errors.New("获取合同详情失败,ERR:" + err.Error()) return } sealAttachements, err := seal.GetAttachmentBySealId(sealId) if err != nil { err = errors.New(fmt.Sprint("获取用印附件失败,Err:"+err.Error(), err)) return } fileUrls := make([]string, 0) if sealInfo.FileUrl != "" { fileUrls = append(fileUrls, sealInfo.FileUrl) } for _, v := range sealAttachements{ fileUrls = append(fileUrls, v.FileUrl) } sealMore = &sealResp.SealMoreResp{ Seal: sealInfo, FileUrls: fileUrls, } //查询最近一次审批单信息 lastApprovalInfo, err := contract_approval.GetLastContractApprovalByContractId(sealInfo.SealId, "seal") if err != nil { if err.Error() != utils.ErrNoRow() { err = errors.New("获取最近一次审批单信息失败,ERR:" + err.Error()) } err = nil return } if lastApprovalInfo != nil { //获取审批操作和审批流数据 tmpSealOpButton, tmpFlowNodeListResp, tmpErr := GetOpButton(sealInfo, lastApprovalInfo, opUser) if tmpErr != nil { err = errors.New("获取用印按钮权限、审批流程失败,Err:" + tmpErr.Error()) return } opButton = tmpSealOpButton flowNodeListResp = tmpFlowNodeListResp } return } // updateSealAndAttachment 更新用印申请,并更新附件 func updateSealAndAttachment(sealInfo *seal.Seal, updateCol []string, fileUrls []string) (err error){ o := orm.NewOrm() to, err := o.Begin() if err != nil { return } defer func() { if err != nil { _ = to.Rollback() } else { _ = to.Commit() } }() err = sealInfo.Update(updateCol) if err != nil { return } now := time.Now() //删除原有的附件,新增最新的附件 err = seal.DelAttachmentBySealId(sealInfo.SealId) if err != nil { return } attachments := make([]*seal.Attachment, 0) if len(fileUrls) > 1 { for _, v := range fileUrls { tmp := &seal.Attachment{ SealId: sealInfo.SealId, FileUrl: v, ModifyTime: now, CreateTime: now, } attachments = append(attachments, tmp) } err = seal.AddAttachments(attachments) } return }