234 lines
6.6 KiB
Go
234 lines
6.6 KiB
Go
package system
|
|
|
|
import (
|
|
"f5"
|
|
"jccommon"
|
|
"main/common"
|
|
"main/constant"
|
|
"main/model/system"
|
|
"net/http"
|
|
"q5"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type MailApi struct {
|
|
}
|
|
|
|
func (this *MailApi) ListMail(c *gin.Context) {
|
|
var pageSize = q5.SafeToInt32(c.DefaultQuery("pagesize", ""))
|
|
var cursor = q5.SafeToInt64(c.DefaultQuery("cursor", ""))
|
|
orderBy := ""
|
|
sql := "SELECT * FROM t_mail WHERE 1=1 and unikey is not null "
|
|
subFilters := []f5.DbQueryFilter{}
|
|
{
|
|
inSub := `tag1 IN (` + q5.ToString(jccommon.MAIL_TAG1_CUSTOM)
|
|
inSub += ")"
|
|
//q5.AppendSlice(&subFilters, f5.GetDbFilter().Custom(inSub).And())
|
|
f5.GetDbFilter().Custom("deleted = 0").And()
|
|
}
|
|
mails := []*system.Mail{}
|
|
rspObj := struct {
|
|
Code int32 `json:"code"`
|
|
Message string `json:"message"`
|
|
Cursor int64 `json:"cursor"`
|
|
Remaining int32 `json:"remaining"`
|
|
Data []*system.Mail `json:"data"`
|
|
}{}
|
|
f5.GetGoStyleDb().StreamPageQuery(
|
|
constant.MAIL_DB,
|
|
pageSize,
|
|
cursor,
|
|
sql,
|
|
[]string{},
|
|
f5.GetDbFilter().Comp(subFilters...),
|
|
orderBy,
|
|
func(err error, pagination *f5.StreamPagination) {
|
|
rspObj.Cursor = pagination.NextCursor
|
|
rspObj.Remaining = pagination.Remaining
|
|
},
|
|
func(ds *f5.DataSet) {
|
|
p := new(system.Mail)
|
|
f5.UnmarshalModel(ds, p)
|
|
q5.AppendSlice(&mails, p)
|
|
})
|
|
rspObj.Data = mails
|
|
c.JSON(http.StatusOK, rspObj)
|
|
}
|
|
|
|
func (this *MailApi) AddMail(c *gin.Context) {
|
|
reqJson := struct {
|
|
UniKey string `json:"unikey"`
|
|
MailType int32 `binding:"required" json:"mailtype"`
|
|
SendTime int32 `json:"sendtime"`
|
|
ExpireTime int32 `json:"expiretime"`
|
|
UserRegStartTime int32 `json:"user_reg_start_time"`
|
|
UserRegEndTime int32 `json:"user_reg_end_time"`
|
|
Subject string `json:"subject"`
|
|
Content string `json:"content"`
|
|
Attachments []common.Attachment `json:"attachments"`
|
|
Recipients []string `json:"recipients"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&reqJson); err != nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 1,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
if reqJson.MailType <= jccommon.MAIL_TYPE_BEGIN || reqJson.MailType >= jccommon.MAIL_TYPE_END {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 2,
|
|
"message": "mail_type参数错误",
|
|
})
|
|
return
|
|
}
|
|
if reqJson.UniKey == "" {
|
|
f5.RspErr2(c, 101, "unikey param error")
|
|
return
|
|
}
|
|
|
|
var count int64 = 0
|
|
if f5.GetApp().GetOrmDb(constant.MAIL_DB).Table("t_mail").Where("unikey = ?", reqJson.UniKey).Count(&count); count > 0 {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 2,
|
|
"message": "unikey 重复",
|
|
})
|
|
return
|
|
}
|
|
|
|
nowDaySeconds := int32(f5.GetApp().GetRealSeconds())
|
|
mail := new(system.Mail)
|
|
mail.MailId = f5.GetApp().NewLockNodeUuid()
|
|
mail.MailType = reqJson.MailType
|
|
mail.UniKey = reqJson.UniKey
|
|
mail.SendTime = reqJson.SendTime
|
|
mail.ExpireTime = reqJson.ExpireTime
|
|
mail.UserRegStartTime = reqJson.UserRegStartTime
|
|
mail.UserRegEndTime = reqJson.UserRegEndTime
|
|
mail.Subject = reqJson.Subject
|
|
mail.Content = reqJson.Content
|
|
mail.Attachments = reqJson.Attachments
|
|
mail.Recipients = reqJson.Recipients
|
|
mail.Tag1 = jccommon.MAIL_TAG1_CUSTOM
|
|
mail.Tag2 = jccommon.MAIL_TAG2_CUSTOM_NORMAL
|
|
mail.CreateTime = nowDaySeconds
|
|
mail.ModifyTime = nowDaySeconds
|
|
if f5.GetApp().GetOrmDb(constant.MAIL_DB).Create(mail).Error != nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 500,
|
|
"message": "",
|
|
})
|
|
return
|
|
}
|
|
{
|
|
e := new(jccommon.MailEvent)
|
|
e.EventName = jccommon.EVENT_MAIL_UPDATE
|
|
e.Param1 = q5.ToString(mail.MailId)
|
|
e.CreateTime = nowDaySeconds
|
|
e.ModifyTime = nowDaySeconds
|
|
f5.GetApp().GetOrmDb(constant.MAIL_DB).Create(e)
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 0,
|
|
"message": "",
|
|
})
|
|
}
|
|
|
|
func (this *MailApi) EditMail(c *gin.Context) {
|
|
reqJson := struct {
|
|
MailId int64 `binding:"required" json:"mail_id,string"`
|
|
MailType int32 `binding:"required" json:"mail_type"`
|
|
SendTime int32 `json:"sendtime"`
|
|
ExpireTime int32 `json:"expiretime"`
|
|
UserRegStartTime int32 `json:"user_reg_start_time"`
|
|
UserRegEndTime int32 `json:"user_reg_end_time"`
|
|
Subject string `json:"subject"`
|
|
Content string `json:"content"`
|
|
Attachments []common.Attachment `json:"attachments"`
|
|
Recipients []string `json:"recipients"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&reqJson); err != nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 1,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
if reqJson.MailType <= jccommon.MAIL_TYPE_BEGIN || reqJson.MailType >= jccommon.MAIL_TYPE_END {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 2,
|
|
"message": "mail_type参数错误",
|
|
})
|
|
return
|
|
}
|
|
|
|
var count int64 = 0
|
|
if f5.GetApp().GetOrmDb(constant.MAIL_DB).Where("mail_id = ?", reqJson.MailId).Count(&count); count < 1 {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 2,
|
|
"message": "mailid不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
nowDaySeconds := int32(f5.GetApp().GetRealSeconds())
|
|
mail := new(system.Mail)
|
|
mail.MailId = reqJson.MailId
|
|
mail.MailType = reqJson.MailType
|
|
mail.SendTime = reqJson.SendTime
|
|
mail.ExpireTime = reqJson.ExpireTime
|
|
mail.UserRegStartTime = reqJson.UserRegStartTime
|
|
mail.UserRegEndTime = reqJson.UserRegEndTime
|
|
mail.Subject = reqJson.Subject
|
|
mail.Content = reqJson.Content
|
|
mail.Attachments = reqJson.Attachments
|
|
mail.Recipients = reqJson.Recipients
|
|
mail.ModifyTime = nowDaySeconds
|
|
f5.GetApp().GetOrmDb(constant.MAIL_DB).Save(mail)
|
|
{
|
|
e := new(jccommon.MailEvent)
|
|
e.EventName = jccommon.EVENT_MAIL_UPDATE
|
|
e.Param1 = q5.ToString(mail.MailId)
|
|
e.CreateTime = nowDaySeconds
|
|
e.ModifyTime = nowDaySeconds
|
|
f5.GetApp().GetOrmDb(constant.MAIL_DB).Create(e)
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 0,
|
|
"message": "",
|
|
})
|
|
}
|
|
|
|
func (this *MailApi) DelMail(c *gin.Context) {
|
|
mailid := q5.ToInt64(c.Param("mailid"))
|
|
|
|
var count int64 = 0
|
|
if f5.GetApp().GetOrmDb(constant.MAIL_DB).Table("t_mail").Where("mail_id = ?", mailid).Count(&count); count < 1 {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 2,
|
|
"message": "mailid不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
nowDaySeconds := int32(f5.GetApp().GetRealSeconds())
|
|
mail := new(system.Mail)
|
|
mail.MailId = mailid
|
|
mail.Deleted = 1
|
|
mail.ModifyTime = nowDaySeconds
|
|
f5.GetApp().GetOrmDb(constant.MAIL_DB).Save(mail)
|
|
{
|
|
e := new(jccommon.MailEvent)
|
|
e.EventName = jccommon.EVENT_MAIL_UPDATE
|
|
e.Param1 = q5.ToString(mail.MailId)
|
|
e.CreateTime = nowDaySeconds
|
|
e.ModifyTime = nowDaySeconds
|
|
f5.GetApp().GetOrmDb(constant.MAIL_DB).Create(e)
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 0,
|
|
"message": "",
|
|
})
|
|
}
|