package project import ( "context" "encoding/json" "unsafe" "github.com/gogf/gf/v2/frame/g" lop "github.com/samber/lo/parallel" ) // 根据大项目ID去查询所有的数据 type ProjectIndexModuleReq struct { g.Meta `path:"index" method:"get" tags:"项目首页相关" summary:"根据项目ID获取数据"` ID int64 `json:"id"` // 项目ID } type ProjectIndexModuleRes struct { Data []PVModule `json:"data"` // PVModule 切片 } // 根据大项目ID去查询所有的 pv_module 数据 func (p Project) ProjectIndexModule(ctx context.Context, req *ProjectIndexModuleReq) (res *ProjectIndexModuleRes, err error) { // 准备返回结果 res = &ProjectIndexModuleRes{} // 构建查询 var modules []PVModule err = g.Model("pv_module").As("pm"). Fields("pm.id, pm.fangzhen_id, pm.sub_projectid, pm.work_id, pm.name, pm.status, pm.done_time, pm.detail, pm.type, pm.tilt,pm.azimuth"). InnerJoin("sub_project sp", "pm.sub_projectid = sp.id"). InnerJoin("sys_project sysp", "sp.project_id = sysp.id"). Where("sysp.id", req.ID). Where("sysp.deleted_at IS NULL"). Scan(&modules) // 错误处理 if err != nil { return nil, err } // 并行处理每个元素 lop.ForEach[PVModule](modules, func(x PVModule, i int) { // 如果不是光伏板则不处理 if x.Type != 15 { return } // 反序列化 detail 字段 var detail map[string]interface{} if err := json.Unmarshal([]byte(x.Detail), &detail); err != nil { return } // 为其添加倾角和方位角 //detail["roll"] = x.Tilt //detail["heading"] = x.Azimuth // 重新序列化 detailBytes, err := json.Marshal(detail) if err != nil { return } // 重新赋值给 modules[i].Detail modules[i].Detail = *(*string)(unsafe.Pointer(&detailBytes)) }) // 将查询结果赋值给返回结构 res.Data = modules return res, nil }