goploy/model/CrontabServerModel.go

123 lines
3.0 KiB
Go
Raw Normal View History

2020-08-04 14:28:25 +08:00
package model
import (
sq "github.com/Masterminds/squirrel"
)
const crontabServerTable = "`crontab_server`"
2020-08-15 13:38:06 +08:00
// CrontabServer -
2020-08-04 14:28:25 +08:00
type CrontabServer struct {
ID int64 `json:"id"`
CrontabID int64 `json:"crontabId"`
ServerID int64 `json:"serverId"`
ServerName string `json:"serverName"`
ServerIP string `json:"serverIP"`
ServerPort int64 `json:"serverPort"`
ServerOwner string `json:"serverOwner"`
ServerDescription string `json:"serverDescription"`
InsertTime string `json:"insertTime"`
UpdateTime string `json:"updateTime"`
}
2020-08-15 13:38:06 +08:00
// CrontabServers -
2020-08-04 14:28:25 +08:00
type CrontabServers []CrontabServer
2020-08-15 13:38:06 +08:00
// GetAllByCrontabID return CrontabServers by CrontabID
2020-08-04 14:28:25 +08:00
func (cs CrontabServer) GetAllByCrontabID() (CrontabServers, error) {
rows, err := sq.
Select("id, crontab_id, server_id").
From(crontabServerTable).
Where(sq.Eq{"crontab_id": cs.CrontabID}).
RunWith(DB).
Query()
if err != nil {
return nil, err
}
crontabServers := CrontabServers{}
for rows.Next() {
var crontabServer CrontabServer
if err := rows.Scan(
&crontabServer.ID,
&crontabServer.CrontabID,
&crontabServer.ServerID); err != nil {
return nil, err
}
crontabServers = append(crontabServers, crontabServer)
}
return crontabServers, nil
}
2020-08-15 13:38:06 +08:00
// GetBindServerListByProjectID return bind server list by project id
2020-08-04 14:28:25 +08:00
func (cs CrontabServer) GetBindServerListByProjectID() (CrontabServers, error) {
rows, err := sq.
Select("crontab_server.id, crontab_id, server_id, server.name, server.ip, server.port, server.owner, server.description, crontab_server.insert_time, crontab_server.update_time").
From(crontabServerTable).
LeftJoin(serverTable + " ON crontab_server.server_id = server.id").
Where(sq.Eq{"crontab_id": cs.CrontabID}).
RunWith(DB).
Query()
if err != nil {
return nil, err
}
crontabServers := CrontabServers{}
for rows.Next() {
var crontabServer CrontabServer
if err := rows.Scan(
&crontabServer.ID,
&crontabServer.CrontabID,
&crontabServer.ServerID,
&crontabServer.ServerName,
&crontabServer.ServerIP,
&crontabServer.ServerPort,
&crontabServer.ServerOwner,
&crontabServer.ServerDescription,
&crontabServer.InsertTime,
&crontabServer.UpdateTime); err != nil {
return nil, err
}
crontabServers = append(crontabServers, crontabServer)
}
return crontabServers, nil
}
2020-08-15 13:38:06 +08:00
// AddMany -
2020-08-04 14:28:25 +08:00
func (cs CrontabServers) AddMany() error {
if len(cs) == 0 {
return nil
}
builder := sq.
Insert(crontabServerTable).
Columns("crontab_id", "server_id")
for _, row := range cs {
builder = builder.Values(row.CrontabID, row.ServerID)
}
_, err := builder.RunWith(DB).Exec()
return err
}
2020-08-15 13:38:06 +08:00
// DeleteRow -
2020-08-04 14:28:25 +08:00
func (cs CrontabServer) DeleteRow() error {
_, err := sq.
Delete(crontabServerTable).
Where(sq.Eq{"id": cs.ID}).
RunWith(DB).
Exec()
return err
}
2020-08-15 13:38:06 +08:00
// DeleteByCrontabID -
func (cs CrontabServer) DeleteByCrontabID() error {
2020-08-04 14:28:25 +08:00
_, err := sq.
Delete(crontabServerTable).
Where(sq.Eq{"crontab_id": cs.CrontabID}).
RunWith(DB).
Exec()
return err
}