初始
This commit is contained in:
34
third/excel/equipment_materials.go
Normal file
34
third/excel/equipment_materials.go
Normal file
@ -0,0 +1,34 @@
|
||||
package excel
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
|
||||
)
|
||||
|
||||
// EquipmentMaterials 设备和材料
|
||||
type EquipmentMaterials struct {
|
||||
EquipmentMaterialsName string `json:"equipment_materials_name"`
|
||||
TotalNumber int `json:"total_number"`
|
||||
TotalQuantityCount int `json:"total_quantity_count"`
|
||||
CumulativeArrivalQuantity int `json:"cumulative_arrival_quantity"`
|
||||
}
|
||||
|
||||
// 传入项目ID,开始时间,结束时间返回 EquipmentMaterials 列表
|
||||
func GetEquipmentMaterials(projectID string, startAt, endAt string) ([]EquipmentMaterials, error) {
|
||||
var list []EquipmentMaterials
|
||||
|
||||
if err := dao.BusEquipmentMaterialsInventory.Ctx(context.Background()).As("bmi").
|
||||
Fields("bem.equipment_materials_name, SUM(bmi.number) AS total_number, SUM(bem.quantity_count) AS total_quantity_count, (SELECT SUM(bi.number) FROM `bus_equipment_materials_inventory` bi WHERE bi.equipment_materials_id = bmi.equipment_materials_id AND bi.deleted_at IS NULL AND bi.out_put = 2) AS cumulative_arrival_quantity").
|
||||
LeftJoin("bus_equipment_materials bem", "bmi.equipment_materials_id = bem.equipment_materials_id").
|
||||
Where("bem.project_id = ?", projectID).
|
||||
Where("bmi.out_put = ?", 2).
|
||||
Where("bmi.created_at >= ?", startAt).
|
||||
Where("bmi.created_at <= ?", endAt).
|
||||
Group("bem.equipment_materials_name").
|
||||
Scan(&list); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
513
third/excel/export.go
Normal file
513
third/excel/export.go
Normal file
@ -0,0 +1,513 @@
|
||||
package excel
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/samber/lo"
|
||||
"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"
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
// 记录累计完成量和下一周计划量
|
||||
type ExcelInfo struct {
|
||||
// 累计完成量
|
||||
Accumulated int
|
||||
// 下一周计划量
|
||||
NextWeek int
|
||||
}
|
||||
|
||||
// 导出单个方阵
|
||||
func ExportExcel(fangZhenID string, startAt, endAt string) ([]*model.WorkStatusProgressRes, map[string]ExcelInfo, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
type scheduler struct {
|
||||
// 实体
|
||||
WorkStatus *entity.WorkSchedule
|
||||
// 总进度
|
||||
FinishedPtr int
|
||||
// 总量
|
||||
Total int
|
||||
}
|
||||
|
||||
type Schdule map[string]scheduler
|
||||
|
||||
Get := func(scheduleMap Schdule, workID string) (int, int) {
|
||||
if scheduler, ok := scheduleMap[workID]; ok {
|
||||
return scheduler.FinishedPtr, scheduler.Total
|
||||
}
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
// 获取指定方阵ID的所有计划
|
||||
ScheduleData := []entity.WorkSchedule{}
|
||||
if err := dao.WorkSchedule.Ctx(ctx).Where(dao.WorkSchedule.Columns().FangzhenId, fangZhenID).
|
||||
Scan(&ScheduleData); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// 存储指定 workID 的累计完成量
|
||||
workMap := make(map[string]ExcelInfo)
|
||||
|
||||
// 计算下一周的日期
|
||||
nextWeekStart := lo.Must(time.Parse("2006-01-02", startAt)).AddDate(0, 0, 7)
|
||||
nextWeekEnd := lo.Must(time.Parse("2006-01-02", endAt)).AddDate(0, 0, 7)
|
||||
|
||||
scheduleMap := make(Schdule)
|
||||
// 遍历 ScheduleData 将数据按照 WorkID 分组
|
||||
lo.ForEach(ScheduleData, func(item entity.WorkSchedule, _ int) {
|
||||
if work, ok := workMap[item.WorkId]; ok {
|
||||
// 累加完成量
|
||||
work.Accumulated += item.FinishedNum
|
||||
workMap[item.WorkId] = work
|
||||
} else {
|
||||
workMap[item.WorkId] = ExcelInfo{
|
||||
Accumulated: item.FinishedNum,
|
||||
}
|
||||
}
|
||||
|
||||
// 如果计划处于下一周,则添加到下一周计划量中
|
||||
stime := lo.Must(time.Parse("2006-01-02", item.StartAt.Format("2006-01-02")))
|
||||
if stime.After(nextWeekStart) && stime.Before(nextWeekEnd) {
|
||||
var scheduleDetails []model.WorkScheduleDetail
|
||||
if err := json.Unmarshal([]byte(item.Detail), &scheduleDetails); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
nextweekPlanNum := 0
|
||||
|
||||
for _, detail := range scheduleDetails {
|
||||
// 如果 Date 大于开始时间并小于结束时间
|
||||
if detail.Date <= endAt {
|
||||
nextweekPlanNum += detail.PlanNum
|
||||
}
|
||||
}
|
||||
|
||||
work := workMap[item.WorkId]
|
||||
work.NextWeek += nextweekPlanNum
|
||||
workMap[item.WorkId] = work
|
||||
}
|
||||
|
||||
// 如果开始时间大于当前时间,则跳过
|
||||
startTime := item.StartAt.Format("Y-m-d")
|
||||
if startTime > startAt {
|
||||
return
|
||||
}
|
||||
|
||||
// 如果不存在则直接添加
|
||||
_, ok := scheduleMap[item.WorkId]
|
||||
if !ok {
|
||||
scheduleMap[item.WorkId] = scheduler{
|
||||
WorkStatus: &item,
|
||||
}
|
||||
}
|
||||
|
||||
// 反序列化 item.Detail 字段
|
||||
var scheduleDetails []model.WorkScheduleDetail
|
||||
if err := json.Unmarshal([]byte(item.Detail), &scheduleDetails); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
lo.ForEach(scheduleDetails, func(detail model.WorkScheduleDetail, _ int) {
|
||||
// 如果 Date 大于开始时间并小于结束时间
|
||||
if detail.Date <= endAt {
|
||||
scheduler := scheduleMap[item.WorkId]
|
||||
scheduler.Total += detail.PlanNum
|
||||
scheduler.FinishedPtr += detail.FinishedNum
|
||||
scheduleMap[item.WorkId] = scheduler
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
// 获取指定方阵ID的所有项目
|
||||
workStatusList := []entity.WorkStatus{}
|
||||
if err := dao.WorkStatus.Ctx(ctx).Where(dao.WorkStatus.Columns().FangzhenId, fangZhenID).Scan(&workStatusList); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// 父列表
|
||||
parentList := []entity.WorkStatus{}
|
||||
// 子列表,使用 map 存储,键为父元素的 ID
|
||||
childrenMap := make(map[int][]entity.WorkStatus)
|
||||
|
||||
// 遍历所有数据,将父级和子集分开
|
||||
for i := 0; i < len(workStatusList); i++ {
|
||||
item := workStatusList[i]
|
||||
if item.Parent == 0 {
|
||||
parentList = append(parentList, item)
|
||||
} else {
|
||||
childrenMap[item.Parent] = append(childrenMap[item.Parent], item)
|
||||
}
|
||||
}
|
||||
|
||||
index := 1
|
||||
|
||||
projectList := make([]*model.WorkStatusProgressRes, 0, len(parentList))
|
||||
// 遍历父级,将子集添加到父级的字段中
|
||||
for _, parent := range parentList {
|
||||
projectItem := model.WorkStatusProgressRes{
|
||||
ID: index,
|
||||
WorkID: parent.WorkId,
|
||||
Startat: parent.StartAt.String(),
|
||||
Endat: parent.EndAt.String(),
|
||||
Name: parent.WorkName,
|
||||
Total: parent.Total,
|
||||
Finished: parent.Finished,
|
||||
Status: parent.Status,
|
||||
WorkType: parent.Type,
|
||||
Children: make([]model.WorkStatusProgressRes, 0, len(workStatusList)),
|
||||
}
|
||||
index++
|
||||
|
||||
// 子类的总量和完成量
|
||||
var subTotal, subDone int
|
||||
|
||||
// 从 map 中获取子元素,将子元素添加到父元素的字段中
|
||||
for _, child := range childrenMap[int(parent.Id)] {
|
||||
// 如果计划数量等于实际数量,且原状态非3,则更新状态为2
|
||||
if child.Total == child.Finished && child.Status != 3 {
|
||||
// 并且 total,finished 不为 0
|
||||
if child.Total != 0 && child.Finished != 0 {
|
||||
child.Status = 2
|
||||
}
|
||||
}
|
||||
|
||||
// 根据 WorkID 获取计划总量和实际进度
|
||||
finishedPtr, total := Get(scheduleMap, child.WorkId)
|
||||
|
||||
subTotal += child.Total
|
||||
subDone += child.Finished
|
||||
|
||||
projectItem.Children = append(projectItem.Children, model.WorkStatusProgressRes{
|
||||
ID: index,
|
||||
WorkID: child.WorkId,
|
||||
Startat: child.StartAt.String(),
|
||||
Endat: child.EndAt.String(),
|
||||
Name: child.WorkName,
|
||||
Total: child.Total,
|
||||
Finished: child.Finished,
|
||||
IsPercent: child.IsPercent,
|
||||
Status: child.Status,
|
||||
WorkType: child.Type,
|
||||
PlanProgress: &model.PlanProgress{
|
||||
Finished: &finishedPtr,
|
||||
Total: &total,
|
||||
},
|
||||
})
|
||||
index++
|
||||
}
|
||||
|
||||
// 修改父项目的总量和进度
|
||||
projectItem.Total = subTotal
|
||||
projectItem.Finished = subDone
|
||||
|
||||
projectList = append(projectList, &projectItem)
|
||||
}
|
||||
|
||||
return projectList, workMap, nil
|
||||
}
|
||||
|
||||
// 导出一个子项目
|
||||
func AccumulateProject(subProjectID string, startAt, endAt string) ([]*model.WorkStatusProgressRes, map[string]ExcelInfo, error) {
|
||||
fangzhenList := []entity.QianqiFangzhen{}
|
||||
if err := dao.QianqiFangzhen.Ctx(context.Background()).
|
||||
Where(dao.QianqiFangzhen.Columns().ProjectId, subProjectID).Scan(&fangzhenList); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
cumulativeProjects := []*model.WorkStatusProgressRes{}
|
||||
|
||||
// 用于判断是否为第一次迭代
|
||||
firstTime := true
|
||||
|
||||
excelMap := make(map[string]ExcelInfo)
|
||||
|
||||
// 遍历项目列表
|
||||
for index := 0; index < len(fangzhenList); index++ {
|
||||
// 获取当前项目
|
||||
project := fangzhenList[index]
|
||||
|
||||
// 获取当前项目的子项目
|
||||
childProjects, emap, err := ExportExcel(strconv.Itoa(project.Id), startAt, endAt)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
excelMap = lo.Assign(emap, excelMap)
|
||||
|
||||
// 如果是第一次迭代,将子项目列表赋值给累积项目列表
|
||||
if firstTime {
|
||||
cumulativeProjects = childProjects
|
||||
// 更新标志变量,表示已经不是第一次迭代
|
||||
firstTime = false
|
||||
continue
|
||||
}
|
||||
|
||||
// 遍历子项目列表
|
||||
for childIndex := 0; childIndex < len(childProjects); childIndex++ {
|
||||
// 获取当前子项目和对应的累积项目
|
||||
singleChild := childProjects[childIndex]
|
||||
cumulativeChild := cumulativeProjects[childIndex]
|
||||
|
||||
// 更新累积项目的总数和完成数
|
||||
cumulativeChild.Total += singleChild.Total
|
||||
cumulativeChild.Finished += singleChild.Finished
|
||||
|
||||
// 如果当前子项目还有子项目,也进行同样的操作
|
||||
if len(singleChild.Children) > 0 {
|
||||
for subChildIndex := 0; subChildIndex < len(singleChild.Children); subChildIndex++ {
|
||||
// 获取当前子项目的子项目和对应的累积项目
|
||||
singleSubChild := singleChild.Children[subChildIndex]
|
||||
cumulativeSubChild := cumulativeChild.Children[subChildIndex]
|
||||
|
||||
// 更新累积项目的总数和完成数
|
||||
cumulativeSubChild.Total += singleSubChild.Total
|
||||
cumulativeSubChild.Finished += singleSubChild.Finished
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return cumulativeProjects, excelMap, nil
|
||||
}
|
||||
|
||||
// 传递一个 []*model.WorkStatusProgressRes,将数据写入到 excel 中
|
||||
func WriteExcel(tableName string, list []*model.WorkStatusProgressRes, aiResult map[string]int) (*excelize.File, error) {
|
||||
f, err := excelize.OpenFile("resource/template.xlsx")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 创建一个工作表
|
||||
sheetName := "Sheet1"
|
||||
|
||||
headerPrefixes := []string{"一、", "二、", "三、", "四、", "五、", "六、", "七、", "八、", "九、", "十、", "十一、", "十二、"}
|
||||
childPrefixes := []string{"1、", "2、", "3、", "4、", "5、", "6、", "7、", "8、", "9、", "10、", "11、", "12、"}
|
||||
|
||||
// 修改表头
|
||||
f.SetCellValue(sheetName, "A1", tableName)
|
||||
|
||||
// 遍历 list
|
||||
rowIndex := 3
|
||||
lo.ForEach(list, func(item *model.WorkStatusProgressRes, index int) {
|
||||
setExcelValues(f, sheetName, rowIndex, item, headerPrefixes[index], aiResult)
|
||||
rowIndex++
|
||||
|
||||
// 遍历子集
|
||||
lo.ForEach(item.Children, func(child model.WorkStatusProgressRes, childIndex int) {
|
||||
setExcelValues(f, sheetName, rowIndex, &child, childPrefixes[childIndex], aiResult)
|
||||
rowIndex++
|
||||
})
|
||||
})
|
||||
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func setExcelValues(f *excelize.File, sheetName string, i int, item *model.WorkStatusProgressRes, prefix string, aiResult map[string]int) {
|
||||
// 单元格 A
|
||||
f.SetCellValue(sheetName, fmt.Sprintf("A%d", i), prefix+item.Name) // 父名
|
||||
f.SetCellValue(sheetName, fmt.Sprintf("B%d", i), item.Total) // 总量
|
||||
f.SetCellValue(sheetName, fmt.Sprintf("C%d", i), item.Finished) // 完成
|
||||
|
||||
var percentComplete float64
|
||||
if item.Total != 0 {
|
||||
percentComplete = float64(item.Finished) / float64(item.Total) * 100
|
||||
} else {
|
||||
percentComplete = 0
|
||||
}
|
||||
f.SetCellValue(sheetName, fmt.Sprintf("D%d", i), fmt.Sprintf("%.f%%", percentComplete)) // 总量完成百分比
|
||||
|
||||
if item.PlanProgress != nil {
|
||||
total := 0
|
||||
if item.PlanProgress.Total != nil {
|
||||
total = *item.PlanProgress.Total
|
||||
}
|
||||
f.SetCellValue(sheetName, fmt.Sprintf("E%d", i), total) // 计划量
|
||||
|
||||
// AI 填报数量 F
|
||||
// 用 workID 作为 key,获取 aiResult 中的值,如果不存在则为 0
|
||||
aiResultValue := aiResult[item.WorkID]
|
||||
f.SetCellValue(sheetName, fmt.Sprintf("F%d", i), aiResultValue)
|
||||
|
||||
finished := 0
|
||||
if item.PlanProgress.Finished != nil {
|
||||
finished = *item.PlanProgress.Finished
|
||||
}
|
||||
f.SetCellValue(sheetName, fmt.Sprintf("G%d", i), finished) // 计划完成量
|
||||
|
||||
var plannedPercentComplete float64
|
||||
if item.PlanProgress != nil && item.PlanProgress.Finished != nil && item.PlanProgress.Total != nil && *item.PlanProgress.Total != 0 {
|
||||
plannedPercentComplete = float64(*item.PlanProgress.Finished) / float64(*item.PlanProgress.Total) * 100
|
||||
} else {
|
||||
plannedPercentComplete = 0
|
||||
}
|
||||
|
||||
f.SetCellValue(sheetName, fmt.Sprintf("H%d", i), fmt.Sprintf("%.f%%", plannedPercentComplete)) // 计划完成百分比
|
||||
}
|
||||
}
|
||||
|
||||
// 根据方阵ID获取子项目名
|
||||
func GetSubProjectName(fangZhenID string) (string, string) {
|
||||
// select sb.project_name
|
||||
// from sub_project as sb
|
||||
// left join qianqi_fangzhen as qf on qf.project_id = sb.id
|
||||
// where qf.id = 1959;
|
||||
|
||||
result, err := dao.SubProject.Ctx(context.Background()).As("sb").
|
||||
Where("qf.id", fangZhenID).
|
||||
InnerJoin("qianqi_fangzhen as qf", "qf.project_id = sb.id").
|
||||
Fields("sb.project_name,qf.name").All()
|
||||
if err != nil {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
// 获取子项目名
|
||||
subProjectName := result[0].GMap().Get("project_name").(string)
|
||||
// 方阵名
|
||||
fangZhenName := result[0].GMap().Get("name").(string)
|
||||
|
||||
return subProjectName, fangZhenName
|
||||
}
|
||||
|
||||
// 根据子项目ID获取其主项目名和子项目名
|
||||
func GetNameById(subProjectID string) (string, string) {
|
||||
// select sp.project_name as projectName,sb.project_name as subProjectName
|
||||
// from sys_project as sp
|
||||
// inner join sub_project as sb on sb.project_id = sp.id
|
||||
// where sb.id = 23;
|
||||
|
||||
result, err := dao.SubProject.Ctx(context.Background()).As("sb").
|
||||
Where("sb.id", subProjectID).
|
||||
InnerJoin("sys_project as sp", "sb.project_id = sp.id").
|
||||
Fields("sp.project_name as projectName,sb.project_name as subProjectName").All()
|
||||
if err != nil {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
// 获取主项目名
|
||||
projectName := result[0].GMap().Get("projectName").(string)
|
||||
// 获取子项目名
|
||||
subProjectName := result[0].GMap().Get("subProjectName").(string)
|
||||
|
||||
return projectName, subProjectName
|
||||
}
|
||||
|
||||
func SetExcelValue(f *excelize.File, sheetName string, index int, item model.WorkStatusProgressRes, excelmap map[string]ExcelInfo, postionName string) {
|
||||
// 设计数量 F10
|
||||
total := item.Total
|
||||
// 本周完成量 G10
|
||||
finished := item.Finished
|
||||
|
||||
// 累计完成量和下周计划量
|
||||
accumulated, nextWeek := 0, 0
|
||||
|
||||
if data, exists := excelmap[item.WorkID]; exists {
|
||||
accumulated = data.Accumulated
|
||||
nextWeek = data.NextWeek
|
||||
}
|
||||
|
||||
// 累计完成百分比 单元格 I10
|
||||
percentComplete := 0.0
|
||||
if total != 0 {
|
||||
percentComplete = float64(accumulated) / float64(total) * 100
|
||||
}
|
||||
|
||||
// 名称
|
||||
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", "C", index), item.Name)
|
||||
f.SetCellStyle(sheetName, fmt.Sprintf("%s%d", "C", index), fmt.Sprintf("%s%d", "C", index), lo.Must(f.NewStyle(Style)))
|
||||
|
||||
// 位置
|
||||
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", "D", index), postionName)
|
||||
f.SetCellStyle(sheetName, fmt.Sprintf("%s%d", "D", index), fmt.Sprintf("%s%d", "D", index), lo.Must(f.NewStyle(Style)))
|
||||
|
||||
// 单位 样式
|
||||
f.SetCellStyle(sheetName, fmt.Sprintf("%s%d", "E", index), fmt.Sprintf("%s%d", "E", index), lo.Must(f.NewStyle(Style)))
|
||||
|
||||
// 设计数量
|
||||
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", "F", index), total)
|
||||
f.SetCellStyle(sheetName, fmt.Sprintf("%s%d", "F", index), fmt.Sprintf("%s%d", "F", index), lo.Must(f.NewStyle(Style)))
|
||||
|
||||
// 本周
|
||||
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", "G", index), finished)
|
||||
f.SetCellStyle(sheetName, fmt.Sprintf("%s%d", "G", index), fmt.Sprintf("%s%d", "G", index), lo.Must(f.NewStyle(Style)))
|
||||
|
||||
// 累计完成量
|
||||
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", "H", index), accumulated)
|
||||
f.SetCellStyle(sheetName, fmt.Sprintf("%s%d", "H", index), fmt.Sprintf("%s%d", "H", index), lo.Must(f.NewStyle(Style)))
|
||||
|
||||
// 累计完成百分比
|
||||
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", "I", index), fmt.Sprintf("%.f%%", percentComplete))
|
||||
f.SetCellStyle(sheetName, fmt.Sprintf("%s%d", "I", index), fmt.Sprintf("%s%d", "I", index), lo.Must(f.NewStyle(Style)))
|
||||
|
||||
// 合并 jk 单元格
|
||||
_ = f.MergeCell(sheetName, fmt.Sprintf("J%d", index), fmt.Sprintf("K%d", index))
|
||||
f.SetCellStyle(sheetName, fmt.Sprintf("J%d", index), fmt.Sprintf("K%d", index), lo.Must(f.NewStyle(Style)))
|
||||
|
||||
// 水平居中 jk 单元格
|
||||
f.SetCellStyle(sheetName, fmt.Sprintf("J%d", index), fmt.Sprintf("K%d", index), lo.Must(f.NewStyle(&excelize.Style{
|
||||
Border: []excelize.Border{
|
||||
{Type: "left", Color: "000000", Style: 1},
|
||||
{Type: "top", Color: "000000", Style: 1},
|
||||
{Type: "bottom", Color: "000000", Style: 1},
|
||||
{Type: "right", Color: "000000", Style: 1},
|
||||
},
|
||||
Fill: excelize.Fill{},
|
||||
Font: nil,
|
||||
Alignment: &excelize.Alignment{
|
||||
Horizontal: "center", // 水平居中
|
||||
Indent: 0,
|
||||
JustifyLastLine: false,
|
||||
ReadingOrder: 0,
|
||||
RelativeIndent: 0,
|
||||
ShrinkToFit: false,
|
||||
TextRotation: 0,
|
||||
Vertical: "", // 垂直居中
|
||||
WrapText: false,
|
||||
},
|
||||
})))
|
||||
|
||||
// 下周计划量
|
||||
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", "J", index), nextWeek)
|
||||
|
||||
// L
|
||||
f.SetCellStyle(sheetName, fmt.Sprintf("%s%d", "L", index), fmt.Sprintf("%s%d", "L", index), lo.Must(f.NewStyle(Style)))
|
||||
}
|
||||
|
||||
func GetWeekNumbers(endTime string) (int, error) {
|
||||
end, err := time.Parse(time.DateOnly, endTime)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("解析结束时间错误: %w", err)
|
||||
}
|
||||
|
||||
_, endWeek := end.ISOWeek()
|
||||
|
||||
return endWeek, nil
|
||||
}
|
||||
|
||||
func CalculateTotalProgress(list []*model.WorkStatusProgressRes) float64 {
|
||||
var totalFinished, total int
|
||||
|
||||
var calculate func(w model.WorkStatusProgressRes)
|
||||
calculate = func(w model.WorkStatusProgressRes) {
|
||||
totalFinished += w.Finished
|
||||
total += w.Total
|
||||
for _, child := range w.Children {
|
||||
calculate(child)
|
||||
}
|
||||
}
|
||||
|
||||
for _, item := range list {
|
||||
calculate(*item)
|
||||
}
|
||||
|
||||
if total == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
return (float64(totalFinished) / float64(total)) * 100
|
||||
}
|
302
third/excel/template.go
Normal file
302
third/excel/template.go
Normal file
@ -0,0 +1,302 @@
|
||||
package excel
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
GlobalStyle int // 全局样式
|
||||
GlobalStyleBold int // 基于GlobalStyle的 加粗
|
||||
GlobalStyleBoldSize int // 基于GlobalStyle的 加粗 加字体大小
|
||||
)
|
||||
|
||||
var Style = &excelize.Style{
|
||||
// 边框
|
||||
Border: []excelize.Border{
|
||||
{Type: "left", Color: "000000", Style: 1},
|
||||
{Type: "top", Color: "000000", Style: 1},
|
||||
{Type: "bottom", Color: "000000", Style: 1},
|
||||
{Type: "right", Color: "000000", Style: 1},
|
||||
},
|
||||
// 居中
|
||||
Alignment: &excelize.Alignment{
|
||||
Vertical: "center", // 上下居中
|
||||
Horizontal: "center", // 左右居中
|
||||
WrapText: true, // 自动换行
|
||||
},
|
||||
}
|
||||
|
||||
const (
|
||||
excelName = "Book1.xlsx"
|
||||
sheetName = "Sheet1"
|
||||
)
|
||||
|
||||
func styleFunc(f *excelize.File) {
|
||||
baseStyle := excelize.Style{
|
||||
Border: []excelize.Border{
|
||||
{Type: "left", Color: "000000", Style: 1},
|
||||
{Type: "top", Color: "000000", Style: 1},
|
||||
{Type: "bottom", Color: "000000", Style: 1},
|
||||
{Type: "right", Color: "000000", Style: 1},
|
||||
},
|
||||
Alignment: &excelize.Alignment{
|
||||
Vertical: "center",
|
||||
Horizontal: "center",
|
||||
WrapText: true,
|
||||
},
|
||||
}
|
||||
|
||||
font := excelize.Font{
|
||||
Color: "#262626",
|
||||
Bold: true,
|
||||
}
|
||||
|
||||
// 创建全局样式
|
||||
GlobalStyle, _ = f.NewStyle(&baseStyle)
|
||||
|
||||
// 创建加粗样式
|
||||
baseStyle.Font = &font
|
||||
GlobalStyleBold, _ = f.NewStyle(&baseStyle)
|
||||
|
||||
// 创建加粗加字体大小样式
|
||||
font.Size = 12
|
||||
baseStyle.Font = &font
|
||||
GlobalStyleBoldSize, _ = f.NewStyle(&baseStyle)
|
||||
}
|
||||
|
||||
// @Title cellMerge
|
||||
// @Description 合并单元格
|
||||
// @Author 铁憨憨[cory] 2024-10-18 14:20:40
|
||||
// @Param f
|
||||
// @Param qie 单元格切片
|
||||
// @Param qie 影响行(height高度相关)
|
||||
// @Param height 单元格高度(0隐藏当前行 -1取消自定义高度 其他单元格高度)
|
||||
// @Param style 样式
|
||||
// @Param str 单元格值
|
||||
func cellMerge(f *excelize.File, qie []string, height float64, style int, str string) {
|
||||
if len(qie) > 1 {
|
||||
f.MergeCell(sheetName, qie[0], qie[1]) // 合并单元格
|
||||
}
|
||||
f.SetCellStyle(sheetName, qie[0], qie[len(qie)-1], style) // 设置样式
|
||||
|
||||
if height != -1 {
|
||||
re := regexp.MustCompile("[0-9]+")
|
||||
rowStr := strings.Join(re.FindAllString(qie[0], -1), "")
|
||||
if rowNum, err := strconv.Atoi(rowStr); err == nil {
|
||||
f.SetRowHeight(sheetName, rowNum, height) // 设置行高
|
||||
}
|
||||
}
|
||||
|
||||
f.SetCellValue(sheetName, qie[0], str) // 设置单元格的值
|
||||
}
|
||||
|
||||
// 创建一个基础的 Excel 模板返回 *excelize.File
|
||||
func CreateExcelTemplate() (*excelize.File, error) {
|
||||
f := excelize.NewFile()
|
||||
defer f.Close()
|
||||
|
||||
// 初始化样式
|
||||
styleFunc(f)
|
||||
|
||||
// 初始化单元格布局
|
||||
businessFunc(f)
|
||||
|
||||
index, err := f.NewSheet(sheetName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
f.SetActiveSheet(index)
|
||||
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func businessFunc(f *excelize.File) {
|
||||
// 一、
|
||||
one := []string{"A1", "L1"}
|
||||
cellMerge(f, one, 64, GlobalStyleBoldSize, "兴义市捧乍猪场坪农业光伏电站项目EPC总承包项目周报")
|
||||
// 二、
|
||||
two := []string{"A2", "B2"}
|
||||
cellMerge(f, two, 30, GlobalStyle, "项目名称:")
|
||||
two2 := []string{"C2", "G2"}
|
||||
cellMerge(f, two2, -1, GlobalStyle, "兴义市捧乍猪场坪农业光伏电站项目EPC总承包项目")
|
||||
two3 := []string{"H2"}
|
||||
cellMerge(f, two3, -1, GlobalStyleBold, "周报期数")
|
||||
two4 := []string{"I2"}
|
||||
cellMerge(f, two4, -1, GlobalStyle, "53")
|
||||
two5 := []string{"J2", "J3"}
|
||||
cellMerge(f, two5, -1, GlobalStyle, "日期:")
|
||||
two6 := []string{"K2", "L3"}
|
||||
cellMerge(f, two6, -1, GlobalStyle, "2024.10.05-2024.10.11")
|
||||
// 三、
|
||||
three := []string{"A3"}
|
||||
cellMerge(f, three, -1, GlobalStyle, "序号")
|
||||
three2 := []string{"B3", "I3"}
|
||||
cellMerge(f, three2, -1, GlobalStyle, "工程实施主要情况")
|
||||
// 四、
|
||||
four := []string{"A4"}
|
||||
cellMerge(f, four, -1, GlobalStyle, "1")
|
||||
four2 := []string{"B4"}
|
||||
cellMerge(f, four2, -1, GlobalStyle, "项目基本信息")
|
||||
four3 := []string{"C4"}
|
||||
cellMerge(f, four3, -1, GlobalStyle, "装机容量:")
|
||||
four4 := []string{"D4", "E4"}
|
||||
cellMerge(f, four4, -1, GlobalStyle, "开工日期:2022-08-15")
|
||||
four5 := []string{"F4", "H4"}
|
||||
cellMerge(f, four5, -1, GlobalStyle, "计划并网日期:")
|
||||
four6 := []string{"I4"}
|
||||
cellMerge(f, four6, -1, GlobalStyle, "总进度完成:")
|
||||
four7 := []string{"J4", "L4"}
|
||||
cellMerge(f, four7, -1, GlobalStyle, "96.5%")
|
||||
// 五、
|
||||
five := []string{"A5"}
|
||||
cellMerge(f, five, -1, GlobalStyle, "2")
|
||||
five2 := []string{"B5"}
|
||||
cellMerge(f, five2, -1, GlobalStyle, "本周完成主要形象进度")
|
||||
five3 := []string{"C5", "L5"}
|
||||
cellMerge(f, five3, -1, GlobalStyle, "")
|
||||
// 六、
|
||||
six := []string{"A6"}
|
||||
cellMerge(f, six, -1, GlobalStyle, "3")
|
||||
six2 := []string{"B6"}
|
||||
cellMerge(f, six2, -1, GlobalStyle, "下周计划主要形象进度")
|
||||
six3 := []string{"C6", "L6"}
|
||||
cellMerge(f, six3, -1, GlobalStyle, "")
|
||||
// 七、
|
||||
seven := []string{"A7"}
|
||||
cellMerge(f, seven, -1, GlobalStyle, "4")
|
||||
seven2 := []string{"B7"}
|
||||
cellMerge(f, seven2, -1, GlobalStyle, "质量(施工质量、设备材料到货验收)情况")
|
||||
seven3 := []string{"C7", "L7"}
|
||||
cellMerge(f, seven3, -1, GlobalStyle, "")
|
||||
// 八、
|
||||
eight := []string{"A8"}
|
||||
cellMerge(f, eight, -1, GlobalStyle, "5")
|
||||
eight2 := []string{"B8"}
|
||||
cellMerge(f, eight2, -1, GlobalStyle, "人员到位情况及施工器具")
|
||||
eight3 := []string{"C8", "L8"}
|
||||
cellMerge(f, eight3, -1, GlobalStyle, "")
|
||||
// 九、
|
||||
nine := []string{"A9", "A23"}
|
||||
cellMerge(f, nine, -1, GlobalStyle, "6")
|
||||
nine2 := []string{"B9", "B23"}
|
||||
cellMerge(f, nine2, -1, GlobalStyle, "")
|
||||
nine3 := []string{"C9"}
|
||||
cellMerge(f, nine3, -1, GlobalStyle, "名称")
|
||||
nine4 := []string{"D9"}
|
||||
cellMerge(f, nine4, -1, GlobalStyle, "位置")
|
||||
nine5 := []string{"E9"}
|
||||
cellMerge(f, nine5, -1, GlobalStyle, "单位")
|
||||
nine6 := []string{"F9"}
|
||||
cellMerge(f, nine6, -1, GlobalStyle, "设计数量")
|
||||
nine7 := []string{"G9"}
|
||||
cellMerge(f, nine7, -1, GlobalStyle, "本周完成情况")
|
||||
nine8 := []string{"H9"}
|
||||
cellMerge(f, nine8, -1, GlobalStyle, "累计完成量")
|
||||
nine9 := []string{"I9"}
|
||||
cellMerge(f, nine9, -1, GlobalStyle, "累计完成率")
|
||||
nine10 := []string{"J9", "K9"}
|
||||
cellMerge(f, nine10, -1, GlobalStyle, "下周计划完成")
|
||||
nine11 := []string{"L9"}
|
||||
cellMerge(f, nine11, -1, GlobalStyle, "备注")
|
||||
// 十
|
||||
ten := []string{"B10", "B23"}
|
||||
cellMerge(f, ten, -1, GlobalStyle, "施工进度(根据项目类型及进度情况自行分项,不用太细,能体现整体进度即可)")
|
||||
ten2 := []string{"C10"}
|
||||
cellMerge(f, ten2, -1, GlobalStyle, "基础")
|
||||
ten3 := []string{"D10"}
|
||||
cellMerge(f, ten3, -1, GlobalStyle, "光伏区")
|
||||
ten4 := []string{"E10"}
|
||||
cellMerge(f, ten4, -1, GlobalStyle, "个")
|
||||
ten5 := []string{"F10"}
|
||||
cellMerge(f, ten5, -1, GlobalStyle, "45450")
|
||||
ten6 := []string{"G10"}
|
||||
cellMerge(f, ten6, -1, GlobalStyle, "1415")
|
||||
ten7 := []string{"H10"}
|
||||
cellMerge(f, ten7, -1, GlobalStyle, "15445")
|
||||
ten8 := []string{"I10"}
|
||||
cellMerge(f, ten8, -1, GlobalStyle, "90")
|
||||
ten9 := []string{"J10", "K10"}
|
||||
cellMerge(f, ten9, -1, GlobalStyle, "545")
|
||||
ten10 := []string{"L10"}
|
||||
cellMerge(f, ten10, -1, GlobalStyle, "备注")
|
||||
// 二十四、
|
||||
twentyFour := []string{"A24", "A41"}
|
||||
cellMerge(f, twentyFour, -1, GlobalStyle, "7")
|
||||
twentyFour2 := []string{"B24", "B41"}
|
||||
cellMerge(f, twentyFour2, -1, GlobalStyle, "主要设备材料到货情况(列出主要材料即可)")
|
||||
twentyFour3 := []string{"C24"}
|
||||
cellMerge(f, twentyFour3, -1, GlobalStyle, "设备材料名称")
|
||||
twentyFour4 := []string{"D24"}
|
||||
cellMerge(f, twentyFour4, -1, GlobalStyle, "")
|
||||
twentyFour5 := []string{"E24"}
|
||||
cellMerge(f, twentyFour5, -1, GlobalStyle, "单位")
|
||||
twentyFour6 := []string{"F24"}
|
||||
cellMerge(f, twentyFour6, -1, GlobalStyle, "设计数量")
|
||||
twentyFour7 := []string{"G24"}
|
||||
cellMerge(f, twentyFour7, -1, GlobalStyle, "本周到货量")
|
||||
twentyFour8 := []string{"H24"}
|
||||
cellMerge(f, twentyFour8, -1, GlobalStyle, "累计到货量")
|
||||
twentyFour9 := []string{"I24"}
|
||||
cellMerge(f, twentyFour9, -1, GlobalStyle, "累计到货率")
|
||||
twentyFour10 := []string{"J24", "K24"}
|
||||
cellMerge(f, twentyFour10, -1, GlobalStyle, "计划到货日期")
|
||||
twentyFour11 := []string{"L24"}
|
||||
cellMerge(f, twentyFour11, -1, GlobalStyle, "备注")
|
||||
// 二十五
|
||||
twentyFive2 := []string{"C25"}
|
||||
cellMerge(f, twentyFive2, -1, GlobalStyle, "角钢")
|
||||
twentyFive3 := []string{"D25"}
|
||||
cellMerge(f, twentyFive3, -1, GlobalStyle, "")
|
||||
twentyFive4 := []string{"E25"}
|
||||
cellMerge(f, twentyFive4, -1, GlobalStyle, "")
|
||||
twentyFive5 := []string{"F25"}
|
||||
cellMerge(f, twentyFive5, -1, GlobalStyle, "")
|
||||
twentyFive6 := []string{"G25"}
|
||||
cellMerge(f, twentyFive6, -1, GlobalStyle, "")
|
||||
twentyFive7 := []string{"H25"}
|
||||
cellMerge(f, twentyFive7, -1, GlobalStyle, "")
|
||||
twentyFive8 := []string{"I25"}
|
||||
cellMerge(f, twentyFive8, -1, GlobalStyle, "90")
|
||||
twentyFive9 := []string{"J25", "K25"}
|
||||
cellMerge(f, twentyFive9, -1, GlobalStyle, "")
|
||||
twentyFive10 := []string{"L25"}
|
||||
cellMerge(f, twentyFive10, -1, GlobalStyle, "")
|
||||
// 四十二
|
||||
fortyTwo := []string{"A42"}
|
||||
cellMerge(f, fortyTwo, -1, GlobalStyle, "8")
|
||||
fortyTwo2 := []string{"B42"}
|
||||
cellMerge(f, fortyTwo2, -1, GlobalStyle, "存在问题及需要协调的事项")
|
||||
fortyTwo3 := []string{"C42", "L42"}
|
||||
cellMerge(f, fortyTwo3, -1, GlobalStyle, "无")
|
||||
// 四十三
|
||||
fortyThree := []string{"A43"}
|
||||
cellMerge(f, fortyThree, 90, GlobalStyle, "9")
|
||||
fortyThree2 := []string{"B43"}
|
||||
cellMerge(f, fortyThree2, -1, GlobalStyle, "照片")
|
||||
fortyThree3 := []string{"C43", "E43"}
|
||||
cellMerge(f, fortyThree3, -1, GlobalStyle, "")
|
||||
fortyThree4 := []string{"F43", "H43"}
|
||||
cellMerge(f, fortyThree4, -1, GlobalStyle, "")
|
||||
fortyThree5 := []string{"I43", "L43"}
|
||||
cellMerge(f, fortyThree5, -1, GlobalStyle, "")
|
||||
// 四十四
|
||||
fortyFour := []string{"A44"}
|
||||
cellMerge(f, fortyFour, -1, GlobalStyle, "10")
|
||||
fortyFour2 := []string{"B44"}
|
||||
cellMerge(f, fortyFour2, -1, GlobalStyle, "填报:")
|
||||
fortyFour3 := []string{"C44", "L44"}
|
||||
cellMerge(f, fortyFour3, -1, GlobalStyleBold, "审核:")
|
||||
|
||||
// 整体样式,不需要动
|
||||
f.SetColWidth(sheetName, "A", "A", 8)
|
||||
f.SetColWidth(sheetName, "B", "B", 11)
|
||||
f.SetColWidth(sheetName, "C", "C", 26)
|
||||
f.SetColWidth(sheetName, "D", "D", 26)
|
||||
f.SetColWidth(sheetName, "H", "H", 13)
|
||||
f.SetCellStyle(sheetName, "A1", "B44", GlobalStyleBold) // 注意:B44到时候需要取excel最大行数
|
||||
}
|
Reference in New Issue
Block a user