133 lines
3.6 KiB
Go
133 lines
3.6 KiB
Go
package controllers
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"server/models"
|
|
"server/pkg/jwtutil"
|
|
"server/services"
|
|
approvalEngine "server/services/approval"
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
)
|
|
|
|
func isReimburseBizType(bizType string) bool {
|
|
return approvalEngine.IsReimburseBizType(bizType)
|
|
}
|
|
|
|
func claimsReimburseUIDs(claims *jwtutil.Claims) []interface{} {
|
|
raw := uint64(claims.UserID)
|
|
out := []interface{}{raw}
|
|
if uid := services.ResolveTenantUID(uint64(claims.TenantId), raw); uid != raw {
|
|
out = append(out, uid)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func reimburseMyTaskCore(claims *jwtutil.Claims, id uint64) (*models.ApprovalTask, error) {
|
|
tid := uint64(claims.TenantId)
|
|
var task models.ApprovalTask
|
|
err := models.Orm.QueryTable(new(models.ApprovalTask)).
|
|
Filter("scope", models.ApprovalScopeTenant).
|
|
Filter("tid", tid).
|
|
Filter("biz_type", models.ApprovalBizReimburse).
|
|
Filter("biz_id", id).
|
|
Filter("status", models.ApprovalTaskPending).
|
|
Filter("is_deleted", 0).
|
|
Filter("approver_id__in", claimsReimburseUIDs(claims)...).
|
|
OrderBy("-id").One(&task)
|
|
if err == orm.ErrNoRows {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &task, nil
|
|
}
|
|
|
|
func reimburseActCore(claims *jwtutil.Claims, id uint64, pass bool, comment string) (int, string, string) {
|
|
if _, err := findOwnedReimbursement(claims, id); err != nil {
|
|
return 404, "报销单不存在", ""
|
|
}
|
|
task, err := reimburseMyTaskCore(claims, id)
|
|
if err != nil {
|
|
return 500, "查询审批任务失败", ""
|
|
}
|
|
if task == nil {
|
|
return 400, "当前没有待您审批的节点", ""
|
|
}
|
|
|
|
result, err := approvalEngine.Approve(task.ID, approvalEngine.ApproveInput{
|
|
ApproverID: task.ApproverID,
|
|
Pass: pass,
|
|
Comment: comment,
|
|
})
|
|
if err != nil {
|
|
return 400, err.Error(), ""
|
|
}
|
|
|
|
applyReimburseApprovalResult(uint64(claims.TenantId), id,
|
|
uint64(claims.UserID), reimburseActorName(uint64(claims.TenantId), claims), task.Sort,
|
|
pass, comment, result.Finished, result.InstanceStat)
|
|
|
|
hint := result.Message
|
|
if hint == "" {
|
|
if pass {
|
|
hint = "审批通过"
|
|
} else {
|
|
hint = "已驳回"
|
|
}
|
|
}
|
|
return 200, "", hint
|
|
}
|
|
|
|
func applyReimburseApprovalResult(tid, bizID, actorID uint64, actorName string, step int,
|
|
pass bool, comment string, finished bool, instStatus int8) {
|
|
if tid == 0 || bizID == 0 {
|
|
return
|
|
}
|
|
action := int8(models.ApprovalActionApprove)
|
|
if !pass {
|
|
action = models.ApprovalActionReject
|
|
}
|
|
|
|
var row models.BackendReimbursement
|
|
if err := models.Orm.QueryTable(new(models.BackendReimbursement)).
|
|
Filter("id", bizID).Filter("tid", tid).Filter("is_deleted", 0).One(&row); err != nil {
|
|
return
|
|
}
|
|
|
|
var bizStatus int8
|
|
if finished {
|
|
switch instStatus {
|
|
case models.ApprovalInstanceApproved:
|
|
bizStatus = 2
|
|
case models.ApprovalInstanceRejected:
|
|
bizStatus = 3
|
|
}
|
|
if bizStatus != 0 && row.Status != bizStatus {
|
|
_, _ = models.Orm.QueryTable(new(models.BackendReimbursement)).
|
|
Filter("id", bizID).Filter("tid", tid).
|
|
Update(orm.Params{"status": bizStatus, "current_approver_id": 0, "update_time": time.Now()})
|
|
}
|
|
}
|
|
|
|
dup := models.Orm.QueryTable(new(models.BackendApprovalRecord)).
|
|
Filter("tid", tid).Filter("reimbursement_id", bizID).
|
|
Filter("approver_id", actorID).Filter("action", action).
|
|
Filter("step", int8(step)).Filter("is_deleted", 0).Exist()
|
|
if dup {
|
|
return
|
|
}
|
|
var commentPtr *string
|
|
if comment != "" {
|
|
commentPtr = &comment
|
|
}
|
|
_, _ = models.Orm.Insert(&models.BackendApprovalRecord{
|
|
Tid: tid, UID: uint64Ptr(actorID), ReimbursementID: bizID,
|
|
ApproverID: actorID, ApproverName: fmt.Sprintf("%s", actorName), Action: action,
|
|
Comment: commentPtr, Step: int8(step), PrevStep: int8(step),
|
|
})
|
|
}
|