2024-08-05 17:09:38 +08:00

249 lines
5.8 KiB
Go

package system
import (
"f5"
"main/constant"
"main/model/system"
"net/http"
"q5"
"github.com/gin-gonic/gin"
"github.com/xuri/excelize/v2"
)
type BlockPlayerApi struct {
}
func (bpa *BlockPlayerApi) List(c *gin.Context) {
req := struct {
PageDto system.PageDto `json:"page_dto"`
}{}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusOK, gin.H{
"code": 1,
"message": err.Error(),
})
return
}
result := []*system.BlockPlayer{}
f5.GetGoStyleDb().PageQuery(
constant.CONF_DB,
q5.ToInt32(req.PageDto.PageSize),
q5.ToInt32(req.PageDto.Page),
"SELECT * FROM t_blockplayer WHERE 1=1",
[]string{},
f5.GetDbFilter().Comp([]f5.DbQueryFilter{}...),
" ORDER BY account_id ",
func(err error, pg *f5.Pagination) {
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": 1,
"message": err.Error(),
})
return
}
for pg.Rows.Next() {
p := new(system.BlockPlayer)
p.Blocked = q5.SafeToInt32(pg.Rows.GetByName("blocked"))
p.Account = pg.Rows.GetByName("account_id")
p.CreateTime = q5.SafeToInt32(pg.Rows.GetByName("createtime"))
p.ModifyTime = q5.SafeToInt32(pg.Rows.GetByName("modifytime"))
q5.AppendSlice(&result, p)
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "获取成功",
"data": result,
"total": pg.Total,
"total_page": pg.TotalPages,
"cur_page": pg.CurrentPage,
})
})
}
func (bpa *BlockPlayerApi) Add(c *gin.Context) {
req := struct {
Account string `binding:"required" json:"account_id"`
}{}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusOK, gin.H{
"code": 1,
"message": err.Error(),
})
return
}
var count int64 = 0
if err := f5.GetApp().GetOrmDb(constant.CONF_DB).Table("t_blockplayer").Where("account_id =?", req.Account).Count(&count).Error; err == nil && count > 0 {
c.JSON(http.StatusOK, gin.H{
"code": 1,
"message": "数据存在",
})
return
}
info := new(system.BlockPlayer)
nowDaySeconds := int32(f5.GetApp().GetRealSeconds())
info.Account = req.Account
info.Blocked = 1
info.CreateTime = nowDaySeconds
info.ModifyTime = nowDaySeconds
if err := f5.GetApp().GetOrmDb(constant.CONF_DB).Create(info).Error; err != nil {
c.JSON(http.StatusOK, gin.H{
"code": 1,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "添加成功",
})
}
func (bpa *BlockPlayerApi) Edit(c *gin.Context) {
req := struct {
Account string `binding:"required" json:"account_id"`
Blocked int32 `json:"blocked"`
}{}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusOK, gin.H{
"code": 1,
"message": err.Error(),
})
return
}
blockplayer := new(system.BlockPlayer)
db := f5.GetApp().GetOrmDb(constant.CONF_DB)
if err := db.Take(blockplayer, "account_id =?", req.Account).Error; err != nil {
if !f5.IsOrmErrRecordNotFound(err) {
c.JSON(http.StatusOK, gin.H{
"code": 500,
"message": "sever internal error:" + err.Error(),
})
return
} else {
c.JSON(http.StatusOK, gin.H{
"code": 2,
"message": "无法查到记录",
})
return
}
}
nowDaySeconds := int32(f5.GetApp().GetRealSeconds())
blockplayer.Blocked = req.Blocked
blockplayer.ModifyTime = nowDaySeconds
if err := db.Where("account_id = ?", req.Account).Save(blockplayer).Error; err != nil {
c.JSON(http.StatusOK, gin.H{
"code": 500,
"message": "sever internal error:" + err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "",
})
}
func (bpa *BlockPlayerApi) Del(c *gin.Context) {
req := struct {
Account string `binding:"required" json:"account_id"`
}{}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusOK, gin.H{
"code": 1,
"message": err.Error(),
})
return
}
blockplayer := new(system.BlockPlayer)
db := f5.GetApp().GetOrmDb(constant.CONF_DB)
if err := db.Take(blockplayer, "account_id =?", req.Account).Error; err != nil {
if !f5.IsOrmErrRecordNotFound(err) {
c.JSON(http.StatusOK, gin.H{
"code": 500,
"message": "sever internal error:" + err.Error(),
})
return
} else {
c.JSON(http.StatusOK, gin.H{
"code": 2,
"message": "无法查到记录",
})
return
}
} else {
}
nowDaySeconds := int32(f5.GetApp().GetRealSeconds())
blockplayer.Blocked = -1
blockplayer.ModifyTime = nowDaySeconds
if err := db.Where("account_id = ?", req.Account).Delete(blockplayer).Error; err != nil {
c.JSON(http.StatusOK, gin.H{
"code": 500,
"message": "sever internal error:" + err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "",
})
}
func (bpa *BlockPlayerApi) UploadExcel(c *gin.Context) {
file, _, err := c.Request.FormFile("file")
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": 1,
"message": "上传文件失败",
})
return
}
xlsx, err := excelize.OpenReader(file)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": 1,
"message": "解析文件失败",
})
return
}
rows, err := xlsx.GetRows(xlsx.GetSheetName(xlsx.GetActiveSheetIndex()))
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": 1,
"message": err.Error(),
})
return
}
nowDaySeconds := int32(f5.GetApp().GetRealSeconds())
blockplayers := []*system.BlockPlayer{}
for i, row := range rows {
if i == 0 {
continue
}
member := new(system.BlockPlayer)
member.Account = row[0]
member.Blocked = 1
member.CreateTime = nowDaySeconds
member.ModifyTime = nowDaySeconds
blockplayers = append(blockplayers, member)
}
if err := f5.GetApp().GetOrmDb(constant.CONF_DB).Create(blockplayers).Error; err != nil {
c.JSON(http.StatusOK, gin.H{
"code": 1,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "添加成功",
})
}