150 lines
4.3 KiB
Go
150 lines
4.3 KiB
Go
|
package progress
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"strconv"
|
||
|
|
||
|
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
|
||
|
"github.com/tiger1103/gfast/v3/internal/app/system/model"
|
||
|
"github.com/tiger1103/gfast/v3/internal/app/system/model/entity"
|
||
|
)
|
||
|
|
||
|
type Scheduler struct {
|
||
|
// 总进度
|
||
|
FinishedPtr int
|
||
|
// 总量
|
||
|
Total int
|
||
|
// 实体
|
||
|
WorkStatus *entity.WorkSchedule
|
||
|
}
|
||
|
|
||
|
type Schdule map[string]Scheduler
|
||
|
|
||
|
// 获取指定 WorkID 的计划和总量
|
||
|
func (s Schdule) Get(workID string) (int, int) {
|
||
|
if scheduler, ok := s[workID]; ok {
|
||
|
return scheduler.FinishedPtr, scheduler.Total
|
||
|
}
|
||
|
return 0, 0
|
||
|
}
|
||
|
|
||
|
// WorkStatusGetProgress 传入一个方阵ID 获取其所有父级的进度
|
||
|
func WorkStatusGetProgress(fangzhenID string) ([]*model.WorkStatusProgressRes, error) {
|
||
|
var parentList []*model.WorkStatusProgressRes
|
||
|
err := dao.WorkStatus.Ctx(context.Background()).As("parent").
|
||
|
Fields("parent.id, parent.work_name as name ,parent.work_id,IFNULL(SUM(child.total), 0) AS total, IFNULL(SUM(child.finished), 0) AS finished").
|
||
|
LeftJoin("work_status AS child", "parent.id = child.parent").
|
||
|
Where("parent.parent IS NULL").
|
||
|
Where("parent.fangzhen_id", fangzhenID).
|
||
|
Group("parent.id, parent.work_name, parent.total").
|
||
|
Scan(&parentList)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
if len(parentList) == 0 {
|
||
|
return parentList, fmt.Errorf("未找到父级计划")
|
||
|
}
|
||
|
|
||
|
return parentList, err
|
||
|
}
|
||
|
|
||
|
// GetProgressBySubID 获取一共子项目下所有的父级进度
|
||
|
func GetProgressBySubID(subProjectID string) ([]*model.WorkStatusProgressRes, error) {
|
||
|
var fangzhenList []entity.QianqiFangzhen
|
||
|
if err := dao.QianqiFangzhen.Ctx(context.Background()).
|
||
|
Where(dao.QianqiFangzhen.Columns().ProjectId, subProjectID).Scan(&fangzhenList); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
if len(fangzhenList) == 0 {
|
||
|
return nil, fmt.Errorf("未找到方阵")
|
||
|
}
|
||
|
|
||
|
cumulativeProjects := []*model.WorkStatusProgressRes{}
|
||
|
firstTime := true
|
||
|
|
||
|
// 遍历项目列表
|
||
|
for index := 0; index < len(fangzhenList); index++ {
|
||
|
// 获取当前项目
|
||
|
project := fangzhenList[index]
|
||
|
|
||
|
// 获取当前项目的子项目
|
||
|
childProjects, err := WorkStatusGetProgress(strconv.Itoa(project.Id))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// 如果是第一次迭代,将子项目列表赋值给累积项目列表
|
||
|
if firstTime {
|
||
|
cumulativeProjects = childProjects
|
||
|
// 更新标志变量,表示已经不是第一次迭代
|
||
|
firstTime = false
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
// 遍历子项目列表后叠加到累积项目列表
|
||
|
for childIndex := 0; childIndex < len(childProjects); childIndex++ {
|
||
|
// 获取根据索引获取当前的 cumulativeProjects
|
||
|
singleChild := childProjects[childIndex]
|
||
|
comulativeChild := cumulativeProjects[childIndex]
|
||
|
|
||
|
// 将 singleChild 的 Total 和 Finished 叠加到 comulativeChild
|
||
|
comulativeChild.Total += singleChild.Total
|
||
|
comulativeChild.Finished += singleChild.Finished
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return cumulativeProjects, nil
|
||
|
}
|
||
|
|
||
|
// 传入主项目ID
|
||
|
func GetProgressByProjectID(projectID string) ([]*model.WorkStatusProgressRes, error) {
|
||
|
subProjects := []entity.SubProject{}
|
||
|
if err := dao.SubProject.Ctx(context.Background()).
|
||
|
Where(dao.SubProject.Columns().ProjectId, projectID).Scan(&subProjects); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
cumulativeProjects := []*model.WorkStatusProgressRes{}
|
||
|
firstTime := true
|
||
|
|
||
|
// 遍历项目列表
|
||
|
for index := 0; index < len(subProjects); index++ {
|
||
|
// 获取当前项目
|
||
|
project := subProjects[index]
|
||
|
|
||
|
// 获取当前项目的子项目
|
||
|
childProjects, err := GetProgressBySubID(strconv.Itoa(int(project.Id)))
|
||
|
if err != nil {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
// 如果是第一次迭代,将子项目列表赋值给累积项目列表
|
||
|
if firstTime {
|
||
|
cumulativeProjects = childProjects
|
||
|
// 更新标志变量,表示已经不是第一次迭代
|
||
|
firstTime = false
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
// 遍历子项目列表后叠加到累积项目列表
|
||
|
for childIndex := 0; childIndex < len(childProjects); childIndex++ {
|
||
|
// 获取根据索引获取当前的 cumulativeProjects
|
||
|
singleChild := childProjects[childIndex]
|
||
|
|
||
|
if childIndex < 0 || childIndex >= len(cumulativeProjects) {
|
||
|
continue
|
||
|
}
|
||
|
comulativeChild := cumulativeProjects[childIndex]
|
||
|
|
||
|
// 将 singleChild 的 Total 和 Finished 叠加到 comulativeChild
|
||
|
comulativeChild.Total += singleChild.Total
|
||
|
comulativeChild.Finished += singleChild.Finished
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return cumulativeProjects, nil
|
||
|
}
|