package backend import ( "bytes" "context" "encoding/json" "errors" "fmt" "kpt-tmr-group/model" "net/http" "sort" "sync" operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation" "gitee.com/xuyiping_admin/pkg/logger/zaplog" "gitee.com/xuyiping_admin/pkg/xerr" "gorm.io/gorm" "github.com/xuri/excelize/v2" "go.uber.org/zap" ) type PastureClientHandler func(ctx context.Context, pastureId int64, body interface{}) error // PastureDetailById 获取指定牧场详情 func (s *StoreEntry) PastureDetailById(ctx context.Context, pastureId int64) (*model.GroupPasture, error) { result := &model.GroupPasture{Id: pastureId} if err := s.DB.Where("is_delete = ? and is_show = ?", operationPb.IsShow_OK, operationPb.IsShow_OK).First(result).Error; err != nil { return nil, xerr.WithStack(err) } return result, nil } func (s *StoreEntry) PastureHttpClient(ctx context.Context, apiUrl string, pastureId int64, body, response interface{}) error { pastureDetail, err := s.PastureDetailById(ctx, pastureId) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return xerr.Customf("该牧场不存在") } zaplog.Error("PastureHttpClient", zap.Any("Err", err), zap.Int64("pastureId", pastureId)) return xerr.Customf("该牧场数据错误,Err:%s", err) } pastureClient := model.NewPastureClient(pastureDetail) url := fmt.Sprintf("%s/%s", pastureDetail.Domain, apiUrl) result, err := pastureClient.DoPost(url, body) if err != nil { return xerr.WithStack(err) } zaplog.Info("PastureHttpClient", zap.String("url", url), zap.Any("request", body), zap.String("response", string(result))) if err = json.Unmarshal(result, response); err != nil { return xerr.WithStack(err) } return nil } // SearchFormulaEstimateList 配方评估 func (s *StoreEntry) SearchFormulaEstimateList(ctx context.Context, req *operationPb.SearchFormulaEstimateRequest) (*model.PastureCommonResponse, error) { groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId)) if err != nil { return nil, xerr.WithStack(err) } pastureId := req.PastureId if groupPasture.PastureId > 0 { pastureId = int32(groupPasture.PastureId) } body := &model.PastureCommonRequest{ Name: req.ApiName, Page: req.Pagination.Page, Offset: req.Pagination.PageOffset, ReturnType: "Map", ParamMaps: &model.FormulaEstimateParams{ PastureId: fmt.Sprintf("%d", pastureId), StartTime: req.StartTime, StopTime: req.EndTime, Search: fmt.Sprintf("%d", req.SearchType), TempletId: fmt.Sprintf("%d", req.TemplateId), Barid: fmt.Sprintf("%d", req.BarnId), }, } response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}} if err = s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil { return nil, xerr.WithStack(err) } return response, nil } // SearchInventoryStatistics 库存管理-库存统计 func (s *StoreEntry) SearchInventoryStatistics(ctx context.Context, req *operationPb.SearchInventoryStatisticsRequest) (*model.PastureCommonResponse, error) { groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId)) if err != nil { return nil, xerr.WithStack(err) } pastureId := req.PastureId if groupPasture.PastureId > 0 { pastureId = int32(groupPasture.PastureId) } body := &model.PastureCommonRequest{ Name: req.ApiName, Page: req.Pagination.Page, Offset: req.Pagination.PageOffset, ReturnType: "Map", ParamMaps: &model.InventoryStatisticsParams{ PastureId: fmt.Sprintf("%d", pastureId), StartTime: req.StartTime, StopTime: req.EndTime, FeedName: req.FeedName, }, } response := &model.PastureCommonResponse{ Data: &model.PastureCommonData{ List: make([]*model.InventoryStatisticsList, 0), }, } if err = s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil { return nil, xerr.WithStack(err) } return response, nil } // InventoryStatisticsExcelExport 库存管理-库存统计-报表导出 func (s *StoreEntry) InventoryStatisticsExcelExport(ctx context.Context, req *operationPb.SearchInventoryStatisticsRequest) (*bytes.Buffer, error) { result, err := s.SearchInventoryStatistics(ctx, req) if err != nil { return nil, xerr.WithStack(err) } b, _ := json.Marshal(result.Data.List) inventoryStatisticsList := make([]*model.InventoryStatisticsList, 0) if err = json.Unmarshal(b, &inventoryStatisticsList); err != nil { return nil, xerr.Customf("牧场端返回数据错误") } file := excelize.NewFile() defer file.Close() streamWriter, err := file.NewStreamWriter(model.DefaultSheetName) if err != nil { return nil, xerr.WithStack(err) } // 表头 titles := map[string][]interface{}{ "A1": {"饲料名称", "期初", nil, "用料", nil, nil, nil, "期末"}, "A2": {nil, "期初库存(kg)", "期初金额(元)", "入库重量(kg)", "系统出库重量(kg)", "人工用料重量(kg)", "损耗重量", "期末库存(kg)", "期末金额(kg)"}, } for cell, values := range titles { if err = streamWriter.SetRow(cell, values); err != nil { return nil, xerr.WithStack(err) } } for i, item := range inventoryStatisticsList { cell, err := excelize.CoordinatesToCellName(1, i+3) if err != nil { zaplog.Error("InventoryStatisticsExcelExport CoordinatesToCellName", zap.Any("Err", err)) continue } row := make([]interface{}, 0) row = append(row, item.FeedName, item.StartSum, item.StartPrice, item.LaidSum, item.UseSumXT, item.UseSumRG, item.UseSumXH, item.StopSum, item.StopPrice) if err = streamWriter.SetRow(cell, row); err != nil { return nil, xerr.WithStack(err) } } hvCell := map[string]string{ "A1": "A2", "B1": "C1", "D1": "G1", "H1": "I1", } // 合并单元格 for h, v := range hvCell { if err = streamWriter.MergeCell(h, v); err != nil { return nil, xerr.WithStack(err) } } // 修改样式 /*style1, err := file.NewStyle(&excelize.Style{ Fill: excelize.Fill{Type: "pattern", Color: []string{"#DFEBF6"}, Pattern: 1}, Alignment: &excelize.Alignment{Horizontal: "center"}, }) if err != nil { return nil, xerr.WithStack(err) } style1, err := file.NewStyle(&excelize.Style{ //Fill: excelize.Fill{Type: "pattern", Color: []string{"#DFEBF6"}, Pattern: 1}, Alignment: &excelize.Alignment{Horizontal: "center"}, }) if err != nil { return nil, xerr.WithStack(err) } if err = file.SetCellStyle(model.DefaultSheetName, "A1", "A1", style1); err != nil { return nil, xerr.WithStack(err) }*/ if err = streamWriter.Flush(); err != nil { return nil, xerr.WithStack(err) } return file.WriteToBuffer() } // SearchUserMaterialsStatistics 库存管理-用料分析 func (s *StoreEntry) SearchUserMaterialsStatistics(ctx context.Context, req *operationPb.SearchUserMaterialsStatisticsRequest) (*model.PastureCommonResponse, error) { groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId)) if err != nil { return nil, xerr.WithStack(err) } pastureId := req.PastureId if groupPasture.PastureId > 0 { pastureId = int32(groupPasture.PastureId) } body := &model.PastureCommonRequest{ Name: req.ApiName, Page: req.Pagination.Page, Offset: req.Pagination.PageOffset, ReturnType: "Map", Checked: req.ErrorCheck, ParamMaps: &model.UserMaterialsStatisticsParams{ PastureId: fmt.Sprintf("%d", pastureId), StartTime: req.StartTime, StopTime: req.EndTime, FeedName: req.FeedName, Typea: fmt.Sprintf("%d", req.TypeCheck), }, } response := &model.PastureCommonResponse{Data: &model.PastureCommonData{ List: &model.UserMaterialsList{}, }} if err = s.PastureHttpClient(ctx, model.UrlReportForm, int64(req.PastureId), body, response); err != nil { return nil, xerr.WithStack(err) } return response, nil } func (s *StoreEntry) UserMaterialsStatisticsExcelExport(ctx context.Context, req *operationPb.SearchUserMaterialsStatisticsRequest) (*bytes.Buffer, error) { result, err := s.SearchUserMaterialsStatistics(ctx, req) if err != nil { return nil, xerr.WithStack(err) } b, _ := json.Marshal(result.Data.List) userMaterialsList := &model.UserMaterialsList{} if err = json.Unmarshal(b, userMaterialsList); err != nil { return nil, xerr.Customf("牧场端返回数据错误") } file := excelize.NewFile() defer file.Close() streamWriter, err := file.NewStreamWriter(model.DefaultSheetName) if err != nil { return nil, xerr.WithStack(err) } // 表数据 excelValuesList := map[int][]interface{}{} cProp := make([]string, 0) for _, data2 := range userMaterialsList.Data2 { excelValuesList[1] = append(excelValuesList[1], data2.Label) for _, c := range data2.Children { excelValuesList[2] = append(excelValuesList[2], c.Label) cProp = append(cProp, c.Prop) } } for cell, values := range excelValuesList { if err = streamWriter.SetRow(fmt.Sprintf("A%d", cell), values); err != nil { return nil, xerr.WithStack(err) } delete(excelValuesList, cell) } for i, data1 := range userMaterialsList.Data1 { data1Map, ok := data1.(map[string]interface{}) if ok { for _, prop := range cProp { newValue := "" if value, yes := data1Map[prop]; yes { if value != nil { switch v := value.(type) { case string: newValue = v case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: newValue = fmt.Sprintf("%d", v) default: newValue = fmt.Sprintf("%v", v) } } excelValuesList[i+3] = append(excelValuesList[i+3], newValue) } } } } excelValuesKeys := make([]int, 0) for k := range excelValuesList { excelValuesKeys = append(excelValuesKeys, k) } sort.Ints(excelValuesKeys) for _, v := range excelValuesKeys { if err = streamWriter.SetRow(fmt.Sprintf("A%d", v), excelValuesList[v]); err != nil { return nil, xerr.WithStack(err) } } if err = streamWriter.Flush(); err != nil { return nil, xerr.WithStack(err) } return file.WriteToBuffer() } // SearchPriceStatistics 库存管理-价格分析 func (s *StoreEntry) SearchPriceStatistics(ctx context.Context, req *operationPb.SearchPriceStatisticsRequest) (*model.PastureCommonResponse, error) { groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId)) if err != nil { return nil, xerr.WithStack(err) } pastureId := req.PastureId if groupPasture.PastureId > 0 { pastureId = int32(groupPasture.PastureId) } body := &model.PastureCommonRequest{ Name: req.ApiName, Page: req.Pagination.Page, Offset: req.Pagination.PageOffset, ReturnType: "Map", ParamMaps: &model.PriceStatisticsParams{ PastureId: fmt.Sprintf("%d", pastureId), StartTime: req.StartTime, StopTime: req.EndTime, FeedName: req.FeedName, }, } response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}} if err = s.PastureHttpClient(ctx, model.UrlReportForm, int64(req.PastureId), body, response); err != nil { return nil, xerr.WithStack(err) } return response, nil } // SearchFeedStatistics 饲喂效率-效率统计 func (s *StoreEntry) SearchFeedStatistics(ctx context.Context, req *operationPb.SearchFeedStatisticsRequest) (*model.FeedStatisticsResponse, error) { res := &model.FeedStatisticsResponse{ Code: http.StatusOK, Msg: "ok", Data: &model.FeedStatisticsData{ List: make(map[string]interface{}), }, } if len(req.PastureId) <= 0 { return res, nil } if req.CattleCategoryId > 0 { cattleList := s.ForageEnumList(ctx).Data.CattleParentCategory for _, v := range cattleList { if v.Value == operationPb.CattleCategoryParent_Kind(req.CattleCategoryId) { req.CattleCategoryName = v.Label break } } } wg := sync.WaitGroup{} wg.Add(len(req.PastureId)) for _, v := range req.PastureId { go func(pastureId int32) { defer wg.Done() groupPasture, err := s.GetGroupPastureListById(ctx, int64(pastureId)) if err != nil { zaplog.Error("SearchFeedStatistics", zap.Any("GetGroupPastureListById", err)) return } pastureID := pastureId if groupPasture.PastureId > 0 { pastureID = int32(groupPasture.PastureId) } body := &model.PastureCommonRequest{ Name: req.ApiName, Page: req.Pagination.Page, Offset: req.Pagination.PageOffset, ReturnType: "Map", ParamMaps: model.NewFeedStatisticsParams(int64(pastureID), req), } response := &model.PastureCommonResponse{ Data: &model.PastureCommonData{}, } if err = s.PastureHttpClient(ctx, model.UrlDataByName, int64(pastureId), body, response); err != nil { zaplog.Error("SearchFeedStatistics", zap.Any("pastureId", pastureId), zap.Any("url", model.UrlDataByName), zap.Any("body", body), zap.Any("response", response)) return } if response.Code == http.StatusOK && response.Data.List != nil { feedStatisticsConversions := FeedStatisticsConversions(response.Data.List, groupPasture.Name) res.Data.List[groupPasture.Name] = feedStatisticsConversions } }(v) } wg.Wait() return res, nil } // FeedStatisticsConversions 数据转换 func FeedStatisticsConversions(req interface{}, pastureName string) []*model.FeedStatisticsConversions { feedStatisticsList := make([]*model.FeedStatisticsConversions, 0) if data, ok := req.([]interface{}); ok && len(data) > 0 { for _, value := range data { feedStatistics := &model.FeedStatisticsConversions{ PastureName: pastureName, } if v, yes := value.(map[string]interface{}); yes { if d, t := v["TMR干物质"]; t { feedStatistics.TmrDryMatter = d.(string) } if d, t := v["barname"]; t { feedStatistics.BarName = d.(string) } if d, t := v["产奶量"]; t { feedStatistics.MilkYield = d.(string) } if d, t := v["今日剩料量"]; t { feedStatistics.TodayLeftovers = d.(string) } if d, t := v["公斤奶饲料成本"]; t { feedStatistics.FeedCosts = d.(float64) } if d, t := v["剩料率"]; t { feedStatistics.LeftoverRate = d.(string) } if d, t := v["实际干物质采食量"]; t { feedStatistics.ActualDryMatterIntake = d.(string) } if d, t := v["实际成本"]; t { feedStatistics.ActualCost = d.(string) } if d, t := v["实际牛头数"]; t { feedStatistics.ActualNumberOfCattle = d.(string) } if d, t := v["应混料量"]; t { feedStatistics.AmountToBeMixed = d.(string) } if d, t := v["混料时间"]; t { feedStatistics.MixingTime = d.(string) } if d, t := v["牲畜类别"]; t { feedStatistics.CategoryCattleName = d.(string) } if d, t := v["理论干物质"]; t { feedStatistics.TheoreticalDryMatter = d.(string) } if d, t := v["转投剩料量"]; t { feedStatistics.LeftoverMaterial = d.(float64) } if d, t := v["配方干物质采食量"]; t { feedStatistics.FormulatedDryMatterIntake = d.(string) } if d, t := v["配方成本"]; t { feedStatistics.CostOfFormulation = d.(string) } if d, t := v["采食率"]; t { feedStatistics.FoodIntakeRate = d.(string) } if d, t := v["饲料转化率"]; t { feedStatistics.FeedConversionRatio = d.(float64) } if d, t := v["配方模板"]; t { feedStatistics.FeedFormulaName = d.(string) } if d, t := v["实际混料量"]; t { feedStatistics.ActualMixedVolume = d.(string) } if d, t := v["撒料量"]; t { feedStatistics.SprinkleVolume = d.(string) } } feedStatisticsList = append(feedStatisticsList, feedStatistics) } } return feedStatisticsList } // FeedChartStatistics 饲喂效率图表分析 func (s *StoreEntry) FeedChartStatistics(ctx context.Context, req *operationPb.FeedChartStatisticsRequest) (*model.PastureCommonResponse, error) { groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId)) if err != nil { return nil, xerr.WithStack(err) } pastureId := req.PastureId if groupPasture.PastureId > 0 { pastureId = int32(groupPasture.PastureId) } body := &model.FeedChartParams{ ParamMaps: &model.ParamMaps{ PastureId: fmt.Sprintf("%d", pastureId), StartTime: req.StartTime, StopTime: req.EndTime, Status: req.Status, }, } url, ok := model.UrlChart[req.ApiType] if !ok { return nil, xerr.Customf("错误的接口类型:%s", req.ApiType) } response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}} if err = s.PastureHttpClient(ctx, url, int64(req.PastureId), body, response); err != nil { return nil, xerr.WithStack(err) } return response, nil } // CowsAnalysis 饲喂效率-牛群评估 func (s *StoreEntry) CowsAnalysis(ctx context.Context, req *operationPb.CowsAnalysisRequest) (*model.PastureCommonResponse, error) { groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId)) if err != nil { return nil, xerr.WithStack(err) } pastureId := req.PastureId if groupPasture.PastureId > 0 { pastureId = int32(groupPasture.PastureId) } body := &model.PastureCommonRequest{ Name: req.ApiName, Page: req.Pagination.Page, Offset: req.Pagination.PageOffset, ReturnType: "Map", ParamMaps: &model.MixFeedStatisticsParams{ PastureId: fmt.Sprintf("%d", pastureId), StartTime: req.StartTime, StopTime: req.StartTime, }, } response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}} if err = s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil { return nil, xerr.WithStack(err) } return response, nil } // SearchAccuracyAggStatistics 准确性分析-汇总分析 func (s *StoreEntry) SearchAccuracyAggStatistics(ctx context.Context, req *operationPb.AccuracyAggStatisticsRequest) (*model.PastureCommonResponse, error) { groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId)) if err != nil { return nil, xerr.WithStack(err) } pastureId := req.PastureId if groupPasture.PastureId > 0 { pastureId = int32(groupPasture.PastureId) } body := &model.FeedChartParams{ ParamMaps: model.NewAccuracyAggParams(int64(pastureId), req), } response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}} if err = s.PastureHttpClient(ctx, model.UrlSummary, int64(req.PastureId), body, response); err != nil { return nil, xerr.WithStack(err) } return response, nil } // SearchMixFeedStatistics 准确性分析-混料统计 func (s *StoreEntry) SearchMixFeedStatistics(ctx context.Context, req *operationPb.MixFeedStatisticsRequest) (*model.PastureCommonResponse, error) { groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId)) if err != nil { return nil, xerr.WithStack(err) } pastureId := req.PastureId if groupPasture.PastureId > 0 { pastureId = int32(groupPasture.PastureId) } body := &model.PastureCommonRequest{ Name: req.ApiName, Page: req.Pagination.Page, Offset: req.Pagination.PageOffset, ReturnType: "Map", ParamMaps: model.NewMixFeedStatisticsParams(int64(pastureId), req), } response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}} if err = s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil { return nil, xerr.WithStack(err) } return response, nil } // SearchSprinkleStatistics 准确性分析-撒料统计 func (s *StoreEntry) SearchSprinkleStatistics(ctx context.Context, req *operationPb.SprinkleStatisticsRequest) (*model.PastureCommonResponse, error) { groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId)) if err != nil { return nil, xerr.WithStack(err) } pastureId := req.PastureId if groupPasture.PastureId > 0 { pastureId = int32(groupPasture.PastureId) } body := &model.PastureCommonRequest{ Name: req.ApiName, Page: req.Pagination.Page, Offset: req.Pagination.PageOffset, ReturnType: "Map", ParamMaps: model.NewSprinkleStatisticsParams(int64(pastureId), req), } response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}} if err = s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil { return nil, xerr.WithStack(err) } return response, nil } // SearchProcessAnalysis 过程分析 func (s *StoreEntry) SearchProcessAnalysis(ctx context.Context, req *operationPb.ProcessAnalysisRequest) (*model.PastureCommonResponse, error) { groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId)) if err != nil { return nil, xerr.WithStack(err) } pastureId := req.PastureId if groupPasture.PastureId > 0 { pastureId = int32(groupPasture.PastureId) } body := &model.PastureCommonRequest{ Name: req.ApiName, Page: req.Pagination.Page, Offset: req.Pagination.PageOffset, ReturnType: "Map", ParamMaps: model.NewProcessAnalysisParams(int64(pastureId), req), } response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}} if err = s.PastureHttpClient(ctx, model.UrlProcess, int64(req.PastureId), body, response); err != nil { return nil, xerr.WithStack(err) } if response.Data.List == nil { response.Data.List = []int64{} } return response, nil } func (s *StoreEntry) AnalysisMixedSprinkleDetail(ctx context.Context, req *operationPb.ProcessMixedSprinkleDetailRequest) (*model.PastureCommonResponse, error) { groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId)) if err != nil { return nil, xerr.WithStack(err) } pastureId := req.PastureId if groupPasture.PastureId > 0 { pastureId = int32(groupPasture.PastureId) } body := &model.PastureCommonRequest{ Name: req.ApiName, Page: req.Pagination.Page, Offset: req.Pagination.PageOffset, ReturnType: "Map", ParamMaps: model.NewMixedSprinkleDetailRequest(int64(pastureId), req), } response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}} apiURl := model.UrlDataByName if req.ApiName == "getprocessAnalysisTB" { apiURl = model.UrlReportForm } if err = s.PastureHttpClient(ctx, apiURl, int64(req.PastureId), body, response); err != nil { return nil, xerr.WithStack(err) } return response, nil } // GetDataByName 共同接口 func (s *StoreEntry) GetDataByName(ctx context.Context, req *operationPb.GetDataByNameRequest) (*model.PastureCommonResponse, error) { groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId)) if err != nil { return nil, xerr.WithStack(err) } pastureId := req.PastureId if groupPasture.PastureId > 0 { pastureId = int32(groupPasture.PastureId) } body := &model.PastureCommonRequest{ Name: req.ApiName, Page: req.Page, Offset: req.Offset, ParamMaps: &model.GetDataByNameParams{ PastureId: fmt.Sprintf("%d", pastureId), StartTime: req.StartTime, StopTime: req.EndTime, Pid: req.Pid, Optdevice: req.Optdevice, FtId: req.Ftid, Name: req.Name, }, } response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}} if err = s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil { return nil, xerr.WithStack(err) } return response, nil } // GetTrainNumber 获取班次 func (s *StoreEntry) GetTrainNumber(ctx context.Context, req *operationPb.TrainNumberRequest) (*operationPb.TrainNumberResponse, error) { groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId)) if err != nil { return nil, xerr.WithStack(err) } pastureId := req.PastureId if groupPasture.PastureId > 0 { pastureId = int32(groupPasture.PastureId) } body := &model.PastureCommonRequest{ Name: req.ApiName, Page: req.Pagination.Page, Offset: req.Pagination.PageOffset, ReturnType: "Map", ParamMaps: &model.TrainNumberParams{ PastureId: fmt.Sprintf("%d", pastureId), InfoRName: req.InfoName, }, } response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}} if err = s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil { return nil, xerr.WithStack(err) } result := &operationPb.TrainNumberResponse{ Code: http.StatusOK, Msg: "ok", Data: &operationPb.TrainNumberData{List: make([]*operationPb.FormulaOptionEnum, 0)}, } if response.Data.List == nil { return result, nil } b, _ := json.Marshal(response.Data.List) trainNumberList := make([]*model.TrainNumberList, 0) if err := json.Unmarshal(b, &trainNumberList); err != nil { return nil, xerr.WithStack(err) } formulaOption := make([]*operationPb.FormulaOptionEnum, 0) if len(trainNumberList) > 0 { infoValue := trainNumberList[0].InfoValue switch infoValue { case "1": formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{ Value: 1, Label: "第一班", }) case "2": formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{ Value: 1, Label: "第一班", }, &operationPb.FormulaOptionEnum{ Value: 2, Label: "第二班", }) case "3": formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{ Value: 1, Label: "第一班", }, &operationPb.FormulaOptionEnum{ Value: 2, Label: "第二班", }, &operationPb.FormulaOptionEnum{ Value: 3, Label: "第三班", }) } } result.Data.List = formulaOption return result, nil }