第一版完成
This commit is contained in:
0
internal/dao/.gitkeep
Normal file
0
internal/dao/.gitkeep
Normal file
95
internal/dao/admin.go
Normal file
95
internal/dao/admin.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"cms-api/internal/model/entity"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
// AdminDao 管理员数据访问对象
|
||||
type AdminDao struct {
|
||||
table string
|
||||
group string
|
||||
columns AdminColumns
|
||||
}
|
||||
|
||||
// AdminColumns 管理员表字段
|
||||
type AdminColumns struct {
|
||||
Id string
|
||||
Username string
|
||||
Password string
|
||||
RealName string
|
||||
Email string
|
||||
Phone string
|
||||
Avatar string
|
||||
RoleId string
|
||||
Status string
|
||||
LastLoginAt string
|
||||
LastLoginIp string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
DeletedAt string
|
||||
}
|
||||
|
||||
// NewAdminDao 创建管理员DAO
|
||||
func NewAdminDao() *AdminDao {
|
||||
return &AdminDao{
|
||||
group: "default",
|
||||
table: "cms_admin",
|
||||
columns: AdminColumns{
|
||||
Id: "id",
|
||||
Username: "username",
|
||||
Password: "password",
|
||||
RealName: "real_name",
|
||||
Email: "email",
|
||||
Phone: "phone",
|
||||
Avatar: "avatar",
|
||||
RoleId: "role_id",
|
||||
Status: "status",
|
||||
LastLoginAt: "last_login_at",
|
||||
LastLoginIp: "last_login_ip",
|
||||
CreatedAt: "created_at",
|
||||
UpdatedAt: "updated_at",
|
||||
DeletedAt: "deleted_at",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Admin 管理员DAO实例
|
||||
var Admin = NewAdminDao()
|
||||
|
||||
// DB 获取数据库连接
|
||||
func (dao *AdminDao) DB() gdb.DB {
|
||||
return g.DB(dao.group)
|
||||
}
|
||||
|
||||
// Ctx 创建上下文查询
|
||||
func (dao *AdminDao) Ctx(ctx context.Context) *gdb.Model {
|
||||
return dao.DB().Model(dao.table).Safe().Ctx(ctx)
|
||||
}
|
||||
|
||||
// GetByUsername 根据用户名获取管理员
|
||||
func (dao *AdminDao) GetByUsername(ctx context.Context, username string) (*entity.Admin, error) {
|
||||
var admin *entity.Admin
|
||||
err := dao.Ctx(ctx).Where(dao.columns.Username, username).Where(dao.columns.DeletedAt, 0).Scan(&admin)
|
||||
return admin, err
|
||||
}
|
||||
|
||||
// GetById 根据ID获取管理员
|
||||
func (dao *AdminDao) GetById(ctx context.Context, id uint64) (*entity.Admin, error) {
|
||||
var admin *entity.Admin
|
||||
err := dao.Ctx(ctx).Where(dao.columns.Id, id).Where(dao.columns.DeletedAt, 0).Scan(&admin)
|
||||
return admin, err
|
||||
}
|
||||
|
||||
// UpdateLastLogin 更新最后登录信息
|
||||
func (dao *AdminDao) UpdateLastLogin(ctx context.Context, id uint64, ip string) error {
|
||||
_, err := dao.Ctx(ctx).Data(g.Map{
|
||||
dao.columns.LastLoginAt: gtime.Now(),
|
||||
dao.columns.LastLoginIp: ip,
|
||||
dao.columns.UpdatedAt: gtime.Now(),
|
||||
}).Where(dao.columns.Id, id).Where(dao.columns.DeletedAt, 0).Update()
|
||||
return err
|
||||
}
|
||||
276
internal/dao/article_methods.go
Normal file
276
internal/dao/article_methods.go
Normal file
@@ -0,0 +1,276 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"cms-api/internal/model"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
// GetById 根据ID获取文章
|
||||
func (dao *ArticleDao) GetById(ctx context.Context, id int) (*model.Article, error) {
|
||||
var article *model.Article
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Scan(&article)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return article, nil
|
||||
}
|
||||
|
||||
// GetBySlug 根据URL别名获取文章
|
||||
func (dao *ArticleDao) GetBySlug(ctx context.Context, slug string) (*model.Article, error) {
|
||||
var article *model.Article
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Slug, slug).Scan(&article)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return article, nil
|
||||
}
|
||||
|
||||
// IncrementViewCount 增加浏览次数
|
||||
func (dao *ArticleDao) IncrementViewCount(ctx context.Context, id int) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Increment(dao.Columns().ViewCount, 1)
|
||||
return err
|
||||
}
|
||||
|
||||
// List 获取文章列表
|
||||
func (dao *ArticleDao) List(ctx context.Context, req *model.ArticleListRequest) ([]*model.Article, int, error) {
|
||||
var (
|
||||
articles []*model.Article
|
||||
total int
|
||||
db = dao.Ctx(ctx)
|
||||
)
|
||||
|
||||
// 添加查询条件
|
||||
if req.CategoryId > 0 {
|
||||
db = db.Where(dao.Columns().CategoryId, req.CategoryId)
|
||||
}
|
||||
if req.AuthorId > 0 {
|
||||
db = db.Where(dao.Columns().AuthorId, req.AuthorId)
|
||||
}
|
||||
if req.IsPublished >= 0 {
|
||||
db = db.Where(dao.Columns().IsPublished, req.IsPublished)
|
||||
}
|
||||
if req.IsFeatured >= 0 {
|
||||
db = db.Where(dao.Columns().IsFeatured, req.IsFeatured)
|
||||
}
|
||||
if req.IsTop >= 0 {
|
||||
db = db.Where(dao.Columns().IsTop, req.IsTop)
|
||||
}
|
||||
if req.Keyword != "" {
|
||||
db = db.Where(dao.Columns().Title+" LIKE ? OR "+dao.Columns().Content+" LIKE ?", "%"+req.Keyword+"%", "%"+req.Keyword+"%")
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
count, err := db.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total = count
|
||||
|
||||
// 分页查询
|
||||
offset := (req.Page - 1) * req.PageSize
|
||||
err = db.Order(dao.Columns().IsTop + " DESC, " + dao.Columns().CreatedAt + " DESC").
|
||||
Limit(req.PageSize).
|
||||
Offset(offset).
|
||||
Scan(&articles)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return articles, total, nil
|
||||
}
|
||||
|
||||
// Create 创建文章
|
||||
func (dao *ArticleDao) Create(ctx context.Context, article *model.Article) (int64, error) {
|
||||
result, err := dao.Ctx(ctx).Data(article).Insert()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// Update 更新文章
|
||||
func (dao *ArticleDao) Update(ctx context.Context, id int, data interface{}) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Data(data).Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete 删除文章
|
||||
func (dao *ArticleDao) Delete(ctx context.Context, id int) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
// GetCount 获取文章总数
|
||||
func (dao *ArticleDao) GetCount(ctx context.Context) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetPublishedCount 获取已发布文章数
|
||||
func (dao *ArticleDao) GetPublishedCount(ctx context.Context) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Where(dao.Columns().IsPublished, 1).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetFeaturedCount 获取推荐文章数
|
||||
func (dao *ArticleDao) GetFeaturedCount(ctx context.Context) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Where(dao.Columns().IsFeatured, 1).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetFeatured 获取推荐文章
|
||||
func (dao *ArticleDao) GetFeatured(ctx context.Context, limit int) ([]*model.Article, error) {
|
||||
var articles []*model.Article
|
||||
err := dao.Ctx(ctx).
|
||||
Where(dao.Columns().IsPublished, 1).
|
||||
Where(dao.Columns().IsFeatured, 1).
|
||||
Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(limit).
|
||||
Scan(&articles)
|
||||
return articles, err
|
||||
}
|
||||
|
||||
// GetLatest 获取最新文章
|
||||
func (dao *ArticleDao) GetLatest(ctx context.Context, limit int) ([]*model.Article, error) {
|
||||
var articles []*model.Article
|
||||
err := dao.Ctx(ctx).
|
||||
Where(dao.Columns().IsPublished, 1).
|
||||
Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(limit).
|
||||
Scan(&articles)
|
||||
return articles, err
|
||||
}
|
||||
|
||||
// Search 搜索文章
|
||||
func (dao *ArticleDao) Search(ctx context.Context, keyword string, page, pageSize int) ([]*model.Article, int, error) {
|
||||
var (
|
||||
articles []*model.Article
|
||||
total int
|
||||
db = dao.Ctx(ctx)
|
||||
)
|
||||
|
||||
// 添加搜索条件
|
||||
if keyword != "" {
|
||||
db = db.Where(dao.Columns().IsPublished, 1).
|
||||
Where(dao.Columns().Title+" LIKE ? OR "+dao.Columns().Content+" LIKE ? OR "+dao.Columns().Summary+" LIKE ?",
|
||||
"%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%")
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
count, err := db.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total = count
|
||||
|
||||
// 分页查询
|
||||
offset := (page - 1) * pageSize
|
||||
err = db.Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(pageSize).
|
||||
Offset(offset).
|
||||
Scan(&articles)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return articles, total, nil
|
||||
}
|
||||
|
||||
// GetByCategory 根据分类获取文章
|
||||
func (dao *ArticleDao) GetByCategory(ctx context.Context, categoryId int, page, pageSize int) ([]*model.Article, int, error) {
|
||||
var (
|
||||
articles []*model.Article
|
||||
total int
|
||||
db = dao.Ctx(ctx)
|
||||
)
|
||||
|
||||
// 添加分类条件
|
||||
db = db.Where(dao.Columns().CategoryId, categoryId).Where(dao.Columns().IsPublished, 1)
|
||||
|
||||
// 获取总数
|
||||
count, err := db.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total = count
|
||||
|
||||
// 分页查询
|
||||
offset := (page - 1) * pageSize
|
||||
err = db.Order(dao.Columns().IsTop + " DESC, " + dao.Columns().CreatedAt + " DESC").
|
||||
Limit(pageSize).
|
||||
Offset(offset).
|
||||
Scan(&articles)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return articles, total, nil
|
||||
}
|
||||
|
||||
// GetByAuthor 根据作者获取文章
|
||||
func (dao *ArticleDao) GetByAuthor(ctx context.Context, authorId int, page, pageSize int) ([]*model.Article, int, error) {
|
||||
var (
|
||||
articles []*model.Article
|
||||
total int
|
||||
db = dao.Ctx(ctx)
|
||||
)
|
||||
|
||||
// 添加作者条件
|
||||
db = db.Where(dao.Columns().AuthorId, authorId).Where(dao.Columns().IsPublished, 1)
|
||||
|
||||
// 获取总数
|
||||
count, err := db.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total = count
|
||||
|
||||
// 分页查询
|
||||
offset := (page - 1) * pageSize
|
||||
err = db.Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(pageSize).
|
||||
Offset(offset).
|
||||
Scan(&articles)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return articles, total, nil
|
||||
}
|
||||
|
||||
// GetTopArticles 获取置顶文章
|
||||
func (dao *ArticleDao) GetTopArticles(ctx context.Context, limit int) ([]*model.Article, error) {
|
||||
var articles []*model.Article
|
||||
err := dao.Ctx(ctx).
|
||||
Where(dao.Columns().IsPublished, 1).
|
||||
Where(dao.Columns().IsTop, 1).
|
||||
Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(limit).
|
||||
Scan(&articles)
|
||||
return articles, err
|
||||
}
|
||||
|
||||
// BatchUpdateStatus 批量更新文章状态
|
||||
func (dao *ArticleDao) BatchUpdateStatus(ctx context.Context, ids []int, isPublished int) error {
|
||||
_, err := dao.Ctx(ctx).
|
||||
Where(dao.Columns().Id+" IN (?)", ids).
|
||||
Data(g.Map{
|
||||
dao.Columns().IsPublished: isPublished,
|
||||
dao.Columns().UpdatedAt: gdb.Raw("NOW()"),
|
||||
}).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// BatchDelete 批量删除文章
|
||||
func (dao *ArticleDao) BatchDelete(ctx context.Context, ids []int) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id+" IN (?)", ids).Delete()
|
||||
return err
|
||||
}
|
||||
165
internal/dao/attachment_methods.go
Normal file
165
internal/dao/attachment_methods.go
Normal file
@@ -0,0 +1,165 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"cms-api/internal/model"
|
||||
)
|
||||
|
||||
// Create 创建附件
|
||||
func (dao *AttachmentDao) Create(ctx context.Context, attachment *model.Attachment) (int64, error) {
|
||||
result, err := dao.Ctx(ctx).Data(attachment).Insert()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// GetById 根据ID获取附件
|
||||
func (dao *AttachmentDao) GetById(ctx context.Context, id int) (*model.Attachment, error) {
|
||||
var attachment *model.Attachment
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Scan(&attachment)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
// List 获取附件列表
|
||||
func (dao *AttachmentDao) List(ctx context.Context, req *model.AttachmentListRequest) ([]*model.Attachment, int, error) {
|
||||
var (
|
||||
attachments []*model.Attachment
|
||||
total int
|
||||
db = dao.Ctx(ctx)
|
||||
)
|
||||
|
||||
// 添加查询条件
|
||||
if req.FileType != "" {
|
||||
db = db.Where(dao.Columns().FileType, req.FileType)
|
||||
}
|
||||
if req.UploadedBy > 0 {
|
||||
db = db.Where(dao.Columns().UploadedBy, req.UploadedBy)
|
||||
}
|
||||
if req.StorageType != "" {
|
||||
db = db.Where(dao.Columns().StorageType, req.StorageType)
|
||||
}
|
||||
if req.Keyword != "" {
|
||||
db = db.Where(dao.Columns().OriginalName+" LIKE ? OR "+dao.Columns().FileName+" LIKE ?", "%"+req.Keyword+"%", "%"+req.Keyword+"%")
|
||||
}
|
||||
if req.StartDate != "" {
|
||||
db = db.Where(dao.Columns().CreatedAt+" >= ?", req.StartDate)
|
||||
}
|
||||
if req.EndDate != "" {
|
||||
db = db.Where(dao.Columns().CreatedAt+" <= ?", req.EndDate)
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
count, err := db.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total = count
|
||||
|
||||
// 分页查询
|
||||
offset := (req.Page - 1) * req.PageSize
|
||||
err = db.Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(req.PageSize).
|
||||
Offset(offset).
|
||||
Scan(&attachments)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return attachments, total, nil
|
||||
}
|
||||
|
||||
// Delete 删除附件
|
||||
func (dao *AttachmentDao) Delete(ctx context.Context, id int) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
// IncrementUsageCount 增加使用次数
|
||||
func (dao *AttachmentDao) IncrementUsageCount(ctx context.Context, id int) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Increment(dao.Columns().UsageCount, 1)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetCount 获取附件总数
|
||||
func (dao *AttachmentDao) GetCount(ctx context.Context) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetTotalSize 获取附件总大小
|
||||
func (dao *AttachmentDao) GetTotalSize(ctx context.Context) (int64, error) {
|
||||
var totalSize int64
|
||||
err := dao.Ctx(ctx).Fields("SUM(" + dao.Columns().FileSize + ") as total_size").Scan(&totalSize)
|
||||
return totalSize, err
|
||||
}
|
||||
|
||||
// GetCountByType 根据类型获取附件数量
|
||||
func (dao *AttachmentDao) GetCountByType(ctx context.Context, fileType string) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Where(dao.Columns().FileType, fileType).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// BatchDelete 批量删除附件
|
||||
func (dao *AttachmentDao) BatchDelete(ctx context.Context, ids []int) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id+" IN (?)", ids).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
// GetByFileName 根据文件名获取附件
|
||||
func (dao *AttachmentDao) GetByFileName(ctx context.Context, fileName string) (*model.Attachment, error) {
|
||||
var attachment *model.Attachment
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().FileName, fileName).Scan(&attachment)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
// GetByUploader 根据上传者获取附件列表
|
||||
func (dao *AttachmentDao) GetByUploader(ctx context.Context, uploaderId int, page, pageSize int) ([]*model.Attachment, int, error) {
|
||||
var (
|
||||
attachments []*model.Attachment
|
||||
total int
|
||||
db = dao.Ctx(ctx)
|
||||
)
|
||||
|
||||
// 添加上传者条件
|
||||
db = db.Where(dao.Columns().UploadedBy, uploaderId)
|
||||
|
||||
// 获取总数
|
||||
count, err := db.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total = count
|
||||
|
||||
// 分页查询
|
||||
offset := (page - 1) * pageSize
|
||||
err = db.Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(pageSize).
|
||||
Offset(offset).
|
||||
Scan(&attachments)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return attachments, total, nil
|
||||
}
|
||||
|
||||
// GetByIds 根据ID列表获取附件
|
||||
func (dao *AttachmentDao) GetByIds(ctx context.Context, ids []int) ([]*model.Attachment, error) {
|
||||
var attachments []*model.Attachment
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Id+" IN (?)", ids).Scan(&attachments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return attachments, nil
|
||||
}
|
||||
161
internal/dao/config_methods.go
Normal file
161
internal/dao/config_methods.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"cms-api/internal/model"
|
||||
)
|
||||
|
||||
// GetByKey 根据配置键获取配置
|
||||
func (dao *ConfigDao) GetByKey(ctx context.Context, key string) (*model.Config, error) {
|
||||
var config *model.Config
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().ConfigKey, key).Scan(&config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// List 获取配置列表
|
||||
func (dao *ConfigDao) List(ctx context.Context, req *model.ConfigListRequest) ([]*model.Config, int, error) {
|
||||
var (
|
||||
configs []*model.Config
|
||||
total int
|
||||
db = dao.Ctx(ctx)
|
||||
)
|
||||
|
||||
// 添加查询条件
|
||||
if req.GroupName != "" {
|
||||
db = db.Where(dao.Columns().GroupName, req.GroupName)
|
||||
}
|
||||
if req.IsSystem >= 0 {
|
||||
db = db.Where(dao.Columns().IsSystem, req.IsSystem)
|
||||
}
|
||||
if req.Keyword != "" {
|
||||
db = db.Where(dao.Columns().ConfigKey+" LIKE ? OR "+dao.Columns().Description+" LIKE ?", "%"+req.Keyword+"%", "%"+req.Keyword+"%")
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
count, err := db.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total = count
|
||||
|
||||
// 分页查询
|
||||
offset := (req.Page - 1) * req.PageSize
|
||||
err = db.Order(dao.Columns().GroupName + " ASC, " + dao.Columns().SortOrder + " ASC").
|
||||
Limit(req.PageSize).
|
||||
Offset(offset).
|
||||
Scan(&configs)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return configs, total, nil
|
||||
}
|
||||
|
||||
// GetByGroup 根据分组获取配置
|
||||
func (dao *ConfigDao) GetByGroup(ctx context.Context, groupName string) ([]*model.Config, error) {
|
||||
var configs []*model.Config
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().GroupName, groupName).Order(dao.Columns().SortOrder + " ASC").Scan(&configs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return configs, nil
|
||||
}
|
||||
|
||||
// GetGroups 获取所有配置分组
|
||||
func (dao *ConfigDao) GetGroups(ctx context.Context) ([]string, error) {
|
||||
var groups []string
|
||||
err := dao.Ctx(ctx).Fields("DISTINCT " + dao.Columns().GroupName).Scan(&groups)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return groups, nil
|
||||
}
|
||||
|
||||
// Create 创建配置
|
||||
func (dao *ConfigDao) Create(ctx context.Context, config *model.Config) (int64, error) {
|
||||
result, err := dao.Ctx(ctx).Data(config).Insert()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// Update 更新配置 - 根据ID更新
|
||||
func (dao *ConfigDao) Update(ctx context.Context, id int, data interface{}) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Data(data).Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateByKey 更新配置 - 根据配置键更新
|
||||
func (dao *ConfigDao) UpdateByKey(ctx context.Context, key string, data interface{}) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().ConfigKey, key).Data(data).Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateValue 更新配置值
|
||||
func (dao *ConfigDao) UpdateValue(ctx context.Context, key string, value string) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().ConfigKey, key).Data(map[string]interface{}{
|
||||
dao.Columns().ConfigValue: value,
|
||||
}).Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete 删除配置 - 根据ID删除
|
||||
func (dao *ConfigDao) Delete(ctx context.Context, id int) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteByKey 删除配置 - 根据配置键删除
|
||||
func (dao *ConfigDao) DeleteByKey(ctx context.Context, key string) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().ConfigKey, key).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
// GetById 根据ID获取配置
|
||||
func (dao *ConfigDao) GetById(ctx context.Context, id int) (*model.Config, error) {
|
||||
var config *model.Config
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Scan(&config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// BatchUpdate 批量更新配置
|
||||
func (dao *ConfigDao) BatchUpdate(ctx context.Context, configs map[string]string) error {
|
||||
for key, value := range configs {
|
||||
err := dao.UpdateValue(ctx, key, value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSystemConfigs 获取系统配置
|
||||
func (dao *ConfigDao) GetSystemConfigs(ctx context.Context) ([]*model.Config, error) {
|
||||
var configs []*model.Config
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().IsSystem, 1).Order(dao.Columns().GroupName + " ASC, " + dao.Columns().SortOrder + " ASC").Scan(&configs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return configs, nil
|
||||
}
|
||||
|
||||
// GetUserConfigs 获取用户配置
|
||||
func (dao *ConfigDao) GetUserConfigs(ctx context.Context) ([]*model.Config, error) {
|
||||
var configs []*model.Config
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().IsSystem, 0).Order(dao.Columns().GroupName + " ASC, " + dao.Columns().SortOrder + " ASC").Scan(&configs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return configs, nil
|
||||
}
|
||||
177
internal/dao/contact_methods.go
Normal file
177
internal/dao/contact_methods.go
Normal file
@@ -0,0 +1,177 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"cms-api/internal/model"
|
||||
)
|
||||
|
||||
// GetById 根据ID获取联系信息
|
||||
func (dao *ContactDao) GetById(ctx context.Context, id int) (*model.Contact, error) {
|
||||
var contact *model.Contact
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Scan(&contact)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return contact, nil
|
||||
}
|
||||
|
||||
// List 获取联系信息列表
|
||||
func (dao *ContactDao) List(ctx context.Context, req *model.ContactListRequest) ([]*model.Contact, int, error) {
|
||||
var (
|
||||
contacts []*model.Contact
|
||||
total int
|
||||
db = dao.Ctx(ctx)
|
||||
)
|
||||
|
||||
// 添加查询条件
|
||||
if req.Status >= 0 {
|
||||
db = db.Where(dao.Columns().Status, req.Status)
|
||||
}
|
||||
if req.Keyword != "" {
|
||||
db = db.Where(dao.Columns().Name+" LIKE ? OR "+dao.Columns().Email+" LIKE ? OR "+dao.Columns().Subject+" LIKE ?",
|
||||
"%"+req.Keyword+"%", "%"+req.Keyword+"%", "%"+req.Keyword+"%")
|
||||
}
|
||||
if req.StartDate != "" {
|
||||
db = db.Where(dao.Columns().CreatedAt+" >= ?", req.StartDate)
|
||||
}
|
||||
if req.EndDate != "" {
|
||||
db = db.Where(dao.Columns().CreatedAt+" <= ?", req.EndDate)
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
count, err := db.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total = count
|
||||
|
||||
// 分页查询
|
||||
offset := (req.Page - 1) * req.PageSize
|
||||
err = db.Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(req.PageSize).
|
||||
Offset(offset).
|
||||
Scan(&contacts)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return contacts, total, nil
|
||||
}
|
||||
|
||||
// Create 创建联系信息
|
||||
func (dao *ContactDao) Create(ctx context.Context, contact *model.Contact) (int64, error) {
|
||||
result, err := dao.Ctx(ctx).Data(contact).Insert()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// Update 更新联系信息
|
||||
func (dao *ContactDao) Update(ctx context.Context, id int, data interface{}) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Data(data).Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete 删除联系信息
|
||||
func (dao *ContactDao) Delete(ctx context.Context, id int) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
// GetCount 获取联系信息总数
|
||||
func (dao *ContactDao) GetCount(ctx context.Context) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetCountByStatus 根据状态获取联系信息数
|
||||
func (dao *ContactDao) GetCountByStatus(ctx context.Context, status int) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Where(dao.Columns().Status, status).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetTodayCount 获取今日新增联系信息数
|
||||
func (dao *ContactDao) GetTodayCount(ctx context.Context) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Where("DATE("+dao.Columns().CreatedAt+") = CURDATE()").Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetWeekCount 获取本周新增联系信息数
|
||||
func (dao *ContactDao) GetWeekCount(ctx context.Context) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Where("YEARWEEK("+dao.Columns().CreatedAt+") = YEARWEEK(NOW())").Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetMonthCount 获取本月新增联系信息数
|
||||
func (dao *ContactDao) GetMonthCount(ctx context.Context) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Where("YEAR("+dao.Columns().CreatedAt+") = YEAR(NOW()) AND MONTH("+dao.Columns().CreatedAt+") = MONTH(NOW())").Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetLatest 获取最新联系信息
|
||||
func (dao *ContactDao) GetLatest(ctx context.Context, limit int) ([]*model.Contact, error) {
|
||||
var contacts []*model.Contact
|
||||
err := dao.Ctx(ctx).
|
||||
Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(limit).
|
||||
Scan(&contacts)
|
||||
return contacts, err
|
||||
}
|
||||
|
||||
// GetUnprocessed 获取未处理的联系信息
|
||||
func (dao *ContactDao) GetUnprocessed(ctx context.Context, limit int) ([]*model.Contact, error) {
|
||||
var contacts []*model.Contact
|
||||
err := dao.Ctx(ctx).
|
||||
Where(dao.Columns().Status, 0).
|
||||
Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(limit).
|
||||
Scan(&contacts)
|
||||
return contacts, err
|
||||
}
|
||||
|
||||
// Search 搜索联系信息
|
||||
func (dao *ContactDao) Search(ctx context.Context, keyword string, page, pageSize int) ([]*model.Contact, int, error) {
|
||||
var (
|
||||
contacts []*model.Contact
|
||||
total int
|
||||
db = dao.Ctx(ctx)
|
||||
)
|
||||
|
||||
// 添加搜索条件
|
||||
if keyword != "" {
|
||||
db = db.Where(dao.Columns().Name+" LIKE ? OR "+dao.Columns().Email+" LIKE ? OR "+dao.Columns().Subject+" LIKE ? OR "+dao.Columns().Message+" LIKE ?",
|
||||
"%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%")
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
count, err := db.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total = count
|
||||
|
||||
// 分页查询
|
||||
offset := (page - 1) * pageSize
|
||||
err = db.Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(pageSize).
|
||||
Offset(offset).
|
||||
Scan(&contacts)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return contacts, total, nil
|
||||
}
|
||||
|
||||
// GetTrends 获取联系信息趋势数据
|
||||
func (dao *ContactDao) GetTrends(ctx context.Context, days int) ([]map[string]interface{}, error) {
|
||||
var trends []map[string]interface{}
|
||||
// 这里应该实现趋势数据查询逻辑
|
||||
// 暂时返回空数组,实际项目中需要根据具体需求实现
|
||||
return trends, nil
|
||||
}
|
||||
571
internal/dao/dao.go
Normal file
571
internal/dao/dao.go
Normal file
@@ -0,0 +1,571 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
// 数据访问对象实例
|
||||
var (
|
||||
User = NewUserDao()
|
||||
Role = NewRoleDao()
|
||||
Article = NewArticleDao()
|
||||
News = NewNewsDao()
|
||||
Attachment = NewAttachmentDao()
|
||||
Partner = NewPartnerDao()
|
||||
Contact = NewContactDao()
|
||||
Config = NewConfigDao()
|
||||
)
|
||||
|
||||
// UserDao 用户数据访问对象
|
||||
type UserDao struct {
|
||||
table string
|
||||
group string
|
||||
columns UserColumns
|
||||
}
|
||||
|
||||
type UserColumns struct {
|
||||
Id string
|
||||
Account string
|
||||
NickName string
|
||||
Avatar string
|
||||
Email string
|
||||
Password string
|
||||
Balance string
|
||||
QrCode string
|
||||
RoleId string
|
||||
IsSysNotifications string
|
||||
IsCollectionNotifications string
|
||||
IsMarketingNotifications string
|
||||
Ip string
|
||||
IpTable string
|
||||
Status string
|
||||
LastResetPasswordAt string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
DeletedAt string
|
||||
}
|
||||
|
||||
func NewUserDao() *UserDao {
|
||||
return &UserDao{
|
||||
group: "default",
|
||||
table: "users",
|
||||
columns: UserColumns{
|
||||
Id: "id",
|
||||
Account: "account",
|
||||
NickName: "nick_name",
|
||||
Avatar: "avatar",
|
||||
Email: "email",
|
||||
Password: "password",
|
||||
Balance: "balance",
|
||||
QrCode: "qr_code",
|
||||
RoleId: "role_id",
|
||||
IsSysNotifications: "is_sys_notifications",
|
||||
IsCollectionNotifications: "is_collection_notifications",
|
||||
IsMarketingNotifications: "is_marketing_notifications",
|
||||
Ip: "ip",
|
||||
IpTable: "ip_table",
|
||||
Status: "status",
|
||||
LastResetPasswordAt: "last_reset_password_at",
|
||||
CreatedAt: "created_at",
|
||||
UpdatedAt: "updated_at",
|
||||
DeletedAt: "deleted_at",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (dao *UserDao) DB() gdb.DB {
|
||||
return g.DB(dao.group)
|
||||
}
|
||||
|
||||
func (dao *UserDao) Table() string {
|
||||
return dao.table
|
||||
}
|
||||
|
||||
func (dao *UserDao) Columns() UserColumns {
|
||||
return dao.columns
|
||||
}
|
||||
|
||||
func (dao *UserDao) Group() string {
|
||||
return dao.group
|
||||
}
|
||||
|
||||
func (dao *UserDao) Ctx(ctx context.Context) *gdb.Model {
|
||||
return dao.DB().Model(dao.table).Safe().Ctx(ctx)
|
||||
}
|
||||
|
||||
// RoleDao 角色数据访问对象
|
||||
type RoleDao struct {
|
||||
table string
|
||||
group string
|
||||
columns RoleColumns
|
||||
}
|
||||
|
||||
type RoleColumns struct {
|
||||
Id string
|
||||
Name string
|
||||
Description string
|
||||
Permissions string
|
||||
Status string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
DeletedAt string
|
||||
}
|
||||
|
||||
func NewRoleDao() *RoleDao {
|
||||
return &RoleDao{
|
||||
group: "default",
|
||||
table: "roles",
|
||||
columns: RoleColumns{
|
||||
Id: "id",
|
||||
Name: "name",
|
||||
Description: "description",
|
||||
Permissions: "permissions",
|
||||
Status: "status",
|
||||
CreatedAt: "created_at",
|
||||
UpdatedAt: "updated_at",
|
||||
DeletedAt: "deleted_at",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (dao *RoleDao) DB() gdb.DB {
|
||||
return g.DB(dao.group)
|
||||
}
|
||||
|
||||
func (dao *RoleDao) Table() string {
|
||||
return dao.table
|
||||
}
|
||||
|
||||
func (dao *RoleDao) Columns() RoleColumns {
|
||||
return dao.columns
|
||||
}
|
||||
|
||||
func (dao *RoleDao) Group() string {
|
||||
return dao.group
|
||||
}
|
||||
|
||||
func (dao *RoleDao) Ctx(ctx context.Context) *gdb.Model {
|
||||
return dao.DB().Model(dao.table).Safe().Ctx(ctx)
|
||||
}
|
||||
|
||||
// ArticleDao 文章数据访问对象
|
||||
type ArticleDao struct {
|
||||
table string
|
||||
group string
|
||||
columns ArticleColumns
|
||||
}
|
||||
|
||||
type ArticleColumns struct {
|
||||
Id string
|
||||
Title string
|
||||
Slug string
|
||||
Summary string
|
||||
Content string
|
||||
HtmlContent string
|
||||
CoverImage string
|
||||
CategoryId string
|
||||
Tags string
|
||||
AuthorId string
|
||||
ViewCount string
|
||||
LikeCount string
|
||||
CommentCount string
|
||||
IsPublished string
|
||||
IsFeatured string
|
||||
IsTop string
|
||||
SeoTitle string
|
||||
SeoDescription string
|
||||
SeoKeywords string
|
||||
PublishedAt string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
DeletedAt string
|
||||
}
|
||||
|
||||
func NewArticleDao() *ArticleDao {
|
||||
return &ArticleDao{
|
||||
group: "default",
|
||||
table: "articles",
|
||||
columns: ArticleColumns{
|
||||
Id: "id",
|
||||
Title: "title",
|
||||
Slug: "slug",
|
||||
Summary: "summary",
|
||||
Content: "content",
|
||||
HtmlContent: "html_content",
|
||||
CoverImage: "cover_image",
|
||||
CategoryId: "category_id",
|
||||
Tags: "tags",
|
||||
AuthorId: "author_id",
|
||||
ViewCount: "view_count",
|
||||
LikeCount: "like_count",
|
||||
CommentCount: "comment_count",
|
||||
IsPublished: "is_published",
|
||||
IsFeatured: "is_featured",
|
||||
IsTop: "is_top",
|
||||
SeoTitle: "seo_title",
|
||||
SeoDescription: "seo_description",
|
||||
SeoKeywords: "seo_keywords",
|
||||
PublishedAt: "published_at",
|
||||
CreatedAt: "created_at",
|
||||
UpdatedAt: "updated_at",
|
||||
DeletedAt: "deleted_at",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (dao *ArticleDao) DB() gdb.DB {
|
||||
return g.DB(dao.group)
|
||||
}
|
||||
|
||||
func (dao *ArticleDao) Table() string {
|
||||
return dao.table
|
||||
}
|
||||
|
||||
func (dao *ArticleDao) Columns() ArticleColumns {
|
||||
return dao.columns
|
||||
}
|
||||
|
||||
func (dao *ArticleDao) Group() string {
|
||||
return dao.group
|
||||
}
|
||||
|
||||
func (dao *ArticleDao) Ctx(ctx context.Context) *gdb.Model {
|
||||
return dao.DB().Model(dao.table).Safe().Ctx(ctx)
|
||||
}
|
||||
|
||||
// NewsDao 新闻数据访问对象
|
||||
type NewsDao struct {
|
||||
table string
|
||||
group string
|
||||
columns NewsColumns
|
||||
}
|
||||
|
||||
type NewsColumns struct {
|
||||
Id string
|
||||
Title string
|
||||
Slug string
|
||||
Summary string
|
||||
Content string
|
||||
CoverImage string
|
||||
Category string
|
||||
Source string
|
||||
Author string
|
||||
ViewCount string
|
||||
IsPublished string
|
||||
IsFeatured string
|
||||
IsTop string
|
||||
PublishedAt string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
DeletedAt string
|
||||
}
|
||||
|
||||
func NewNewsDao() *NewsDao {
|
||||
return &NewsDao{
|
||||
group: "default",
|
||||
table: "news",
|
||||
columns: NewsColumns{
|
||||
Id: "id",
|
||||
Title: "title",
|
||||
Slug: "slug",
|
||||
Summary: "summary",
|
||||
Content: "content",
|
||||
CoverImage: "cover_image",
|
||||
Category: "category",
|
||||
Source: "source",
|
||||
Author: "author",
|
||||
ViewCount: "view_count",
|
||||
IsPublished: "is_published",
|
||||
IsFeatured: "is_featured",
|
||||
IsTop: "is_top",
|
||||
PublishedAt: "published_at",
|
||||
CreatedAt: "created_at",
|
||||
UpdatedAt: "updated_at",
|
||||
DeletedAt: "deleted_at",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (dao *NewsDao) DB() gdb.DB {
|
||||
return g.DB(dao.group)
|
||||
}
|
||||
|
||||
func (dao *NewsDao) Table() string {
|
||||
return dao.table
|
||||
}
|
||||
|
||||
func (dao *NewsDao) Columns() NewsColumns {
|
||||
return dao.columns
|
||||
}
|
||||
|
||||
func (dao *NewsDao) Group() string {
|
||||
return dao.group
|
||||
}
|
||||
|
||||
func (dao *NewsDao) Ctx(ctx context.Context) *gdb.Model {
|
||||
return dao.DB().Model(dao.table).Safe().Ctx(ctx)
|
||||
}
|
||||
|
||||
// AttachmentDao 附件数据访问对象
|
||||
type AttachmentDao struct {
|
||||
table string
|
||||
group string
|
||||
columns AttachmentColumns
|
||||
}
|
||||
|
||||
type AttachmentColumns struct {
|
||||
Id string
|
||||
OriginalName string
|
||||
FileName string
|
||||
FilePath string
|
||||
FileUrl string
|
||||
FileSize string
|
||||
FileType string
|
||||
MimeType string
|
||||
FileExt string
|
||||
StorageType string
|
||||
UploadIp string
|
||||
UploadedBy string
|
||||
UsageCount string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
DeletedAt string
|
||||
}
|
||||
|
||||
func NewAttachmentDao() *AttachmentDao {
|
||||
return &AttachmentDao{
|
||||
group: "default",
|
||||
table: "attachments",
|
||||
columns: AttachmentColumns{
|
||||
Id: "id",
|
||||
OriginalName: "original_name",
|
||||
FileName: "file_name",
|
||||
FilePath: "file_path",
|
||||
FileUrl: "file_url",
|
||||
FileSize: "file_size",
|
||||
FileType: "file_type",
|
||||
MimeType: "mime_type",
|
||||
FileExt: "file_ext",
|
||||
StorageType: "storage_type",
|
||||
UploadIp: "upload_ip",
|
||||
UploadedBy: "uploaded_by",
|
||||
UsageCount: "usage_count",
|
||||
CreatedAt: "created_at",
|
||||
UpdatedAt: "updated_at",
|
||||
DeletedAt: "deleted_at",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (dao *AttachmentDao) DB() gdb.DB {
|
||||
return g.DB(dao.group)
|
||||
}
|
||||
|
||||
func (dao *AttachmentDao) Table() string {
|
||||
return dao.table
|
||||
}
|
||||
|
||||
func (dao *AttachmentDao) Columns() AttachmentColumns {
|
||||
return dao.columns
|
||||
}
|
||||
|
||||
func (dao *AttachmentDao) Group() string {
|
||||
return dao.group
|
||||
}
|
||||
|
||||
func (dao *AttachmentDao) Ctx(ctx context.Context) *gdb.Model {
|
||||
return dao.DB().Model(dao.table).Safe().Ctx(ctx)
|
||||
}
|
||||
|
||||
// PartnerDao 合作伙伴数据访问对象
|
||||
type PartnerDao struct {
|
||||
table string
|
||||
group string
|
||||
columns PartnerColumns
|
||||
}
|
||||
|
||||
type PartnerColumns struct {
|
||||
Id string
|
||||
Name string
|
||||
Logo string
|
||||
Website string
|
||||
Description string
|
||||
Category string
|
||||
SortOrder string
|
||||
IsFeatured string
|
||||
Status string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
DeletedAt string
|
||||
}
|
||||
|
||||
func NewPartnerDao() *PartnerDao {
|
||||
return &PartnerDao{
|
||||
group: "default",
|
||||
table: "partners",
|
||||
columns: PartnerColumns{
|
||||
Id: "id",
|
||||
Name: "name",
|
||||
Logo: "logo",
|
||||
Website: "website",
|
||||
Description: "description",
|
||||
Category: "category",
|
||||
SortOrder: "sort_order",
|
||||
IsFeatured: "is_featured",
|
||||
Status: "status",
|
||||
CreatedAt: "created_at",
|
||||
UpdatedAt: "updated_at",
|
||||
DeletedAt: "deleted_at",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (dao *PartnerDao) DB() gdb.DB {
|
||||
return g.DB(dao.group)
|
||||
}
|
||||
|
||||
func (dao *PartnerDao) Table() string {
|
||||
return dao.table
|
||||
}
|
||||
|
||||
func (dao *PartnerDao) Columns() PartnerColumns {
|
||||
return dao.columns
|
||||
}
|
||||
|
||||
func (dao *PartnerDao) Group() string {
|
||||
return dao.group
|
||||
}
|
||||
|
||||
func (dao *PartnerDao) Ctx(ctx context.Context) *gdb.Model {
|
||||
return dao.DB().Model(dao.table).Safe().Ctx(ctx)
|
||||
}
|
||||
|
||||
// ContactDao 联系我们数据访问对象
|
||||
type ContactDao struct {
|
||||
table string
|
||||
group string
|
||||
columns ContactColumns
|
||||
}
|
||||
|
||||
type ContactColumns struct {
|
||||
Id string
|
||||
Name string
|
||||
Email string
|
||||
Phone string
|
||||
Company string
|
||||
Subject string
|
||||
Message string
|
||||
Ip string
|
||||
UserAgent string
|
||||
Status string
|
||||
Reply string
|
||||
RepliedAt string
|
||||
RepliedBy string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
func NewContactDao() *ContactDao {
|
||||
return &ContactDao{
|
||||
group: "default",
|
||||
table: "contacts",
|
||||
columns: ContactColumns{
|
||||
Id: "id",
|
||||
Name: "name",
|
||||
Email: "email",
|
||||
Phone: "phone",
|
||||
Company: "company",
|
||||
Subject: "subject",
|
||||
Message: "message",
|
||||
Ip: "ip",
|
||||
UserAgent: "user_agent",
|
||||
Status: "status",
|
||||
Reply: "reply",
|
||||
RepliedAt: "replied_at",
|
||||
RepliedBy: "replied_by",
|
||||
CreatedAt: "created_at",
|
||||
UpdatedAt: "updated_at",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (dao *ContactDao) DB() gdb.DB {
|
||||
return g.DB(dao.group)
|
||||
}
|
||||
|
||||
func (dao *ContactDao) Table() string {
|
||||
return dao.table
|
||||
}
|
||||
|
||||
func (dao *ContactDao) Columns() ContactColumns {
|
||||
return dao.columns
|
||||
}
|
||||
|
||||
func (dao *ContactDao) Group() string {
|
||||
return dao.group
|
||||
}
|
||||
|
||||
func (dao *ContactDao) Ctx(ctx context.Context) *gdb.Model {
|
||||
return dao.DB().Model(dao.table).Safe().Ctx(ctx)
|
||||
}
|
||||
|
||||
// ConfigDao 配置数据访问对象
|
||||
type ConfigDao struct {
|
||||
table string
|
||||
group string
|
||||
columns ConfigColumns
|
||||
}
|
||||
|
||||
type ConfigColumns struct {
|
||||
Id string
|
||||
ConfigKey string
|
||||
ConfigValue string
|
||||
ConfigType string
|
||||
GroupName string
|
||||
Description string
|
||||
SortOrder string
|
||||
IsSystem string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
func NewConfigDao() *ConfigDao {
|
||||
return &ConfigDao{
|
||||
group: "default",
|
||||
table: "site_configs",
|
||||
columns: ConfigColumns{
|
||||
Id: "id",
|
||||
ConfigKey: "config_key",
|
||||
ConfigValue: "config_value",
|
||||
ConfigType: "config_type",
|
||||
GroupName: "group_name",
|
||||
Description: "description",
|
||||
SortOrder: "sort_order",
|
||||
IsSystem: "is_system",
|
||||
CreatedAt: "created_at",
|
||||
UpdatedAt: "updated_at",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (dao *ConfigDao) DB() gdb.DB {
|
||||
return g.DB(dao.group)
|
||||
}
|
||||
|
||||
func (dao *ConfigDao) Table() string {
|
||||
return dao.table
|
||||
}
|
||||
|
||||
func (dao *ConfigDao) Columns() ConfigColumns {
|
||||
return dao.columns
|
||||
}
|
||||
|
||||
func (dao *ConfigDao) Group() string {
|
||||
return dao.group
|
||||
}
|
||||
|
||||
func (dao *ConfigDao) Ctx(ctx context.Context) *gdb.Model {
|
||||
return dao.DB().Model(dao.table).Safe().Ctx(ctx)
|
||||
}
|
||||
227
internal/dao/news_methods.go
Normal file
227
internal/dao/news_methods.go
Normal file
@@ -0,0 +1,227 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"cms-api/internal/model"
|
||||
)
|
||||
|
||||
// GetById 根据ID获取新闻
|
||||
func (dao *NewsDao) GetById(ctx context.Context, id int) (*model.News, error) {
|
||||
var news *model.News
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Scan(&news)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return news, nil
|
||||
}
|
||||
|
||||
// GetBySlug 根据URL别名获取新闻
|
||||
func (dao *NewsDao) GetBySlug(ctx context.Context, slug string) (*model.News, error) {
|
||||
var news *model.News
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Slug, slug).Scan(&news)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return news, nil
|
||||
}
|
||||
|
||||
// IncrementViewCount 增加浏览次数
|
||||
func (dao *NewsDao) IncrementViewCount(ctx context.Context, id int) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Increment(dao.Columns().ViewCount, 1)
|
||||
return err
|
||||
}
|
||||
|
||||
// List 获取新闻列表
|
||||
func (dao *NewsDao) List(ctx context.Context, req *model.NewsListRequest) ([]*model.News, int, error) {
|
||||
var (
|
||||
newsList []*model.News
|
||||
total int
|
||||
db = dao.Ctx(ctx)
|
||||
)
|
||||
|
||||
// 添加查询条件
|
||||
if req.Category != "" {
|
||||
db = db.Where(dao.Columns().Category, req.Category)
|
||||
}
|
||||
if req.Author != "" {
|
||||
db = db.Where(dao.Columns().Author, req.Author)
|
||||
}
|
||||
if req.Source != "" {
|
||||
db = db.Where(dao.Columns().Source, req.Source)
|
||||
}
|
||||
if req.IsPublished >= 0 {
|
||||
db = db.Where(dao.Columns().IsPublished, req.IsPublished)
|
||||
}
|
||||
if req.IsFeatured >= 0 {
|
||||
db = db.Where(dao.Columns().IsFeatured, req.IsFeatured)
|
||||
}
|
||||
if req.IsTop >= 0 {
|
||||
db = db.Where(dao.Columns().IsTop, req.IsTop)
|
||||
}
|
||||
if req.Keyword != "" {
|
||||
db = db.Where(dao.Columns().Title+" LIKE ? OR "+dao.Columns().Content+" LIKE ?", "%"+req.Keyword+"%", "%"+req.Keyword+"%")
|
||||
}
|
||||
if req.StartDate != "" {
|
||||
db = db.Where(dao.Columns().CreatedAt+" >= ?", req.StartDate)
|
||||
}
|
||||
if req.EndDate != "" {
|
||||
db = db.Where(dao.Columns().CreatedAt+" <= ?", req.EndDate)
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
count, err := db.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total = count
|
||||
|
||||
// 分页查询
|
||||
offset := (req.Page - 1) * req.PageSize
|
||||
err = db.Order(dao.Columns().IsTop + " DESC, " + dao.Columns().CreatedAt + " DESC").
|
||||
Limit(req.PageSize).
|
||||
Offset(offset).
|
||||
Scan(&newsList)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return newsList, total, nil
|
||||
}
|
||||
|
||||
// Create 创建新闻
|
||||
func (dao *NewsDao) Create(ctx context.Context, news *model.News) (int64, error) {
|
||||
result, err := dao.Ctx(ctx).Data(news).Insert()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// Update 更新新闻
|
||||
func (dao *NewsDao) Update(ctx context.Context, id int, data interface{}) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Data(data).Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete 删除新闻
|
||||
func (dao *NewsDao) Delete(ctx context.Context, id int) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
// GetCount 获取新闻总数
|
||||
func (dao *NewsDao) GetCount(ctx context.Context) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetPublishedCount 获取已发布新闻数
|
||||
func (dao *NewsDao) GetPublishedCount(ctx context.Context) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Where(dao.Columns().IsPublished, 1).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetFeaturedCount 获取推荐新闻数
|
||||
func (dao *NewsDao) GetFeaturedCount(ctx context.Context) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Where(dao.Columns().IsFeatured, 1).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetCountByCategory 根据分类获取新闻数
|
||||
func (dao *NewsDao) GetCountByCategory(ctx context.Context, category string) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Where(dao.Columns().Category, category).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetFeatured 获取推荐新闻
|
||||
func (dao *NewsDao) GetFeatured(ctx context.Context, limit int) ([]*model.News, error) {
|
||||
var newsList []*model.News
|
||||
err := dao.Ctx(ctx).
|
||||
Where(dao.Columns().IsPublished, 1).
|
||||
Where(dao.Columns().IsFeatured, 1).
|
||||
Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(limit).
|
||||
Scan(&newsList)
|
||||
return newsList, err
|
||||
}
|
||||
|
||||
// GetLatest 获取最新新闻
|
||||
func (dao *NewsDao) GetLatest(ctx context.Context, limit int) ([]*model.News, error) {
|
||||
var newsList []*model.News
|
||||
err := dao.Ctx(ctx).
|
||||
Where(dao.Columns().IsPublished, 1).
|
||||
Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(limit).
|
||||
Scan(&newsList)
|
||||
return newsList, err
|
||||
}
|
||||
|
||||
// GetByCategory 根据分类获取新闻
|
||||
func (dao *NewsDao) GetByCategory(ctx context.Context, category string, page, pageSize int) ([]*model.News, int, error) {
|
||||
var (
|
||||
newsList []*model.News
|
||||
total int
|
||||
db = dao.Ctx(ctx)
|
||||
)
|
||||
|
||||
// 添加分类条件
|
||||
db = db.Where(dao.Columns().Category, category).Where(dao.Columns().IsPublished, 1)
|
||||
|
||||
// 获取总数
|
||||
count, err := db.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total = count
|
||||
|
||||
// 分页查询
|
||||
offset := (page - 1) * pageSize
|
||||
err = db.Order(dao.Columns().IsTop + " DESC, " + dao.Columns().CreatedAt + " DESC").
|
||||
Limit(pageSize).
|
||||
Offset(offset).
|
||||
Scan(&newsList)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return newsList, total, nil
|
||||
}
|
||||
|
||||
// Search 搜索新闻
|
||||
func (dao *NewsDao) Search(ctx context.Context, keyword string, page, pageSize int) ([]*model.News, int, error) {
|
||||
var (
|
||||
newsList []*model.News
|
||||
total int
|
||||
db = dao.Ctx(ctx)
|
||||
)
|
||||
|
||||
// 添加搜索条件
|
||||
if keyword != "" {
|
||||
db = db.Where(dao.Columns().IsPublished, 1).
|
||||
Where(dao.Columns().Title+" LIKE ? OR "+dao.Columns().Content+" LIKE ? OR "+dao.Columns().Summary+" LIKE ?",
|
||||
"%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%")
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
count, err := db.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total = count
|
||||
|
||||
// 分页查询
|
||||
offset := (page - 1) * pageSize
|
||||
err = db.Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(pageSize).
|
||||
Offset(offset).
|
||||
Scan(&newsList)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return newsList, total, nil
|
||||
}
|
||||
168
internal/dao/partner_methods.go
Normal file
168
internal/dao/partner_methods.go
Normal file
@@ -0,0 +1,168 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"cms-api/internal/model"
|
||||
)
|
||||
|
||||
// GetById 根据ID获取合作伙伴
|
||||
func (dao *PartnerDao) GetById(ctx context.Context, id int) (*model.Partner, error) {
|
||||
var partner *model.Partner
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Scan(&partner)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return partner, nil
|
||||
}
|
||||
|
||||
// List 获取合作伙伴列表
|
||||
func (dao *PartnerDao) List(ctx context.Context, req *model.PartnerListRequest) ([]*model.Partner, int, error) {
|
||||
var (
|
||||
partners []*model.Partner
|
||||
total int
|
||||
db = dao.Ctx(ctx)
|
||||
)
|
||||
|
||||
// 添加查询条件
|
||||
if req.Category != "" {
|
||||
db = db.Where(dao.Columns().Category, req.Category)
|
||||
}
|
||||
if req.IsFeatured >= 0 {
|
||||
db = db.Where(dao.Columns().IsFeatured, req.IsFeatured)
|
||||
}
|
||||
if req.Status >= 0 {
|
||||
db = db.Where(dao.Columns().Status, req.Status)
|
||||
}
|
||||
if req.Keyword != "" {
|
||||
db = db.Where(dao.Columns().Name+" LIKE ? OR "+dao.Columns().Description+" LIKE ?", "%"+req.Keyword+"%", "%"+req.Keyword+"%")
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
count, err := db.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total = count
|
||||
|
||||
// 分页查询
|
||||
offset := (req.Page - 1) * req.PageSize
|
||||
err = db.Order(dao.Columns().SortOrder + " ASC, " + dao.Columns().CreatedAt + " DESC").
|
||||
Limit(req.PageSize).
|
||||
Offset(offset).
|
||||
Scan(&partners)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return partners, total, nil
|
||||
}
|
||||
|
||||
// GetAll 获取所有合作伙伴
|
||||
func (dao *PartnerDao) GetAll(ctx context.Context) ([]*model.Partner, error) {
|
||||
var partners []*model.Partner
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Status, 1).Order(dao.Columns().SortOrder + " ASC").Scan(&partners)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return partners, nil
|
||||
}
|
||||
|
||||
// GetFeatured 获取推荐合作伙伴
|
||||
func (dao *PartnerDao) GetFeatured(ctx context.Context, limit int) ([]*model.Partner, error) {
|
||||
var partners []*model.Partner
|
||||
err := dao.Ctx(ctx).
|
||||
Where(dao.Columns().Status, 1).
|
||||
Where(dao.Columns().IsFeatured, 1).
|
||||
Order(dao.Columns().SortOrder + " ASC").
|
||||
Limit(limit).
|
||||
Scan(&partners)
|
||||
return partners, err
|
||||
}
|
||||
|
||||
// GetByCategory 根据分类获取合作伙伴
|
||||
func (dao *PartnerDao) GetByCategory(ctx context.Context, category string) ([]*model.Partner, error) {
|
||||
var partners []*model.Partner
|
||||
err := dao.Ctx(ctx).
|
||||
Where(dao.Columns().Category, category).
|
||||
Where(dao.Columns().Status, 1).
|
||||
Order(dao.Columns().SortOrder + " ASC").
|
||||
Scan(&partners)
|
||||
return partners, err
|
||||
}
|
||||
|
||||
// GetByName 根据名称获取合作伙伴
|
||||
func (dao *PartnerDao) GetByName(ctx context.Context, name string) (*model.Partner, error) {
|
||||
var partner *model.Partner
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Name, name).Scan(&partner)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return partner, nil
|
||||
}
|
||||
|
||||
// Create 创建合作伙伴
|
||||
func (dao *PartnerDao) Create(ctx context.Context, partner *model.Partner) (int64, error) {
|
||||
result, err := dao.Ctx(ctx).Data(partner).Insert()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// Update 更新合作伙伴
|
||||
func (dao *PartnerDao) Update(ctx context.Context, id int, data interface{}) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Data(data).Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete 删除合作伙伴
|
||||
func (dao *PartnerDao) Delete(ctx context.Context, id int) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateStatus 更新合作伙伴状态
|
||||
func (dao *PartnerDao) UpdateStatus(ctx context.Context, id int, status int) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Data(map[string]interface{}{
|
||||
dao.Columns().Status: status,
|
||||
}).Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// GetCount 获取合作伙伴总数
|
||||
func (dao *PartnerDao) GetCount(ctx context.Context) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetCountByStatus 根据状态获取合作伙伴数
|
||||
func (dao *PartnerDao) GetCountByStatus(ctx context.Context, status int) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Where(dao.Columns().Status, status).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetFeaturedCount 获取推荐合作伙伴数
|
||||
func (dao *PartnerDao) GetFeaturedCount(ctx context.Context) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Where(dao.Columns().IsFeatured, 1).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetCategories 获取所有分类
|
||||
func (dao *PartnerDao) GetCategories(ctx context.Context) ([]string, error) {
|
||||
var categories []string
|
||||
err := dao.Ctx(ctx).Fields("DISTINCT " + dao.Columns().Category).Scan(&categories)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return categories, nil
|
||||
}
|
||||
|
||||
// GetCountByCategory 根据分类获取合作伙伴数
|
||||
func (dao *PartnerDao) GetCountByCategory(ctx context.Context, category string) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Where(dao.Columns().Category, category).Count()
|
||||
return count, err
|
||||
}
|
||||
121
internal/dao/role_methods.go
Normal file
121
internal/dao/role_methods.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"cms-api/internal/model"
|
||||
)
|
||||
|
||||
// GetById 根据ID获取角色
|
||||
func (dao *RoleDao) GetById(ctx context.Context, id int) (*model.Role, error) {
|
||||
var role *model.Role
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Scan(&role)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return role, nil
|
||||
}
|
||||
|
||||
// List 获取角色列表
|
||||
func (dao *RoleDao) List(ctx context.Context, req *model.RoleListRequest) ([]*model.Role, int, error) {
|
||||
var (
|
||||
roles []*model.Role
|
||||
total int
|
||||
db = dao.Ctx(ctx)
|
||||
)
|
||||
|
||||
// 添加查询条件
|
||||
if req.Status >= 0 {
|
||||
db = db.Where(dao.Columns().Status, req.Status)
|
||||
}
|
||||
if req.Keyword != "" {
|
||||
db = db.Where(dao.Columns().Name+" LIKE ? OR "+dao.Columns().Description+" LIKE ?", "%"+req.Keyword+"%", "%"+req.Keyword+"%")
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
count, err := db.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total = count
|
||||
|
||||
// 分页查询
|
||||
offset := (req.Page - 1) * req.PageSize
|
||||
err = db.Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(req.PageSize).
|
||||
Offset(offset).
|
||||
Scan(&roles)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return roles, total, nil
|
||||
}
|
||||
|
||||
// GetAll 获取所有角色
|
||||
func (dao *RoleDao) GetAll(ctx context.Context) ([]*model.Role, error) {
|
||||
var roles []*model.Role
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Status, 1).Order(dao.Columns().CreatedAt + " ASC").Scan(&roles)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return roles, nil
|
||||
}
|
||||
|
||||
// GetByName 根据名称获取角色
|
||||
func (dao *RoleDao) GetByName(ctx context.Context, name string) (*model.Role, error) {
|
||||
var role *model.Role
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Name, name).Scan(&role)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return role, nil
|
||||
}
|
||||
|
||||
// Create 创建角色
|
||||
func (dao *RoleDao) Create(ctx context.Context, role *model.Role) (int64, error) {
|
||||
result, err := dao.Ctx(ctx).Data(role).Insert()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// Update 更新角色
|
||||
func (dao *RoleDao) Update(ctx context.Context, id int, data interface{}) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Data(data).Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete 删除角色
|
||||
func (dao *RoleDao) Delete(ctx context.Context, id int) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateStatus 更新角色状态
|
||||
func (dao *RoleDao) UpdateStatus(ctx context.Context, id int, status int) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Data(map[string]interface{}{
|
||||
dao.Columns().Status: status,
|
||||
}).Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// CheckPermission 检查权限
|
||||
func (dao *RoleDao) CheckPermission(ctx context.Context, roleId int, permission string) (bool, error) {
|
||||
var role *model.Role
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Id, roleId).Scan(&role)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if role == nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// 这里应该实现权限检查逻辑
|
||||
// 暂时返回true,实际项目中需要根据role.Permissions字段进行权限验证
|
||||
return true, nil
|
||||
}
|
||||
121
internal/dao/user_methods.go
Normal file
121
internal/dao/user_methods.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"cms-api/internal/model"
|
||||
)
|
||||
|
||||
// GetByAccount 根据账号获取用户
|
||||
func (dao *UserDao) GetByAccount(ctx context.Context, account string) (*model.User, error) {
|
||||
var user *model.User
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Account, account).Scan(&user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// GetByEmail 根据邮箱获取用户
|
||||
func (dao *UserDao) GetByEmail(ctx context.Context, email string) (*model.User, error) {
|
||||
var user *model.User
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Email, email).Scan(&user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// GetById 根据ID获取用户
|
||||
func (dao *UserDao) GetById(ctx context.Context, id int) (*model.User, error) {
|
||||
var user *model.User
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Scan(&user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// List 获取用户列表
|
||||
func (dao *UserDao) List(ctx context.Context, req *model.UserListRequest) ([]*model.User, int, error) {
|
||||
var (
|
||||
users []*model.User
|
||||
total int
|
||||
db = dao.Ctx(ctx)
|
||||
)
|
||||
|
||||
// 添加查询条件
|
||||
if req.RoleId > 0 {
|
||||
db = db.Where(dao.Columns().RoleId, req.RoleId)
|
||||
}
|
||||
if req.Status >= 0 {
|
||||
db = db.Where(dao.Columns().Status, req.Status)
|
||||
}
|
||||
if req.Keyword != "" {
|
||||
db = db.Where(dao.Columns().Account+" LIKE ? OR "+dao.Columns().NickName+" LIKE ? OR "+dao.Columns().Email+" LIKE ?",
|
||||
"%"+req.Keyword+"%", "%"+req.Keyword+"%", "%"+req.Keyword+"%")
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
count, err := db.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total = count
|
||||
|
||||
// 分页查询
|
||||
offset := (req.Page - 1) * req.PageSize
|
||||
err = db.Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(req.PageSize).
|
||||
Offset(offset).
|
||||
Scan(&users)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return users, total, nil
|
||||
}
|
||||
|
||||
// Create 创建用户
|
||||
func (dao *UserDao) Create(ctx context.Context, user *model.User) (int64, error) {
|
||||
result, err := dao.Ctx(ctx).Data(user).Insert()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// Update 更新用户
|
||||
func (dao *UserDao) Update(ctx context.Context, id int, data interface{}) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Data(data).Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete 删除用户
|
||||
func (dao *UserDao) Delete(ctx context.Context, id int) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateStatus 更新用户状态
|
||||
func (dao *UserDao) UpdateStatus(ctx context.Context, id int, status int) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Data(map[string]interface{}{
|
||||
dao.Columns().Status: status,
|
||||
}).Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// GetCount 获取用户总数
|
||||
func (dao *UserDao) GetCount(ctx context.Context) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetCountByStatus 根据状态获取用户数
|
||||
func (dao *UserDao) GetCountByStatus(ctx context.Context, status int) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Where(dao.Columns().Status, status).Count()
|
||||
return count, err
|
||||
}
|
||||
Reference in New Issue
Block a user