初始化提交
This commit is contained in:
parent
9c1e56d5af
commit
70bac9077e
289
api/controller/Article.go
Normal file
289
api/controller/Article.go
Normal file
@ -0,0 +1,289 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
"vote-api/api/dao"
|
||||
"vote-api/api/dto"
|
||||
"vote-api/api/model"
|
||||
"vote-api/api/requests"
|
||||
"vote-api/api/responses"
|
||||
"vote-api/api/service"
|
||||
"vote-api/consts"
|
||||
"vote-api/global"
|
||||
"vote-api/utils"
|
||||
)
|
||||
|
||||
type Article struct{}
|
||||
|
||||
// GetArticlePage 获取图文列表-分页
|
||||
func (r *Article) GetArticlePage(c *gin.Context) {
|
||||
articleRequest := requests.ArticleRequest{}
|
||||
req := articleRequest.GetArticlePage
|
||||
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 req.Page == 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = 20
|
||||
}
|
||||
|
||||
// 获取数据
|
||||
articleDao := dao.ArticleDao{}
|
||||
articles, total, err := articleDao.GetArticlePageSearch(req, req.Page, req.PageSize)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 处理返回值
|
||||
g := dto.GetArticleListDto(articles)
|
||||
|
||||
// 检测用户今日是否投票
|
||||
userId := c.GetInt64("UserId")
|
||||
if userId != 0 {
|
||||
userService := service.UserService{}
|
||||
for _, articleDto := range g {
|
||||
articleId, err := strconv.ParseInt(articleDto.ArticleId, 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
articleDto.IsVote = userService.CheckUserVoteDay(userId, articleId, 1)
|
||||
}
|
||||
}
|
||||
|
||||
result := make(map[string]interface{})
|
||||
result["page"] = req.Page
|
||||
result["page_size"] = req.PageSize
|
||||
result["total"] = total
|
||||
result["data"] = g
|
||||
responses.OkWithData(result, c)
|
||||
}
|
||||
|
||||
// GetArticle 获取图文详情
|
||||
func (r *Article) GetArticle(c *gin.Context) {
|
||||
id := c.Param("article_id")
|
||||
if id == "" {
|
||||
responses.FailWithMessage("缺少参数", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 将 id 转换为 int64 类型
|
||||
articleId, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
responses.Fail(c)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取数据
|
||||
articleDao := dao.ArticleDao{}
|
||||
article, err := articleDao.GetArticlePreloadById(articleId)
|
||||
if err != nil {
|
||||
responses.FailWithMessage("文章错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取排名
|
||||
rank, _ := articleDao.GetArticleRank(article.ArticleId)
|
||||
|
||||
// 处理返回值
|
||||
g := dto.GetArticleDto(article)
|
||||
|
||||
// 加载数据-作者
|
||||
g.LoadArticleAuthor(article.ArticleAuthor)
|
||||
|
||||
// 加载数据-作者排名
|
||||
g.LoadRank(rank)
|
||||
|
||||
// 检测用户今日是否投票
|
||||
userId := c.GetInt64("UserId")
|
||||
if userId != 0 {
|
||||
userService := service.UserService{}
|
||||
isVote := userService.CheckUserVoteDay(userId, articleId, 1)
|
||||
|
||||
// 加载数据-投票状态
|
||||
g.LoadVoteStatus(isVote)
|
||||
}
|
||||
|
||||
responses.OkWithData(g, c)
|
||||
}
|
||||
|
||||
// GetArticleRankList 获取文章排名列表
|
||||
func (r *Article) GetArticleRankList(c *gin.Context) {
|
||||
// 检测投票有效期
|
||||
systemTimeService := service.SystemTimeService{}
|
||||
isValid := systemTimeService.CheckVoteValidStatus()
|
||||
if isValid == false {
|
||||
responses.OkWithData(nil, c)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取数据
|
||||
articleDao := dao.ArticleDao{}
|
||||
maps := make(map[string]interface{})
|
||||
maps["article_status"] = 1
|
||||
articles, err := articleDao.GetArticleOrderList(maps, "vote_num desc", 15)
|
||||
if err != nil {
|
||||
responses.OkWithData(nil, c)
|
||||
return
|
||||
}
|
||||
|
||||
// 处理返回值
|
||||
g := dto.GetArticleListDto(articles)
|
||||
|
||||
// 检测用户今日是否投票
|
||||
userId := c.GetInt64("UserId")
|
||||
if userId != 0 {
|
||||
userService := service.UserService{}
|
||||
for _, articleDto := range g {
|
||||
articleId, err := strconv.ParseInt(articleDto.ArticleId, 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
articleDto.IsVote = userService.CheckUserVoteDay(userId, articleId, 1)
|
||||
}
|
||||
}
|
||||
|
||||
responses.OkWithData(g, c)
|
||||
}
|
||||
|
||||
// AddArticleVote 文章投票
|
||||
func (r *Article) AddArticleVote(c *gin.Context) {
|
||||
id := c.Param("article_id")
|
||||
if id == "" {
|
||||
responses.FailWithMessage("缺少参数", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 将 id 转换为 int64 类型
|
||||
articleId, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
responses.Fail(c)
|
||||
return
|
||||
}
|
||||
|
||||
userId := c.GetInt64("UserId")
|
||||
if userId == 0 {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{
|
||||
"message": "请登录后投票",
|
||||
"code": consts.TokenError,
|
||||
"data": "",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 检测并发请求
|
||||
redisKey := "AddArticleVote" + fmt.Sprintf("%d", userId) + fmt.Sprintf("%d", articleId)
|
||||
res, _ := global.Redis.Get(c, redisKey).Result()
|
||||
if res != "" {
|
||||
responses.FailWithMessage("请勿重复操作", c)
|
||||
return
|
||||
}
|
||||
|
||||
defer func(redisKey string) {
|
||||
global.Redis.Del(c, redisKey)
|
||||
}(redisKey)
|
||||
|
||||
// 添加缓存
|
||||
_, err = global.Redis.Set(c, redisKey, "1", (1)*time.Second).Result()
|
||||
if err != nil {
|
||||
responses.FailWithMessage("投票失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取数据
|
||||
articleDao := dao.ArticleDao{}
|
||||
article, err := articleDao.GetArticleById(articleId)
|
||||
if err != nil || article == nil {
|
||||
responses.FailWithMessage("非法数据", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 检测投票有效期
|
||||
systemTimeService := service.SystemTimeService{}
|
||||
isValid := systemTimeService.CheckVoteValidStatus()
|
||||
if isValid == false {
|
||||
responses.FailWithMessage("投票已结束", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 检测用户今日是否投票
|
||||
userService := service.UserService{}
|
||||
isVote := userService.CheckUserVoteDay(userId, articleId, 1)
|
||||
if isVote == true {
|
||||
responses.FailWithMessage("请勿重复投票", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
utils.LogJsonErr("投票失败", r)
|
||||
responses.FailWithMessage("投票失败", c)
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
// 增加投票数
|
||||
err = articleDao.Inc(tx, articleId, "vote_num", 1)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
responses.FailWithMessage("投票失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 增加投票记录
|
||||
nowDay, _ := time.Parse("2006-01-02", time.Now().Format("2006-01-02"))
|
||||
votedAt := model.LocalTime(nowDay)
|
||||
articleVoteDay := &model.ArticleVoteDay{
|
||||
ArticleId: articleId,
|
||||
UserId: userId,
|
||||
VotedAt: &votedAt,
|
||||
}
|
||||
|
||||
articleVoteDayDao := dao.ArticleVoteDayDao{}
|
||||
articleVoteDay, err = articleVoteDayDao.AddArticleVoteDay(tx, articleVoteDay)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
responses.FailWithMessage("投票失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 增加数据表-投票数量
|
||||
dataDao := dao.DataDao{}
|
||||
err = dataDao.Inc(tx, 1, "vote_num", 1)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
responses.FailWithMessage("投票失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 新增投票缓存
|
||||
result := userService.AddUserVoteDayCache(userId, articleId, 1)
|
||||
if result == false {
|
||||
tx.Rollback()
|
||||
responses.FailWithMessage("投票失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
responses.Ok(c)
|
||||
}
|
||||
@ -2,5 +2,8 @@ package controller
|
||||
|
||||
// Api api接口
|
||||
type Api struct {
|
||||
Login // 登录
|
||||
Login // 登录
|
||||
Article // 图文
|
||||
Video // 视频
|
||||
Public // 公共方法
|
||||
}
|
||||
|
||||
38
api/controller/Public.go
Normal file
38
api/controller/Public.go
Normal file
@ -0,0 +1,38 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"vote-api/api/dao"
|
||||
"vote-api/api/responses"
|
||||
"vote-api/global"
|
||||
"vote-api/utils"
|
||||
)
|
||||
|
||||
type Public struct {
|
||||
}
|
||||
|
||||
// AddBrowse 增加浏览数量
|
||||
func (r *Public) AddBrowse(c *gin.Context) {
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
utils.LogJsonErr("增加浏览数量失败", r)
|
||||
responses.Ok(c)
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
// 增加数据表-投票数量
|
||||
dataDao := dao.DataDao{}
|
||||
err := dataDao.Inc(tx, 1, "view_num", 1)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
responses.Ok(c)
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
responses.Ok(c)
|
||||
}
|
||||
289
api/controller/Video.go
Normal file
289
api/controller/Video.go
Normal file
@ -0,0 +1,289 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
"vote-api/api/dao"
|
||||
"vote-api/api/dto"
|
||||
"vote-api/api/model"
|
||||
"vote-api/api/requests"
|
||||
"vote-api/api/responses"
|
||||
"vote-api/api/service"
|
||||
"vote-api/consts"
|
||||
"vote-api/global"
|
||||
"vote-api/utils"
|
||||
)
|
||||
|
||||
type Video struct{}
|
||||
|
||||
// GetVideoPage 获取视频列表-分页
|
||||
func (r *Video) GetVideoPage(c *gin.Context) {
|
||||
videoRequest := requests.VideoRequest{}
|
||||
req := videoRequest.GetVideoPage
|
||||
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 req.Page == 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = 20
|
||||
}
|
||||
|
||||
// 获取数据
|
||||
videoDao := dao.VideoDao{}
|
||||
videos, total, err := videoDao.GetVideoPageSearch(req, req.Page, req.PageSize)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 处理返回值
|
||||
g := dto.GetVideoListDto(videos)
|
||||
|
||||
// 检测用户今日是否投票
|
||||
userId := c.GetInt64("UserId")
|
||||
if userId != 0 {
|
||||
userService := service.UserService{}
|
||||
for _, videoDto := range g {
|
||||
videoId, err := strconv.ParseInt(videoDto.VideoId, 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
videoDto.IsVote = userService.CheckUserVoteDay(userId, videoId, 2)
|
||||
}
|
||||
}
|
||||
|
||||
result := make(map[string]interface{})
|
||||
result["page"] = req.Page
|
||||
result["page_size"] = req.PageSize
|
||||
result["total"] = total
|
||||
result["data"] = g
|
||||
responses.OkWithData(result, c)
|
||||
}
|
||||
|
||||
// GetVideo 获取视频详情
|
||||
func (r *Video) GetVideo(c *gin.Context) {
|
||||
id := c.Param("video_id")
|
||||
if id == "" {
|
||||
responses.FailWithMessage("缺少参数", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 将 id 转换为 int64 类型
|
||||
videoId, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
responses.Fail(c)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取数据
|
||||
videoDao := dao.VideoDao{}
|
||||
video, err := videoDao.GetVideoPreloadById(videoId)
|
||||
if err != nil {
|
||||
responses.FailWithMessage("文章错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取排名
|
||||
rank, _ := videoDao.GetVideoRank(video.VideoId)
|
||||
|
||||
// 处理返回值
|
||||
g := dto.GetVideoDto(video)
|
||||
|
||||
// 加载数据-作者
|
||||
g.LoadVideoAuthor(video.VideoAuthor)
|
||||
|
||||
// 加载数据-作者排名
|
||||
g.LoadRank(rank)
|
||||
|
||||
// 检测用户今日是否投票
|
||||
userId := c.GetInt64("UserId")
|
||||
if userId != 0 {
|
||||
userService := service.UserService{}
|
||||
isVote := userService.CheckUserVoteDay(userId, videoId, 2)
|
||||
|
||||
// 加载数据-投票状态
|
||||
g.LoadVoteStatus(isVote)
|
||||
}
|
||||
|
||||
responses.OkWithData(g, c)
|
||||
}
|
||||
|
||||
// GetVideoRankList 获取视频排名列表
|
||||
func (r *Video) GetVideoRankList(c *gin.Context) {
|
||||
// 检测投票状态
|
||||
systemTimeService := service.SystemTimeService{}
|
||||
isValid := systemTimeService.CheckVoteValidStatus()
|
||||
if isValid == false {
|
||||
responses.OkWithData(nil, c)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取数据
|
||||
videoDao := dao.VideoDao{}
|
||||
maps := make(map[string]interface{})
|
||||
maps["video_status"] = 1
|
||||
videos, err := videoDao.GetVideoOrderList(maps, "vote_num desc", 15)
|
||||
if err != nil {
|
||||
responses.OkWithData(nil, c)
|
||||
return
|
||||
}
|
||||
|
||||
// 处理返回值
|
||||
g := dto.GetVideoListDto(videos)
|
||||
|
||||
// 检测用户今日是否投票
|
||||
userId := c.GetInt64("UserId")
|
||||
if userId != 0 {
|
||||
userService := service.UserService{}
|
||||
for _, videoDto := range g {
|
||||
videoId, err := strconv.ParseInt(videoDto.VideoId, 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
videoDto.IsVote = userService.CheckUserVoteDay(userId, videoId, 2)
|
||||
}
|
||||
}
|
||||
|
||||
responses.OkWithData(g, c)
|
||||
}
|
||||
|
||||
// AddVideoVote 视频投票
|
||||
func (r *Video) AddVideoVote(c *gin.Context) {
|
||||
id := c.Param("video_id")
|
||||
if id == "" {
|
||||
responses.FailWithMessage("缺少参数", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 将 id 转换为 int64 类型
|
||||
videoId, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
responses.Fail(c)
|
||||
return
|
||||
}
|
||||
|
||||
userId := c.GetInt64("UserId")
|
||||
if userId == 0 {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{
|
||||
"message": "请登录后投票",
|
||||
"code": consts.TokenError,
|
||||
"data": "",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 检测并发请求
|
||||
redisKey := "AddVideoVote" + fmt.Sprintf("%d", userId) + fmt.Sprintf("%d", videoId)
|
||||
res, _ := global.Redis.Get(c, redisKey).Result()
|
||||
if res != "" {
|
||||
responses.FailWithMessage("请勿重复操作", c)
|
||||
return
|
||||
}
|
||||
|
||||
defer func(redisKey string) {
|
||||
global.Redis.Del(c, redisKey)
|
||||
}(redisKey)
|
||||
|
||||
// 添加缓存
|
||||
_, err = global.Redis.Set(c, redisKey, "1", (1)*time.Second).Result()
|
||||
if err != nil {
|
||||
responses.FailWithMessage("投票失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取数据
|
||||
videoDao := dao.VideoDao{}
|
||||
video, err := videoDao.GetVideoById(videoId)
|
||||
if err != nil || video == nil {
|
||||
responses.FailWithMessage("非法数据", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 检测投票有效期
|
||||
systemTimeService := service.SystemTimeService{}
|
||||
isValid := systemTimeService.CheckVoteValidStatus()
|
||||
if isValid == false {
|
||||
responses.FailWithMessage("投票已结束", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 检测用户今日是否投票
|
||||
userService := service.UserService{}
|
||||
isVote := userService.CheckUserVoteDay(userId, videoId, 2)
|
||||
if isVote == true {
|
||||
responses.FailWithMessage("请勿重复投票", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
utils.LogJsonErr("投票失败", r)
|
||||
responses.FailWithMessage("投票失败", c)
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
// 增加投票数
|
||||
err = videoDao.Inc(tx, videoId, "vote_num", 1)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
responses.FailWithMessage("投票失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 增加投票记录
|
||||
nowDay, _ := time.Parse("2006-01-02", time.Now().Format("2006-01-02"))
|
||||
votedAt := model.LocalTime(nowDay)
|
||||
videoVoteDay := &model.VideoVoteDay{
|
||||
VideoId: videoId,
|
||||
UserId: userId,
|
||||
VotedAt: &votedAt,
|
||||
}
|
||||
|
||||
videoVoteDayDao := dao.VideoVoteDayDao{}
|
||||
videoVoteDay, err = videoVoteDayDao.AddVideoVoteDay(tx, videoVoteDay)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
responses.FailWithMessage("投票失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 增加数据表-投票数量
|
||||
dataDao := dao.DataDao{}
|
||||
err = dataDao.Inc(tx, 1, "vote_num", 1)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
responses.FailWithMessage("投票失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 新增投票缓存
|
||||
result := userService.AddUserVoteDayCache(userId, videoId, 2)
|
||||
if result == false {
|
||||
tx.Rollback()
|
||||
responses.FailWithMessage("投票失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
responses.Ok(c)
|
||||
}
|
||||
@ -4,6 +4,7 @@ import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"vote-api/api/model"
|
||||
"vote-api/api/requests"
|
||||
"vote-api/global"
|
||||
)
|
||||
|
||||
@ -124,3 +125,95 @@ func (r *ArticleDao) Dec(tx *gorm.DB, ArticleId int64, field string, numeral int
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
72
api/dao/BaseArea.go
Normal file
72
api/dao/BaseArea.go
Normal file
@ -0,0 +1,72 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"vote-api/api/model"
|
||||
"vote-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
|
||||
}
|
||||
117
api/dao/SystemTime.go
Normal file
117
api/dao/SystemTime.go
Normal file
@ -0,0 +1,117 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"vote-api/api/model"
|
||||
"vote-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
|
||||
}
|
||||
@ -4,6 +4,7 @@ import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"vote-api/api/model"
|
||||
"vote-api/api/requests"
|
||||
"vote-api/global"
|
||||
)
|
||||
|
||||
@ -124,3 +125,95 @@ func (r *VideoDao) Dec(tx *gorm.DB, VideoId int64, field string, numeral int) er
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
84
api/dto/Article.go
Normal file
84
api/dto/Article.go
Normal file
@ -0,0 +1,84 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"vote-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
|
||||
}
|
||||
42
api/dto/ArticleAuthor.go
Normal file
42
api/dto/ArticleAuthor.go
Normal file
@ -0,0 +1,42 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"vote-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
|
||||
}
|
||||
85
api/dto/Video.go
Normal file
85
api/dto/Video.go
Normal file
@ -0,0 +1,85 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"vote-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
|
||||
}
|
||||
42
api/dto/VideoAuthor.go
Normal file
42
api/dto/VideoAuthor.go
Normal file
@ -0,0 +1,42 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"vote-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
|
||||
}
|
||||
@ -11,29 +11,23 @@ func Auth() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 获取用户id
|
||||
userId := c.GetInt64("UserId")
|
||||
if userId == 0 {
|
||||
responses.Fail(c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if userId != 0 {
|
||||
// 获取用户数据
|
||||
userDao := dao.UserDao{}
|
||||
user, err := userDao.GetUserById(userId)
|
||||
if err != nil || user == nil {
|
||||
responses.FailWithMessage("用户数据错误", 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
|
||||
}
|
||||
}
|
||||
|
||||
if user.UserStatus == 2 {
|
||||
responses.FailWithMessage("用户已禁用", c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("UserId", userId) // 用户id
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,12 +15,7 @@ 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()
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@ -8,14 +8,14 @@ import (
|
||||
|
||||
// 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"`
|
||||
StartTime *LocalTime `gorm:"column:start_time;type:datetime;comment:开始投票时间" json:"start_time"`
|
||||
EndTime *LocalTime `gorm:"column:end_time;type:datetime;comment:结束投票时间" json:"end_time"`
|
||||
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"`
|
||||
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 {
|
||||
|
||||
@ -13,6 +13,7 @@ type ArticleAuthor struct {
|
||||
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 {
|
||||
|
||||
26
api/model/BaseArea.go
Normal file
26
api/model/BaseArea.go
Normal file
@ -0,0 +1,26 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"vote-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
|
||||
}
|
||||
46
api/model/BaseHospital.go
Normal file
46
api/model/BaseHospital.go
Normal file
@ -0,0 +1,46 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-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
|
||||
}
|
||||
@ -10,7 +10,7 @@ import (
|
||||
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;comment:投票数量" json:"vote_num"`
|
||||
VoteNum uint `gorm:"column:vote_num;type:int(10) unsigned;default:0;comment:投票数量" json:"vote_num"`
|
||||
Model
|
||||
}
|
||||
|
||||
|
||||
33
api/model/SystemTime.go
Normal file
33
api/model/SystemTime.go
Normal file
@ -0,0 +1,33 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-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
|
||||
}
|
||||
@ -8,14 +8,13 @@ import (
|
||||
|
||||
// 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"`
|
||||
StartTime *LocalTime `gorm:"column:start_time;type:datetime;comment:开始投票时间" json:"start_time"`
|
||||
EndTime *LocalTime `gorm:"column:end_time;type:datetime;comment:结束投票时间" json:"end_time"`
|
||||
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"`
|
||||
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 {
|
||||
|
||||
@ -13,6 +13,7 @@ type VideoAuthor struct {
|
||||
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 {
|
||||
|
||||
12
api/requests/Article.go
Normal file
12
api/requests/Article.go
Normal file
@ -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:"搜索关键字"`
|
||||
}
|
||||
12
api/requests/Video.go
Normal file
12
api/requests/Video.go
Normal file
@ -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:"搜索关键字"`
|
||||
}
|
||||
@ -74,6 +74,9 @@ func Init() *gin.Engine {
|
||||
func publicRouter(r *gin.Engine, api controller.Api) {
|
||||
// 登录
|
||||
r.POST("/login", api.Login.Login)
|
||||
|
||||
// 增加浏览数量
|
||||
r.POST("/browse", api.Public.AddBrowse)
|
||||
}
|
||||
|
||||
// adminRouter 公共路由-验证权限
|
||||
@ -88,4 +91,43 @@ func basicRouter(r *gin.Engine, api controller.Api) {
|
||||
|
||||
// privateRouter 私有路由-验证权限
|
||||
func privateRouter(r *gin.Engine, api controller.Api) {
|
||||
// 图文
|
||||
articleGroup := r.Group("/article")
|
||||
{
|
||||
// 获取图文列表-分页
|
||||
articleGroup.GET("/page", api.Article.GetArticlePage)
|
||||
|
||||
// 获取图文详情
|
||||
articleGroup.GET("/:article_id", api.Article.GetArticle)
|
||||
}
|
||||
|
||||
// 视频
|
||||
videoGroup := r.Group("/video")
|
||||
{
|
||||
// 获取视频列表-分页
|
||||
videoGroup.GET("/page", api.Video.GetVideoPage)
|
||||
|
||||
// 获取视频详情
|
||||
videoGroup.GET("/:video_id", api.Video.GetVideo)
|
||||
}
|
||||
|
||||
// 排名
|
||||
rankGroup := r.Group("/rank")
|
||||
{
|
||||
// 获取文章排名列表
|
||||
rankGroup.GET("/article", api.Article.GetArticleRankList)
|
||||
|
||||
// 获取视频排名列表
|
||||
rankGroup.GET("/video", api.Video.GetVideoRankList)
|
||||
}
|
||||
|
||||
// 投票
|
||||
voteGroup := r.Group("/vote")
|
||||
{
|
||||
// 文章投票
|
||||
voteGroup.POST("/article/:article_id", api.Article.AddArticleVote)
|
||||
|
||||
// 视频投票
|
||||
voteGroup.POST("/video/:video_id", api.Video.AddVideoVote)
|
||||
}
|
||||
}
|
||||
|
||||
49
api/service/SystemTime.go
Normal file
49
api/service/SystemTime.go
Normal file
@ -0,0 +1,49 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
"vote-api/api/dao"
|
||||
"vote-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
|
||||
}
|
||||
109
api/service/User.go
Normal file
109
api/service/User.go
Normal file
@ -0,0 +1,109 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
"vote-api/config"
|
||||
"vote-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
|
||||
//}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user