Skip to content

Commit

Permalink
1.add read quark from ai
Browse files Browse the repository at this point in the history
  • Loading branch information
ghjkg546 committed Oct 29, 2024
1 parent b5627a2 commit 8a3b577
Show file tree
Hide file tree
Showing 23 changed files with 1,072 additions and 91 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# go-blog
# go-blog

gentool -dsn "root:root@tcp(localhost:3306)/go-test?charset=utf8mb4&parseTime=True&loc=Local" -tables "sys_dict_item"
6 changes: 6 additions & 0 deletions app/common/request/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ type BatchSave struct {
Content string `form:"content" json:"content"`
CategoryId uint `form:"category_id" json:"category_id"`
}

type BatchShare struct {
PageSize int `form:"page_size" json:"page_size"`
Fid string `form:"fid" json:"fid"`
CategoryId uint `form:"category_id" json:"category_id"`
}
8 changes: 8 additions & 0 deletions app/common/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package response

import (
"github.com/gin-gonic/gin"
"github.com/jassue/jassue-gin/app/models"
"github.com/jassue/jassue-gin/global"
"net/http"
"os"
Expand All @@ -13,6 +14,13 @@ type Response struct {
Message string `json:"msg"`
}

type DirResponse struct {
Status int `json:"status"`
Code int `json:"code"`
Data []models.FileInfo `json:"data"`
Total int `json:"total"`
}

func ServerError(c *gin.Context, err interface{}) {
msg := "Internal Server Error"
if global.App.Config.App.Env != "production" && os.Getenv(gin.EnvGinMode) != gin.ReleaseMode {
Expand Down
123 changes: 123 additions & 0 deletions app/controllers/adminapi/dict.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package adminapi

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/jassue/jassue-gin/app/common/response"
"github.com/jassue/jassue-gin/app/models"
"github.com/jassue/jassue-gin/global"
"log"
"net/http"
"strconv"
)

type DictController struct{}

// GetList handles GET requests for dict details
func (uc *DictController) GetList(c *gin.Context) {
var menus []models.SysDict
var totaldicts int64

pageStr := c.DefaultQuery("page", "1")
pageSizeStr := c.DefaultQuery("pageSize", "10")
keyword := c.DefaultQuery("keywords", "")

page, err := strconv.Atoi(pageStr)
if err != nil || page < 1 {
page = 1
}

pageSize, err := strconv.Atoi(pageSizeStr)
if err != nil || pageSize < 1 {
pageSize = 10
}
db := global.App.DB

offset := (page - 1) * pageSize
limit := pageSize

query := db.Model(models.SysDict{})
if keyword != "" {
query.Where("name LIKE ?", "%"+keyword+"%")
}

query.Count(&totaldicts).Limit(limit).Offset(offset).Order("id desc").Find(&menus)
var res = gin.H{
"list": menus,
"total": totaldicts,
}
response.Success(c, res)
}

// GetDetail handles GET requests for dict details
func (uc *DictController) GetDetail(c *gin.Context) {
id := c.Param("id")
var dict models.SysDict
db := global.App.DB
db.Model(models.SysDict{}).Preload("DictItems").First(&dict, id)

response.Success(c, dict)
}

// Create handles POST requests to create a new dict
func (uc *DictController) Create(c *gin.Context) {
var input models.SysDict
db := global.App.DB
// Bind JSON payload to input
if err := c.BindJSON(&input); err != nil {
response.Fail(c, 500, err.Error())
return
}

// Save data to database
result := db.Save(&input)
if result.Error != nil {
response.Fail(c, 500, result.Error.Error())
return
}
response.Success(c, nil)
}

// Update handles PUT requests to update a dict
func (uc *DictController) Update(c *gin.Context) {
var input models.SysDict
db := global.App.DB
// Bind JSON payload to input
if err := c.BindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

for _, item := range input.DictItems {
fmt.Printf("Sort: %d, Status: %d, Name: %s, Value: %s\n", item.Sort, item.Status, item.Name, item.Value)
if item.ID != 0 {
// Update existing item if ID is present
if err := db.Model(&models.SysDictItem{}).Where("id = ?", item.ID).Updates(item).Error; err != nil {
log.Println("Error updating item:", err)
continue
}
fmt.Printf("Updated item with ID %d\n", item.ID)
}
} // Save data to database
result := db.Save(&input)
if result.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()})
return
}
response.Success(c, nil)

}

// Delete handles DELETE requests to delete a dict
func (uc *DictController) Delete(c *gin.Context) {
id := c.Param("id")
db := global.App.DB
var menu models.SysDictItem
db.Where("dict_id=?", id).First(&menu)
if menu.ID > 0 {
response.Fail(c, 500, "该字典下还有字典项,请先删除字典项")
return
}
db.Delete(&models.SysDict{}, id)
response.Success(c, nil)
}
102 changes: 51 additions & 51 deletions app/controllers/adminapi/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,57 +94,6 @@ func (uc *MenuController) GetList(c *gin.Context) {
response.Success(c, a)
}

func ListToTree(stuAll []models.SysMenu, pid uint, lev uint) []models.SysMenu {
var goodArr []models.SysMenu
for _, v := range stuAll {
if v.ParentID == pid {
// 这里可以理解为每次都从最原始的数据里面找出相对就的ID进行匹配,直到找不到就返回
child := ListToTree(stuAll, v.ID, lev+1)
if child == nil {
child = []models.SysMenu{}
}
node := models.SysMenu{
ID: v.ID,
//Path: menu.RoutePath,
Component: v.Component,
Redirect: v.Redirect,
Name: v.Name,
ParentID: v.ParentID,
Visible: v.Visible,
Type: v.Type,
Level: lev,
Children: child,
}

goodArr = append(goodArr, node)
}
}
return goodArr
}

func OptionListToTree(stuAll []models.MenuOption, pid uint, lev uint) []models.MenuOption {
var goodArr []models.MenuOption
for _, v := range stuAll {
if v.ParentId == pid {
// 这里可以理解为每次都从最原始的数据里面找出相对就的ID进行匹配,直到找不到就返回
child := OptionListToTree(stuAll, v.ID, lev+1)
if child == nil {
child = []models.MenuOption{}
}
node := models.MenuOption{
Value: v.ID,
//Path: menu.RoutePath,
ParentId: v.ParentId,
Label: v.Label,
Children: child,
}

goodArr = append(goodArr, node)
}
}
return goodArr
}

// GetDetail handles GET requests for user details
func (uc *MenuController) GetDetail(c *gin.Context) {
id := c.Param("id")
Expand Down Expand Up @@ -208,3 +157,54 @@ func (uc *MenuController) Delete(c *gin.Context) {
db.Delete(&models.SysMenu{}, id)
response.Success(c, nil)
}

func ListToTree(stuAll []models.SysMenu, pid uint, lev uint) []models.SysMenu {
var goodArr []models.SysMenu
for _, v := range stuAll {
if v.ParentID == pid {
// 这里可以理解为每次都从最原始的数据里面找出相对就的ID进行匹配,直到找不到就返回
child := ListToTree(stuAll, v.ID, lev+1)
if child == nil {
child = []models.SysMenu{}
}
node := models.SysMenu{
ID: v.ID,
//Path: menu.RoutePath,
Component: v.Component,
Redirect: v.Redirect,
Name: v.Name,
ParentID: v.ParentID,
Visible: v.Visible,
Type: v.Type,
Level: lev,
Children: child,
}

goodArr = append(goodArr, node)
}
}
return goodArr
}

func OptionListToTree(stuAll []models.MenuOption, pid uint, lev uint) []models.MenuOption {
var goodArr []models.MenuOption
for _, v := range stuAll {
if v.ParentId == pid {
// 这里可以理解为每次都从最原始的数据里面找出相对就的ID进行匹配,直到找不到就返回
child := OptionListToTree(stuAll, v.ID, lev+1)
if child == nil {
child = []models.MenuOption{}
}
node := models.MenuOption{
Value: v.ID,
//Path: menu.RoutePath,
ParentId: v.ParentId,
Label: v.Label,
Children: child,
}

goodArr = append(goodArr, node)
}
}
return goodArr
}
86 changes: 84 additions & 2 deletions app/controllers/adminapi/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
"github.com/jassue/jassue-gin/app/services"
"github.com/jassue/jassue-gin/global"
client "github.com/zinclabs/sdk-go-zincsearch"
"math"
"net/http"
"strconv"
"strings"
"time"
)

type ResourceController struct{}
Expand Down Expand Up @@ -192,12 +194,10 @@ func (uc *ResourceController) BatchCreate(c *gin.Context) {
continue

}

err1 := db.Create(&models.ResourceItem{Views: 0, Title: parts[0], DiskItems: string(jsonData), CategoryId: input.CategoryId, Status: 1, CoverImg: ""})

if err1.Error != nil {
response.Fail(c, 500, err1.Error.Error())

return
}

Expand Down Expand Up @@ -249,3 +249,85 @@ func (uc *ResourceController) Delete(c *gin.Context) {
}
response.Success(c, nil)
}

// 等待分享列表
func (uc *ResourceController) WaitShareList(c *gin.Context) {
pageStr := c.DefaultQuery("pageNum", "1")
pageSizeStr := c.DefaultQuery("pageSize", "10")
fidStr := c.DefaultQuery("fid", "")

page, err := strconv.Atoi(pageStr)
if err != nil || page < 1 {
fmt.Println(err)
page = 1
}
fmt.Println(page)
pageSize, err := strconv.Atoi(pageSizeStr)
if err != nil || pageSize < 1 {
pageSize = 10
}
var dirResp response.DirResponse
dirResp = services.QuarkService.GetDirInfo(fidStr, page, pageSize)

var res = gin.H{
"list": dirResp.Data,
"total": dirResp.Total,
}
response.Success(c, res)
}

func calculatePages(totalItems, itemsPerPage int) int {
return int(math.Ceil(float64(totalItems) / float64(itemsPerPage)))
}

// 批量分享
func (uc *ResourceController) BatchShare(c *gin.Context) {
var input request.BatchShare

if err := c.BindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var dirResp response.DirResponse
dirResp = services.QuarkService.GetDirInfo(input.Fid, 1, 50)
fmt.Println("总数", dirResp.Total)
response.Success(c, nil)
var ids []string
if input.PageSize >= dirResp.Total {
var chunks []models.ShareItem
for _, item := range dirResp.Data {

ids = append(ids, item.Fid)

chunks = append(chunks, models.ShareItem{Name: item.FileName, ID: item.Fid})
}
services.QuarkService.SaveResouceByUrl(ids, "test", chunks, input.CategoryId)
ids = []string{} // Clear the ids slice
chunks = []models.ShareItem{}

} else {
pages := calculatePages(dirResp.Total, 50)
for i := 0; i < pages; i++ {
dirResp = services.QuarkService.GetDirInfo(input.Fid, i+1, 50)
fmt.Printf("Processing page %d\n", i+1)
var chunks []models.ShareItem
for _, item := range dirResp.Data {

ids = append(ids, item.Fid)

chunks = append(chunks, models.ShareItem{Name: item.FileName, ID: item.Fid})

if len(ids) >= input.PageSize {

services.QuarkService.SaveResouceByUrl(ids, item.FileName, chunks, input.CategoryId)
ids = []string{} // Clear the ids slice
chunks = []models.ShareItem{}
time.Sleep(5 * time.Second)
}
}
}

}

response.Success(c, nil)
}
Loading

0 comments on commit 8a3b577

Please sign in to comment.