Files
zmkgC/api/project/index_count.go
2025-07-07 20:11:59 +08:00

163 lines
4.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package project
import (
"context"
"encoding/json"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/glog"
)
// PvModuleStatsReq 用于查询统计信息的请求结构体
type PvModuleStatsReq struct {
g.Meta `path:"moduleStats" method:"get" tags:"项目首页相关" summary:"获取模块的统计信息"`
SubProjectId string `json:"subProjectId"`
FangzhenId string `json:"fangzhenId"`
}
// PvModuleStatsRes 用于封装统计结果的响应结构体,调整为使用字符串描述类型
type PvModuleStatsRes struct {
Data []struct {
TypeDesc string `json:"typeDesc"`
Total int `json:"total"`
Complete int `json:"complete"`
} `json:"data"`
}
func (p Project) QueryPvModuleStats(ctx context.Context, req *PvModuleStatsReq) (res *PvModuleStatsRes, err error) {
model := g.Model("pv_module").Fields("type, COUNT(*) as total, SUM(CASE WHEN status='2' THEN 1 ELSE 0 END) as complete").Group("type")
if req.SubProjectId != "" {
model = model.Where("sub_projectid", req.SubProjectId)
}
if req.FangzhenId != "" {
model = model.Where("fangzhen_id", req.FangzhenId)
}
var stats []struct {
Type int `json:"type"`
Total int `json:"total"`
Complete int `json:"complete"`
}
err = model.Scan(&stats)
if err != nil {
glog.Error(ctx, "查询 pv_module 统计信息失败:", err)
return nil, err
}
var resultStats []struct {
TypeDesc string `json:"typeDesc"`
Total int `json:"total"`
Complete int `json:"complete"`
}
for _, stat := range stats {
typeDesc, exists := ConstMap[stat.Type]
if !exists {
glog.Error(ctx, "未知的 type", stat.Type)
continue // 或者处理未知类型
}
resultStats = append(resultStats, struct {
TypeDesc string `json:"typeDesc"`
Total int `json:"total"`
Complete int `json:"complete"`
}{TypeDesc: typeDesc, Total: stat.Total, Complete: stat.Complete})
}
return &PvModuleStatsRes{Data: resultStats}, nil
}
// 修改请求结构体以接收类型切片
type PvModuleQueryReq struct {
g.Meta `path:"indexCount" method:"get" tags:"项目首页相关" summary:"根据子项目ID、方针ID和类型来获取数据"`
SubProjectId string `json:"subProjectId"`
FangzhenId string `json:"fangzhenId"`
Types []int `json:"types"` // 新增字段,用于指定需要查询的类型
}
// 修改返回结果结构体,去除 total 和 complete
type PvModuleQueryRes struct {
Data map[int][]PVModule `json:"data"`
View string `json:"view" orm:"view"`
}
func (p Project) QueryPVModule(ctx context.Context, req *PvModuleQueryReq) (res *PvModuleQueryRes, err error) {
res = new(PvModuleQueryRes)
// 查询对应方针的 View 值
g.Model("qianqi_fangzhen").Where("id", req.FangzhenId).Scan(&res)
// 准备存储每种 type 对应的数据
data := make(map[int][]PVModule)
// 仅针对请求中指定的 type 进行查询
for _, t := range req.Types {
var modules []PVModule
queryModel := g.Model("pv_module").Where("type", t)
if req.SubProjectId != "" {
queryModel = queryModel.Where("sub_projectid", req.SubProjectId)
}
if req.FangzhenId != "" {
queryModel = queryModel.Where("fangzhen_id", req.FangzhenId)
}
err = queryModel.Scan(&modules)
if err != nil {
continue // 或处理错误
}
// 对 modules 进行处理,比如反序列化 Detail 字段、添加倾角和方位角等
var processedModules []PVModule
for _, m := range modules {
// 反序列化 Detail 字段
var detailMap map[string]interface{}
err = json.Unmarshal([]byte(m.Detail), &detailMap)
if err != nil {
continue
}
// 为其添加倾角和方位角
//detailMap["roll"] = m.Tilt
//detailMap["heading"] = m.Azimuth
// 重新序列化
serializedDetail, _ := json.Marshal(detailMap)
processedModules = append(processedModules, PVModule{
ID: m.ID,
FangzhenID: m.FangzhenID,
SubProjectID: m.SubProjectID,
WorkID: m.WorkID,
Name: m.Name,
Status: m.Status,
DoneTime: m.DoneTime,
Detail: string(serializedDetail),
Type: t,
DeviceID: m.DeviceID,
EquipmentSn: "",
})
}
// 遍历processedModules中那些DeviceID不为空的值取出数据然后查询equipment表根据设备id查询equipment表得到设备sn赋值给EquipmentSn
for i, mod := range processedModules {
if mod.DeviceID != "" {
var equipment struct {
EquipmentSn string
}
// 使用g.DB模型查询equipment表中的SN字段假定设备的序列号字段为SN
err := g.Model("equipment").Fields("equipmentSn").Where("id", mod.DeviceID).Scan(&equipment)
if err != nil {
// 处理查询错误,例如记录日志或跳过
continue
}
// 更新模块的EquipmentSn字段
processedModules[i].EquipmentSn = equipment.EquipmentSn
}
}
// 更新数据存储结构
data[t] = processedModules
}
res.Data = data
return res, nil
}