Skip to content

Commit 05bf35f

Browse files
Merge pull request #99 from CodeNoobLH/dev
feat(frontend): 添加股票分组拖拽排序功能
2 parents 86f4e54 + 567c81a commit 05bf35f

File tree

5 files changed

+480
-180
lines changed

5 files changed

+480
-180
lines changed

app.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,14 @@ func (a *App) GetGroupList() []data.Group {
12191219
return data.NewStockGroupApi(db.Dao).GetGroupList()
12201220
}
12211221

1222+
func (a *App) UpdateGroupSort(id int, newSort int) bool {
1223+
return data.NewStockGroupApi(db.Dao).UpdateGroupSort(id, newSort)
1224+
}
1225+
1226+
func (a *App) InitializeGroupSort() bool {
1227+
return data.NewStockGroupApi(db.Dao).InitializeGroupSort()
1228+
}
1229+
12221230
func (a *App) GetGroupStockList(groupId int) []data.GroupStock {
12231231
return data.NewStockGroupApi(db.Dao).GetGroupStockByGroupId(groupId)
12241232
}

backend/data/stock_group_api.go

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,74 @@ func NewStockGroupApi(dao *gorm.DB) *StockGroupApi {
3939
}
4040

4141
func (receiver StockGroupApi) AddGroup(group Group) bool {
42-
err := receiver.dao.Where("name = ?", group.Name).FirstOrCreate(&group).Updates(&Group{
43-
Name: group.Name,
44-
Sort: group.Sort,
45-
}).Error
42+
// 检查是否已存在相同sort的组
43+
var existingGroup Group
44+
err := receiver.dao.Where("sort = ?", group.Sort).First(&existingGroup).Error
45+
46+
// 如果存在相同sort的组,则将该组及之后的所有组向后移动一位
47+
if err == nil {
48+
// 处理sort冲突:将相同sort值及之后的所有组向后移动一位
49+
receiver.dao.Model(&Group{}).Where("sort >= ?", group.Sort).Update("sort", gorm.Expr("sort + ?", 1))
50+
}
51+
52+
// 创建新组
53+
err = receiver.dao.Create(&group).Error
4654
return err == nil
4755
}
4856
func (receiver StockGroupApi) GetGroupList() []Group {
4957
var groups []Group
50-
receiver.dao.Find(&groups)
58+
receiver.dao.Order("sort ASC").Find(&groups)
5159
return groups
5260
}
61+
func (receiver StockGroupApi) UpdateGroupSort(id int, newSort int) bool {
62+
// First, get the current group to check if it exists
63+
var currentGroup Group
64+
if err := receiver.dao.First(&currentGroup, id).Error; err != nil {
65+
return false
66+
}
67+
68+
// If the new sort is the same as current, no need to update
69+
if currentGroup.Sort == newSort {
70+
return true
71+
}
72+
73+
// Get all groups ordered by sort
74+
var allGroups []Group
75+
receiver.dao.Order("sort ASC").Find(&allGroups)
76+
77+
// Adjust sort numbers to make space for the new sort value
78+
if newSort > currentGroup.Sort {
79+
// Moving down: decrease sort of groups between old and new position
80+
receiver.dao.Model(&Group{}).Where("sort > ? AND sort <= ? AND id != ?", currentGroup.Sort, newSort, id).Update("sort", gorm.Expr("sort - ?", 1))
81+
} else {
82+
// Moving up: increase sort of groups between new and old position
83+
receiver.dao.Model(&Group{}).Where("sort >= ? AND sort < ? AND id != ?", newSort, currentGroup.Sort, id).Update("sort", gorm.Expr("sort + ?", 1))
84+
}
85+
86+
// Update the target group's sort
87+
err := receiver.dao.Model(&Group{}).Where("id = ?", id).Update("sort", newSort).Error
88+
return err == nil
89+
}
90+
91+
// InitializeGroupSort initializes sort order for all groups based on created time
92+
func (receiver StockGroupApi) InitializeGroupSort() bool {
93+
// Get all groups ordered by created time
94+
var groups []Group
95+
err := receiver.dao.Order("created_at ASC").Find(&groups).Error
96+
if err != nil {
97+
return false
98+
}
99+
100+
// Update each group with new sort value based on their position
101+
for i, group := range groups {
102+
newSort := i + 1
103+
err := receiver.dao.Model(&Group{}).Where("id = ?", group.ID).Update("sort", newSort).Error
104+
if err != nil {
105+
return false
106+
}
107+
}
108+
return true
109+
}
53110
func (receiver StockGroupApi) GetGroupStockByGroupId(groupId int) []GroupStock {
54111
var stockGroup []GroupStock
55112
receiver.dao.Preload("GroupInfo").Where("group_id = ?", groupId).Find(&stockGroup)

0 commit comments

Comments
 (0)