statistic_service.go 25 KB

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