初始化
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
package consumer
|
||||
|
||||
func checkHandleNumber(key string) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package controller
|
||||
|
||||
// Api api接口
|
||||
type Api struct {
|
||||
Public // 公共方法
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"vote-admin-api/api/dao"
|
||||
"vote-admin-api/api/dto"
|
||||
"vote-admin-api/api/requests"
|
||||
"vote-admin-api/api/responses"
|
||||
"vote-admin-api/config"
|
||||
"vote-admin-api/global"
|
||||
"vote-admin-api/utils"
|
||||
)
|
||||
|
||||
type Public struct{}
|
||||
|
||||
// Login 登陆
|
||||
func (b *Public) Login(c *gin.Context) {
|
||||
publicRequest := requests.PublicRequest{}
|
||||
req := publicRequest.Login
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 参数验证
|
||||
if err := global.Validate.Struct(req); err != nil {
|
||||
responses.FailWithMessage(utils.Translate(err), c)
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 验证验证码
|
||||
if config.C.Env == "prod" {
|
||||
isValid := utils.VerifyCaptcha(req.CaptchaId, req.Captcha)
|
||||
if !isValid {
|
||||
// 验证码错误
|
||||
responses.FailWithMessage("验证码错误", c)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
AdminUserDao := dao.AdminUserDao{}
|
||||
maps := make(map[string]interface{})
|
||||
maps["access"] = req.Access
|
||||
adminUser, err := AdminUserDao.GetAdminUser(maps)
|
||||
if err != nil || adminUser == nil {
|
||||
responses.FailWithMessage("用户名或密码错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 检测用户密码
|
||||
password := md5.Sum([]byte(req.Password + adminUser.Salt))
|
||||
// 将哈希值转换为16进制字符串
|
||||
passwordString := hex.EncodeToString(password[:])
|
||||
|
||||
fmt.Println(passwordString)
|
||||
if passwordString != adminUser.Password {
|
||||
responses.FailWithMessage("用户名或密码错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 检测用户状态
|
||||
if adminUser.IsDeleted == 1 {
|
||||
responses.FailWithMessage("非法用户", c)
|
||||
return
|
||||
}
|
||||
|
||||
if adminUser.IsDisabled == 1 {
|
||||
responses.FailWithMessage("您的账号已被禁用,请联系管理员处理", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 下发token
|
||||
token := &utils.Token{
|
||||
UserId: fmt.Sprintf("%d", adminUser.UserId),
|
||||
}
|
||||
|
||||
// 生成jwt
|
||||
jwt, err := token.NewJWT()
|
||||
if err != nil || jwt == "" {
|
||||
responses.FailWithMessage("登陆失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
g := dto.AdminLoginDto(adminUser)
|
||||
|
||||
g.LoadToken(jwt)
|
||||
|
||||
responses.OkWithData(g, c)
|
||||
}
|
||||
|
||||
// GetCaptcha 获取验证码
|
||||
func (b *Public) GetCaptcha(c *gin.Context) {
|
||||
id, b64s, err := utils.GenerateCaptcha()
|
||||
if err != nil {
|
||||
responses.FailWithMessage("验证码获取失败", c)
|
||||
}
|
||||
|
||||
responses.OkWithData(gin.H{
|
||||
"id": id,
|
||||
"b64s": b64s,
|
||||
}, c)
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"vote-admin-api/api/model"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
type AdminUserDao struct {
|
||||
}
|
||||
|
||||
// GetAdminUserById 获取数据-id
|
||||
func (r *AdminUserDao) GetAdminUserById(AdminUserId int64) (m *model.AdminUser, err error) {
|
||||
err = global.Db.First(&m, AdminUserId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetAdminUserPreloadById 获取数据-加载全部关联-id
|
||||
func (r *AdminUserDao) GetAdminUserPreloadById(AdminUserId int64) (m *model.AdminUser, err error) {
|
||||
err = global.Db.Preload(clause.Associations).First(&m, AdminUserId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// DeleteAdminUser 删除
|
||||
func (r *AdminUserDao) DeleteAdminUser(tx *gorm.DB, maps interface{}) error {
|
||||
err := tx.Where(maps).Delete(&model.AdminUser{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteAdminUserById 删除-id
|
||||
func (r *AdminUserDao) DeleteAdminUserById(tx *gorm.DB, AdminUserId int64) error {
|
||||
if err := tx.Delete(&model.AdminUser{}, AdminUserId).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditAdminUser 修改
|
||||
func (r *AdminUserDao) EditAdminUser(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.AdminUser{}).Where(maps).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditAdminUserById 修改-id
|
||||
func (r *AdminUserDao) EditAdminUserById(tx *gorm.DB, AdminUserId int64, data interface{}) error {
|
||||
err := tx.Model(&model.AdminUser{}).Where("AdminUser_id = ?", AdminUserId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAdminUserList 获取列表
|
||||
func (r *AdminUserDao) GetAdminUserList(maps interface{}) (m []*model.AdminUser, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetAdminUserCount 获取数量
|
||||
func (r *AdminUserDao) GetAdminUserCount(maps interface{}) (total int64, err error) {
|
||||
err = global.Db.Model(&model.AdminUser{}).Where(maps).Count(&total).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// GetAdminUserListRand 获取列表-随机
|
||||
func (r *AdminUserDao) GetAdminUserListRand(maps interface{}, limit int) (m []*model.AdminUser, err error) {
|
||||
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddAdminUser 新增
|
||||
func (r *AdminUserDao) AddAdminUser(tx *gorm.DB, model *model.AdminUser) (*model.AdminUser, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
// GetAdminUser 获取
|
||||
func (r *AdminUserDao) GetAdminUser(maps interface{}) (m *model.AdminUser, err error) {
|
||||
err = global.Db.Where(maps).First(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"vote-admin-api/api/model"
|
||||
"vote-admin-api/api/requests"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
type ArticleDao struct {
|
||||
}
|
||||
|
||||
// GetArticleById 获取数据-id
|
||||
func (r *ArticleDao) GetArticleById(ArticleId int64) (m *model.Article, err error) {
|
||||
err = global.Db.First(&m, ArticleId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetArticlePreloadById 获取数据-加载全部关联-id
|
||||
func (r *ArticleDao) GetArticlePreloadById(ArticleId int64) (m *model.Article, err error) {
|
||||
err = global.Db.Preload(clause.Associations).First(&m, ArticleId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// DeleteArticle 删除
|
||||
func (r *ArticleDao) DeleteArticle(tx *gorm.DB, maps interface{}) error {
|
||||
err := tx.Where(maps).Delete(&model.Article{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteArticleById 删除-id
|
||||
func (r *ArticleDao) DeleteArticleById(tx *gorm.DB, ArticleId int64) error {
|
||||
if err := tx.Delete(&model.Article{}, ArticleId).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditArticle 修改
|
||||
func (r *ArticleDao) EditArticle(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.Article{}).Where(maps).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditArticleById 修改-id
|
||||
func (r *ArticleDao) EditArticleById(tx *gorm.DB, ArticleId int64, data interface{}) error {
|
||||
err := tx.Model(&model.Article{}).Where("article_id = ?", ArticleId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetArticleList 获取列表
|
||||
func (r *ArticleDao) GetArticleList(maps interface{}) (m []*model.Article, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetArticleCount 获取数量
|
||||
func (r *ArticleDao) GetArticleCount(maps interface{}) (total int64, err error) {
|
||||
err = global.Db.Model(&model.Article{}).Where(maps).Count(&total).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// GetArticleListRand 获取列表-随机
|
||||
func (r *ArticleDao) GetArticleListRand(maps interface{}, limit int) (m []*model.Article, err error) {
|
||||
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddArticle 新增
|
||||
func (r *ArticleDao) AddArticle(tx *gorm.DB, model *model.Article) (*model.Article, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
// GetArticle 获取
|
||||
func (r *ArticleDao) GetArticle(maps interface{}) (m *model.Article, err error) {
|
||||
err = global.Db.Where(maps).First(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Inc 自增
|
||||
func (r *ArticleDao) Inc(tx *gorm.DB, ArticleId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.Article{}).Where("article_id = ?", ArticleId).UpdateColumn(field, gorm.Expr(field+" + ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Dec 自减
|
||||
func (r *ArticleDao) Dec(tx *gorm.DB, ArticleId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.Article{}).Where("article_id = ?", ArticleId).UpdateColumn(field, gorm.Expr(field+" - ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetArticlePageSearch 获取图文列表-分页
|
||||
func (r *ArticleDao) GetArticlePageSearch(req requests.GetArticlePage, page, pageSize int) (m []*model.Article, total int64, err error) {
|
||||
var totalRecords int64
|
||||
|
||||
// 构建查询条件
|
||||
query := global.Db.Model(&model.Article{})
|
||||
|
||||
// 作者
|
||||
query = query.Preload("ArticleAuthor")
|
||||
|
||||
// 作者关联医院
|
||||
query = query.Preload("ArticleAuthor.BaseHospital")
|
||||
|
||||
// 文章状态(1:正常 2:禁用)
|
||||
query = query.Where("article_status = ?", 1)
|
||||
|
||||
// 搜索关键字
|
||||
if req.Keyword != "" {
|
||||
keyword := "%" + req.Keyword + "%" //
|
||||
|
||||
// 标题
|
||||
orQuery := global.Db.Model(&model.Article{}).Or("article_title LIKE ?", keyword)
|
||||
|
||||
// 医院名称
|
||||
hospitalSubQuery := global.Db.Model(&model.BaseHospital{}).
|
||||
Select("hospital_id").
|
||||
Where("hospital_name LIKE ?", keyword)
|
||||
|
||||
articleAuthorSubQuery := global.Db.Model(&model.ArticleAuthor{}).
|
||||
Select("article_id").
|
||||
Where(gorm.Expr("hospital_id IN (?)", hospitalSubQuery))
|
||||
|
||||
orQuery = orQuery.Or(gorm.Expr("article_id IN (?)", articleAuthorSubQuery))
|
||||
|
||||
// 作者姓名
|
||||
subQuery := global.Db.Model(&model.ArticleAuthor{}).
|
||||
Select("article_id").
|
||||
Where("author_name LIKE ?", keyword)
|
||||
|
||||
orQuery = orQuery.Or(gorm.Expr("article_id IN (?)", subQuery))
|
||||
|
||||
// 执行组建
|
||||
query = query.Where(orQuery)
|
||||
}
|
||||
|
||||
// 排序
|
||||
query = query.Order("created_at asc")
|
||||
|
||||
// 查询总数量
|
||||
if err := query.Count(&totalRecords).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
err = query.Scopes(model.Paginate(page, pageSize)).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return m, totalRecords, nil
|
||||
}
|
||||
|
||||
// GetArticleOrderList 获取列表-排序
|
||||
func (r *ArticleDao) GetArticleOrderList(maps interface{}, orderField string, limit int) (m []*model.Article, err error) {
|
||||
err = global.Db.Where(maps).Preload(clause.Associations).Order(orderField).Limit(limit).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetArticleRank 获取某一条数据的排名
|
||||
func (r *ArticleDao) GetArticleRank(articleID int64) (int, error) {
|
||||
var rank int
|
||||
|
||||
// 定义子查询
|
||||
subQuery := global.Db.Model(&model.Article{}).
|
||||
Select("article_id, vote_num, (@rank := @rank + 1) AS rank").
|
||||
Where("article_status = ?", 1).
|
||||
Order("vote_num DESC").
|
||||
Joins(", (SELECT @rank := 0) AS r")
|
||||
|
||||
// 将子查询作为命名子查询的一部分进行查询
|
||||
err := global.Db.Table("(?) AS sub", subQuery).
|
||||
Where("sub.article_id = ?", articleID).
|
||||
Pluck("sub.rank", &rank).Error
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return rank, nil
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"vote-admin-api/api/model"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
type ArticleAuthorDao struct {
|
||||
}
|
||||
|
||||
// GetArticleAuthorById 获取数据-id
|
||||
func (r *ArticleAuthorDao) GetArticleAuthorById(AuthorId int64) (m *model.ArticleAuthor, err error) {
|
||||
err = global.Db.First(&m, AuthorId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetArticleAuthorPreloadById 获取数据-加载全部关联-id
|
||||
func (r *ArticleAuthorDao) GetArticleAuthorPreloadById(AuthorId int64) (m *model.ArticleAuthor, err error) {
|
||||
err = global.Db.Preload(clause.Associations).First(&m, AuthorId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// DeleteArticleAuthor 删除
|
||||
func (r *ArticleAuthorDao) DeleteArticleAuthor(tx *gorm.DB, maps interface{}) error {
|
||||
err := tx.Where(maps).Delete(&model.ArticleAuthor{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteArticleAuthorById 删除-id
|
||||
func (r *ArticleAuthorDao) DeleteArticleAuthorById(tx *gorm.DB, ArticleAuthorId int64) error {
|
||||
if err := tx.Delete(&model.ArticleAuthor{}, ArticleAuthorId).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditArticleAuthor 修改
|
||||
func (r *ArticleAuthorDao) EditArticleAuthor(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.ArticleAuthor{}).Where(maps).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditArticleAuthorById 修改-id
|
||||
func (r *ArticleAuthorDao) EditArticleAuthorById(tx *gorm.DB, AuthorId int64, data interface{}) error {
|
||||
err := tx.Model(&model.ArticleAuthor{}).Where("author_id = ?", AuthorId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetArticleAuthorList 获取列表
|
||||
func (r *ArticleAuthorDao) GetArticleAuthorList(maps interface{}) (m []*model.ArticleAuthor, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetArticleAuthorCount 获取数量
|
||||
func (r *ArticleAuthorDao) GetArticleAuthorCount(maps interface{}) (total int64, err error) {
|
||||
err = global.Db.Model(&model.ArticleAuthor{}).Where(maps).Count(&total).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// GetArticleAuthorListRand 获取列表-随机
|
||||
func (r *ArticleAuthorDao) GetArticleAuthorListRand(maps interface{}, limit int) (m []*model.ArticleAuthor, err error) {
|
||||
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddArticleAuthor 新增
|
||||
func (r *ArticleAuthorDao) AddArticleAuthor(tx *gorm.DB, model *model.ArticleAuthor) (*model.ArticleAuthor, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
// GetArticleAuthor 获取
|
||||
func (r *ArticleAuthorDao) GetArticleAuthor(maps interface{}) (m *model.ArticleAuthor, err error) {
|
||||
err = global.Db.Where(maps).First(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Inc 自增
|
||||
func (r *ArticleAuthorDao) Inc(tx *gorm.DB, AuthorId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.ArticleAuthor{}).Where("author_id = ?", AuthorId).UpdateColumn(field, gorm.Expr(field+" + ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Dec 自减
|
||||
func (r *ArticleAuthorDao) Dec(tx *gorm.DB, AuthorId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.ArticleAuthor{}).Where("author_id = ?", AuthorId).UpdateColumn(field, gorm.Expr(field+" - ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"vote-admin-api/api/model"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
type ArticleVoteDayDao struct {
|
||||
}
|
||||
|
||||
// GetArticleVoteDayById 获取数据-id
|
||||
func (r *ArticleVoteDayDao) GetArticleVoteDayById(voteDayId int64) (m *model.ArticleVoteDay, err error) {
|
||||
err = global.Db.First(&m, voteDayId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetArticleVoteDayPreloadById 获取数据-加载全部关联-id
|
||||
func (r *ArticleVoteDayDao) GetArticleVoteDayPreloadById(voteDayId int64) (m *model.ArticleVoteDay, err error) {
|
||||
err = global.Db.Preload(clause.Associations).First(&m, voteDayId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// DeleteArticleVoteDay 删除
|
||||
func (r *ArticleVoteDayDao) DeleteArticleVoteDay(tx *gorm.DB, maps interface{}) error {
|
||||
err := tx.Where(maps).Delete(&model.ArticleVoteDay{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteArticleVoteDayById 删除-id
|
||||
func (r *ArticleVoteDayDao) DeleteArticleVoteDayById(tx *gorm.DB, voteDayId int64) error {
|
||||
if err := tx.Delete(&model.ArticleVoteDay{}, voteDayId).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditArticleVoteDay 修改
|
||||
func (r *ArticleVoteDayDao) EditArticleVoteDay(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.ArticleVoteDay{}).Where(maps).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditArticleVoteDayById 修改-id
|
||||
func (r *ArticleVoteDayDao) EditArticleVoteDayById(tx *gorm.DB, voteDayId int64, data interface{}) error {
|
||||
err := tx.Model(&model.ArticleVoteDay{}).Where("vote_day_id = ?", voteDayId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetArticleVoteDayList 获取列表
|
||||
func (r *ArticleVoteDayDao) GetArticleVoteDayList(maps interface{}) (m []*model.ArticleVoteDay, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetArticleVoteDayCount 获取数量
|
||||
func (r *ArticleVoteDayDao) GetArticleVoteDayCount(maps interface{}) (total int64, err error) {
|
||||
err = global.Db.Model(&model.ArticleVoteDay{}).Where(maps).Count(&total).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// GetArticleVoteDayListRand 获取列表-随机
|
||||
func (r *ArticleVoteDayDao) GetArticleVoteDayListRand(maps interface{}, limit int) (m []*model.ArticleVoteDay, err error) {
|
||||
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddArticleVoteDay 新增
|
||||
func (r *ArticleVoteDayDao) AddArticleVoteDay(tx *gorm.DB, model *model.ArticleVoteDay) (*model.ArticleVoteDay, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
// GetArticleVoteDay 获取
|
||||
func (r *ArticleVoteDayDao) GetArticleVoteDay(maps interface{}) (m *model.ArticleVoteDay, err error) {
|
||||
err = global.Db.Where(maps).First(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Inc 自增
|
||||
func (r *ArticleVoteDayDao) Inc(tx *gorm.DB, voteDayId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.ArticleVoteDay{}).Where("vote_day_id = ?", voteDayId).UpdateColumn(field, gorm.Expr(field+" + ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Dec 自减
|
||||
func (r *ArticleVoteDayDao) Dec(tx *gorm.DB, voteDayId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.ArticleVoteDay{}).Where("vote_day_id = ?", voteDayId).UpdateColumn(field, gorm.Expr(field+" - ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"vote-admin-api/api/model"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
type BaseAgreementDao struct {
|
||||
}
|
||||
|
||||
// GetBaseAgreementById 获取数据-id
|
||||
func (r *BaseAgreementDao) GetBaseAgreementById(AgreementId int64) (m *model.BaseAgreement, err error) {
|
||||
err = global.Db.First(&m, AgreementId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetBaseAgreementPreloadById 获取数据-加载全部关联-id
|
||||
func (r *BaseAgreementDao) GetBaseAgreementPreloadById(AgreementId int64) (m *model.BaseAgreement, err error) {
|
||||
err = global.Db.Preload(clause.Associations).First(&m, AgreementId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// DeleteBaseAgreement 删除
|
||||
func (r *BaseAgreementDao) DeleteBaseAgreement(tx *gorm.DB, maps interface{}) error {
|
||||
err := tx.Where(maps).Delete(&model.BaseAgreement{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteBaseAgreementById 删除-id
|
||||
func (r *BaseAgreementDao) DeleteBaseAgreementById(tx *gorm.DB, voteDayId int64) error {
|
||||
if err := tx.Delete(&model.BaseAgreement{}, voteDayId).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditBaseAgreement 修改
|
||||
func (r *BaseAgreementDao) EditBaseAgreement(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.BaseAgreement{}).Where(maps).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditBaseAgreementById 修改-id
|
||||
func (r *BaseAgreementDao) EditBaseAgreementById(tx *gorm.DB, AgreementId int64, data interface{}) error {
|
||||
err := tx.Model(&model.BaseAgreement{}).Where("agreement_id = ?", AgreementId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetBaseAgreementList 获取列表
|
||||
func (r *BaseAgreementDao) GetBaseAgreementList(maps interface{}) (m []*model.BaseAgreement, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetBaseAgreementCount 获取数量
|
||||
func (r *BaseAgreementDao) GetBaseAgreementCount(maps interface{}) (total int64, err error) {
|
||||
err = global.Db.Model(&model.BaseAgreement{}).Where(maps).Count(&total).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// GetBaseAgreementListRand 获取列表-随机
|
||||
func (r *BaseAgreementDao) GetBaseAgreementListRand(maps interface{}, limit int) (m []*model.BaseAgreement, err error) {
|
||||
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddBaseAgreement 新增
|
||||
func (r *BaseAgreementDao) AddBaseAgreement(tx *gorm.DB, model *model.BaseAgreement) (*model.BaseAgreement, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
// GetBaseAgreement 获取
|
||||
func (r *BaseAgreementDao) GetBaseAgreement(maps interface{}) (m *model.BaseAgreement, err error) {
|
||||
err = global.Db.Where(maps).First(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Inc 自增
|
||||
func (r *BaseAgreementDao) Inc(tx *gorm.DB, AgreementId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.BaseAgreement{}).Where("agreement_id = ?", AgreementId).UpdateColumn(field, gorm.Expr(field+" + ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Dec 自减
|
||||
func (r *BaseAgreementDao) Dec(tx *gorm.DB, AgreementId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.BaseAgreement{}).Where("agreement_id = ?", AgreementId).UpdateColumn(field, gorm.Expr(field+" - ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"vote-admin-api/api/model"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
type BaseAreaDao struct {
|
||||
}
|
||||
|
||||
// GetBaseAreaById 获取地区-地区id
|
||||
func (r *BaseAreaDao) GetBaseAreaById(BaseAreaId int) (m *model.BaseArea, err error) {
|
||||
err = global.Db.First(&m, BaseAreaId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// DeleteBaseArea 删除地区
|
||||
func (r *BaseAreaDao) DeleteBaseArea(tx *gorm.DB, maps interface{}) error {
|
||||
err := tx.Where(maps).Delete(&model.BaseArea{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditBaseArea 修改地区
|
||||
func (r *BaseAreaDao) EditBaseArea(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.BaseArea{}).Where(maps).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditBaseAreaById 修改地区-医生id
|
||||
func (r *BaseAreaDao) EditBaseAreaById(tx *gorm.DB, BaseAreaId int, data interface{}) error {
|
||||
err := tx.Model(&model.BaseArea{}).Where("BaseArea_id = ?", BaseAreaId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetBaseAreaList 获取地区列表
|
||||
func (r *BaseAreaDao) GetBaseAreaList(maps interface{}) (m []*model.BaseArea, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddBaseArea 新增地区
|
||||
func (r *BaseAreaDao) AddBaseArea(tx *gorm.DB, model *model.BaseArea) (*model.BaseArea, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
// AddBaseAreaByMap 新增地区-map
|
||||
func (r *BaseAreaDao) AddBaseAreaByMap(tx *gorm.DB, data map[string]interface{}) (*model.BaseArea, error) {
|
||||
userDoctorInfo := &model.BaseArea{}
|
||||
if err := tx.Model(&model.BaseArea{}).Create(data).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return userDoctorInfo, nil
|
||||
}
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"vote-admin-api/api/model"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
type DataDao struct {
|
||||
}
|
||||
|
||||
// GetDataById 获取数据-id
|
||||
func (r *DataDao) GetDataById(DataId int64) (m *model.Data, err error) {
|
||||
err = global.Db.First(&m, DataId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetDataPreloadById 获取数据-加载全部关联-id
|
||||
func (r *DataDao) GetDataPreloadById(DataId int64) (m *model.Data, err error) {
|
||||
err = global.Db.Preload(clause.Associations).First(&m, DataId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// DeleteData 删除
|
||||
func (r *DataDao) DeleteData(tx *gorm.DB, maps interface{}) error {
|
||||
err := tx.Where(maps).Delete(&model.Data{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteDataById 删除-id
|
||||
func (r *DataDao) DeleteDataById(tx *gorm.DB, DataId int64) error {
|
||||
if err := tx.Delete(&model.Data{}, DataId).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditData 修改
|
||||
func (r *DataDao) EditData(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.Data{}).Where(maps).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditDataById 修改-id
|
||||
func (r *DataDao) EditDataById(tx *gorm.DB, DataId int64, data interface{}) error {
|
||||
err := tx.Model(&model.Data{}).Where("data_id = ?", DataId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDataList 获取列表
|
||||
func (r *DataDao) GetDataList(maps interface{}) (m []*model.Data, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetDataCount 获取数量
|
||||
func (r *DataDao) GetDataCount(maps interface{}) (total int64, err error) {
|
||||
err = global.Db.Model(&model.Data{}).Where(maps).Count(&total).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// GetDataListRand 获取列表-随机
|
||||
func (r *DataDao) GetDataListRand(maps interface{}, limit int) (m []*model.Data, err error) {
|
||||
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddData 新增
|
||||
func (r *DataDao) AddData(tx *gorm.DB, model *model.Data) (*model.Data, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
// GetData 获取
|
||||
func (r *DataDao) GetData(maps interface{}) (m *model.Data, err error) {
|
||||
err = global.Db.Where(maps).First(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetDataListByMemberValidTime 获取列表-今天开始时间/过期时间
|
||||
func (r *DataDao) GetDataListByMemberValidTime(maps interface{}, startTime, endTime string) (m []*model.Data, err error) {
|
||||
err = global.Db.Where(maps).Where("member_expire_date BETWEEN ? AND ?", startTime, endTime).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Inc 自增
|
||||
func (r *DataDao) Inc(tx *gorm.DB, DataId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.Data{}).Where("data_id = ?", DataId).UpdateColumn(field, gorm.Expr(field+" + ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Dec 自减
|
||||
func (r *DataDao) Dec(tx *gorm.DB, DataId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.Data{}).Where("data_id = ?", DataId).UpdateColumn(field, gorm.Expr(field+" - ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"vote-admin-api/api/model"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
type SystemTimeDao struct {
|
||||
}
|
||||
|
||||
// GetSystemTimeById 获取数据-id
|
||||
func (r *SystemTimeDao) GetSystemTimeById(SystemTimeId int64) (m *model.SystemTime, err error) {
|
||||
err = global.Db.First(&m, SystemTimeId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetSystemTimePreloadById 获取数据-加载全部关联-id
|
||||
func (r *SystemTimeDao) GetSystemTimePreloadById(SystemTimeId int64) (m *model.SystemTime, err error) {
|
||||
err = global.Db.Preload(clause.Associations).First(&m, SystemTimeId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// DeleteSystemTime 删除
|
||||
func (r *SystemTimeDao) DeleteSystemTime(tx *gorm.DB, maps interface{}) error {
|
||||
err := tx.Where(maps).Delete(&model.SystemTime{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteSystemTimeById 删除-id
|
||||
func (r *SystemTimeDao) DeleteSystemTimeById(tx *gorm.DB, SystemTimeId int64) error {
|
||||
if err := tx.Delete(&model.SystemTime{}, SystemTimeId).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditSystemTime 修改
|
||||
func (r *SystemTimeDao) EditSystemTime(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.SystemTime{}).Where(maps).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditSystemTimeById 修改-id
|
||||
func (r *SystemTimeDao) EditSystemTimeById(tx *gorm.DB, SystemTimeId int64, data interface{}) error {
|
||||
err := tx.Model(&model.SystemTime{}).Where("system_time_id = ?", SystemTimeId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSystemTimeList 获取列表
|
||||
func (r *SystemTimeDao) GetSystemTimeList(maps interface{}) (m []*model.SystemTime, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetSystemTimeCount 获取数量
|
||||
func (r *SystemTimeDao) GetSystemTimeCount(maps interface{}) (total int64, err error) {
|
||||
err = global.Db.Model(&model.SystemTime{}).Where(maps).Count(&total).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// GetSystemTimeListRand 获取列表-随机
|
||||
func (r *SystemTimeDao) GetSystemTimeListRand(maps interface{}, limit int) (m []*model.SystemTime, err error) {
|
||||
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddSystemTime 新增
|
||||
func (r *SystemTimeDao) AddSystemTime(tx *gorm.DB, model *model.SystemTime) (*model.SystemTime, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
// Inc 自增
|
||||
func (r *SystemTimeDao) Inc(tx *gorm.DB, SystemTimeId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.SystemTime{}).Where("SystemTime_id = ?", SystemTimeId).UpdateColumn(field, gorm.Expr(field+" + ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Dec 自减
|
||||
func (r *SystemTimeDao) Dec(tx *gorm.DB, SystemTimeId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.SystemTime{}).Where("SystemTime_id = ?", SystemTimeId).UpdateColumn(field, gorm.Expr(field+" - ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"vote-admin-api/api/model"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
type UserDao struct {
|
||||
}
|
||||
|
||||
// GetUserById 获取数据-id
|
||||
func (r *UserDao) GetUserById(UserId int64) (m *model.User, err error) {
|
||||
err = global.Db.First(&m, UserId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetUserPreloadById 获取数据-加载全部关联-id
|
||||
func (r *UserDao) GetUserPreloadById(UserId int64) (m *model.User, err error) {
|
||||
err = global.Db.Preload(clause.Associations).First(&m, UserId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// DeleteUser 删除
|
||||
func (r *UserDao) DeleteUser(tx *gorm.DB, maps interface{}) error {
|
||||
err := tx.Where(maps).Delete(&model.User{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteUserById 删除-id
|
||||
func (r *UserDao) DeleteUserById(tx *gorm.DB, UserId int64) error {
|
||||
if err := tx.Delete(&model.User{}, UserId).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditUser 修改
|
||||
func (r *UserDao) EditUser(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.User{}).Where(maps).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditUserById 修改-id
|
||||
func (r *UserDao) EditUserById(tx *gorm.DB, UserId int64, data interface{}) error {
|
||||
err := tx.Model(&model.User{}).Where("User_id = ?", UserId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUserList 获取列表
|
||||
func (r *UserDao) GetUserList(maps interface{}) (m []*model.User, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetUserCount 获取数量
|
||||
func (r *UserDao) GetUserCount(maps interface{}) (total int64, err error) {
|
||||
err = global.Db.Model(&model.User{}).Where(maps).Count(&total).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// GetUserListRand 获取列表-随机
|
||||
func (r *UserDao) GetUserListRand(maps interface{}, limit int) (m []*model.User, err error) {
|
||||
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddUser 新增
|
||||
func (r *UserDao) AddUser(tx *gorm.DB, model *model.User) (*model.User, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
// GetUser 获取
|
||||
func (r *UserDao) GetUser(maps interface{}) (m *model.User, err error) {
|
||||
err = global.Db.Where(maps).First(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Inc 自增
|
||||
func (r *UserDao) Inc(tx *gorm.DB, UserId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.User{}).Where("User_id = ?", UserId).UpdateColumn(field, gorm.Expr(field+" + ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Dec 自减
|
||||
func (r *UserDao) Dec(tx *gorm.DB, UserId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.User{}).Where("User_id = ?", UserId).UpdateColumn(field, gorm.Expr(field+" - ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"vote-admin-api/api/model"
|
||||
"vote-admin-api/api/requests"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
type VideoDao struct {
|
||||
}
|
||||
|
||||
// GetVideoById 获取数据-id
|
||||
func (r *VideoDao) GetVideoById(VideoId int64) (m *model.Video, err error) {
|
||||
err = global.Db.First(&m, VideoId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetVideoPreloadById 获取数据-加载全部关联-id
|
||||
func (r *VideoDao) GetVideoPreloadById(VideoId int64) (m *model.Video, err error) {
|
||||
err = global.Db.Preload(clause.Associations).First(&m, VideoId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// DeleteVideo 删除
|
||||
func (r *VideoDao) DeleteVideo(tx *gorm.DB, maps interface{}) error {
|
||||
err := tx.Where(maps).Delete(&model.Video{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteVideoById 删除-id
|
||||
func (r *VideoDao) DeleteVideoById(tx *gorm.DB, VideoId int64) error {
|
||||
if err := tx.Delete(&model.Video{}, VideoId).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditVideo 修改
|
||||
func (r *VideoDao) EditVideo(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.Video{}).Where(maps).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditVideoById 修改-id
|
||||
func (r *VideoDao) EditVideoById(tx *gorm.DB, VideoId int64, data interface{}) error {
|
||||
err := tx.Model(&model.Video{}).Where("video_id = ?", VideoId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetVideoList 获取列表
|
||||
func (r *VideoDao) GetVideoList(maps interface{}) (m []*model.Video, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetVideoCount 获取数量
|
||||
func (r *VideoDao) GetVideoCount(maps interface{}) (total int64, err error) {
|
||||
err = global.Db.Model(&model.Video{}).Where(maps).Count(&total).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// GetVideoListRand 获取列表-随机
|
||||
func (r *VideoDao) GetVideoListRand(maps interface{}, limit int) (m []*model.Video, err error) {
|
||||
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddVideo 新增
|
||||
func (r *VideoDao) AddVideo(tx *gorm.DB, model *model.Video) (*model.Video, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
// GetVideo 获取
|
||||
func (r *VideoDao) GetVideo(maps interface{}) (m *model.Video, err error) {
|
||||
err = global.Db.Where(maps).First(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Inc 自增
|
||||
func (r *VideoDao) Inc(tx *gorm.DB, VideoId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.Video{}).Where("video_id = ?", VideoId).UpdateColumn(field, gorm.Expr(field+" + ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Dec 自减
|
||||
func (r *VideoDao) Dec(tx *gorm.DB, VideoId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.Video{}).Where("video_id = ?", VideoId).UpdateColumn(field, gorm.Expr(field+" - ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetVideoPageSearch 获取视频列表-分页
|
||||
func (r *VideoDao) GetVideoPageSearch(req requests.GetVideoPage, page, pageSize int) (m []*model.Video, total int64, err error) {
|
||||
var totalRecords int64
|
||||
|
||||
// 构建查询条件
|
||||
query := global.Db.Model(&model.Video{})
|
||||
|
||||
// 作者
|
||||
query = query.Preload("VideoAuthor")
|
||||
|
||||
// 作者关联医院
|
||||
query = query.Preload("VideoAuthor.BaseHospital")
|
||||
|
||||
// 文章状态(1:正常 2:禁用)
|
||||
query = query.Where("video_status = ?", 1)
|
||||
|
||||
// 搜索关键字
|
||||
if req.Keyword != "" {
|
||||
keyword := "%" + req.Keyword + "%" //
|
||||
|
||||
// 标题
|
||||
orQuery := global.Db.Model(&model.Video{}).Or("video_title LIKE ?", keyword)
|
||||
|
||||
// 医院名称
|
||||
hospitalSubQuery := global.Db.Model(&model.BaseHospital{}).
|
||||
Select("hospital_id").
|
||||
Where("hospital_name LIKE ?", keyword)
|
||||
|
||||
articleAuthorSubQuery := global.Db.Model(&model.VideoAuthor{}).
|
||||
Select("video_id").
|
||||
Where(gorm.Expr("hospital_id IN (?)", hospitalSubQuery))
|
||||
|
||||
orQuery = orQuery.Or(gorm.Expr("video_id IN (?)", articleAuthorSubQuery))
|
||||
|
||||
// 作者姓名
|
||||
subQuery := global.Db.Model(&model.VideoAuthor{}).
|
||||
Select("video_id").
|
||||
Where("author_name LIKE ?", keyword)
|
||||
|
||||
orQuery = orQuery.Or(gorm.Expr("video_id IN (?)", subQuery))
|
||||
|
||||
// 执行组建
|
||||
query = query.Where(orQuery)
|
||||
}
|
||||
|
||||
// 排序
|
||||
query = query.Order("created_at asc")
|
||||
|
||||
// 查询总数量
|
||||
if err := query.Count(&totalRecords).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
err = query.Scopes(model.Paginate(page, pageSize)).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return m, totalRecords, nil
|
||||
}
|
||||
|
||||
// GetVideoOrderList 获取列表-排序
|
||||
func (r *VideoDao) GetVideoOrderList(maps interface{}, orderField string, limit int) (m []*model.Video, err error) {
|
||||
err = global.Db.Where(maps).Preload(clause.Associations).Order(orderField).Limit(limit).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetVideoRank 获取某一条数据的排名
|
||||
func (r *VideoDao) GetVideoRank(videoID int64) (int, error) {
|
||||
var rank int
|
||||
|
||||
// 定义子查询
|
||||
subQuery := global.Db.Model(&model.Video{}).
|
||||
Select("video_id, vote_num, (@rank := @rank + 1) AS rank").
|
||||
Where("video_status = ?", 1).
|
||||
Order("vote_num DESC").
|
||||
Joins(", (SELECT @rank := 0) AS r")
|
||||
|
||||
// 将子查询作为命名子查询的一部分进行查询
|
||||
err := global.Db.Table("(?) AS sub", subQuery).
|
||||
Where("sub.video_id = ?", videoID).
|
||||
Pluck("sub.rank", &rank).Error
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return rank, nil
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"vote-admin-api/api/model"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
type VideoAuthorDao struct {
|
||||
}
|
||||
|
||||
// GetVideoAuthorById 获取数据-id
|
||||
func (r *VideoAuthorDao) GetVideoAuthorById(authorId int64) (m *model.VideoAuthor, err error) {
|
||||
err = global.Db.First(&m, authorId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetVideoAuthorPreloadById 获取数据-加载全部关联-id
|
||||
func (r *VideoAuthorDao) GetVideoAuthorPreloadById(authorId int64) (m *model.VideoAuthor, err error) {
|
||||
err = global.Db.Preload(clause.Associations).First(&m, authorId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// DeleteVideoAuthor 删除
|
||||
func (r *VideoAuthorDao) DeleteVideoAuthor(tx *gorm.DB, maps interface{}) error {
|
||||
err := tx.Where(maps).Delete(&model.VideoAuthor{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteVideoAuthorById 删除-id
|
||||
func (r *VideoAuthorDao) DeleteVideoAuthorById(tx *gorm.DB, authorId int64) error {
|
||||
if err := tx.Delete(&model.VideoAuthor{}, authorId).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditVideoAuthor 修改
|
||||
func (r *VideoAuthorDao) EditVideoAuthor(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.VideoAuthor{}).Where(maps).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditVideoAuthorById 修改-id
|
||||
func (r *VideoAuthorDao) EditVideoAuthorById(tx *gorm.DB, authorId int64, data interface{}) error {
|
||||
err := tx.Model(&model.VideoAuthor{}).Where("authorId = ?", authorId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetVideoAuthorList 获取列表
|
||||
func (r *VideoAuthorDao) GetVideoAuthorList(maps interface{}) (m []*model.VideoAuthor, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetVideoAuthorCount 获取数量
|
||||
func (r *VideoAuthorDao) GetVideoAuthorCount(maps interface{}) (total int64, err error) {
|
||||
err = global.Db.Model(&model.VideoAuthor{}).Where(maps).Count(&total).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// GetVideoAuthorListRand 获取列表-随机
|
||||
func (r *VideoAuthorDao) GetVideoAuthorListRand(maps interface{}, limit int) (m []*model.VideoAuthor, err error) {
|
||||
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddVideoAuthor 新增
|
||||
func (r *VideoAuthorDao) AddVideoAuthor(tx *gorm.DB, model *model.VideoAuthor) (*model.VideoAuthor, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
// GetVideoAuthor 获取
|
||||
func (r *VideoAuthorDao) GetVideoAuthor(maps interface{}) (m *model.VideoAuthor, err error) {
|
||||
err = global.Db.Where(maps).First(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Inc 自增
|
||||
func (r *VideoAuthorDao) Inc(tx *gorm.DB, authorId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.VideoAuthor{}).Where("authorId = ?", authorId).UpdateColumn(field, gorm.Expr(field+" + ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Dec 自减
|
||||
func (r *VideoAuthorDao) Dec(tx *gorm.DB, authorId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.VideoAuthor{}).Where("authorId = ?", authorId).UpdateColumn(field, gorm.Expr(field+" - ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"vote-admin-api/api/model"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
type VideoVoteDayDao struct {
|
||||
}
|
||||
|
||||
// GetVideoVoteDayById 获取数据-id
|
||||
func (r *VideoVoteDayDao) GetVideoVoteDayById(voteDayId int64) (m *model.VideoVoteDay, err error) {
|
||||
err = global.Db.First(&m, voteDayId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetVideoVoteDayPreloadById 获取数据-加载全部关联-id
|
||||
func (r *VideoVoteDayDao) GetVideoVoteDayPreloadById(voteDayId int64) (m *model.VideoVoteDay, err error) {
|
||||
err = global.Db.Preload(clause.Associations).First(&m, voteDayId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// DeleteVideoVoteDay 删除
|
||||
func (r *VideoVoteDayDao) DeleteVideoVoteDay(tx *gorm.DB, maps interface{}) error {
|
||||
err := tx.Where(maps).Delete(&model.VideoVoteDay{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteVideoVoteDayById 删除-id
|
||||
func (r *VideoVoteDayDao) DeleteVideoVoteDayById(tx *gorm.DB, authorId int64) error {
|
||||
if err := tx.Delete(&model.VideoVoteDay{}, authorId).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditVideoVoteDay 修改
|
||||
func (r *VideoVoteDayDao) EditVideoVoteDay(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.VideoVoteDay{}).Where(maps).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditVideoVoteDayById 修改-id
|
||||
func (r *VideoVoteDayDao) EditVideoVoteDayById(tx *gorm.DB, voteDayId int64, data interface{}) error {
|
||||
err := tx.Model(&model.VideoVoteDay{}).Where("vote_day_id = ?", voteDayId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetVideoVoteDayList 获取列表
|
||||
func (r *VideoVoteDayDao) GetVideoVoteDayList(maps interface{}) (m []*model.VideoVoteDay, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetVideoVoteDayCount 获取数量
|
||||
func (r *VideoVoteDayDao) GetVideoVoteDayCount(maps interface{}) (total int64, err error) {
|
||||
err = global.Db.Model(&model.VideoVoteDay{}).Where(maps).Count(&total).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// GetVideoVoteDayListRand 获取列表-随机
|
||||
func (r *VideoVoteDayDao) GetVideoVoteDayListRand(maps interface{}, limit int) (m []*model.VideoVoteDay, err error) {
|
||||
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddVideoVoteDay 新增
|
||||
func (r *VideoVoteDayDao) AddVideoVoteDay(tx *gorm.DB, model *model.VideoVoteDay) (*model.VideoVoteDay, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
// GetVideoVoteDay 获取
|
||||
func (r *VideoVoteDayDao) GetVideoVoteDay(maps interface{}) (m *model.VideoVoteDay, err error) {
|
||||
err = global.Db.Where(maps).First(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Inc 自增
|
||||
func (r *VideoVoteDayDao) Inc(tx *gorm.DB, voteDayId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.VideoVoteDay{}).Where("vote_day_id = ?", voteDayId).UpdateColumn(field, gorm.Expr(field+" + ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Dec 自减
|
||||
func (r *VideoVoteDayDao) Dec(tx *gorm.DB, voteDayId int64, field string, numeral int) error {
|
||||
err := tx.Model(&model.VideoVoteDay{}).Where("vote_day_id = ?", voteDayId).UpdateColumn(field, gorm.Expr(field+" - ?", numeral)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"vote-admin-api/api/model"
|
||||
)
|
||||
|
||||
// ArticleDto 文章表
|
||||
type ArticleDto struct {
|
||||
ArticleId string `json:"article_id"` // 主键id
|
||||
ArticleTitle string `json:"article_title"` // 文章标题
|
||||
ArticleStatus int `json:"article_status"` // 文章状态(1:正常 2:禁用)
|
||||
VoteNum uint `json:"vote_num"` // 总票数
|
||||
ArticleContent string `json:"article_content"` // 文章内容
|
||||
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
||||
UpdatedAt model.LocalTime `json:"updated_at"` // 更新时间
|
||||
ArticleAuthor []*ArticleAuthorDto `json:"article_author"` // 作者
|
||||
Rank *int `json:"rank"` // 排名
|
||||
IsVote bool `json:"is_vote"` // 是否已投票(false:否 true:是)
|
||||
}
|
||||
|
||||
// GetArticleListDto 列表-分页
|
||||
func GetArticleListDto(m []*model.Article) []*ArticleDto {
|
||||
// 处理返回值
|
||||
responses := make([]*ArticleDto, len(m))
|
||||
|
||||
if len(m) > 0 {
|
||||
for i, v := range m {
|
||||
response := &ArticleDto{
|
||||
ArticleId: fmt.Sprintf("%d", v.ArticleId),
|
||||
ArticleTitle: v.ArticleTitle,
|
||||
ArticleStatus: v.ArticleStatus,
|
||||
VoteNum: v.VoteNum,
|
||||
CreatedAt: v.CreatedAt,
|
||||
UpdatedAt: v.UpdatedAt,
|
||||
}
|
||||
|
||||
// 加载数据-作者
|
||||
if v.ArticleAuthor != nil {
|
||||
response = response.LoadArticleAuthor(v.ArticleAuthor)
|
||||
}
|
||||
|
||||
// 将转换后的结构体添加到新切片中
|
||||
responses[i] = response
|
||||
}
|
||||
}
|
||||
|
||||
return responses
|
||||
}
|
||||
|
||||
// GetArticleDto 详情
|
||||
func GetArticleDto(m *model.Article) *ArticleDto {
|
||||
return &ArticleDto{
|
||||
ArticleId: fmt.Sprintf("%d", m.ArticleId),
|
||||
ArticleTitle: m.ArticleTitle,
|
||||
ArticleStatus: m.ArticleStatus,
|
||||
VoteNum: m.VoteNum,
|
||||
ArticleContent: m.ArticleContent,
|
||||
CreatedAt: m.CreatedAt,
|
||||
UpdatedAt: m.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
// LoadArticleAuthor 加载数据-作者
|
||||
func (r *ArticleDto) LoadArticleAuthor(m []*model.ArticleAuthor) *ArticleDto {
|
||||
if len(m) > 0 {
|
||||
r.ArticleAuthor = GetArticleAuthorListDto(m)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// LoadRank 加载数据-排名
|
||||
func (r *ArticleDto) LoadRank(m int) *ArticleDto {
|
||||
if m > 0 {
|
||||
r.Rank = &m
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// LoadVoteStatus 加载数据-投票状态
|
||||
func (r *ArticleDto) LoadVoteStatus(m bool) *ArticleDto {
|
||||
r.IsVote = m
|
||||
return r
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"vote-admin-api/api/model"
|
||||
)
|
||||
|
||||
type ArticleAuthorDto struct {
|
||||
AuthorName string `json:"author_name"` // 作者姓名
|
||||
HospitalName string `json:"hospital_name"` // 作者所属医院
|
||||
}
|
||||
|
||||
// GetArticleAuthorListDto 列表-分页
|
||||
func GetArticleAuthorListDto(m []*model.ArticleAuthor) []*ArticleAuthorDto {
|
||||
// 处理返回值
|
||||
responses := make([]*ArticleAuthorDto, len(m))
|
||||
|
||||
if len(m) > 0 {
|
||||
for i, v := range m {
|
||||
response := &ArticleAuthorDto{
|
||||
AuthorName: v.AuthorName,
|
||||
}
|
||||
|
||||
// 加载数据-医院属性
|
||||
if v.BaseHospital != nil {
|
||||
response = response.LoadBaseHospitalAttr(v.BaseHospital)
|
||||
}
|
||||
|
||||
// 将转换后的结构体添加到新切片中
|
||||
responses[i] = response
|
||||
}
|
||||
}
|
||||
|
||||
return responses
|
||||
}
|
||||
|
||||
// LoadBaseHospitalAttr 加载数据-医院属性
|
||||
func (r *ArticleAuthorDto) LoadBaseHospitalAttr(m *model.BaseHospital) *ArticleAuthorDto {
|
||||
if m != nil {
|
||||
r.HospitalName = m.HospitalName
|
||||
}
|
||||
return r
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"vote-admin-api/api/model"
|
||||
)
|
||||
|
||||
// BaseAgreementDto 基础-协议
|
||||
type BaseAgreementDto struct {
|
||||
AgreementId string `json:"agreement_id"` // 主键id
|
||||
AgreementType int `json:"agreement_type"` // 协议类型(1:大赛介绍 2:投票规则)
|
||||
AgreementContent string `json:"agreement_content"` // 协议内容
|
||||
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
||||
UpdatedAt model.LocalTime `json:"updated_at"` // 修改时间
|
||||
}
|
||||
|
||||
// GetBaseAgreementDto 详情
|
||||
func GetBaseAgreementDto(m *model.BaseAgreement) *BaseAgreementDto {
|
||||
return &BaseAgreementDto{
|
||||
AgreementId: fmt.Sprintf("%d", m.AgreementId),
|
||||
AgreementType: m.AgreementType,
|
||||
AgreementContent: m.AgreementContent,
|
||||
CreatedAt: m.CreatedAt,
|
||||
UpdatedAt: m.UpdatedAt,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"vote-admin-api/api/model"
|
||||
)
|
||||
|
||||
type DataDto struct {
|
||||
ViewNum uint `json:"view_num"` // 浏览数量
|
||||
VoteNum uint `json:"vote_num"` // 投票数量
|
||||
}
|
||||
|
||||
// GetDataDto 详情
|
||||
func GetDataDto(m *model.Data) *DataDto {
|
||||
return &DataDto{
|
||||
ViewNum: m.ViewNum,
|
||||
VoteNum: m.VoteNum,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"vote-admin-api/api/model"
|
||||
"vote-admin-api/utils"
|
||||
)
|
||||
|
||||
// LoginDto 登陆
|
||||
type LoginDto struct {
|
||||
UserId string `json:"user_id"` // 用户id
|
||||
NickName string `json:"nick_name"` // 用户名称
|
||||
Avatar string `json:"avatar"` // 头像
|
||||
Token string `json:"token"` // token
|
||||
}
|
||||
|
||||
// IndexDto 首页
|
||||
type IndexDto struct {
|
||||
QuestionCount int64 `json:"question_count"` // 问题数量
|
||||
UserCount int64 `json:"user_count"` // 用户数量
|
||||
ValidMemberCount int64 `json:"valid_member_count"` // 有效会员数
|
||||
QuestionSubmitCount int64 `json:"question_submit_count"` // 问题总提交次数
|
||||
QuestionPayCount int64 `json:"question_pay_count"` // 问题总支付次数
|
||||
MemberBuyCount int64 `json:"member_buy_count"` // 会员购买次数
|
||||
MemberAmountTotal float64 `json:"member_amount_total"` // 会员购买总金额
|
||||
SingleAmountTotal float64 `json:"single_amount_total"` // 单项购买总金额
|
||||
AmountTotal float64 `json:"amount_total"` // 会员+单项购买总金额
|
||||
}
|
||||
|
||||
// IndexDataDto 首页动态统计数据
|
||||
type IndexDataDto struct {
|
||||
Date string `json:"date"` // 日期
|
||||
Count int64 `json:"count"` // 数量
|
||||
}
|
||||
|
||||
// AdminLoginDto 微信登陆
|
||||
func AdminLoginDto(m *model.AdminUser) *LoginDto {
|
||||
return &LoginDto{
|
||||
UserId: fmt.Sprintf("%d", m.UserId),
|
||||
NickName: m.NickName,
|
||||
Avatar: utils.AddOssDomain(m.Avatar),
|
||||
}
|
||||
}
|
||||
|
||||
// LoadToken 加载token
|
||||
func (r *LoginDto) LoadToken(token string) *LoginDto {
|
||||
r.Token = token
|
||||
return r
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"vote-admin-api/api/model"
|
||||
)
|
||||
|
||||
// UserDto 用户表
|
||||
type UserDto struct {
|
||||
UserId string `json:"user_id"` // 用户id
|
||||
UserStatus int `json:"user_status"` // 状态(1:正常 2:禁用)
|
||||
OpenId string `json:"open_id"` // 用户微信标识
|
||||
LoginAt *model.LocalTime `json:"login_at"` // 登陆时间
|
||||
LoginIp string `json:"login_ip"` // 登陆ip
|
||||
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
||||
UpdatedAt model.LocalTime `json:"updated_at"` // 更新时间
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"vote-admin-api/api/model"
|
||||
)
|
||||
|
||||
// VideoDto 视频表
|
||||
type VideoDto struct {
|
||||
VideoId string `json:"video_id"` // 主键id
|
||||
VideoTitle string `json:"video_title"` // 视频标题
|
||||
VideoStatus int `json:"article_status"` // 视频状态(1:正常 2:禁用)
|
||||
VoteNum uint `json:"vote_num"` // 总票数
|
||||
VideoUrl string `json:"video_url"` // 视频地址
|
||||
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
||||
UpdatedAt model.LocalTime `json:"updated_at"` // 更新时间
|
||||
VideoAuthor []*VideoAuthorDto `json:"video_author"` // 作者
|
||||
Rank *int `json:"rank"` // 排名
|
||||
IsVote bool `json:"is_vote"` // 是否已投票(false:否 true:是)
|
||||
}
|
||||
|
||||
// GetVideoListDto 列表-分页
|
||||
func GetVideoListDto(m []*model.Video) []*VideoDto {
|
||||
// 处理返回值
|
||||
responses := make([]*VideoDto, len(m))
|
||||
|
||||
if len(m) > 0 {
|
||||
for i, v := range m {
|
||||
response := &VideoDto{
|
||||
VideoId: fmt.Sprintf("%d", v.VideoId),
|
||||
VideoTitle: v.VideoTitle,
|
||||
VideoStatus: v.VideoStatus,
|
||||
VoteNum: v.VoteNum,
|
||||
VideoUrl: v.VideoUrl,
|
||||
CreatedAt: v.CreatedAt,
|
||||
UpdatedAt: v.UpdatedAt,
|
||||
}
|
||||
|
||||
// 加载数据-作者
|
||||
if v.VideoAuthor != nil {
|
||||
response = response.LoadVideoAuthor(v.VideoAuthor)
|
||||
}
|
||||
|
||||
// 将转换后的结构体添加到新切片中
|
||||
responses[i] = response
|
||||
}
|
||||
}
|
||||
|
||||
return responses
|
||||
}
|
||||
|
||||
// GetVideoDto 详情
|
||||
func GetVideoDto(m *model.Video) *VideoDto {
|
||||
return &VideoDto{
|
||||
VideoId: fmt.Sprintf("%d", m.VideoId),
|
||||
VideoTitle: m.VideoTitle,
|
||||
VideoStatus: m.VideoStatus,
|
||||
VoteNum: m.VoteNum,
|
||||
VideoUrl: m.VideoUrl,
|
||||
CreatedAt: m.CreatedAt,
|
||||
UpdatedAt: m.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
// LoadVideoAuthor 加载数据-作者
|
||||
func (r *VideoDto) LoadVideoAuthor(m []*model.VideoAuthor) *VideoDto {
|
||||
if len(m) > 0 {
|
||||
r.VideoAuthor = GetVideoAuthorListDto(m)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// LoadRank 加载数据-排名
|
||||
func (r *VideoDto) LoadRank(m int) *VideoDto {
|
||||
if m > 0 {
|
||||
r.Rank = &m
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// LoadVoteStatus 加载数据-投票状态
|
||||
func (r *VideoDto) LoadVoteStatus(m bool) *VideoDto {
|
||||
r.IsVote = m
|
||||
return r
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"vote-admin-api/api/model"
|
||||
)
|
||||
|
||||
type VideoAuthorDto struct {
|
||||
AuthorName string `json:"author_name"` // 作者姓名
|
||||
HospitalName string `json:"hospital_name"` // 作者所属医院
|
||||
}
|
||||
|
||||
// GetVideoAuthorListDto 列表-分页
|
||||
func GetVideoAuthorListDto(m []*model.VideoAuthor) []*VideoAuthorDto {
|
||||
// 处理返回值
|
||||
responses := make([]*VideoAuthorDto, len(m))
|
||||
|
||||
if len(m) > 0 {
|
||||
for i, v := range m {
|
||||
response := &VideoAuthorDto{
|
||||
AuthorName: v.AuthorName,
|
||||
}
|
||||
|
||||
// 加载数据-医院属性
|
||||
if v.BaseHospital != nil {
|
||||
response = response.LoadBaseHospitalAttr(v.BaseHospital)
|
||||
}
|
||||
|
||||
// 将转换后的结构体添加到新切片中
|
||||
responses[i] = response
|
||||
}
|
||||
}
|
||||
|
||||
return responses
|
||||
}
|
||||
|
||||
// LoadBaseHospitalAttr 加载数据-医院属性
|
||||
func (r *VideoAuthorDto) LoadBaseHospitalAttr(m *model.BaseHospital) *VideoAuthorDto {
|
||||
if m != nil {
|
||||
r.HospitalName = m.HospitalName
|
||||
}
|
||||
return r
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package exception
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
"vote-admin-api/consts"
|
||||
)
|
||||
|
||||
// Recover
|
||||
// @Description: 处理全局异常
|
||||
// @return gin.HandlerFunc
|
||||
func Recover() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
// 打印错误堆栈信息
|
||||
log.Printf("panic: %v\n", r)
|
||||
debug.PrintStack()
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"code": consts.ServerError,
|
||||
"message": errorToString(r),
|
||||
"data": "",
|
||||
})
|
||||
// 终止后续接口调用,不加的话recover到异常后,还会继续执行接口里后续代码
|
||||
c.Abort()
|
||||
}
|
||||
}()
|
||||
// 加载完 defer recover,继续后续接口调用
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// recover错误,转string
|
||||
func errorToString(r interface{}) string {
|
||||
switch v := r.(type) {
|
||||
case error:
|
||||
return v.Error()
|
||||
default:
|
||||
return r.(string)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"vote-admin-api/api/dao"
|
||||
"vote-admin-api/api/responses"
|
||||
)
|
||||
|
||||
// Auth Auth认证
|
||||
func Auth() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 获取用户id
|
||||
userId := c.GetInt64("UserId")
|
||||
if userId == 0 {
|
||||
responses.Fail(c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 获取用户数据
|
||||
userDao := dao.UserDao{}
|
||||
user, err := userDao.GetUserById(userId)
|
||||
if err != nil || user == nil {
|
||||
responses.FailWithMessage("用户数据错误", c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
if user.UserStatus == 2 {
|
||||
responses.FailWithMessage("用户已禁用", c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Cors
|
||||
// @Description: 跨域中间件
|
||||
// @return gin.HandlerFunc
|
||||
func Cors() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
method := c.Request.Method
|
||||
origin := c.Request.Header.Get("Origin")
|
||||
if origin != "" {
|
||||
c.Header("Access-Control-Allow-Origin", origin)
|
||||
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
|
||||
c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
|
||||
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
|
||||
c.Header("Access-Control-Allow-Credentials", "false")
|
||||
c.Set("content-type", "application/json")
|
||||
}
|
||||
if method == "OPTIONS" {
|
||||
c.AbortWithStatus(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"vote-admin-api/consts"
|
||||
"vote-admin-api/global"
|
||||
"vote-admin-api/utils"
|
||||
)
|
||||
|
||||
// Jwt jwt认证
|
||||
func Jwt() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
authorization := c.Request.Header.Get("Authorization")
|
||||
if authorization == "" || !strings.HasPrefix(authorization, "Bearer ") {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{
|
||||
"message": "请求未授权",
|
||||
"code": consts.TokenError,
|
||||
"data": "",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 去除Bearer
|
||||
authorization = authorization[7:] // 截取字符
|
||||
|
||||
// 检测是否存在黑名单
|
||||
res, _ := global.Redis.Get(c, "jwt_black_"+authorization).Result()
|
||||
if res != "" {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "token错误/过期",
|
||||
"code": consts.TokenError,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 解析jwt
|
||||
t, err := utils.ParseJwt(authorization)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "token错误/过期",
|
||||
"code": consts.TokenError,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 转换类型
|
||||
userId, err := strconv.ParseInt(t.UserId, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "token错误",
|
||||
"code": consts.TokenError,
|
||||
"data": "",
|
||||
})
|
||||
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("UserId", userId) // 用户id
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
"time"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
// Logrus 日志中间件
|
||||
func Logrus() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
||||
// 开始时间
|
||||
startTime := time.Now()
|
||||
|
||||
// 处理请求
|
||||
c.Next()
|
||||
|
||||
// 获取 请求 参数
|
||||
params := make(map[string]string)
|
||||
|
||||
paramsRaw, ok := c.Get("params")
|
||||
if ok {
|
||||
requestParams, ok := paramsRaw.(map[string]string)
|
||||
if ok || len(requestParams) > 0 {
|
||||
params = requestParams
|
||||
}
|
||||
}
|
||||
|
||||
// 结束时间
|
||||
endTime := time.Now()
|
||||
|
||||
// 执行时间
|
||||
latencyTime := fmt.Sprintf("%6v", endTime.Sub(startTime))
|
||||
|
||||
// 请求方式
|
||||
reqMethod := c.Request.Method
|
||||
|
||||
// 请求路由
|
||||
reqUri := c.Request.RequestURI
|
||||
|
||||
// 状态码
|
||||
statusCode := c.Writer.Status()
|
||||
|
||||
// 请求IP
|
||||
clientIP := c.ClientIP()
|
||||
|
||||
// 日志格式
|
||||
global.Logger.WithFields(logrus.Fields{
|
||||
"http_status": statusCode,
|
||||
"total_time": latencyTime,
|
||||
"ip": clientIP,
|
||||
"method": reqMethod,
|
||||
"uri": reqUri,
|
||||
"params": params,
|
||||
}).Info("access")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"net/http"
|
||||
"vote-admin-api/consts"
|
||||
)
|
||||
|
||||
// RequestParamsMiddleware 获取请求参数中间件
|
||||
func RequestParamsMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
contentType := c.Request.Header.Get("Content-Type")
|
||||
|
||||
params := make(map[string]string)
|
||||
|
||||
// 判断请求参数类型
|
||||
switch contentType {
|
||||
case "application/json":
|
||||
// 解析 application/json 请求体
|
||||
bodyBytes, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read request body"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 创建新的请求对象,并设置请求体数据
|
||||
c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
|
||||
|
||||
var jsonParams map[string]interface{}
|
||||
err = json.Unmarshal(bodyBytes, &jsonParams)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "Invalid JSON data",
|
||||
"code": consts.HttpError,
|
||||
"data": "",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
for key, value := range jsonParams {
|
||||
params[key] = fmt.Sprintf("%v", value)
|
||||
}
|
||||
|
||||
// 存储参数到上下文
|
||||
c.Set("params", params)
|
||||
|
||||
case "multipart/form-data", "application/form-data", "application/x-www-form-urlencoded":
|
||||
// 解析 Form 表单参数
|
||||
err := c.Request.ParseForm()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"message": "Invalid form data",
|
||||
"code": consts.HttpError,
|
||||
"data": "",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
for key, values := range c.Request.Form {
|
||||
if len(values) > 0 {
|
||||
params[key] = fmt.Sprintf("%v", values[0])
|
||||
}
|
||||
}
|
||||
|
||||
// 存储参数到上下文
|
||||
c.Set("params", params)
|
||||
|
||||
default:
|
||||
// 解析 URL 参数
|
||||
queryParams := c.Request.URL.Query()
|
||||
|
||||
for key, values := range queryParams {
|
||||
if len(values) > 0 {
|
||||
params[key] = fmt.Sprintf("%v", values[0])
|
||||
}
|
||||
}
|
||||
|
||||
// 存储参数到上下文
|
||||
c.Set("params", params)
|
||||
}
|
||||
|
||||
// 继续处理请求
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
// AdminUser 后台-用户表
|
||||
type AdminUser struct {
|
||||
UserId int64 `gorm:"column:user_id;type:bigint(19);primary_key;comment:主键id" json:"user_id"`
|
||||
Access string `gorm:"column:access;type:varchar(64);comment:账号;NOT NULL" json:"access"`
|
||||
Password string `gorm:"column:password;type:varchar(128);comment:密码;NOT NULL" json:"password"`
|
||||
Salt string `gorm:"column:salt;type:varchar(255);comment:密码掩码;NOT NULL" json:"salt"`
|
||||
NickName string `gorm:"column:nick_name;type:varchar(255);comment:昵称" json:"nick_name"`
|
||||
Status int `gorm:"column:status;type:tinyint(1);default:2;comment:状态(1:正常 2:审核中 3:审核失败)" json:"status"`
|
||||
IsDeleted int `gorm:"column:is_deleted;type:tinyint(1);default:0;comment:是否被删除(0:否 1:是)" json:"is_deleted"`
|
||||
IsDisabled int `gorm:"column:is_disabled;type:tinyint(1);default:0;comment:是否被禁用(0:否 1:是)" json:"is_disabled"`
|
||||
Phone string `gorm:"column:phone;type:varchar(11);comment:手机号" json:"phone"`
|
||||
Avatar string `gorm:"column:avatar;type:varchar(255);comment:头像" json:"avatar"`
|
||||
Sex int `gorm:"column:sex;type:tinyint(1);comment:性别(1:男 2:女)" json:"sex"`
|
||||
Email string `gorm:"column:email;type:varchar(100);comment:邮箱" json:"email"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *AdminUser) TableName() string {
|
||||
return "admin_user"
|
||||
}
|
||||
|
||||
func (m *AdminUser) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.UserId == 0 {
|
||||
m.UserId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
// Article 文章表
|
||||
type Article struct {
|
||||
ArticleId int64 `gorm:"column:article_id;type:bigint(19);primary_key;comment:主键id" json:"article_id"`
|
||||
ArticleTitle string `gorm:"column:article_title;type:varchar(200);comment:文章标题" json:"article_title"`
|
||||
ArticleStatus int `gorm:"column:article_status;type:tinyint(1);default:1;comment:文章状态(1:正常 2:禁用)" json:"article_status"`
|
||||
VoteNum uint `gorm:"column:vote_num;type:int(10) unsigned;default:0;comment:总票数" json:"vote_num"`
|
||||
ArticleContent string `gorm:"column:article_content;type:text;comment:文章内容" json:"article_content"`
|
||||
Model
|
||||
ArticleAuthor []*ArticleAuthor `gorm:"foreignKey:ArticleId;references:article_id" json:"article_author"`
|
||||
Rank *int `gorm:"column:rank;type:tinyint(1) unsigned;comment:排名" json:"rank"`
|
||||
}
|
||||
|
||||
func (m *Article) TableName() string {
|
||||
return "article"
|
||||
}
|
||||
|
||||
func (m *Article) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.ArticleId == 0 {
|
||||
m.ArticleId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
// ArticleAuthor 文章-作者表
|
||||
type ArticleAuthor struct {
|
||||
AuthorId int64 `gorm:"column:author_id;type:bigint(19);primary_key;comment:主键id" json:"author_id"`
|
||||
ArticleId int64 `gorm:"column:article_id;type:bigint(19);comment:文章id;NOT NULL" json:"article_id"`
|
||||
AuthorName string `gorm:"column:author_name;type:varchar(100);comment:作者姓名" json:"author_name"`
|
||||
HospitalId int64 `gorm:"column:hospital_id;type:bigint(19);comment:作者所属医院id" json:"hospital_id"`
|
||||
Model
|
||||
BaseHospital *BaseHospital `gorm:"foreignKey:HospitalId;references:hospital_id" json:"base_hospital"`
|
||||
}
|
||||
|
||||
func (m *ArticleAuthor) TableName() string {
|
||||
return "article_author"
|
||||
}
|
||||
|
||||
func (m *ArticleAuthor) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.AuthorId == 0 {
|
||||
m.AuthorId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
// ArticleVoteDay 文章-每日投票
|
||||
type ArticleVoteDay struct {
|
||||
VoteDayId int64 `gorm:"column:vote_day_id;type:bigint(19);primary_key;comment:主键id" json:"vote_day_id"`
|
||||
ArticleId int64 `gorm:"column:article_id;type:bigint(19);comment:文章id;NOT NULL" json:"article_id"`
|
||||
UserId int64 `gorm:"column:user_id;type:bigint(19);comment:用户id;NOT NULL" json:"user_id"`
|
||||
VotedAt *LocalTime `gorm:"column:voted_at;type:date;comment:投票时间(日)" json:"voted_at"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *ArticleVoteDay) TableName() string {
|
||||
return "article_vote_day"
|
||||
}
|
||||
|
||||
func (m *ArticleVoteDay) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.VoteDayId == 0 {
|
||||
m.VoteDayId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
// BaseAgreement 基础-协议
|
||||
type BaseAgreement struct {
|
||||
AgreementId int64 `gorm:"column:agreement_id;type:bigint(19);primary_key;comment:主键id" json:"agreement_id"`
|
||||
AgreementType int `gorm:"column:agreement_type;type:tinyint(1);comment:协议类型(1:大赛介绍 2:投票规则);NOT NULL" json:"agreement_type"`
|
||||
AgreementContent string `gorm:"column:agreement_content;type:text;comment:协议内容" json:"agreement_content"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *BaseAgreement) TableName() string {
|
||||
return "base_agreement"
|
||||
}
|
||||
|
||||
func (m *BaseAgreement) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.AgreementId == 0 {
|
||||
m.AgreementId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
// BaseArea 地区表
|
||||
type BaseArea struct {
|
||||
AreaId int64 `gorm:"column:area_id;type:bigint(19);primary_key;comment:地区编号" json:"area_id"`
|
||||
AreaName string `gorm:"column:area_name;type:varchar(255);comment:名称" json:"area_name"`
|
||||
ParentId int64 `gorm:"column:parent_id;type:bigint(19);comment:上级编号" json:"parent_id"`
|
||||
Zip string `gorm:"column:zip;type:varchar(10);comment:邮编" json:"zip"`
|
||||
AreaType int `gorm:"column:area_type;type:tinyint(4);comment:类型(1:国家,2:省,3:市,4:区县)" json:"area_type"`
|
||||
}
|
||||
|
||||
func (m *BaseArea) TableName() string {
|
||||
return "base_area"
|
||||
}
|
||||
|
||||
func (m *BaseArea) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.AreaId == 0 {
|
||||
m.AreaId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
// BaseHospital 医院表
|
||||
type BaseHospital struct {
|
||||
HospitalId int64 `gorm:"column:hospital_id;type:bigint(19);primary_key;comment:主键id" json:"hospital_id"`
|
||||
HospitalName string `gorm:"column:hospital_name;type:varchar(255);comment:医院名称" json:"hospital_name"`
|
||||
HospitalStatus int `gorm:"column:hospital_status;type:tinyint(1);default:1;comment:状态(0:禁用 1:正常 2:删除)" json:"hospital_status"`
|
||||
HospitalLevelName string `gorm:"column:hospital_level_name;type:varchar(20);comment:医院等级名称" json:"hospital_level_name"`
|
||||
PostCode string `gorm:"column:post_code;type:varchar(50);comment:邮政编码" json:"post_code"`
|
||||
TelePhone string `gorm:"column:tele_phone;type:varchar(20);comment:电话" json:"tele_phone"`
|
||||
ProvinceId int `gorm:"column:province_id;type:int(11);comment:省份id" json:"province_id"`
|
||||
Province string `gorm:"column:province;type:varchar(50);comment:省份" json:"province"`
|
||||
CityId int `gorm:"column:city_id;type:int(11);comment:城市id" json:"city_id"`
|
||||
City string `gorm:"column:city;type:varchar(50);comment:城市" json:"city"`
|
||||
CountyId int `gorm:"column:county_id;type:int(11);comment:区县id" json:"county_id"`
|
||||
County string `gorm:"column:county;type:varchar(50);comment:区县" json:"county"`
|
||||
Address string `gorm:"column:address;type:varchar(255);comment:地址" json:"address"`
|
||||
Lat string `gorm:"column:lat;type:varchar(255);comment:纬度" json:"lat"`
|
||||
Lng string `gorm:"column:lng;type:varchar(255);comment:经度" json:"lng"`
|
||||
Desc string `gorm:"column:desc;type:varchar(255);comment:简介" json:"desc"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *BaseHospital) TableName() string {
|
||||
return "base_hospital"
|
||||
}
|
||||
|
||||
func (m *BaseHospital) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.HospitalId == 0 {
|
||||
m.HospitalId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
// Data 数据表
|
||||
type Data struct {
|
||||
DataId int64 `gorm:"column:data_id;type:bigint(19);primary_key;comment:主键id" json:"data_id"`
|
||||
ViewNum uint `gorm:"column:view_num;type:int(10) unsigned;default:0;comment:浏览数量" json:"view_num"`
|
||||
VoteNum uint `gorm:"column:vote_num;type:int(10) unsigned;default:0;comment:投票数量" json:"vote_num"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *Data) TableName() string {
|
||||
return "data"
|
||||
}
|
||||
|
||||
func (m *Data) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.DataId == 0 {
|
||||
m.DataId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
// SystemTime 配置-时间
|
||||
type SystemTime struct {
|
||||
SystemTimeId int64 `gorm:"column:system_time_id;type:bigint(19);primary_key;comment:主键id" json:"system_time_id"`
|
||||
StartTime *LocalTime `gorm:"column:start_time;type:datetime;comment:开始投票时间;NOT NULL" json:"start_time"`
|
||||
EndTime *LocalTime `gorm:"column:end_time;type:datetime;comment:结束投票时间;NOT NULL" json:"end_time"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *SystemTime) TableName() string {
|
||||
return "system_time"
|
||||
}
|
||||
|
||||
func (m *SystemTime) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.SystemTimeId == 0 {
|
||||
m.SystemTimeId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
// User 用户表
|
||||
type User struct {
|
||||
UserId int64 `gorm:"column:user_id;type:bigint(19);primary_key;comment:用户id" json:"user_id"`
|
||||
AppIden string `gorm:"column:app_iden;type:varchar(50);comment:app唯一标识" json:"app_iden"`
|
||||
UserStatus int `gorm:"column:user_status;type:tinyint(1);default:1;comment:状态(1:正常 2:禁用)" json:"user_status"`
|
||||
OpenId string `gorm:"column:open_id;type:varchar(100);comment:用户微信标识" json:"open_id"`
|
||||
LoginAt *LocalTime `gorm:"column:login_at;type:datetime;comment:登陆时间" json:"login_at"`
|
||||
LoginIp string `gorm:"column:login_ip;type:varchar(255);comment:登陆ip" json:"login_ip"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *User) TableName() string {
|
||||
return "user"
|
||||
}
|
||||
|
||||
func (m *User) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.UserId == 0 {
|
||||
m.UserId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
// Video 视频表
|
||||
type Video struct {
|
||||
VideoId int64 `gorm:"column:video_id;type:bigint(19);primary_key;comment:主键id" json:"video_id"`
|
||||
VideoTitle string `gorm:"column:video_title;type:varchar(200);comment:视频标题" json:"video_title"`
|
||||
VideoStatus int `gorm:"column:video_status;type:tinyint(1);default:1;comment:视频状态(1:正常 2:禁用)" json:"video_status"`
|
||||
VoteNum uint `gorm:"column:vote_num;type:int(10) unsigned;default:0;comment:总票数" json:"vote_num"`
|
||||
VideoUrl string `gorm:"column:video_url;type:varchar(255);comment:视频地址" json:"video_url"`
|
||||
Model
|
||||
VideoAuthor []*VideoAuthor `gorm:"foreignKey:VideoId;references:video_id" json:"video_author"`
|
||||
}
|
||||
|
||||
func (m *Video) TableName() string {
|
||||
return "video"
|
||||
}
|
||||
|
||||
func (m *Video) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.VideoId == 0 {
|
||||
m.VideoId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
// VideoAuthor 视频-作者表
|
||||
type VideoAuthor struct {
|
||||
AuthorId int64 `gorm:"column:author_id;type:bigint(19);primary_key;comment:主键id" json:"author_id"`
|
||||
VideoId int64 `gorm:"column:video_id;type:bigint(19);comment:视频id;NOT NULL" json:"video_id"`
|
||||
AuthorName string `gorm:"column:author_name;type:varchar(100);comment:作者姓名" json:"author_name"`
|
||||
HospitalId int64 `gorm:"column:hospital_id;type:bigint(19);comment:作者所属医院id" json:"hospital_id"`
|
||||
Model
|
||||
BaseHospital *BaseHospital `gorm:"foreignKey:HospitalId;references:hospital_id" json:"base_hospital"`
|
||||
}
|
||||
|
||||
func (m *VideoAuthor) TableName() string {
|
||||
return "video_author"
|
||||
}
|
||||
|
||||
func (m *VideoAuthor) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.AuthorId == 0 {
|
||||
m.AuthorId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
// VideoVoteDay 视频-每日投票
|
||||
type VideoVoteDay struct {
|
||||
VoteDayId int64 `gorm:"column:vote_day_id;type:bigint(19);primary_key;comment:主键id" json:"vote_day_id"`
|
||||
VideoId int64 `gorm:"column:video_id;type:bigint(19);comment:视频id;NOT NULL" json:"video_id"`
|
||||
UserId int64 `gorm:"column:user_id;type:bigint(19);comment:用户id;NOT NULL" json:"user_id"`
|
||||
VotedAt *LocalTime `gorm:"column:voted_at;type:date;comment:投票时间(日)" json:"voted_at"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *VideoVoteDay) TableName() string {
|
||||
return "video_vote_day"
|
||||
}
|
||||
|
||||
func (m *VideoVoteDay) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.VoteDayId == 0 {
|
||||
m.VoteDayId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
CreatedAt LocalTime `gorm:"column:created_at;type:datetime;comment:创建时间" json:"created_at"`
|
||||
UpdatedAt LocalTime `gorm:"column:updated_at;type:datetime;comment:修改时间" json:"updated_at"`
|
||||
}
|
||||
|
||||
// LocalTime 自定义数据类型
|
||||
type LocalTime time.Time
|
||||
|
||||
func (t *LocalTime) UnmarshalJSON(data []byte) error {
|
||||
if string(data) == "null" {
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
// 前端接收的时间字符串
|
||||
str := string(data)
|
||||
// 去除接收的str收尾多余的"
|
||||
timeStr := strings.Trim(str, "\"")
|
||||
t1, err := time.Parse("2006-01-02 15:04:05", timeStr)
|
||||
*t = LocalTime(t1)
|
||||
return err
|
||||
}
|
||||
|
||||
func (t LocalTime) MarshalJSON() ([]byte, error) {
|
||||
formatted := fmt.Sprintf("\"%v\"", time.Time(t).Format("2006-01-02 15:04:05"))
|
||||
return []byte(formatted), nil
|
||||
}
|
||||
|
||||
func (t LocalTime) Value() (driver.Value, error) {
|
||||
// MyTime 转换成 time.Time 类型
|
||||
tTime := time.Time(t)
|
||||
return tTime.Format("2006-01-02 15:04:05"), nil
|
||||
}
|
||||
|
||||
func (t *LocalTime) Scan(v interface{}) error {
|
||||
switch vt := v.(type) {
|
||||
case time.Time:
|
||||
// 字符串转成 time.Time 类型
|
||||
*t = LocalTime(vt)
|
||||
default:
|
||||
return errors.New("类型处理错误")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *LocalTime) String() string {
|
||||
return fmt.Sprintf("hhh:%s", time.Time(*t).String())
|
||||
}
|
||||
|
||||
func (t *LocalTime) IsEmpty() bool {
|
||||
return time.Time(*t).IsZero()
|
||||
}
|
||||
|
||||
func (m *Model) BeforeUpdate(tx *gorm.DB) (err error) {
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
switch {
|
||||
case pageSize > 100:
|
||||
pageSize = 100
|
||||
case pageSize <= 0:
|
||||
pageSize = 10
|
||||
}
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
return db.Offset(offset).Limit(pageSize)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package requests
|
||||
|
||||
type ArticleRequest struct {
|
||||
GetArticlePage // 获取图文列表-分页
|
||||
}
|
||||
|
||||
// GetArticlePage 获取图文列表-分页
|
||||
type GetArticlePage struct {
|
||||
Page int `json:"page" form:"page" label:"页码"`
|
||||
PageSize int `json:"page_size" form:"page_size" label:"每页个数"`
|
||||
Keyword string `json:"keyword" form:"keyword" label:"搜索关键字"`
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package requests
|
||||
|
||||
type BasicRequest struct {
|
||||
GetBasicAgreement // 获取协议详情
|
||||
}
|
||||
|
||||
// GetBasicAgreement 获取协议详情
|
||||
type GetBasicAgreement struct {
|
||||
AgreementType int `json:"agreement_type" form:"agreement_type" label:"协议类型" validate:"required,oneof=1 2"` // 协议类型(1:大赛介绍 2:投票规则)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package requests
|
||||
|
||||
type PublicRequest struct {
|
||||
Login // 登陆
|
||||
}
|
||||
|
||||
// Login 登陆
|
||||
type Login struct {
|
||||
Access string `json:"access" form:"access" validate:"required" label:"用户名"` // 用户名
|
||||
Password string `json:"password" form:"password" validate:"required" label:"密码"` // 密码
|
||||
Captcha string `json:"captcha" form:"captcha" validate:"required" label:"验证码"` // 验证码
|
||||
CaptchaId string `json:"captchaId" form:"captchaId" validate:"required"` // 验证码ID
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package requests
|
||||
|
||||
type VideoRequest struct {
|
||||
GetVideoPage // 获取视频列表-分页
|
||||
}
|
||||
|
||||
// GetVideoPage 获取视频列表-分页
|
||||
type GetVideoPage struct {
|
||||
Page int `json:"page" form:"page" label:"页码"`
|
||||
PageSize int `json:"page_size" form:"page_size" label:"每页个数"`
|
||||
Keyword string `json:"keyword" form:"keyword" label:"搜索关键字"`
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package requests
|
||||
|
||||
type Requests struct {
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package responses
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"vote-admin-api/consts"
|
||||
)
|
||||
|
||||
type res struct {
|
||||
Code int `json:"code"`
|
||||
Data interface{} `json:"data"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func result(code int, data interface{}, msg string, c *gin.Context) {
|
||||
//if data == nil {
|
||||
// data = gin.H{}
|
||||
//}
|
||||
c.JSON(http.StatusOK, res{
|
||||
code,
|
||||
data,
|
||||
msg,
|
||||
})
|
||||
}
|
||||
|
||||
func Ok(c *gin.Context) {
|
||||
result(consts.HttpSuccess, map[string]interface{}{}, "成功", c)
|
||||
}
|
||||
|
||||
func OkWithMessage(message string, c *gin.Context) {
|
||||
result(consts.HttpSuccess, map[string]interface{}{}, message, c)
|
||||
}
|
||||
|
||||
func OkWithData(data interface{}, c *gin.Context) {
|
||||
result(consts.HttpSuccess, data, "成功", c)
|
||||
}
|
||||
|
||||
func OkWithDetailed(data interface{}, message string, c *gin.Context) {
|
||||
result(consts.HttpSuccess, data, message, c)
|
||||
}
|
||||
|
||||
func Fail(c *gin.Context) {
|
||||
result(consts.HttpError, map[string]interface{}{}, "失败", c)
|
||||
}
|
||||
|
||||
func FailWithMessage(message string, c *gin.Context) {
|
||||
result(consts.HttpError, map[string]interface{}{}, message, c)
|
||||
}
|
||||
|
||||
func FailWithDetailed(data interface{}, message string, c *gin.Context) {
|
||||
result(consts.HttpError, data, message, c)
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"vote-admin-api/api/controller"
|
||||
"vote-admin-api/api/exception"
|
||||
"vote-admin-api/api/middlewares"
|
||||
"vote-admin-api/config"
|
||||
"vote-admin-api/consts"
|
||||
)
|
||||
|
||||
// Init 初始化路由
|
||||
func Init() *gin.Engine {
|
||||
r := gin.New()
|
||||
|
||||
// 环境设置
|
||||
if config.C.Env == "prod" {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
|
||||
// 获取请求参数中间件-json格式下会导致接口获取不到请求数据
|
||||
r.Use(middlewares.RequestParamsMiddleware())
|
||||
|
||||
// 日志中间件
|
||||
r.Use(middlewares.Logrus())
|
||||
|
||||
// 异常
|
||||
r.Use(gin.Recovery())
|
||||
|
||||
// 404处理
|
||||
r.NoRoute(func(c *gin.Context) {
|
||||
path := c.Request.URL.Path
|
||||
method := c.Request.Method
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"msg": fmt.Sprintf("%s %s not found", method, path),
|
||||
"code": consts.ClientHttpNotFound,
|
||||
"data": "",
|
||||
})
|
||||
})
|
||||
|
||||
// 异常处理
|
||||
r.Use(exception.Recover())
|
||||
|
||||
// 跨域处理
|
||||
r.Use(middlewares.Cors())
|
||||
|
||||
// 加载基础路由
|
||||
api := controller.Api{}
|
||||
|
||||
// 公开路由-不验证权限
|
||||
publicRouter(r, api)
|
||||
|
||||
// 验证jwt
|
||||
r.Use(middlewares.Jwt())
|
||||
|
||||
// 验证权限
|
||||
r.Use(middlewares.Auth())
|
||||
|
||||
// 私有路由-验证权限
|
||||
privateRouter(r, api)
|
||||
|
||||
// 公共路由-验证权限
|
||||
adminRouter(r, api)
|
||||
|
||||
// 基础数据-验证权限
|
||||
basicRouter(r, api)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// publicRouter 公开路由-不验证权限
|
||||
func publicRouter(r *gin.Engine, api controller.Api) {
|
||||
adminGroup := r.Group("/admin")
|
||||
|
||||
// 登录
|
||||
adminGroup.POST("/login", api.Public.Login)
|
||||
|
||||
// 验证码
|
||||
adminGroup.GET("/captcha", api.Public.GetCaptcha)
|
||||
}
|
||||
|
||||
// adminRouter 公共路由-验证权限
|
||||
func adminRouter(r *gin.Engine, api controller.Api) {
|
||||
|
||||
}
|
||||
|
||||
// basicRouter 基础数据-验证权限
|
||||
func basicRouter(r *gin.Engine, api controller.Api) {
|
||||
|
||||
}
|
||||
|
||||
// privateRouter 私有路由-验证权限
|
||||
func privateRouter(r *gin.Engine, api controller.Api) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type PublicService struct {
|
||||
}
|
||||
|
||||
// GetUserIP 获取用户ip
|
||||
func (r *PublicService) GetUserIP(h *http.Request) string {
|
||||
forwarded := h.Header.Get("X-FORWARDED-FOR")
|
||||
if forwarded != "" {
|
||||
return forwarded
|
||||
}
|
||||
return h.RemoteAddr
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
"vote-admin-api/api/dao"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
type SystemTimeService struct {
|
||||
}
|
||||
|
||||
// CheckVoteValidStatus 检测投票有效期
|
||||
// bool true:未结束 false:已结束
|
||||
func (r *SystemTimeService) CheckVoteValidStatus() bool {
|
||||
redisKey := "VoteSystemTime"
|
||||
res, _ := global.Redis.Get(context.Background(), redisKey).Result()
|
||||
if res == "" {
|
||||
// 获取配置-时间
|
||||
systemTimeDao := dao.SystemTimeDao{}
|
||||
systemTime, err := systemTimeDao.GetSystemTimeById(1)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if systemTime.EndTime == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// 结束时间
|
||||
endTime := time.Time(*systemTime.EndTime)
|
||||
|
||||
// 当前时间
|
||||
now := time.Now()
|
||||
|
||||
duration := endTime.Sub(now)
|
||||
if duration < 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
// 添加缓存
|
||||
_, err = global.Redis.Set(context.Background(), redisKey, "1", duration).Result()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
"vote-admin-api/config"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
}
|
||||
|
||||
// AddUserVoteDayCache 新增用户每日投票记录缓存
|
||||
// t 1:文章 2:视频
|
||||
func (r *UserService) AddUserVoteDayCache(userId, id int64, t int) bool {
|
||||
now := time.Now()
|
||||
|
||||
redisKey := "UserVoteDay" + fmt.Sprintf("%d", userId) + fmt.Sprintf("%d", id) + now.Format("2006-01-02") + fmt.Sprintf("%d", t)
|
||||
|
||||
// 缓存过期时间
|
||||
year, month, day := now.Date()
|
||||
location := now.Location()
|
||||
validTime := time.Date(year, month, day, 23, 59, 59, 0, location)
|
||||
duration := validTime.Sub(now)
|
||||
if duration < 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
if config.C.Env == "dev" {
|
||||
duration = 60 * 5 * time.Second
|
||||
}
|
||||
|
||||
// 添加缓存
|
||||
_, err := global.Redis.Set(context.Background(), redisKey, "1", duration).Result()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// CheckUserVoteDay 检测用户今日是否投票
|
||||
// t 1:文章 2:视频
|
||||
func (r *UserService) CheckUserVoteDay(userId, id int64, t int) bool {
|
||||
if t != 1 && t != 2 {
|
||||
return false
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
redisKey := "UserVoteDay" + fmt.Sprintf("%d", userId) + fmt.Sprintf("%d", id) + now.Format("2006-01-02") + fmt.Sprintf("%d", t)
|
||||
res, _ := global.Redis.Get(context.Background(), redisKey).Result()
|
||||
if res == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
if res == "1" {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
//if res == "" {
|
||||
// // 是否已投票(1:已投票 0:未投票)
|
||||
// isVote := "0"
|
||||
// if t == 1 {
|
||||
// // 文章
|
||||
// maps := make(map[string]interface{})
|
||||
// maps["article_id"] = id
|
||||
// maps["user_id"] = userId
|
||||
// maps["voted_at"] = now.Format("2006-01-02")
|
||||
//
|
||||
// articleVoteDayDao := dao.ArticleVoteDayDao{}
|
||||
// articleVoteDay, _ := articleVoteDayDao.GetArticleVoteDayById(id)
|
||||
// if articleVoteDay != nil {
|
||||
// isVote = "1"
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if t == 2 {
|
||||
// // 视频
|
||||
// maps := make(map[string]interface{})
|
||||
// maps["article_id"] = id
|
||||
// maps["user_id"] = userId
|
||||
// maps["voted_at"] = now.Format("2006-01-02")
|
||||
//
|
||||
// videoVoteDayDao := dao.VideoVoteDayDao{}
|
||||
// videoVoteDay, _ := videoVoteDayDao.GetVideoVoteDayById(id)
|
||||
// if videoVoteDay != nil {
|
||||
// isVote = "1"
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // 添加缓存
|
||||
// if isVote == "1" {
|
||||
// result := r.AddUserVoteDayCache(userId, id, t)
|
||||
// if result == false {
|
||||
// return false
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// res = isVote
|
||||
//}
|
||||
//
|
||||
//if res == "0" {
|
||||
// return false
|
||||
//} else {
|
||||
// return true
|
||||
//}
|
||||
}
|
||||
Reference in New Issue
Block a user