怎么精简代码func BasinTree(id string) ([]*models.Basin, error) { var basins []*models.Basin res := common.DB.Where("watershed_id = ?", id).Find(&basins) for _, item := range basins { if res.RowsAffected > 0 { //查询流域内所有河道 var subrivers []*models.SubRiver var rivers models.PsRiver common.DB.Model(&rivers).Where("watershed_id = ?", item.ID).Find(&subrivers) item.SubRivers = subrivers var totalL float64 common.DB.Table("ps_rivers").Select("COALESCE(sum(segment_length), 0)").Where("watershed_id = ?", item.ID).Scan(&totalL) item.TotalLength = totalL //查询流域内所有湖泊 var sublakes []*models.SubLake var lakes models.PsLake common.DB.Model(&lakes).Where("watershed_id = ?", item.ID).Find(&sublakes) var totalA float64 common.DB.Table("ps_lakes").Select("COALESCE(sum(area),0)").Where("watershed_id = ?", item.ID).Scan(&totalA) item.TotalArea = totalA item.SubLakes = sublakes } } for _, item := range basins { if res.RowsAffected > 0 { id = strconv.FormatUint(uint64(item.ID), 10) item.SubBasins, _ = BasinTree(id) for _, v := range item.SubBasins { item.TotalArea = item.TotalArea + v.TotalArea item.TotalLength = item.TotalLength + v.TotalLength } if len(item.SubBasins) == 0 { return nil, nil } } } return basins, nil } func BasinInfo(ctx *gin.Context) { id := ctx.Query("id") var req models.Basin var err error resp := models.Response{ Code: 0, Msg: "success", } if len(id) == 0 { resp.Code = 400 resp.Msg = "请输入id值" ctx.JSON(400, resp) return } res := common.DB.Where("id = ?", id).Take(&req) if res.Error != nil { resp.Code = 400 resp.Msg = "查询失败" resp.Data = res.Error ctx.JSON(400, resp) return } //查询流域内所有河道 var subrivers []*models.SubRiver var rivers models.PsRiver var totalL float64 common.DB.Model(&rivers).Where("watershed_id = ?", id).Find(&subrivers) common.DB.Table("ps_rivers").Select("COALESCE(sum(segment_length), 0)").Where("watershed_id = ?", id).Scan(&totalL) req.SubRivers = subrivers req.TotalLength = totalL //查询流域内所有湖泊 var sublakes []*models.SubLake var lakes models.PsLake var totalA float64 common.DB.Model(&lakes).Where("watershed_id = ?", id).Find(&sublakes) common.DB.Table("ps_lakes").Select("COALESCE(sum(area),0)").Where("watershed_id = ?", id).Scan(&totalA) req.SubLakes = sublakes req.TotalArea = totalA req.SubBasins, err = BasinTree(id) if err != nil { resp.Code = 500 resp.Msg = "创建树失败" resp.Data = err ctx.JSON(500, resp) return } for _, v := range req.SubBasins { req.TotalArea = req.TotalArea + v.TotalArea req.TotalLength = req.TotalLength + v.TotalLength } resp.Data = req ctx.JSON(200, resp) }
时间: 2023-11-27 16:50:40 浏览: 123
golang sqlx err missing destination name id in *main.User
可以采用递归的方式,将查询流域信息以及子流域信息的代码进行整合,避免代码重复。
```
func getSubRivers(id uint) ([]*models.SubRiver, float64) {
var subrivers []*models.SubRiver
var rivers models.PsRiver
var totalL float64
common.DB.Model(&rivers).Where("watershed_id = ?", id).Find(&subrivers)
common.DB.Table("ps_rivers").Select("COALESCE(sum(segment_length), 0)").Where("watershed_id = ?", id).Scan(&totalL)
return subrivers, totalL
}
func getSubLakes(id uint) ([]*models.SubLake, float64) {
var sublakes []*models.SubLake
var lakes models.PsLake
var totalA float64
common.DB.Model(&lakes).Where("watershed_id = ?", id).Find(&sublakes)
common.DB.Table("ps_lakes").Select("COALESCE(sum(area),0)").Where("watershed_id = ?", id).Scan(&totalA)
return sublakes, totalA
}
func BasinTree(id string) ([]*models.Basin, error) {
var basins []*models.Basin
res := common.DB.Where("watershed_id = ?", id).Find(&basins)
if res.RowsAffected == 0 {
return nil, nil
}
for _, item := range basins {
item.SubRivers, item.TotalLength = getSubRivers(item.ID)
item.SubLakes, item.TotalArea = getSubLakes(item.ID)
item.SubBasins, _ = BasinTree(strconv.FormatUint(uint64(item.ID), 10))
for _, v := range item.SubBasins {
item.TotalArea += v.TotalArea
item.TotalLength += v.TotalLength
}
}
return basins, nil
}
func BasinInfo(ctx *gin.Context) {
id := ctx.Query("id")
var req models.Basin
var err error
resp := models.Response{
Code: 0,
Msg: "success",
}
if len(id) == 0 {
resp.Code = 400
resp.Msg = "请输入id值"
ctx.JSON(400, resp)
return
}
res := common.DB.Where("id = ?", id).Take(&req)
if res.Error != nil {
resp.Code = 400
resp.Msg = "查询失败"
resp.Data = res.Error
ctx.JSON(400, resp)
return
}
req.SubRivers, req.TotalLength = getSubRivers(req.ID)
req.SubLakes, req.TotalArea = getSubLakes(req.ID)
req.SubBasins, err = BasinTree(id)
if err != nil {
resp.Code = 500
resp.Msg = "创建树失败"
resp.Data = err
ctx.JSON(500, resp)
return
}
for _, v := range req.SubBasins {
req.TotalArea += v.TotalArea
req.TotalLength += v.TotalLength
}
resp.Data = req
ctx.JSON(200, resp)
}
```
阅读全文