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