1 parent
096b4b8fed
commit
cc4aa851bc
43 files changed
+256
-396
No files matched your search
@@ -14,7 +14,7 @@ func GetAttachments(ctx *context.Context, emailId int, cid string) (string, []by
|
||||
|
||||
// 获取邮件内容
|
||||
var email models.Email
|
||||
err := db.Instance.Get(&email, db.WithContext(ctx, "select * from email where id = ?"), emailId)
|
||||
_, err := db.Instance.ID(emailId).Get(&email)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("SQL error:%+v", err)
|
||||
return "", nil
|
||||
@@ -39,7 +39,7 @@ func GetAttachmentsByIndex(ctx *context.Context, emailId int, index int) (string
|
||||
|
||||
// 获取邮件内容
|
||||
var email models.Email
|
||||
err := db.Instance.Get(&email, db.WithContext(ctx, "select * from email where id = ?"), emailId)
|
||||
_, err := db.Instance.ID(emailId).Get(&email)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("SQL error:%+v", err)
|
||||
return "", nil
|
||||
|
||||
@@ -19,7 +19,7 @@ import (
|
||||
func HasAuth(ctx *context.Context, email *models.Email) bool {
|
||||
// 获取当前用户的auth
|
||||
var auth []models.UserAuth
|
||||
err := db.Instance.Select(&auth, db.WithContext(ctx, "select * from user_auth where user_id = ?"), ctx.UserID)
|
||||
err := db.Instance.Where("user_id = ?", ctx.UserID).Find(&auth)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("SQL error:%+v", err)
|
||||
return false
|
||||
|
||||
@@ -13,8 +13,10 @@ import (
|
||||
func DelEmail(ctx *context.Context, ids []int) error {
|
||||
var emails []*models.Email
|
||||
|
||||
db.Instance.Select(&emails, db.WithContext(ctx, fmt.Sprintf("select * from email where id in (%s)", array.Join(ids, ","))))
|
||||
|
||||
err := db.Instance.ID(ids).Find(&emails)
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
for _, email := range emails {
|
||||
// 检查是否有权限
|
||||
hasAuth := auth.HasAuth(ctx, email)
|
||||
@@ -23,8 +25,7 @@ func DelEmail(ctx *context.Context, ids []int) error {
|
||||
}
|
||||
}
|
||||
|
||||
//_, err := db.Instance.Exec(db.WithContext(ctx, fmt.Sprintf("delete from email where id in (%s)", array.Join(ids, ","))))
|
||||
_, err := db.Instance.Exec(db.WithContext(ctx, fmt.Sprintf("update email set status = 3 where id in (%s)", array.Join(ids, ","))))
|
||||
_, err = db.Instance.Exec(db.WithContext(ctx, fmt.Sprintf("update email set status = 3 where id in (%s)", array.Join(ids, ","))))
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
func GetEmailDetail(ctx *context.Context, id int, markRead bool) (*models.Email, error) {
|
||||
// 获取邮件内容
|
||||
var email models.Email
|
||||
err := db.Instance.Get(&email, db.WithContext(ctx, "select * from email where id = ?"), id)
|
||||
_, err := db.Instance.ID(id).Get(&email)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("SQL error:%+v", err)
|
||||
return nil, err
|
||||
|
||||
@@ -23,10 +23,7 @@ func DelGroup(ctx *context.Context, groupId int) (bool, error) {
|
||||
allGroupIds = append(allGroupIds, groupId)
|
||||
|
||||
// 开启一个事务
|
||||
trans, err := db.Instance.Begin()
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err)
|
||||
}
|
||||
trans := db.Instance.NewSession()
|
||||
|
||||
res, err := trans.Exec(db.WithContext(ctx, fmt.Sprintf("delete from `group` where id in (%s) and user_id =?", array.Join(allGroupIds, ","))), ctx.UserID)
|
||||
if err != nil {
|
||||
@@ -57,7 +54,10 @@ type id struct {
|
||||
func getAllChildId(ctx *context.Context, rootId int) []int {
|
||||
var ids []id
|
||||
var ret []int
|
||||
db.Instance.Select(&ids, db.WithContext(ctx, "select id from `group` where parent_id=? and user_id=?"), rootId, ctx.UserID)
|
||||
err := db.Instance.Table("group").Where("parent_id=? and user_id=?", rootId, ctx.UserID).Find(&ids)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("getAllChildId err: %v", err)
|
||||
}
|
||||
for _, item := range ids {
|
||||
ret = array.Merge(ret, getAllChildId(ctx, item.Id))
|
||||
ret = append(ret, item.Id)
|
||||
@@ -89,7 +89,7 @@ func MoveMailToGroup(ctx *context.Context, mailId []int, groupId int) bool {
|
||||
func buildChildren(ctx *context.Context, parentId int) []*GroupItem {
|
||||
var ret []*GroupItem
|
||||
var rootGroup []*models.Group
|
||||
err := db.Instance.Select(&rootGroup, db.WithContext(ctx, "select * from `group` where parent_id=? and user_id=?"), parentId, ctx.UserID)
|
||||
err := db.Instance.Table("group").Where("parent_id=? and user_id=?", parentId, ctx.UserID).Find(&rootGroup)
|
||||
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("SQL Error:%v", err)
|
||||
@@ -110,6 +110,6 @@ func buildChildren(ctx *context.Context, parentId int) []*GroupItem {
|
||||
|
||||
func GetGroupList(ctx *context.Context) []*models.Group {
|
||||
var ret []*models.Group
|
||||
db.Instance.Select(&ret, db.WithContext(ctx, "select * from `group` where user_id=?"), ctx.UserID)
|
||||
db.Instance.Table("group").Where("user_id=?", ctx.UserID).Find(&ret)
|
||||
return ret
|
||||
}
|
||||
@@ -9,17 +9,11 @@ import (
|
||||
"pmail/utils/context"
|
||||
)
|
||||
|
||||
func GetEmailList(ctx *context.Context, tag string, keyword string, offset, limit int) (emailList []*models.Email, total int) {
|
||||
func GetEmailList(ctx *context.Context, tag string, keyword string, offset, limit int) (emailList []*models.Email, total int64) {
|
||||
|
||||
querySQL, queryParams := genSQL(ctx, false, tag, keyword, offset, limit)
|
||||
counterSQL, counterParams := genSQL(ctx, true, tag, keyword, offset, limit)
|
||||
querySQL, queryParams := genSQL(ctx, tag, keyword)
|
||||
|
||||
err := db.Instance.Select(&emailList, db.WithContext(ctx, querySQL), queryParams...)
|
||||
if err != nil {
|
||||
log.Errorf("SQL ERROR: %s ,Error:%s", querySQL, err)
|
||||
}
|
||||
|
||||
err = db.Instance.Get(&total, db.WithContext(ctx, counterSQL), counterParams...)
|
||||
total, err := db.Instance.Table("email").Where(querySQL, queryParams...).Desc("id").Limit(limit, offset).FindAndCount(&emailList)
|
||||
if err != nil {
|
||||
log.Errorf("SQL ERROR: %s ,Error:%s", querySQL, err)
|
||||
}
|
||||
@@ -27,12 +21,9 @@ func GetEmailList(ctx *context.Context, tag string, keyword string, offset, limi
|
||||
return
|
||||
}
|
||||
|
||||
func genSQL(ctx *context.Context, counter bool, tag, keyword string, offset, limit int) (string, []any) {
|
||||
func genSQL(ctx *context.Context, tag, keyword string) (string, []any) {
|
||||
|
||||
sql := "select * from email where 1=1 "
|
||||
if counter {
|
||||
sql = "select count(1) from email where 1=1 "
|
||||
}
|
||||
sql := "1=1 "
|
||||
|
||||
sqlParams := []any{}
|
||||
|
||||
@@ -61,8 +52,5 @@ func genSQL(ctx *context.Context, counter bool, tag, keyword string, offset, lim
|
||||
sqlParams = append(sqlParams, "%"+keyword+"%", "%"+keyword+"%")
|
||||
}
|
||||
|
||||
sql += " order by id desc limit ? offset ?"
|
||||
sqlParams = append(sqlParams, limit, offset)
|
||||
|
||||
return sql, sqlParams
|
||||
}
|
||||
@@ -18,9 +18,9 @@ func GetAllRules(ctx *context.Context) []*dto.Rule {
|
||||
var res []*models.Rule
|
||||
var err error
|
||||
if ctx == nil || ctx.UserID == 0 {
|
||||
err = db.Instance.Select(&res, "select * from rule order by sort desc")
|
||||
err = db.Instance.Decr("sort").Find(&res)
|
||||
} else {
|
||||
err = db.Instance.Select(&res, db.WithContext(ctx, "select * from rule where user_id=? order by sort desc"), ctx.UserID)
|
||||
err = db.Instance.Where("user_id=?", ctx.UserID).Decr("sort").Find(&res)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -29,7 +29,7 @@ func GetDatabaseSettings(ctx *context.Context) (string, string, error) {
|
||||
func GetAdminPassword(ctx *context.Context) (string, error) {
|
||||
|
||||
users := []*models.User{}
|
||||
err := db.Instance.Select(&users, "select * from user")
|
||||
err := db.Instance.Find(&users)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user