第一版完成
This commit is contained in:
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
|
||||
}
|
||||
Reference in New Issue
Block a user