161 lines
4.9 KiB
Go
161 lines
4.9 KiB
Go
package project_info
|
||
|
||
import (
|
||
"database/sql"
|
||
"fmt"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model"
|
||
"github.com/araujo88/lambda-go/pkg/core"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
type ProjectInfo struct {
|
||
model.BaseWorkspaceModel
|
||
ProjectId string `gorm:"column:project_id" json:"projectId"`
|
||
NodeId string `gorm:"column:node_id" json:"nodeId"`
|
||
Name string `gorm:"column:name" json:"name"`
|
||
Lib string `gorm:"column:lib" json:"lib"`
|
||
WhitelistDirectory string `gorm:"column:whitelist_directory" json:"whitelistDirectory"`
|
||
LogPath string `gorm:"column:log_path" json:"logPath"`
|
||
Jvm string `gorm:"column:jvm" json:"jvm"`
|
||
Args string `gorm:"column:args" json:"args"`
|
||
Token string `gorm:"column:token" json:"token"`
|
||
RunMode string `gorm:"column:run_mode" json:"runMode"`
|
||
OutGivingProject bool `gorm:"column:out_giving_project" json:"outGivingProject"`
|
||
//SortValue string `gorm:"column:sort_value" json:"sortValue"`
|
||
TriggerToken string `gorm:"column:trigger_token" json:"triggerToken"`
|
||
Group *string `gorm:"column:group" json:"group"`
|
||
DslContent string `gorm:"column:dsl_content" json:"dslContent"`
|
||
DslEnv string `gorm:"column:dsl_env" json:"dslEnv"`
|
||
AutoStart bool `gorm:"column:auto_start" json:"autoStart"`
|
||
NodeName string `gorm:"column:node_name" json:"nodeName"`
|
||
WorkspaceName string `gorm:"column:workspace_name" json:"workspaceName"`
|
||
}
|
||
|
||
func New() *ProjectInfo {
|
||
n := &ProjectInfo{}
|
||
n.Handler = n
|
||
return n
|
||
}
|
||
|
||
func (*ProjectInfo) TableName() string {
|
||
return "sched_project_info"
|
||
}
|
||
|
||
// 删除所有项目
|
||
func ClearAll() (int64, error) {
|
||
return model.DB().Where("1 = 1").Delete(&ProjectInfo{}).RowsAffected, nil
|
||
}
|
||
|
||
// 根据Id删除项目
|
||
func DeleteProjectById(id string) (int64, error) {
|
||
return model.DB().Where("id = ?", id).Delete(&ProjectInfo{}).RowsAffected, nil
|
||
}
|
||
|
||
// 根据Id查询项目
|
||
func GetProjectById(id string) (ProjectInfo, error) {
|
||
var project ProjectInfo
|
||
err := model.DB().Where("id = ?", id).First(&project).Error
|
||
return project, err
|
||
}
|
||
|
||
// 根据Id批量查询项目
|
||
func GetProjectByIds(ids []string) []ProjectInfo {
|
||
var projects []ProjectInfo
|
||
model.DB().Where("id in (?)", ids).Find(&projects)
|
||
return projects
|
||
}
|
||
|
||
// 根据nodeId和workspace_id删除项目
|
||
func DeleteProjectByNodeIdAndWorkspaceId(nodeId, workspaceId string) (int64, error) {
|
||
return model.DB().Where("node_id = ? and workspace_id = ?", nodeId, workspaceId).Delete(&ProjectInfo{}).RowsAffected, nil
|
||
}
|
||
|
||
// 根据nodeId删除项目
|
||
func DeleteProjectByNodeId(nodeId string) (int64, error) {
|
||
return model.DB().Where("node_id = ?", nodeId).Delete(&ProjectInfo{}).RowsAffected, nil
|
||
}
|
||
|
||
// 根据nodeId查询项目
|
||
func GetProjectByNodeId(nodeId string) ([]ProjectInfo, error) {
|
||
var result []ProjectInfo
|
||
err := model.DB().Table(New().TableName()).Table(New().TableName()).Where("node_id = ?", nodeId).Find(&result).Error
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return result, nil
|
||
}
|
||
|
||
// 指定workspaceId, nodeId,计算符合条件的项目数
|
||
func Exists_1(workspaceId, nodeId string) (bool, error) {
|
||
var result int64
|
||
tx := model.DB().Table(New().TableName()).Where("workspace_id = ? and node_id = ?", workspaceId, nodeId).Count(&result)
|
||
if tx.Error != nil {
|
||
return false, tx.Error
|
||
}
|
||
return result > 0, nil
|
||
|
||
}
|
||
|
||
// 指定workspaceId, nodeId, projectId,计算符合条件的项目数
|
||
func Exists(workspaceId, nodeId, projectId string) (bool, error) {
|
||
var result int64
|
||
tx := model.DB().Table(New().TableName()).Where("workspace_id = ? and node_id = ? and project_id = ?", workspaceId, nodeId, projectId).Count(&result)
|
||
if tx.Error != nil {
|
||
return false, tx.Error
|
||
}
|
||
return result > 0, nil
|
||
}
|
||
|
||
// 指定Id,计算符合条件的项目数
|
||
func ExistsById(id string) (bool, error) {
|
||
var result int64
|
||
tx := model.DB().Table(New().TableName()).Where("id = ?", id).Count(&result)
|
||
if tx.Error != nil {
|
||
return false, tx.Error
|
||
}
|
||
return result > 0, nil
|
||
}
|
||
|
||
func (p *ProjectInfo) ListGroup() ([]string, error) {
|
||
var result []sql.NullString
|
||
tx := model.DB().Table(p.TableName()).Select("\"group\"").Distinct().Scan(&result)
|
||
if len(result) > 0 {
|
||
us := core.Map(result, func(s sql.NullString) string {
|
||
if s.Valid {
|
||
return s.String
|
||
}
|
||
return ""
|
||
})
|
||
return us, tx.Error
|
||
}
|
||
return []string{}, tx.Error
|
||
}
|
||
|
||
func (p *ProjectInfo) CreateOrUpdate() (err error) {
|
||
_, err = p.Create(p)
|
||
if err != nil {
|
||
fmt.Println(err.Error())
|
||
_, err = p.Update(p, nil)
|
||
}
|
||
return err
|
||
}
|
||
|
||
func (p *ProjectInfo) FindResult(db *gorm.DB) (interface{}, error) {
|
||
var result []ProjectInfo
|
||
if db == nil {
|
||
return result, nil
|
||
}
|
||
err := db.Find(&result).Error
|
||
return result, err
|
||
}
|
||
|
||
// 根据工作空间(WorkspaceId)计算项目数量
|
||
func CountProjectByWorkspaceId(workspaceId string) (int64, error) {
|
||
var result int64
|
||
tx := model.DB().Table(New().TableName()).Where("workspace_id = ?", workspaceId).Count(&result)
|
||
if tx.Error != nil {
|
||
return 0, tx.Error
|
||
}
|
||
return result, nil
|
||
}
|