statistic_service.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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. 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, groupPasture.Name)
  373. res.Data.List[groupPasture.Name] = feedStatisticsConversions
  374. }
  375. }(v)
  376. }
  377. wg.Wait()
  378. return res, nil
  379. }
  380. // FeedStatisticsConversions 数据转换
  381. func FeedStatisticsConversions(req interface{}, pastureName string) []*model.FeedStatisticsConversions {
  382. feedStatisticsList := make([]*model.FeedStatisticsConversions, 0)
  383. if data, ok := req.([]interface{}); ok && len(data) > 0 {
  384. for _, value := range data {
  385. feedStatistics := &model.FeedStatisticsConversions{
  386. PastureName: pastureName,
  387. }
  388. if v, yes := value.(map[string]interface{}); yes {
  389. if d, t := v["TMR干物质"]; t {
  390. feedStatistics.TmrDryMatter = d.(string)
  391. }
  392. if d, t := v["barname"]; t {
  393. feedStatistics.BarName = d.(string)
  394. }
  395. if d, t := v["产奶量"]; t {
  396. feedStatistics.MilkYield = d.(string)
  397. }
  398. if d, t := v["今日剩料量"]; t {
  399. feedStatistics.TodayLeftovers = d.(string)
  400. }
  401. if d, t := v["公斤奶饲料成本"]; t {
  402. feedStatistics.FeedCosts = d.(float64)
  403. }
  404. if d, t := v["剩料率"]; t {
  405. feedStatistics.LeftoverRate = d.(string)
  406. }
  407. if d, t := v["实际干物质采食量"]; t {
  408. feedStatistics.ActualDryMatterIntake = d.(string)
  409. }
  410. if d, t := v["实际成本"]; t {
  411. feedStatistics.ActualCost = d.(string)
  412. }
  413. if d, t := v["实际牛头数"]; t {
  414. feedStatistics.ActualNumberOfCattle = d.(string)
  415. }
  416. if d, t := v["应混料量"]; t {
  417. feedStatistics.AmountToBeMixed = d.(string)
  418. }
  419. if d, t := v["混料时间"]; t {
  420. feedStatistics.MixingTime = d.(string)
  421. }
  422. if d, t := v["牲畜类别"]; t {
  423. feedStatistics.CategoryCattleName = d.(string)
  424. }
  425. if d, t := v["理论干物质"]; t {
  426. feedStatistics.TheoreticalDryMatter = d.(string)
  427. }
  428. if d, t := v["转投剩料量"]; t {
  429. feedStatistics.LeftoverMaterial = d.(float64)
  430. }
  431. if d, t := v["配方干物质采食量"]; t {
  432. feedStatistics.FormulatedDryMatterIntake = d.(string)
  433. }
  434. if d, t := v["配方成本"]; t {
  435. feedStatistics.CostOfFormulation = d.(string)
  436. }
  437. if d, t := v["采食率"]; t {
  438. feedStatistics.FoodIntakeRate = d.(string)
  439. }
  440. if d, t := v["饲料转化率"]; t {
  441. feedStatistics.FeedConversionRatio = d.(float64)
  442. }
  443. if d, t := v["配方模板"]; t {
  444. feedStatistics.FeedFormulaName = d.(string)
  445. }
  446. if d, t := v["实际混料量"]; t {
  447. feedStatistics.ActualMixedVolume = d.(string)
  448. }
  449. if d, t := v["撒料量"]; t {
  450. feedStatistics.SprinkleVolume = d.(string)
  451. }
  452. }
  453. feedStatisticsList = append(feedStatisticsList, feedStatistics)
  454. }
  455. }
  456. return feedStatisticsList
  457. }
  458. // FeedChartStatistics 饲喂效率图表分析
  459. func (s *StoreEntry) FeedChartStatistics(ctx context.Context, req *operationPb.FeedChartStatisticsRequest) (*model.PastureCommonResponse, error) {
  460. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  461. if err != nil {
  462. return nil, xerr.WithStack(err)
  463. }
  464. pastureId := req.PastureId
  465. if groupPasture.PastureId > 0 {
  466. pastureId = int32(groupPasture.PastureId)
  467. }
  468. body := &model.FeedChartParams{
  469. ParamMaps: &model.ParamMaps{
  470. PastureId: fmt.Sprintf("%d", pastureId),
  471. StartTime: req.StartTime,
  472. StopTime: req.EndTime,
  473. Status: req.Status,
  474. },
  475. }
  476. url, ok := model.UrlChart[req.ApiType]
  477. if !ok {
  478. return nil, xerr.Customf("错误的接口类型:%s", req.ApiType)
  479. }
  480. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  481. if err = s.PastureHttpClient(ctx, url, int64(req.PastureId), body, response); err != nil {
  482. return nil, xerr.WithStack(err)
  483. }
  484. return response, nil
  485. }
  486. // CowsAnalysis 饲喂效率-牛群评估
  487. func (s *StoreEntry) CowsAnalysis(ctx context.Context, req *operationPb.CowsAnalysisRequest) (*model.PastureCommonResponse, error) {
  488. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  489. if err != nil {
  490. return nil, xerr.WithStack(err)
  491. }
  492. pastureId := req.PastureId
  493. if groupPasture.PastureId > 0 {
  494. pastureId = int32(groupPasture.PastureId)
  495. }
  496. body := &model.PastureCommonRequest{
  497. Name: req.ApiName,
  498. Page: req.Pagination.Page,
  499. Offset: req.Pagination.PageOffset,
  500. PageCount: req.Pagination.PageSize,
  501. ReturnType: "Map",
  502. ParamMaps: &model.MixFeedStatisticsParams{
  503. PastureId: fmt.Sprintf("%d", pastureId),
  504. StartTime: req.StartTime,
  505. StopTime: req.StartTime,
  506. },
  507. }
  508. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  509. if err = s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  510. return nil, xerr.WithStack(err)
  511. }
  512. return response, nil
  513. }
  514. // SearchAccuracyAggStatistics 准确性分析-汇总分析
  515. func (s *StoreEntry) SearchAccuracyAggStatistics(ctx context.Context, req *operationPb.AccuracyAggStatisticsRequest) (*model.PastureCommonResponse, error) {
  516. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  517. if err != nil {
  518. return nil, xerr.WithStack(err)
  519. }
  520. pastureId := req.PastureId
  521. if groupPasture.PastureId > 0 {
  522. pastureId = int32(groupPasture.PastureId)
  523. }
  524. body := &model.FeedChartParams{
  525. ParamMaps: model.NewAccuracyAggParams(int64(pastureId), req),
  526. }
  527. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  528. if err = s.PastureHttpClient(ctx, model.UrlSummary, int64(req.PastureId), body, response); err != nil {
  529. return nil, xerr.WithStack(err)
  530. }
  531. return response, nil
  532. }
  533. // SearchMixFeedStatistics 准确性分析-混料统计
  534. func (s *StoreEntry) SearchMixFeedStatistics(ctx context.Context, req *operationPb.MixFeedStatisticsRequest) (*model.PastureCommonResponse, error) {
  535. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  536. if err != nil {
  537. return nil, xerr.WithStack(err)
  538. }
  539. pastureId := req.PastureId
  540. if groupPasture.PastureId > 0 {
  541. pastureId = int32(groupPasture.PastureId)
  542. }
  543. body := &model.PastureCommonRequest{
  544. Name: req.ApiName,
  545. Page: req.Pagination.Page,
  546. Offset: req.Pagination.PageOffset,
  547. ReturnType: "Map",
  548. ParamMaps: model.NewMixFeedStatisticsParams(int64(pastureId), req),
  549. }
  550. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  551. if err = s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  552. return nil, xerr.WithStack(err)
  553. }
  554. return response, nil
  555. }
  556. // SearchSprinkleStatistics 准确性分析-撒料统计
  557. func (s *StoreEntry) SearchSprinkleStatistics(ctx context.Context, req *operationPb.SprinkleStatisticsRequest) (*model.PastureCommonResponse, error) {
  558. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  559. if err != nil {
  560. return nil, xerr.WithStack(err)
  561. }
  562. pastureId := req.PastureId
  563. if groupPasture.PastureId > 0 {
  564. pastureId = int32(groupPasture.PastureId)
  565. }
  566. body := &model.PastureCommonRequest{
  567. Name: req.ApiName,
  568. Page: req.Pagination.Page,
  569. Offset: req.Pagination.PageOffset,
  570. ReturnType: "Map",
  571. ParamMaps: model.NewSprinkleStatisticsParams(int64(pastureId), req),
  572. }
  573. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  574. if err = s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  575. return nil, xerr.WithStack(err)
  576. }
  577. return response, nil
  578. }
  579. // SearchProcessAnalysis 过程分析
  580. func (s *StoreEntry) SearchProcessAnalysis(ctx context.Context, req *operationPb.ProcessAnalysisRequest) (*model.PastureCommonResponse, error) {
  581. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  582. if err != nil {
  583. return nil, xerr.WithStack(err)
  584. }
  585. pastureId := req.PastureId
  586. if groupPasture.PastureId > 0 {
  587. pastureId = int32(groupPasture.PastureId)
  588. }
  589. body := &model.PastureCommonRequest{
  590. Name: req.ApiName,
  591. Page: req.Pagination.Page,
  592. Offset: req.Pagination.PageOffset,
  593. ReturnType: "Map",
  594. ParamMaps: model.NewProcessAnalysisParams(int64(pastureId), req),
  595. }
  596. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  597. if err = s.PastureHttpClient(ctx, model.UrlProcess, int64(req.PastureId), body, response); err != nil {
  598. return nil, xerr.WithStack(err)
  599. }
  600. if response.Data.List == nil {
  601. response.Data.List = []int64{}
  602. }
  603. return response, nil
  604. }
  605. func (s *StoreEntry) AnalysisMixedSprinkleDetail(ctx context.Context, req *operationPb.ProcessMixedSprinkleDetailRequest) (*model.PastureCommonResponse, error) {
  606. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  607. if err != nil {
  608. return nil, xerr.WithStack(err)
  609. }
  610. pastureId := req.PastureId
  611. if groupPasture.PastureId > 0 {
  612. pastureId = int32(groupPasture.PastureId)
  613. }
  614. body := &model.PastureCommonRequest{
  615. Name: req.ApiName,
  616. Page: req.Pagination.Page,
  617. Offset: req.Pagination.PageOffset,
  618. PageCount: req.Pagination.PageSize,
  619. ReturnType: "Map",
  620. ParamMaps: model.NewMixedSprinkleDetailRequest(int64(pastureId), req),
  621. }
  622. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  623. apiURl := model.UrlDataByName
  624. if req.ApiName == "getprocessAnalysisTB" {
  625. apiURl = model.UrlReportForm
  626. }
  627. if err = s.PastureHttpClient(ctx, apiURl, int64(req.PastureId), body, response); err != nil {
  628. return nil, xerr.WithStack(err)
  629. }
  630. return response, nil
  631. }
  632. // GetDataByName 共同接口
  633. func (s *StoreEntry) GetDataByName(ctx context.Context, req *operationPb.GetDataByNameRequest) (*model.PastureCommonResponse, error) {
  634. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  635. if err != nil {
  636. return nil, xerr.WithStack(err)
  637. }
  638. pastureId := req.PastureId
  639. if groupPasture.PastureId > 0 {
  640. pastureId = int32(groupPasture.PastureId)
  641. }
  642. body := &model.PastureCommonRequest{
  643. Name: req.ApiName,
  644. Page: req.Page,
  645. Offset: req.Offset,
  646. ParamMaps: &model.GetDataByNameParams{
  647. PastureId: fmt.Sprintf("%d", pastureId),
  648. StartTime: req.StartTime,
  649. StopTime: req.EndTime,
  650. Pid: req.Pid,
  651. Optdevice: req.Optdevice,
  652. FtId: req.Ftid,
  653. Name: req.Name,
  654. },
  655. }
  656. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  657. if err = s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  658. return nil, xerr.WithStack(err)
  659. }
  660. return response, nil
  661. }
  662. // GetTrainNumber 获取班次
  663. func (s *StoreEntry) GetTrainNumber(ctx context.Context, req *operationPb.TrainNumberRequest) (*operationPb.TrainNumberResponse, error) {
  664. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  665. if err != nil {
  666. return nil, xerr.WithStack(err)
  667. }
  668. pastureId := req.PastureId
  669. if groupPasture.PastureId > 0 {
  670. pastureId = int32(groupPasture.PastureId)
  671. }
  672. body := &model.PastureCommonRequest{
  673. Name: req.ApiName,
  674. Page: req.Pagination.Page,
  675. Offset: req.Pagination.PageOffset,
  676. PageCount: req.Pagination.PageSize,
  677. ReturnType: "Map",
  678. ParamMaps: &model.TrainNumberParams{
  679. PastureId: fmt.Sprintf("%d", pastureId),
  680. InfoRName: req.InfoName,
  681. },
  682. }
  683. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  684. if err = s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  685. return nil, xerr.WithStack(err)
  686. }
  687. result := &operationPb.TrainNumberResponse{
  688. Code: http.StatusOK,
  689. Msg: "ok",
  690. Data: &operationPb.TrainNumberData{List: make([]*operationPb.FormulaOptionEnum, 0)},
  691. }
  692. if response.Data.List == nil {
  693. return result, nil
  694. }
  695. b, _ := json.Marshal(response.Data.List)
  696. trainNumberList := make([]*model.TrainNumberList, 0)
  697. if err := json.Unmarshal(b, &trainNumberList); err != nil {
  698. return nil, xerr.WithStack(err)
  699. }
  700. formulaOption := make([]*operationPb.FormulaOptionEnum, 0)
  701. if len(trainNumberList) > 0 {
  702. infoValue := trainNumberList[0].InfoValue
  703. switch infoValue {
  704. case "1":
  705. formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{
  706. Value: 1,
  707. Label: "第一班",
  708. })
  709. case "2":
  710. formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{
  711. Value: 1,
  712. Label: "第一班",
  713. }, &operationPb.FormulaOptionEnum{
  714. Value: 2,
  715. Label: "第二班",
  716. })
  717. case "3":
  718. formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{
  719. Value: 1,
  720. Label: "第一班",
  721. }, &operationPb.FormulaOptionEnum{
  722. Value: 2,
  723. Label: "第二班",
  724. }, &operationPb.FormulaOptionEnum{
  725. Value: 3,
  726. Label: "第三班",
  727. })
  728. }
  729. }
  730. result.Data.List = formulaOption
  731. return result, nil
  732. }