97 lines
2.7 KiB
Go
97 lines
2.7 KiB
Go
package model
|
|
|
|
import (
|
|
"backend/msg"
|
|
util "backend/util"
|
|
"fmt"
|
|
"log"
|
|
)
|
|
|
|
type Mail struct {
|
|
AppId int `json:"AppId"`
|
|
ServerId int `json:"ServerId"`
|
|
PageSize int `json:"PageSize"`
|
|
CurrentPage int `json:"CurrentPage"`
|
|
|
|
MailId int `json:"mail_id" db:"mail_id"`
|
|
Title string `json:"title" db:"title"`
|
|
Content string `json:"content" db:"content"`
|
|
StartTime int64 `json:"start_time" db:"start_time"`
|
|
EndTime int64 `json:"end_time" db:"end_time"`
|
|
Items string `json:"items" db:"items"`
|
|
RegisterTime int64 `json:"register_time" db:"register_time"`
|
|
MailType int `json:"mail_type" db:"mail_type"`
|
|
ToUids string `json:"to_uids" db:"to_uids"`
|
|
CreateTime int64 `json:"create_time" db:"create_time"`
|
|
}
|
|
|
|
type Result struct {
|
|
Data interface{} `json:"data"`
|
|
Result int `json:"result"`
|
|
}
|
|
|
|
func (m *Mail) MailList() (*Result, error) {
|
|
// do something
|
|
AppCfg, err := util.GetAppConfig(m.AppId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
Db := util.MPool.GetMysqlDB(AppCfg.ServerName, AppCfg.MysqlName, m.ServerId)
|
|
defer Db.Close()
|
|
var mail []*Mail
|
|
err = Db.Select(&mail, "SELECT `mail_id`, `title`, `content`, `start_time`, `end_time`, `items`, `register_time`, `mail_type`, `to_uids`, `create_time` FROM system_mail_info")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to scan rows: %v", err)
|
|
}
|
|
for _, v := range mail {
|
|
v.AppId = m.AppId
|
|
v.ServerId = m.ServerId
|
|
}
|
|
return &Result{
|
|
Data: mail,
|
|
Result: 0,
|
|
}, nil
|
|
}
|
|
|
|
func (m *Mail) SendMail() error {
|
|
AppCfg, err := util.GetAppConfig(m.AppId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
Db := util.MPool.GetMysqlDB(AppCfg.ServerName, AppCfg.MysqlName, m.ServerId)
|
|
defer Db.Close()
|
|
_, err = Db.Exec("INSERT INTO system_mail_info (`title`, `content`, `start_time`, `end_time`, `items`, `register_time`, `mail_type`, `to_uids`, `create_time`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.Title, m.Content, m.StartTime, m.EndTime, m.Items, m.RegisterTime, m.MailType, m.ToUids, m.CreateTime)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to insert mail: %v", err)
|
|
}
|
|
go func() {
|
|
ws, err := util.GetWebsocket(m.AppId, m.ServerId)
|
|
if err != nil {
|
|
log.Printf("failed to get websocket: %v", err)
|
|
}
|
|
defer ws.Close()
|
|
|
|
req := &msg.ReqReloadServerMail{}
|
|
_, err = util.SendAdminMsg(ws, req)
|
|
if err != nil {
|
|
log.Printf("failed to send admin message: %v", err)
|
|
}
|
|
}()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (m *Mail) DeleteMail() error {
|
|
AppCfg, err := util.GetAppConfig(m.AppId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
Db := util.MPool.GetMysqlDB(AppCfg.ServerName, AppCfg.MysqlName, m.ServerId)
|
|
defer Db.Close()
|
|
_, err = Db.Exec("DELETE FROM system_mail_info WHERE `mail_id` = ?", m.MailId)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to delete mail: %v", err)
|
|
}
|
|
return nil
|
|
}
|