fonchain-approval-fork/pkg/service/approval.go
gy e77bd62ba8 feat 多级审批
feat 审批类型
2022-08-18 13:26:14 +08:00

375 lines
9.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"context"
"errors"
"github.com/fonchain_enterprise/fonchain-approval/api/approval"
"github.com/fonchain_enterprise/fonchain-approval/pkg/common/page"
"github.com/fonchain_enterprise/fonchain-approval/pkg/m"
"github.com/fonchain_enterprise/fonchain-approval/pkg/model"
"github.com/fonchain_enterprise/fonchain-approval/pkg/serializer"
"gorm.io/gorm"
)
type ApprovalProvider struct {
approval.UnimplementedApprovalServer
}
func (a *ApprovalProvider) Create(ctx context.Context, in *approval.CreateRequest) (*approval.ApprovalResponse, error) {
response := &approval.ApprovalResponse{}
entity, err := model.StoreApproval(in)
if err != nil {
return response, err
}
response.ID = entity.ID
response.Success = true
return response, err
}
func (a *ApprovalProvider) Update(ctx context.Context, in *approval.CreateRequest) (*approval.ApprovalResponse, error) {
response := &approval.ApprovalResponse{}
entity, err := model.UpdateApproval(in)
if err == nil {
response.ID = entity.ID
response.Success = true
}
return response, err
}
func (a *ApprovalProvider) NowLevelByInfo(ctx context.Context, in *approval.NowLevelByInfoRequest) (*approval.WorkFlowResponse, error) {
response := &approval.WorkFlowResponse{}
var approvalObj *model.Approval
if err := model.DB.First(&approvalObj, in.ApprovalID).Error; err != nil {
return response, err
}
workFlow, err1 := model.GetNowWorkFlowByApprovalId(approvalObj.ID)
if err1 != nil {
return response, err1
}
response.ID = workFlow.ID
response.UserID = workFlow.UserId
response.Status = uint64(workFlow.Status)
return response, nil
}
func (a *ApprovalProvider) SetStatus(ctx context.Context, in *approval.StatusRequest) (*approval.StatusResponse, error) {
response := &approval.StatusResponse{}
var approvalObj *model.Approval
var err error
if in.Status == model.StatusDoing {
return response, errors.New("status code wrong")
}
if err := model.DB.First(&approvalObj, in.ID).Error; err != nil {
return response, err
}
if approvalObj.Status != model.StatusDoing {
return response, errors.New(m.ErrorWrongStatus)
}
// 审批人审批内容
if in.Status == model.StatusFail {
err = approvalObj.Refuse(in.Reply, in.WorkFlowId)
} else {
err = approvalObj.Pass(in.Reply, in.WorkFlowId)
}
return response, err
}
func (a *ApprovalProvider) Remove(ctx context.Context, in *approval.RemoveRequest) (*approval.RemoveResponse, error) {
response := &approval.RemoveResponse{}
for _, ID := range in.IDs {
err := model.DB.Transaction(func(tx *gorm.DB) error {
var err error
var entity *model.Approval
// 确定有数据则执行删除
if err = model.DB.Where(&model.Approval{Domain: &in.Domain}).
Find(&entity, ID).Error; err != nil {
return err
}
//删除子关联数据
if err = entity.DeleteContent(); err != nil {
return err
}
if err = model.DB.Where(&model.Approval{Domain: &in.Domain}).
Delete(&model.Approval{}, ID).Error; err != nil {
return err
}
//删除流
if err = model.DB.Where(&model.ApprovalWorkFlow{ApprovalID: ID}).
Delete(&model.ApprovalWorkFlow{}).Error; err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
}
response.Success = true
return response, nil
}
func (a *ApprovalProvider) Detail(ctx context.Context, in *approval.DetailRequest) (*approval.CreateRequest, error) {
response := &approval.CreateRequest{}
var err error
var entity *model.Approval
if err := model.DB.
Preload("Show").
Preload("Work.ApprovalWorks").
Preload("Bundle").
Preload("Exhibition.ApprovalExhibitions").
Preload("ApprovalWorkFlows").
First(&entity, in.ID).Error; err != nil {
return response, err
}
response = serializer.BuildApproval(entity, in.UserId)
return response, err
}
func (a *ApprovalProvider) Information(ctx context.Context, in *approval.InformationRequest) (*approval.InformationResponse, error) {
return model.ApprovalInfo(in)
}
func (a *ApprovalProvider) Viewed(ctx context.Context, in *approval.ViewedRequest) (*approval.ApprovalResponse, error) {
response := &approval.ApprovalResponse{}
var err error
if err = model.Viewed(in); err != nil {
return response, err
}
response.ID = in.ID
response.Success = true
return response, err
}
func (a *ApprovalProvider) MySubmit(ctx context.Context, in *approval.ListRequest) (*approval.ListResponse, error) {
//获取人的 部门角色1、管理组 2、超级管理 3、Leader角色
response := &approval.ListResponse{}
var list []*model.Approval
var count int64
list, count = model.MySubmitApprovals(in)
response.Count = uint64(count)
response.Data = serializer.BuildApprovals(list, in.UserID)
return response, nil
}
func (a *ApprovalProvider) MyWork(ctx context.Context, in *approval.ListRequest) (*approval.ListResponse, error) {
//获取人的 部门角色1、管理组 2、超级管理 3、Leader角色
response := &approval.ListResponse{}
var list []*model.Approval
var count int64
list, count = model.MyWorkApprovals(in)
response.Count = uint64(count)
response.Data = serializer.BuildApprovals(list, in.UserID)
return response, nil
}
// UpdateSetting 设置配置抄送者和审核者
func (a *ApprovalProvider) UpdateSetting(ctx context.Context, in *approval.SettingRequest) (*approval.SettingResponse, error) {
var err error
response := &approval.SettingResponse{}
var setting model.ApprovalSetting
copyUsers := model.FormatCopyUsers(in.CopyUsers)
approvalUsers := model.FormatApproveUsers(in.ApprovalUsers)
if approvalUsers == nil && len(approvalUsers) == 0 {
return response, errors.New("必须要有抄送人")
}
//首次
if err := model.DB.Model(&model.ApprovalSetting{}).
Where(&model.ApprovalSetting{Domain: in.Domain, KeyWord: in.KeyWord}).
First(&setting).Error; err != nil {
setting = model.ApprovalSetting{
KeyWord: in.KeyWord,
CopyUsers: copyUsers,
ApprovalUsers: approvalUsers,
Domain: in.Domain,
}
err = model.DB.Save(&setting).Error
} else {
setting.KeyWord = in.KeyWord
setting.CopyUsers = copyUsers
setting.ApprovalUsers = approvalUsers
//setting.ApproverID = in.ApproverID
//setting.ApproverName = in.ApproverName
err = model.DB.Where(&model.ApprovalSetting{Domain: in.Domain, KeyWord: in.KeyWord}).Updates(&setting).Error
}
if err == nil {
response.ID = setting.ID
response.Success = true
}
return response, err
}
func (a *ApprovalProvider) DetailSetting(ctx context.Context, in *approval.DetailSettingRequest) (*approval.SettingRequest, error) {
response := &approval.SettingRequest{}
var setting model.ApprovalSetting
if err := model.DB.Model(&model.ApprovalSetting{}).
Where(&model.ApprovalSetting{Domain: in.Domain, KeyWord: in.KeyWord}).
First(&setting).Error; err != nil {
response.Domain = in.Domain
} else {
response = serializer.BuildSetting(&setting)
}
return response, nil
}
func (a *ApprovalProvider) CreateType(ctx context.Context, in *approval.CreateTypeRequest) (*approval.TypeResponse, error) {
response := &approval.TypeResponse{}
keyWord := model.KeyWordFromTitle(in.Title)
if model.DataExist(in.Domain, keyWord) == true {
return response, errors.New(m.ErrorAlreadyExist)
}
typeObj := model.ApprovalType{
KeyWord: keyWord,
Title: in.Title,
Domain: in.Domain,
Remark: in.Remark,
}
if err := model.DB.Create(&typeObj).Error; err != nil {
return response, err
}
response.ID = typeObj.ID
response.Domain = typeObj.Domain
return response, nil
}
func (a *ApprovalProvider) DetailType(ctx context.Context, in *approval.TypeResponse) (*approval.CreateTypeRequest, error) {
response := &approval.CreateTypeRequest{}
var typeObj model.ApprovalType
if err := model.DB.First(&typeObj, in.ID).Error; err != nil {
return nil, err
}
response = serializer.BuildType(&typeObj)
return response, nil
}
func (a *ApprovalProvider) UpdateType(ctx context.Context, in *approval.CreateTypeRequest) (*approval.TypeResponse, error) {
response := &approval.TypeResponse{}
var typeObj model.ApprovalType
if err := model.DB.First(&typeObj, in.ID).Error; err != nil {
return nil, err
}
if typeObj.Title != in.Title && model.IsSystemKeyWork(in.Title) == true {
return response, errors.New(m.ErrorChangeType)
}
typeObj.Title = in.Title
typeObj.Remark = in.Remark
if err := model.DB.Save(&typeObj).Error; err != nil {
return nil, err
}
response.ID = in.ID
response.Domain = in.Domain
return response, nil
}
func (a *ApprovalProvider) AllType(ctx context.Context, in *approval.CommonRequest) (*approval.AllTypeResponse, error) {
response := &approval.AllTypeResponse{}
var list []*model.ApprovalType
var count int64
modelObj := model.DB.Model(&model.ApprovalType{}).Where(&model.ApprovalType{Domain: in.Domain}).Order("id desc")
modelObj.Count(&count)
if in.Page != 0 && in.PageSize != 0 {
modelObj.Limit(int(in.PageSize)).Offset(page.GetOffset(in.Page, in.PageSize))
}
modelObj.Find(&list)
response.Data = serializer.BuildTypes(list)
response.Count = uint64(count)
return response, nil
}
func (a *ApprovalProvider) RemoveType(ctx context.Context, in *approval.TypeResponse) (*approval.TypeResponse, error) {
response := &approval.TypeResponse{}
var typeObj model.ApprovalType
if err := model.DB.First(&typeObj, in.ID).Error; err != nil {
return nil, err
}
if err := model.DB.Delete(&typeObj, in.ID).Error; err != nil {
return nil, err
}
response.ID = in.ID
response.Domain = in.Domain
return response, nil
}