207 lines
5.8 KiB
Go
207 lines
5.8 KiB
Go
|
package model
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"github.com/fonchain_enterprise/fonchain-approval/api/approval"
|
||
|
"github.com/fonchain_enterprise/fonchain-approval/pkg/m"
|
||
|
"gorm.io/gorm"
|
||
|
"gorm.io/plugin/soft_delete"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// Approval 审批
|
||
|
type Approval struct {
|
||
|
ID uint64 `gorm:"primaryKey;column:id" json:"id"` // ID
|
||
|
DeletedAt soft_delete.DeletedAt `gorm:"column:deleted_at" json:"deletedAt"` // 删除时间
|
||
|
CreatedAt time.Time `gorm:"column:created_at" json:"createdAt"` // 创建时间
|
||
|
UpdatedAt time.Time `gorm:"column:updated_at" json:"updatedAt"` // 更新时间
|
||
|
SubmitterID uint64 `gorm:"column:submitter_id" json:"submitterId"` // 提交人ID
|
||
|
ApproverID uint64 `gorm:"column:approver_id" json:"approverId"` // 审核人ID
|
||
|
Status int8 `gorm:"column:status" json:"status"` // 状态0-待 1-可 2-否
|
||
|
SubmitterName string `gorm:"column:submitter_name" json:"submitterName"` // 提交人名称缓存
|
||
|
ApproverName string `gorm:"column:approver_name" json:"approverName"` // 审核人名称缓存
|
||
|
CopyUsers CopyUsers `gorm:"column:copy_users" json:"copyUsers"` // 抄送人
|
||
|
Type string `gorm:"column:type" json:"type"` // 类型
|
||
|
Content string `gorm:"column:content" json:"content"` // 提交审批内容
|
||
|
Reply string `gorm:"column:reply" json:"reply"` // 回复内容
|
||
|
Domain string `gorm:"column:domain" json:"domain"` // 环境变量
|
||
|
Show *Show //`gorm:"foreignKey:ApprovalID" json:"Show"` // 环境变量
|
||
|
Work *Work //`gorm:"foreignKey:ApprovalID" json:"Work"` // 环境变量
|
||
|
Bundle *Bundle //`gorm:"foreignKey:ApprovalID" json:"Bundle"` // 环境变量
|
||
|
Exhibition *Exhibition //`gorm:"foreignKey:ApprovalID" json:"Exhibition"` // 环境变量
|
||
|
}
|
||
|
|
||
|
const (
|
||
|
TypeContent = "content"
|
||
|
TypeWork = "work"
|
||
|
TypeExhibition = "exhibition"
|
||
|
TypeShow = "show"
|
||
|
TypeBundle = "bundle"
|
||
|
)
|
||
|
|
||
|
type ApprovalContentInterface interface {
|
||
|
SaveApprovalContent(in *approval.ApprovalRequest, a *Approval) error
|
||
|
BuildResContent(a *Approval, request *approval.ApprovalRequest)
|
||
|
DeleteApproval(p *Approval) error
|
||
|
}
|
||
|
|
||
|
// TableName get sql table name.获取数据库表名
|
||
|
func (m *Approval) TableName() string {
|
||
|
return "approval"
|
||
|
}
|
||
|
|
||
|
func StoreApproval(in *approval.ApprovalRequest) (*Approval, error) {
|
||
|
var entity *Approval
|
||
|
|
||
|
err := DB.Transaction(func(tx *gorm.DB) error {
|
||
|
|
||
|
// 保存基本信息
|
||
|
entity = &Approval{
|
||
|
Domain: in.Domain,
|
||
|
SubmitterID: in.SubmitterId,
|
||
|
SubmitterName: in.SubmitterName,
|
||
|
ApproverID: in.ApproverId,
|
||
|
ApproverName: in.ApproverName,
|
||
|
CopyUsers: FormatCopyUsers(in.CopyUsers),
|
||
|
Type: in.Type,
|
||
|
Content: in.Content,
|
||
|
}
|
||
|
|
||
|
if err := DB.Create(&entity).Error; err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// 不同类型保存不同的结构体
|
||
|
err := entity.SaveContent(in)
|
||
|
|
||
|
return err
|
||
|
})
|
||
|
|
||
|
return entity, err
|
||
|
|
||
|
}
|
||
|
|
||
|
func (m *Approval) SaveApprovalContent(in *approval.ApprovalRequest, a *Approval) error {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m *Approval) DeleteApproval(p *Approval) error {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m *Approval) DeleteContent() error {
|
||
|
|
||
|
factory, err := GetApprovalContentFactory(m.Type)
|
||
|
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return factory.DeleteApproval(m)
|
||
|
}
|
||
|
|
||
|
func (m *Approval) BuildResContent(a *Approval, request *approval.ApprovalRequest) {
|
||
|
|
||
|
}
|
||
|
|
||
|
func (m *Approval) SetResContent(request *approval.ApprovalRequest) error {
|
||
|
|
||
|
factory, err := GetApprovalContentFactory(m.Type)
|
||
|
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
factory.BuildResContent(m, request)
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m *Approval) SaveContent(in *approval.ApprovalRequest) error {
|
||
|
|
||
|
factory, err := GetApprovalContentFactory(in.Type)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
err = factory.SaveApprovalContent(in, m)
|
||
|
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func ApprovalList(in *approval.ApprovalListRequest) ([]*Approval, int64) {
|
||
|
var list []*Approval
|
||
|
var count int64
|
||
|
|
||
|
//list
|
||
|
modelObj := DB.Preload("Show.Rule").
|
||
|
Preload("Work.RuleDatum").
|
||
|
Preload("Bundle.RuleDatum").
|
||
|
Preload("Exhibition.RuleDatum").
|
||
|
Limit(int(in.Limit)).Offset(int(in.Offset))
|
||
|
|
||
|
if in.Type != "" {
|
||
|
modelObj = modelObj.Where(&Approval{Type: in.Type})
|
||
|
}
|
||
|
|
||
|
if in.Status != 0 {
|
||
|
modelObj = modelObj.Where(&Approval{Status: int8(in.Status)})
|
||
|
}
|
||
|
|
||
|
modelObj.Count(&count)
|
||
|
|
||
|
modelObj.Order("id desc").Find(&list)
|
||
|
|
||
|
return list, count
|
||
|
}
|
||
|
|
||
|
func GetApprovalContentFactory(typeFiled string) (ApprovalContentInterface, error) {
|
||
|
switch typeFiled {
|
||
|
case TypeContent:
|
||
|
return &Approval{}, nil
|
||
|
case TypeShow:
|
||
|
return &Show{}, nil
|
||
|
case TypeWork:
|
||
|
return &Work{}, nil
|
||
|
case TypeBundle:
|
||
|
return &Bundle{}, nil
|
||
|
case TypeExhibition:
|
||
|
return &Exhibition{}, nil
|
||
|
default:
|
||
|
return nil, errors.New(m.ErrorApprovalType)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// ApprovalColumns get sql column name.获取数据库列名
|
||
|
var ApprovalColumns = struct {
|
||
|
ID string
|
||
|
DeletedAt string
|
||
|
CreatedAt string
|
||
|
UpdatedAt string
|
||
|
SubmitterID string
|
||
|
ApproverID string
|
||
|
Status string
|
||
|
SubmitterName string
|
||
|
ApproverName string
|
||
|
CopyName string
|
||
|
Type string
|
||
|
Content string
|
||
|
Reply string
|
||
|
Domain string
|
||
|
}{
|
||
|
ID: "id",
|
||
|
DeletedAt: "deleted_at",
|
||
|
CreatedAt: "created_at",
|
||
|
UpdatedAt: "updated_at",
|
||
|
SubmitterID: "submitter_id",
|
||
|
ApproverID: "approver_id",
|
||
|
Status: "status",
|
||
|
SubmitterName: "submitter_name",
|
||
|
ApproverName: "approver_name",
|
||
|
CopyName: "copy_name",
|
||
|
Type: "type",
|
||
|
Content: "content",
|
||
|
Reply: "reply",
|
||
|
Domain: "domain",
|
||
|
}
|