123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- package seal
- import (
- "encoding/json"
- "errors"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "hongze/hz_crm_api/models/contract"
- "hongze/hz_crm_api/models/seal"
- "hongze/hz_crm_api/models/system"
- "hongze/hz_crm_api/services"
- "hongze/hz_crm_api/services/flow"
- "strings"
- "time"
- )
- func SubmitApproval(sealInfo *seal.Seal) (err error) {
-
- approval, approvalRecordList, err := addApproval(sealInfo)
- if err != nil {
- return
- }
-
- {
- go MessageToNodeUser(approvalRecordList[0].NodeId, approval.ApplyUserId, approval.ContractApprovalId, 1, 1, sealInfo.CompanyName, sealInfo.Use)
- }
- return
- }
- func GetFlowTypeBySealType(sealType string) (flowType int) {
- flowType = 5
- longFlow := "公章,法人章"
- sealTypeArr := strings.Split(sealType, ",")
- for _, itemType := range sealTypeArr {
- if strings.Contains(longFlow, itemType) {
- flowType = 6
- }
- }
- return
- }
- func addApproval(sealInfo *seal.Seal) (approval *contract.ContractApproval, contractApprovalRecordList []*contract.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.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
- }
-
- 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.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)
-
- }
- }
- err = approval.Apply2(approval, contractApprovalRecordList)
- if err != nil {
- return
- }
- return
- }
- func CheckApprovalAuth(sealId int, sysUser *system.Admin) (sealInfo *seal.Seal, approvalInfo *contract.ContractApproval, approvalRecord *contract.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.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.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 == sysUser.AdminId {
- approvalRecord = tmpApprovalRecord
- }
- }
- if approvalRecord == nil {
- err = errors.New("当前账号没有审批权限")
- return
- }
- return
- }
- func ApprovedApproval(sealId int, sysUser *system.Admin, remark string) (err error) {
-
- sealInfo, approvalInfo, approvalRecord, err := CheckApprovalAuth(sealId, sysUser)
- if err != nil {
- return
- }
- if approvalRecord == nil {
- err = errors.New("审批流异常,没有审批流信息")
- return
- }
-
- if sealInfo.Status != "待审批" {
- err = errors.New("用印状态异常,不允许审批,当前用印状态:" + sealInfo.Status)
- return
- }
-
- if approvalInfo.Status != "待审批" {
- err = errors.New("审批单状态异常,不允许审批,当前审批单状态:" + approvalInfo.Status)
- return
- }
-
- if approvalRecord.Status != "待审批" {
- err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + approvalRecord.Status)
- return
- }
-
- if approvalRecord.NodeType != "check" {
- err = errors.New("当前账号没有审批权限")
- return
- }
-
- if sysUser.RoleTypeCode != approvalRecord.ApproveRoleTypeCode {
- err = errors.New("当前账号没有审批权限")
- return
- }
- if approvalRecord.ApproveUserId > 0 && approvalRecord.ApproveUserId != sysUser.AdminId {
- err = errors.New("当前账号没有审批权限,需要指定人操作")
- return
- }
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
-
- err = approvalInfo.Approved(approvalInfo, approvalRecord, remark)
- if err != nil {
- return
- }
-
- err = seal.AddSealOperationRecord(sealInfo.SealId, sysUser.AdminId, approvalRecord.ContractApprovalRecordId, "approval", sysUser.RealName, "通过申请", "")
- if err != nil {
- err = errors.New("添加操作日志失败")
- return
- }
-
- content := sealInfo.CompanyName + " 用印已审核"
-
- if approvalRecord.NextNodeId == 0 {
-
- {
- approvalSysUser, _ := system.GetSysAdminById(sealInfo.UserId)
- go services.AddCompanyApprovalMessage(sysUser.AdminId, sealInfo.UserId, 0, approvalRecord.ContractApprovalRecordId, 2, sourceType, 2, sealInfo.CompanyName, content, content, approvalSysUser.Mobile)
- }
- } else {
-
- flowNodeInfo, tmpErr := system.GetByNodeId(approvalRecord.NextNodeId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
-
- if flowNodeInfo.NodeType == "cc" {
- go approvedByCc(remark, approvalRecord, sysUser)
- } else {
-
- go MessageToNodeUser(approvalRecord.NextNodeId, sealInfo.UserId, approvalInfo.ContractApprovalId, 1, 1, sealInfo.CompanyName, content)
- }
- }
- return
- }
- func RejectApproval(sealId int, sysUser *system.Admin, remark string) (err error) {
-
- sealInfo, approvalInfo, approvalRecord, err := CheckApprovalAuth(sealId, sysUser)
- if err != nil {
- return
- }
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
-
- err = reject(sealInfo, approvalInfo, approvalRecord, sysUser, remark)
- if err != nil {
- return
- }
-
- {
- content := sealInfo.CompanyName + " 用印已驳回"
- approvalSysUser, _ := system.GetSysAdminById(sealInfo.UserId)
- go services.AddCompanyApprovalMessage(sysUser.AdminId, sealInfo.UserId, 0, approvalRecord.ContractApprovalRecordId, 2, sourceType, 3, sealInfo.CompanyName, content, content, approvalSysUser.Mobile)
- }
- return
- }
- func reject(sealInfo *seal.Seal, approvalInfo *contract.ContractApproval, approvalRecord *contract.ContractApprovalRecord, opUser *system.Admin, 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, opUserId, approvalRecord.ContractApprovalRecordId, "reject", opUserName, "驳回申请", rejectRemark)
- if err != nil {
- err = errors.New("添加操作日志失败")
- return
- }
- return
- }
- func approvedByCc(approvedRemark string, sourceApprovalRecord *contract.ContractApprovalRecord, sysUser *system.Admin) (err error) {
-
- nextNodeId := 0
-
- approvalRecordList, err := contract.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.GetContractApprovalById(sourceApprovalRecord.ContractApprovalId)
- if err != nil {
- return
- }
-
- sealInfo, err := seal.GetSealInfoById(approvalInfo.ContractId)
- if err != nil {
- return
- }
-
- 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, _ := system.GetSysAdminById(sealInfo.UserId)
- go services.AddCompanyApprovalMessage(sourceApprovalRecord.ApproveUserId, sealInfo.UserId, 0, sourceApprovalRecord.ContractApprovalRecordId, 2, sourceType, 2, sealInfo.CompanyName, content, content, approvalSysUser.Mobile)
- }
- } else {
-
- flowNodeInfo, tmpErr := system.GetByNodeId(nextNodeId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
-
- if flowNodeInfo.NodeType == "cc" {
- go approvedByCc(approvedRemark, sourceApprovalRecord, sysUser)
- } else {
-
- go MessageToNodeUser(nextNodeId, sealInfo.UserId, approvalInfo.ContractApprovalId, 1, 1, sealInfo.CompanyName, approvalInfo.ApplyContent)
- }
- }
- return
- }
|