123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798 |
- 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
- }
|