gocron/internal/models/setting.go

292 lines
6.1 KiB
Go
Raw Normal View History

2017-04-28 03:54:46 +00:00
package models
2017-04-30 17:12:07 +00:00
import (
2017-09-16 09:58:33 +00:00
"encoding/json"
2017-04-30 17:12:07 +00:00
)
2017-09-16 09:58:33 +00:00
type Setting struct {
Id int `xorm:"int pk autoincr"`
Code string `xorm:"varchar(32) notnull"`
Key string `xorm:"varchar(64) notnull"`
Value string `xorm:"varchar(4096) notnull default '' "`
2017-04-28 03:54:46 +00:00
}
2018-05-07 13:57:19 +00:00
const slackTemplate = `
ID: {{.TaskId}}
: {{.TaskName}}
: {{.Status}}
2018-05-07 13:57:19 +00:00
: {{.Result}}
`
const emailTemplate = `
ID: {{.TaskId}}
: {{.TaskName}}
: {{.Status}}
2018-05-07 13:57:19 +00:00
: {{.Result}}
`
const webhookTemplate = `
{
"task_id": "{{.TaskId}}",
"task_name": "{{.TaskName}}",
"status": "{{.Status}}",
"result": "{{.Result}}"
}
`
const (
SlackCode = "slack"
SlackUrlKey = "url"
SlackTemplateKey = "template"
SlackChannelKey = "channel"
)
2017-04-30 17:12:07 +00:00
const (
MailCode = "mail"
MailTemplateKey = "template"
MailServerKey = "server"
MailUserKey = "user"
)
const (
WebhookCode = "webhook"
WebhookTemplateKey = "template"
WebhookUrlKey = "url"
)
2017-04-28 03:54:46 +00:00
// 初始化基本字段 邮件、slack等
2017-04-30 17:12:07 +00:00
func (setting *Setting) InitBasicField() {
2017-09-16 09:58:33 +00:00
setting.Code = SlackCode
setting.Key = SlackUrlKey
2018-05-07 13:57:19 +00:00
setting.Value = ""
2017-09-16 09:58:33 +00:00
Db.Insert(setting)
2018-05-07 13:57:19 +00:00
setting.Id = 0
2017-04-30 17:12:07 +00:00
2018-05-07 13:57:19 +00:00
setting.Code = SlackCode
setting.Key = SlackTemplateKey
setting.Value = slackTemplate
Db.Insert(setting)
2017-09-16 09:58:33 +00:00
setting.Id = 0
2018-05-07 13:57:19 +00:00
2017-09-16 09:58:33 +00:00
setting.Code = MailCode
setting.Key = MailServerKey
2018-05-07 13:57:19 +00:00
setting.Value = ""
Db.Insert(setting)
setting.Id = 0
setting.Code = MailCode
setting.Key = MailTemplateKey
setting.Value = emailTemplate
2017-09-16 09:58:33 +00:00
Db.Insert(setting)
setting.Id = 0
setting.Code = WebhookCode
setting.Key = WebhookTemplateKey
setting.Value = webhookTemplate
Db.Insert(setting)
setting.Id = 0
setting.Code = WebhookCode
setting.Key = WebhookUrlKey
setting.Value = ""
Db.Insert(setting)
2017-04-30 17:12:07 +00:00
}
// region slack配置
2017-04-28 03:54:46 +00:00
2017-04-30 17:12:07 +00:00
type Slack struct {
2018-05-02 12:41:41 +00:00
Url string `json:"url"`
Channels []Channel `json:"channels"`
2018-05-07 13:57:19 +00:00
Template string `json:"template"`
2017-04-28 03:54:46 +00:00
}
2017-04-30 17:12:07 +00:00
type Channel struct {
2018-05-02 12:41:41 +00:00
Id int `json:"id"`
Name string `json:"name"`
2017-04-30 17:12:07 +00:00
}
2017-09-16 09:58:33 +00:00
func (setting *Setting) Slack() (Slack, error) {
list := make([]Setting, 0)
err := Db.Where("code = ?", SlackCode).Find(&list)
2018-05-07 13:57:19 +00:00
slack := Slack{}
2017-09-16 09:58:33 +00:00
if err != nil {
return slack, err
}
2017-04-30 17:12:07 +00:00
2017-09-16 09:58:33 +00:00
setting.formatSlack(list, &slack)
2017-04-28 03:54:46 +00:00
2017-09-16 09:58:33 +00:00
return slack, err
2017-04-28 03:54:46 +00:00
}
2017-09-16 09:58:33 +00:00
func (setting *Setting) formatSlack(list []Setting, slack *Slack) {
for _, v := range list {
2018-05-07 13:57:19 +00:00
switch v.Key {
case SlackUrlKey:
2017-09-16 09:58:33 +00:00
slack.Url = v.Value
2018-05-07 13:57:19 +00:00
case SlackTemplateKey:
slack.Template = v.Value
default:
slack.Channels = append(slack.Channels, Channel{
v.Id, v.Value,
})
2017-09-16 09:58:33 +00:00
}
}
2017-04-30 17:12:07 +00:00
}
func (setting *Setting) UpdateSlack(url, template string) error {
2017-09-16 09:58:33 +00:00
setting.Value = url
2017-04-28 03:54:46 +00:00
Db.Cols("value").Update(setting, Setting{Code: SlackCode, Key: SlackUrlKey})
setting.Value = template
Db.Cols("value").Update(setting, Setting{Code: SlackCode, Key: SlackTemplateKey})
return nil
2017-04-30 17:12:07 +00:00
}
// 创建slack渠道
func (setting *Setting) CreateChannel(channel string) (int64, error) {
2017-09-16 09:58:33 +00:00
setting.Code = SlackCode
setting.Key = SlackChannelKey
setting.Value = channel
2017-04-30 17:12:07 +00:00
2017-09-16 09:58:33 +00:00
return Db.Insert(setting)
2017-04-30 17:12:07 +00:00
}
2017-09-16 09:58:33 +00:00
func (setting *Setting) IsChannelExist(channel string) bool {
setting.Code = SlackCode
setting.Key = SlackChannelKey
setting.Value = channel
2017-04-30 17:12:07 +00:00
2017-09-16 09:58:33 +00:00
count, _ := Db.Count(setting)
2017-04-30 17:12:07 +00:00
2017-09-16 09:58:33 +00:00
return count > 0
2017-04-28 03:54:46 +00:00
}
2017-04-30 17:12:07 +00:00
// 删除slack渠道
2017-09-16 09:58:33 +00:00
func (setting *Setting) RemoveChannel(id int) (int64, error) {
setting.Code = SlackCode
setting.Key = SlackChannelKey
setting.Id = id
return Db.Delete(setting)
2017-04-30 17:12:07 +00:00
}
// endregion
type Mail struct {
2018-05-02 12:41:41 +00:00
Host string `json:"host"`
Port int `json:"port"`
User string `json:"user"`
Password string `json:"password"`
MailUsers []MailUser `json:"mail_users"`
2018-05-07 13:57:19 +00:00
Template string `json:"template"`
2017-04-28 03:54:46 +00:00
}
2017-04-30 17:12:07 +00:00
type MailUser struct {
2018-05-02 12:41:41 +00:00
Id int `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
2017-04-30 17:12:07 +00:00
}
// region 邮件配置
2017-09-16 09:58:33 +00:00
func (setting *Setting) Mail() (Mail, error) {
list := make([]Setting, 0)
err := Db.Where("code = ?", MailCode).Find(&list)
mail := Mail{MailUsers: make([]MailUser, 0)}
if err != nil {
return mail, err
}
2017-04-30 17:12:07 +00:00
2017-09-16 09:58:33 +00:00
setting.formatMail(list, &mail)
2017-04-30 17:12:07 +00:00
2017-09-16 09:58:33 +00:00
return mail, err
2017-04-30 17:12:07 +00:00
}
2017-09-16 09:58:33 +00:00
func (setting *Setting) formatMail(list []Setting, mail *Mail) {
mailUser := MailUser{}
for _, v := range list {
switch v.Key {
case MailServerKey:
2017-09-16 09:58:33 +00:00
json.Unmarshal([]byte(v.Value), mail)
case MailUserKey:
json.Unmarshal([]byte(v.Value), &mailUser)
mailUser.Id = v.Id
mail.MailUsers = append(mail.MailUsers, mailUser)
case MailTemplateKey:
mail.Template = v.Value
2017-09-16 09:58:33 +00:00
}
2017-09-16 09:58:33 +00:00
}
2017-04-30 17:12:07 +00:00
}
func (setting *Setting) UpdateMail(config, template string) error {
2017-09-16 09:58:33 +00:00
setting.Value = config
Db.Cols("value").Update(setting, Setting{Code: MailCode, Key: MailServerKey})
setting.Value = template
Db.Cols("value").Update(setting, Setting{Code: MailCode, Key: MailTemplateKey})
return nil
2017-04-30 17:12:07 +00:00
}
func (setting *Setting) CreateMailUser(username, email string) (int64, error) {
2017-09-16 09:58:33 +00:00
setting.Code = MailCode
setting.Key = MailUserKey
mailUser := MailUser{0, username, email}
jsonByte, err := json.Marshal(mailUser)
if err != nil {
return 0, err
}
setting.Value = string(jsonByte)
2017-04-30 17:12:07 +00:00
2017-09-16 09:58:33 +00:00
return Db.Insert(setting)
2017-04-30 17:12:07 +00:00
}
2017-09-16 09:58:33 +00:00
func (setting *Setting) RemoveMailUser(id int) (int64, error) {
setting.Code = MailCode
setting.Key = MailUserKey
setting.Id = id
return Db.Delete(setting)
2017-04-30 17:12:07 +00:00
}
2017-09-16 09:58:33 +00:00
type WebHook struct {
Url string `json:"url"`
Template string `json:"template"`
}
func (setting *Setting) Webhook() (WebHook, error) {
list := make([]Setting, 0)
err := Db.Where("code = ?", WebhookCode).Find(&list)
webHook := WebHook{}
if err != nil {
return webHook, err
}
setting.formatWebhook(list, &webHook)
return webHook, err
}
func (setting *Setting) formatWebhook(list []Setting, webHook *WebHook) {
for _, v := range list {
switch v.Key {
case WebhookUrlKey:
webHook.Url = v.Value
case WebhookTemplateKey:
webHook.Template = v.Value
}
}
}
func (setting *Setting) UpdateWebHook(url, template string) error {
setting.Value = url
Db.Cols("value").Update(setting, Setting{Code: WebhookCode, Key: WebhookUrlKey})
setting.Value = template
Db.Cols("value").Update(setting, Setting{Code: WebhookCode, Key: WebhookTemplateKey})
return nil
}
2017-09-16 09:58:33 +00:00
// endregion