This commit is contained in:
jinnrry committed 2023-08-27 19:42:00 +08:00
1 parent d18a665c79
commit 5ef5057032
37 files changed
+773 -59

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
package del_email
import (
"fmt"
"pmail/db"
"pmail/dto"
"pmail/models"
"pmail/services/auth"
"pmail/utils/array"
"pmail/utils/errors"
)
func DelEmail(ctx *dto.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, ","))))
for _, email := range emails {
// 检查是否有权限
hasAuth := auth.HasAuth(ctx, email)
if !hasAuth {
return errors.New("No Auth!")
}
}
_, err := db.Instance.Exec(db.WithContext(ctx, fmt.Sprintf("delete from email where id in (%s)", array.Join(ids, ","))))
if err != nil {
return errors.Wrap(err)
}
return nil
}
+114
View File
@@ -0,0 +1,114 @@
package group
import (
"fmt"
log "github.com/sirupsen/logrus"
"pmail/db"
"pmail/dto"
"pmail/models"
"pmail/utils/array"
"pmail/utils/errors"
)
type GroupItem struct {
Id int `json:"id"`
Label string `json:"label"`
Tag string `json:"tag"`
Children []*GroupItem `json:"children"`
}
func DelGroup(ctx *dto.Context, groupId int) (bool, error) {
allGroupIds := getAllChildId(ctx, groupId)
allGroupIds = append(allGroupIds, groupId)
// 开启一个事务
trans, err := db.Instance.Begin()
if err != nil {
return false, errors.Wrap(err)
}
res, err := trans.Exec(db.WithContext(ctx, fmt.Sprintf("delete from `group` where id in (%s) and user_id =?", array.Join(allGroupIds, ","))), ctx.UserInfo.ID)
if err != nil {
trans.Rollback()
return false, errors.Wrap(err)
}
num, err := res.RowsAffected()
if err != nil {
trans.Rollback()
return false, errors.Wrap(err)
}
_, err = trans.Exec(db.WithContext(ctx, fmt.Sprintf("update email set group_id=0 where group_id in (%s)", array.Join(allGroupIds, ","))))
if err != nil {
trans.Rollback()
return false, errors.Wrap(err)
}
trans.Commit()
return num > 0, nil
}
type id struct {
Id int `db:"id"`
}
func getAllChildId(ctx *dto.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.UserInfo.ID)
for _, item := range ids {
ret = array.Merge(ret, getAllChildId(ctx, item.Id))
ret = append(ret, item.Id)
}
return ret
}
// GetGroupInfoList 获取全部的分组
func GetGroupInfoList(ctx *dto.Context) []*GroupItem {
return buildChildren(ctx, 0)
}
// MoveMailToGroup 将某封邮件移动到某个分组中
func MoveMailToGroup(ctx *dto.Context, mailId []int, groupId int) bool {
res, err := db.Instance.Exec(db.WithContext(ctx, fmt.Sprintf("update email set group_id=? where id in (%s)", array.Join(mailId, ","))), groupId)
if err != nil {
log.WithContext(ctx).Errorf("SQL Error:%+v", err)
return false
}
rowNum, err := res.RowsAffected()
if err != nil {
log.WithContext(ctx).Errorf("SQL Error:%+v", err)
return false
}
return rowNum > 0
}
func buildChildren(ctx *dto.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.UserInfo.ID)
if err != nil {
log.WithContext(ctx).Errorf("SQL Error:%v", err)
}
for _, group := range rootGroup {
ret = append(ret, &GroupItem{
Id: group.ID,
Label: group.Name,
Tag: dto.SearchTag{GroupId: group.ID, Status: -1, Type: -1}.ToString(),
Children: buildChildren(ctx, group.ID),
})
}
return ret
}
func GetGroupList(ctx *dto.Context) []*models.Group {
var ret []*models.Group
db.Instance.Select(&ret, db.WithContext(ctx, "select * from `group` where user_id=?"), ctx.UserInfo.ID)
return ret
}
+5
View File
@@ -48,6 +48,11 @@ func genSQL(ctx *dto.Context, counter bool, tag, keyword string, offset, limit i
sqlParams = append(sqlParams, tagInfo.Status)
}
if tagInfo.GroupId != -1 {
sql += " and group_id=? "
sqlParams = append(sqlParams, tagInfo.GroupId)
}
if keyword != "" {
sql += " and (subject like ? or text like ? )"
sqlParams = append(sqlParams, "%"+keyword+"%", "%"+keyword+"%")
+10
View File
@@ -0,0 +1,10 @@
package setup
import (
"testing"
)
func TestSetAdminPassword(t *testing.T) {
SetAdminPassword(nil, "admin", "admin")
}