statistic_service.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. package backend
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "kpt-tmr-group/model"
  9. "kpt-tmr-group/pkg/logger/zaplog"
  10. "kpt-tmr-group/pkg/xerr"
  11. operationPb "kpt-tmr-group/proto/go/backend/operation"
  12. "net/http"
  13. "sort"
  14. "sync"
  15. "gorm.io/gorm"
  16. "github.com/xuri/excelize/v2"
  17. "go.uber.org/zap"
  18. )
  19. type PastureClientHandler func(ctx context.Context, pastureId int64, body interface{}) error
  20. // PastureDetailById 获取指定牧场详情
  21. func (s *StoreEntry) PastureDetailById(ctx context.Context, pastureId int64) (*model.GroupPasture, error) {
  22. result := &model.GroupPasture{Id: pastureId}
  23. if err := s.DB.Where("is_delete = ? and is_show = ?", operationPb.IsShow_OK, operationPb.IsShow_OK).First(result).Error; err != nil {
  24. return nil, xerr.WithStack(err)
  25. }
  26. return result, nil
  27. }
  28. func (s *StoreEntry) PastureHttpClient(ctx context.Context, apiUrl string, pastureId int64, body, response interface{}) (*model.GroupPasture, error) {
  29. pastureDetail, err := s.PastureDetailById(ctx, pastureId)
  30. if err != nil {
  31. if errors.Is(err, gorm.ErrRecordNotFound) {
  32. return nil, xerr.Customf("该牧场不存在")
  33. }
  34. zaplog.Error("PastureHttpClient", zap.Any("Err", err), zap.Int64("pastureId", pastureId))
  35. return nil, xerr.Customf("该牧场数据错误,Err:%s", err)
  36. }
  37. pastureClient := model.NewPastureClient(pastureDetail)
  38. url := fmt.Sprintf("%s/%s", pastureDetail.Domain, apiUrl)
  39. result, err := pastureClient.DoPost(url, body)
  40. if err != nil {
  41. return pastureDetail, xerr.WithStack(err)
  42. }
  43. zaplog.Info("PastureHttpClient", zap.String("url", url), zap.Any("request", body), zap.String("response", string(result)))
  44. if err = json.Unmarshal(result, response); err != nil {
  45. return pastureDetail, xerr.WithStack(err)
  46. }
  47. return pastureDetail, nil
  48. }
  49. // SearchFormulaEstimateList 配方评估
  50. func (s *StoreEntry) SearchFormulaEstimateList(ctx context.Context, req *operationPb.SearchFormulaEstimateRequest) (*model.PastureCommonResponse, error) {
  51. body := &model.PastureCommonRequest{
  52. Name: req.ApiName,
  53. Page: req.Pagination.Page,
  54. Offset: req.Pagination.PageOffset,
  55. PageCount: req.Pagination.PageSize,
  56. ReturnType: "Map",
  57. ParamMaps: &model.FormulaEstimateParams{
  58. PastureId: fmt.Sprintf("%d", req.PastureId),
  59. StartTime: req.StartTime,
  60. StopTime: req.EndTime,
  61. Search: fmt.Sprintf("%d", req.SearchType),
  62. TempletId: fmt.Sprintf("%d", req.TemplateId),
  63. Barid: fmt.Sprintf("%d", req.BarnId),
  64. },
  65. }
  66. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  67. if _, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  68. return nil, xerr.WithStack(err)
  69. }
  70. return response, nil
  71. }
  72. // SearchInventoryStatistics 库存管理-库存统计
  73. func (s *StoreEntry) SearchInventoryStatistics(ctx context.Context, req *operationPb.SearchInventoryStatisticsRequest) (*model.PastureCommonResponse, error) {
  74. body := &model.PastureCommonRequest{
  75. Name: req.ApiName,
  76. Page: req.Pagination.Page,
  77. Offset: req.Pagination.PageOffset,
  78. PageCount: req.Pagination.PageSize,
  79. ReturnType: "Map",
  80. ParamMaps: &model.InventoryStatisticsParams{
  81. PastureId: fmt.Sprintf("%d", req.PastureId),
  82. StartTime: req.StartTime,
  83. StopTime: req.EndTime,
  84. FeedName: req.FeedName,
  85. },
  86. }
  87. response := &model.PastureCommonResponse{
  88. Data: &model.PastureCommonData{
  89. List: make([]*model.InventoryStatisticsList, 0),
  90. },
  91. }
  92. if _, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  93. return nil, xerr.WithStack(err)
  94. }
  95. return response, nil
  96. }
  97. // InventoryStatisticsExcelExport 库存管理-库存统计-报表导出
  98. func (s *StoreEntry) InventoryStatisticsExcelExport(ctx context.Context, req *operationPb.SearchInventoryStatisticsRequest) (*bytes.Buffer, error) {
  99. result, err := s.SearchInventoryStatistics(ctx, req)
  100. if err != nil {
  101. return nil, xerr.WithStack(err)
  102. }
  103. b, _ := json.Marshal(result.Data.List)
  104. inventoryStatisticsList := make([]*model.InventoryStatisticsList, 0)
  105. if err = json.Unmarshal(b, &inventoryStatisticsList); err != nil {
  106. return nil, xerr.Customf("牧场端返回数据错误")
  107. }
  108. file := excelize.NewFile()
  109. defer file.Close()
  110. streamWriter, err := file.NewStreamWriter(model.DefaultSheetName)
  111. if err != nil {
  112. return nil, xerr.WithStack(err)
  113. }
  114. // 表头
  115. titles := map[string][]interface{}{
  116. "A1": {"饲料名称", "期初", nil, "用料", nil, nil, nil, "期末"},
  117. "A2": {nil, "期初库存(kg)", "期初金额(元)", "入库重量(kg)", "系统出库重量(kg)", "人工用料重量(kg)", "损耗重量", "期末库存(kg)", "期末金额(kg)"},
  118. }
  119. for cell, values := range titles {
  120. if err = streamWriter.SetRow(cell, values); err != nil {
  121. return nil, xerr.WithStack(err)
  122. }
  123. }
  124. for i, item := range inventoryStatisticsList {
  125. cell, err := excelize.CoordinatesToCellName(1, i+3)
  126. if err != nil {
  127. zaplog.Error("InventoryStatisticsExcelExport CoordinatesToCellName", zap.Any("Err", err))
  128. continue
  129. }
  130. row := make([]interface{}, 0)
  131. row = append(row, item.FeedName, item.StartSum, item.StartPrice, item.LaidSum, item.UseSumXT, item.UseSumRG, item.UseSumXH, item.StopSum, item.StopPrice)
  132. if err = streamWriter.SetRow(cell, row); err != nil {
  133. return nil, xerr.WithStack(err)
  134. }
  135. }
  136. hvCell := map[string]string{
  137. "A1": "A2",
  138. "B1": "C1",
  139. "D1": "G1",
  140. "H1": "I1",
  141. }
  142. // 合并单元格
  143. for h, v := range hvCell {
  144. if err = streamWriter.MergeCell(h, v); err != nil {
  145. return nil, xerr.WithStack(err)
  146. }
  147. }
  148. // 修改样式
  149. /*style1, err := file.NewStyle(&excelize.Style{
  150. Fill: excelize.Fill{Type: "pattern", Color: []string{"#DFEBF6"}, Pattern: 1},
  151. Alignment: &excelize.Alignment{Horizontal: "center"},
  152. })
  153. if err != nil {
  154. return nil, xerr.WithStack(err)
  155. }
  156. style1, err := file.NewStyle(&excelize.Style{
  157. //Fill: excelize.Fill{Type: "pattern", Color: []string{"#DFEBF6"}, Pattern: 1},
  158. Alignment: &excelize.Alignment{Horizontal: "center"},
  159. })
  160. if err != nil {
  161. return nil, xerr.WithStack(err)
  162. }
  163. if err = file.SetCellStyle(model.DefaultSheetName, "A1", "A1", style1); err != nil {
  164. return nil, xerr.WithStack(err)
  165. }*/
  166. if err = streamWriter.Flush(); err != nil {
  167. return nil, xerr.WithStack(err)
  168. }
  169. return file.WriteToBuffer()
  170. }
  171. // SearchUserMaterialsStatistics 库存管理-用料分析
  172. func (s *StoreEntry) SearchUserMaterialsStatistics(ctx context.Context, req *operationPb.SearchUserMaterialsStatisticsRequest) (*model.PastureCommonResponse, error) {
  173. body := &model.PastureCommonRequest{
  174. Name: req.ApiName,
  175. Page: req.Pagination.Page,
  176. Offset: req.Pagination.PageOffset,
  177. PageCount: req.Pagination.PageSize,
  178. ReturnType: "Map",
  179. Checked: req.ErrorCheck,
  180. ParamMaps: &model.UserMaterialsStatisticsParams{
  181. PastureId: fmt.Sprintf("%d", req.PastureId),
  182. StartTime: req.StartTime,
  183. StopTime: req.EndTime,
  184. FeedName: req.FeedName,
  185. Typea: fmt.Sprintf("%d", req.TypeCheck),
  186. },
  187. }
  188. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{
  189. List: &model.UserMaterialsList{},
  190. }}
  191. if _, err := s.PastureHttpClient(ctx, model.UrlReportForm, int64(req.PastureId), body, response); err != nil {
  192. return nil, xerr.WithStack(err)
  193. }
  194. return response, nil
  195. }
  196. func (s *StoreEntry) UserMaterialsStatisticsExcelExport(ctx context.Context, req *operationPb.SearchUserMaterialsStatisticsRequest) (*bytes.Buffer, error) {
  197. result, err := s.SearchUserMaterialsStatistics(ctx, req)
  198. if err != nil {
  199. return nil, xerr.WithStack(err)
  200. }
  201. b, _ := json.Marshal(result.Data.List)
  202. userMaterialsList := &model.UserMaterialsList{}
  203. if err = json.Unmarshal(b, userMaterialsList); err != nil {
  204. return nil, xerr.Customf("牧场端返回数据错误")
  205. }
  206. file := excelize.NewFile()
  207. defer file.Close()
  208. streamWriter, err := file.NewStreamWriter(model.DefaultSheetName)
  209. if err != nil {
  210. return nil, xerr.WithStack(err)
  211. }
  212. // 表数据
  213. excelValuesList := map[int][]interface{}{}
  214. cProp := make([]string, 0)
  215. for _, data2 := range userMaterialsList.Data2 {
  216. excelValuesList[1] = append(excelValuesList[1], data2.Label)
  217. for _, c := range data2.Children {
  218. excelValuesList[2] = append(excelValuesList[2], c.Label)
  219. cProp = append(cProp, c.Prop)
  220. }
  221. }
  222. for cell, values := range excelValuesList {
  223. if err = streamWriter.SetRow(fmt.Sprintf("A%d", cell), values); err != nil {
  224. return nil, xerr.WithStack(err)
  225. }
  226. delete(excelValuesList, cell)
  227. }
  228. for i, data1 := range userMaterialsList.Data1 {
  229. data1Map, ok := data1.(map[string]interface{})
  230. if ok {
  231. for _, prop := range cProp {
  232. newValue := ""
  233. if value, yes := data1Map[prop]; yes {
  234. if value != nil {
  235. switch v := value.(type) {
  236. case string:
  237. newValue = v
  238. case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
  239. newValue = fmt.Sprintf("%d", v)
  240. default:
  241. newValue = fmt.Sprintf("%v", v)
  242. }
  243. }
  244. excelValuesList[i+3] = append(excelValuesList[i+3], newValue)
  245. }
  246. }
  247. }
  248. }
  249. excelValuesKeys := make([]int, 0)
  250. for k, _ := range excelValuesList {
  251. excelValuesKeys = append(excelValuesKeys, k)
  252. }
  253. sort.Ints(excelValuesKeys)
  254. for _, v := range excelValuesKeys {
  255. if err = streamWriter.SetRow(fmt.Sprintf("A%d", v), excelValuesList[v]); err != nil {
  256. return nil, xerr.WithStack(err)
  257. }
  258. }
  259. if err = streamWriter.Flush(); err != nil {
  260. return nil, xerr.WithStack(err)
  261. }
  262. return file.WriteToBuffer()
  263. }
  264. // SearchPriceStatistics 库存管理-价格分析
  265. func (s *StoreEntry) SearchPriceStatistics(ctx context.Context, req *operationPb.SearchPriceStatisticsRequest) (*model.PastureCommonResponse, error) {
  266. body := &model.PastureCommonRequest{
  267. Name: req.ApiName,
  268. Page: req.Pagination.Page,
  269. Offset: req.Pagination.PageOffset,
  270. PageCount: req.Pagination.PageSize,
  271. ReturnType: "Map",
  272. ParamMaps: &model.PriceStatisticsParams{
  273. PastureId: fmt.Sprintf("%d", req.PastureId),
  274. StartTime: req.StartTime,
  275. StopTime: req.EndTime,
  276. FeedName: req.FeedName,
  277. },
  278. }
  279. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  280. if _, err := s.PastureHttpClient(ctx, model.UrlReportForm, int64(req.PastureId), body, response); err != nil {
  281. return nil, xerr.WithStack(err)
  282. }
  283. return response, nil
  284. }
  285. // SearchFeedStatistics 饲喂效率-效率统计
  286. func (s *StoreEntry) SearchFeedStatistics(ctx context.Context, req *operationPb.SearchFeedStatisticsRequest) (*model.FeedStatisticsResponse, error) {
  287. res := &model.FeedStatisticsResponse{
  288. Code: http.StatusOK,
  289. Msg: "ok",
  290. Data: &model.FeedStatisticsData{
  291. List: make(map[string]interface{}),
  292. },
  293. }
  294. if len(req.PastureId) <= 0 {
  295. return res, nil
  296. }
  297. if req.CattleCategoryId > 0 {
  298. cattleList := s.ForageEnumList(ctx).Data.CattleParentCategory
  299. for _, v := range cattleList {
  300. if v.Value == operationPb.CattleCategoryParent_Kind(req.CattleCategoryId) {
  301. req.CattleCategoryName = v.Label
  302. break
  303. }
  304. }
  305. }
  306. times := ""
  307. if req.ClassNumber > 0 {
  308. times = fmt.Sprintf("%d", req.ClassNumber)
  309. }
  310. wg := sync.WaitGroup{}
  311. wg.Add(len(req.PastureId))
  312. for _, v := range req.PastureId {
  313. go func(pastureId int32) {
  314. defer wg.Done()
  315. body := &model.PastureCommonRequest{
  316. Name: req.ApiName,
  317. Page: req.Pagination.Page,
  318. Offset: req.Pagination.PageOffset,
  319. PageCount: req.Pagination.PageSize,
  320. ReturnType: "Map",
  321. ParamMaps: &model.FeedStatisticsParams{
  322. PastureId: fmt.Sprintf("%d", pastureId),
  323. StartTime: req.StartTime,
  324. StopTime: req.StartTime,
  325. Date: req.StartTime,
  326. FeedTName: req.FormulaTemplate,
  327. BarName: req.BarnName,
  328. CowClass: req.CattleCategoryName,
  329. Times: times,
  330. },
  331. }
  332. response := &model.PastureCommonResponse{
  333. Data: &model.PastureCommonData{},
  334. }
  335. groupPasture, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(pastureId), body, response)
  336. if err != nil {
  337. zaplog.Error("SearchFeedStatistics",
  338. zap.Any("pastureId", pastureId),
  339. zap.Any("url", model.UrlDataByName),
  340. zap.Any("body", body),
  341. zap.Any("response", response))
  342. return
  343. }
  344. if response.Code == http.StatusOK && response.Data.List != nil {
  345. if req.ApiName == "getFeedEfficiencySC" {
  346. feedStatisticsConversions := FeedStatisticsConversions(response.Data.List)
  347. feedStatisticsConversions.PastureName = groupPasture.Name
  348. res.Data.List[groupPasture.Name] = FeedStatisticsConversions(response.Data.List)
  349. } else {
  350. res.Data.List[groupPasture.Name] = response.Data.List
  351. }
  352. }
  353. }(v)
  354. }
  355. wg.Wait()
  356. return res, nil
  357. }
  358. // FeedStatisticsConversions 数据转换
  359. func FeedStatisticsConversions(req interface{}) *model.FeedStatisticsConversions {
  360. feedStatisticsList := &model.FeedStatisticsConversions{}
  361. if data, ok := req.([]interface{}); ok && len(data) > 0 {
  362. value := data[0]
  363. if v, yes := value.(map[string]interface{}); yes {
  364. if d, t := v["TMR干物质"]; t {
  365. feedStatisticsList.TmrDryMatter = d.(string)
  366. }
  367. if d, t := v["barname"]; t {
  368. feedStatisticsList.BarName = d.(string)
  369. }
  370. if d, t := v["产奶量"]; t {
  371. feedStatisticsList.MilkYield = d.(string)
  372. }
  373. if d, t := v["今日剩料量"]; t {
  374. feedStatisticsList.TodayLeftovers = d.(string)
  375. }
  376. if d, t := v["公斤奶饲料成本"]; t {
  377. feedStatisticsList.FeedCosts = d.(float64)
  378. }
  379. if d, t := v["剩料率"]; t {
  380. feedStatisticsList.LeftoverRate = d.(string)
  381. }
  382. if d, t := v["实际干物质采食量"]; t {
  383. feedStatisticsList.ActualDryMatterIntake = d.(string)
  384. }
  385. if d, t := v["实际成本"]; t {
  386. feedStatisticsList.ActualCost = d.(string)
  387. }
  388. if d, t := v["实际牛头数"]; t {
  389. feedStatisticsList.ActualNumberOfCattle = d.(string)
  390. }
  391. if d, t := v["应混料量"]; t {
  392. feedStatisticsList.AmountToBeMixed = d.(string)
  393. }
  394. if d, t := v["混料时间"]; t {
  395. feedStatisticsList.MixingTime = d.(string)
  396. }
  397. if d, t := v["牲畜类别"]; t {
  398. feedStatisticsList.CategoryCattleName = d.(string)
  399. }
  400. if d, t := v["理论干物质"]; t {
  401. feedStatisticsList.TheoreticalDryMatter = d.(string)
  402. }
  403. if d, t := v["转投剩料量"]; t {
  404. feedStatisticsList.LeftoverMaterial = d.(float64)
  405. }
  406. if d, t := v["配方干物质采食量"]; t {
  407. feedStatisticsList.FormulatedDryMatterIntake = d.(string)
  408. }
  409. if d, t := v["配方成本"]; t {
  410. feedStatisticsList.CostOfFormulation = d.(string)
  411. }
  412. if d, t := v["采食率"]; t {
  413. feedStatisticsList.FoodIntakeRate = d.(string)
  414. }
  415. if d, t := v["饲料转化率"]; t {
  416. feedStatisticsList.FeedConversionRatio = d.(float64)
  417. }
  418. }
  419. }
  420. return feedStatisticsList
  421. }
  422. // FeedChartStatistics 饲喂效率图表分析
  423. func (s *StoreEntry) FeedChartStatistics(ctx context.Context, req *operationPb.FeedChartStatisticsRequest) (*model.PastureCommonResponse, error) {
  424. body := &model.FeedChartParams{
  425. ParamMaps: &model.ParamMaps{
  426. PastureId: fmt.Sprintf("%d", req.PastureId),
  427. StartTime: req.StartTime,
  428. StopTime: req.StartTime,
  429. Status: req.Status,
  430. },
  431. }
  432. url, ok := model.UrlChart[req.ApiType]
  433. if !ok {
  434. return nil, xerr.Customf("错误的接口类型:%s", req.ApiType)
  435. }
  436. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  437. if _, err := s.PastureHttpClient(ctx, url, int64(req.PastureId), body, response); err != nil {
  438. return nil, xerr.WithStack(err)
  439. }
  440. return response, nil
  441. }
  442. // CowsAnalysis 饲喂效率-牛群评估
  443. func (s *StoreEntry) CowsAnalysis(ctx context.Context, req *operationPb.CowsAnalysisRequest) (*model.PastureCommonResponse, error) {
  444. body := &model.PastureCommonRequest{
  445. Name: req.ApiName,
  446. Page: req.Pagination.Page,
  447. Offset: req.Pagination.PageOffset,
  448. PageCount: req.Pagination.PageSize,
  449. ReturnType: "Map",
  450. ParamMaps: &model.MixFeedStatisticsParams{
  451. PastureId: fmt.Sprintf("%d", req.PastureId),
  452. StartTime: req.StartTime,
  453. StopTime: req.StartTime,
  454. },
  455. }
  456. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  457. if _, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  458. return nil, xerr.WithStack(err)
  459. }
  460. return response, nil
  461. }
  462. // SearchAccuracyAggStatistics 准确性分析-汇总分析
  463. func (s *StoreEntry) SearchAccuracyAggStatistics(ctx context.Context, req *operationPb.AccuracyAggStatisticsRequest) (*model.PastureCommonResponse, error) {
  464. body := &model.FeedChartParams{
  465. ParamMaps: &model.AccuracyAggParams{
  466. PastureId: fmt.Sprintf("%d", req.PastureId),
  467. StartTime: req.StartTime,
  468. StopTime: req.EndTime,
  469. FName: req.Fname,
  470. Sort: req.Sort,
  471. Status: req.Status,
  472. Times: req.Times,
  473. Genre: req.Genre,
  474. IsDate: req.Isdate,
  475. Hlwc1: req.Hlwc1,
  476. Hlwc2: req.Hlwc2,
  477. Hlzq1: req.Hlzq1,
  478. Hlzq2: req.Hlzq2,
  479. Hlzql1: req.Hlzql1,
  480. Hlzql2: req.Hlzql2,
  481. Slwc1: req.Slwc1,
  482. Slwc2: req.Slwc2,
  483. Slzq1: req.Slzq1,
  484. Slzq2: req.Slzq2,
  485. Slzql1: req.Slzql1,
  486. Slzql2: req.Slzql2,
  487. Error: req.IsError,
  488. },
  489. }
  490. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  491. if _, err := s.PastureHttpClient(ctx, model.UrlSummary, int64(req.PastureId), body, response); err != nil {
  492. return nil, xerr.WithStack(err)
  493. }
  494. return response, nil
  495. }
  496. // SearchMixFeedStatistics 准确性分析-混料统计
  497. func (s *StoreEntry) SearchMixFeedStatistics(ctx context.Context, req *operationPb.MixFeedStatisticsRequest) (*model.PastureCommonResponse, error) {
  498. times := ""
  499. if req.ClassNumber > 0 {
  500. times = fmt.Sprintf("%d", req.ClassNumber)
  501. }
  502. body := &model.PastureCommonRequest{
  503. Name: req.ApiName,
  504. Page: req.Pagination.Page,
  505. Offset: req.Pagination.PageOffset,
  506. PageCount: req.Pagination.PageSize,
  507. ReturnType: "Map",
  508. ParamMaps: &model.MixFeedStatisticsParams{
  509. PastureId: fmt.Sprintf("%d", req.PastureId),
  510. StartTime: req.StartTime,
  511. StopTime: req.StartTime,
  512. TmrTName: req.EquipmentName,
  513. ProjName: req.TrainNumber,
  514. Times: times,
  515. ButtonType: req.ButtonType,
  516. TempletName: req.FormulationName,
  517. Isuse: req.IsUse,
  518. Hlwc1: req.Hlwc1,
  519. Hlwc2: req.Hlwc2,
  520. Hlzq1: req.Hlzq1,
  521. Hlzq2: req.Hlzq2,
  522. Hlzql1: req.Hlzql1,
  523. Hlzql2: req.Hlzql2,
  524. Error: req.IsError,
  525. },
  526. }
  527. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  528. if _, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  529. return nil, xerr.WithStack(err)
  530. }
  531. return response, nil
  532. }
  533. // SearchSprinkleStatistics 准确性分析-撒料统计
  534. func (s *StoreEntry) SearchSprinkleStatistics(ctx context.Context, req *operationPb.SprinkleStatisticsRequest) (*model.PastureCommonResponse, error) {
  535. times := ""
  536. if req.ClassNumber > 0 {
  537. times = fmt.Sprintf("%d", req.ClassNumber)
  538. }
  539. body := &model.PastureCommonRequest{
  540. Name: req.ApiName,
  541. Page: req.Pagination.Page,
  542. Offset: req.Pagination.PageOffset,
  543. PageCount: req.Pagination.PageSize,
  544. ReturnType: "Map",
  545. ParamMaps: &model.SprinkleStatisticsParams{
  546. PastureId: fmt.Sprintf("%d", req.PastureId),
  547. StartTime: req.StartTime,
  548. StopTime: req.StartTime,
  549. TmrTName: req.EquipmentName,
  550. ProjName: req.TrainNumber,
  551. Times: times,
  552. ButtonType: req.ButtonType,
  553. TempletName: req.FormulationName,
  554. Isuse: req.IsUse,
  555. Fname: req.BarnName,
  556. Slwc1: req.Slwc1,
  557. Slwc2: req.Slwc2,
  558. Slzq2: req.Slzq2,
  559. Slzq1: req.Slzq1,
  560. Slzql1: req.Slzql1,
  561. Slzql2: req.Slzql2,
  562. Error: req.IsError,
  563. },
  564. }
  565. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  566. if _, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  567. return nil, xerr.WithStack(err)
  568. }
  569. return response, nil
  570. }
  571. // SearchProcessAnalysis 过程分析
  572. func (s *StoreEntry) SearchProcessAnalysis(ctx context.Context, req *operationPb.ProcessAnalysisRequest) (*model.PastureCommonResponse, error) {
  573. body := &model.PastureCommonRequest{
  574. Name: req.ApiName,
  575. Page: req.Pagination.Page,
  576. Offset: req.Pagination.PageOffset,
  577. PageCount: req.Pagination.PageSize,
  578. ReturnType: "Map",
  579. ParamMaps: &model.ProcessAnalysisParams{
  580. PastureId: fmt.Sprintf("%d", req.PastureId),
  581. StartTime: req.StartTime,
  582. StopTime: req.StartTime,
  583. TmrTName: req.TmrName,
  584. IsCompleted: "",
  585. LpPlanType: fmt.Sprintf("%d", req.PlanType),
  586. FClassId: req.MixFeedType,
  587. Hlzq1: req.Hlzq1,
  588. Hlzq2: req.Hlzq2,
  589. Hlwc1: req.Hlwc1,
  590. Hlwc2: req.Hlwc2,
  591. Slwc1: req.Slwc1,
  592. Slwc2: req.Slwc2,
  593. Slzq2: req.Slzq2,
  594. Slzq1: req.Slzq1,
  595. Error: req.ErrorRange,
  596. },
  597. }
  598. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  599. if _, err := s.PastureHttpClient(ctx, model.UrlProcess, int64(req.PastureId), body, response); err != nil {
  600. return nil, xerr.WithStack(err)
  601. }
  602. return response, nil
  603. }
  604. // GetDataByName 共同接口
  605. func (s *StoreEntry) GetDataByName(ctx context.Context, req *operationPb.GetDataByNameRequest) (*model.PastureCommonResponse, error) {
  606. body := &model.PastureCommonRequest{
  607. Name: req.ApiName,
  608. ParamMaps: &model.GetDataByNameParams{
  609. PastureId: fmt.Sprintf("%d", req.PastureId),
  610. StartTime: req.StartTime,
  611. StopTime: req.StartTime,
  612. },
  613. }
  614. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  615. if _, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  616. return nil, xerr.WithStack(err)
  617. }
  618. return response, nil
  619. }
  620. // GetTrainNumber 获取班次
  621. func (s *StoreEntry) GetTrainNumber(ctx context.Context, req *operationPb.TrainNumberRequest) (*operationPb.TrainNumberResponse, error) {
  622. body := &model.PastureCommonRequest{
  623. Name: req.ApiName,
  624. Page: req.Pagination.Page,
  625. Offset: req.Pagination.PageOffset,
  626. PageCount: req.Pagination.PageSize,
  627. ReturnType: "Map",
  628. ParamMaps: &model.TrainNumberParams{
  629. PastureId: fmt.Sprintf("%d", req.PastureId),
  630. InfoRName: req.InfoName,
  631. },
  632. }
  633. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  634. if _, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  635. return nil, xerr.WithStack(err)
  636. }
  637. result := &operationPb.TrainNumberResponse{
  638. Code: http.StatusOK,
  639. Msg: "ok",
  640. Data: &operationPb.TrainNumberData{List: make([]*operationPb.FormulaOptionEnum, 0)},
  641. }
  642. if response.Data.List == nil {
  643. return result, nil
  644. }
  645. b, _ := json.Marshal(response.Data.List)
  646. trainNumberList := make([]*model.TrainNumberList, 0)
  647. if err := json.Unmarshal(b, &trainNumberList); err != nil {
  648. return nil, xerr.WithStack(err)
  649. }
  650. formulaOption := make([]*operationPb.FormulaOptionEnum, 0)
  651. if len(trainNumberList) > 0 {
  652. infoValue := trainNumberList[0].InfoValue
  653. switch infoValue {
  654. case "1":
  655. formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{
  656. Value: 1,
  657. Label: "第一班",
  658. })
  659. case "2":
  660. formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{
  661. Value: 1,
  662. Label: "第一班",
  663. }, &operationPb.FormulaOptionEnum{
  664. Value: 2,
  665. Label: "第二班",
  666. })
  667. case "3":
  668. formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{
  669. Value: 1,
  670. Label: "第一班",
  671. }, &operationPb.FormulaOptionEnum{
  672. Value: 2,
  673. Label: "第二班",
  674. }, &operationPb.FormulaOptionEnum{
  675. Value: 3,
  676. Label: "第三班",
  677. })
  678. }
  679. }
  680. result.Data.List = formulaOption
  681. return result, nil
  682. }