statistic_service.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. if groupPasture.PastureId > 0 {
  56. req.PastureId = int32(groupPasture.PastureId)
  57. }
  58. body := &model.PastureCommonRequest{
  59. Name: req.ApiName,
  60. Page: req.Pagination.Page,
  61. Offset: req.Pagination.PageOffset,
  62. PageCount: req.Pagination.PageSize,
  63. ReturnType: "Map",
  64. ParamMaps: &model.FormulaEstimateParams{
  65. PastureId: fmt.Sprintf("%d", req.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. if groupPasture.PastureId > 0 {
  86. req.PastureId = int32(groupPasture.PastureId)
  87. }
  88. body := &model.PastureCommonRequest{
  89. Name: req.ApiName,
  90. Page: req.Pagination.Page,
  91. Offset: req.Pagination.PageOffset,
  92. PageCount: req.Pagination.PageSize,
  93. ReturnType: "Map",
  94. ParamMaps: &model.InventoryStatisticsParams{
  95. PastureId: fmt.Sprintf("%d", req.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. if groupPasture.PastureId > 0 {
  192. req.PastureId = int32(groupPasture.PastureId)
  193. }
  194. body := &model.PastureCommonRequest{
  195. Name: req.ApiName,
  196. Page: req.Pagination.Page,
  197. Offset: req.Pagination.PageOffset,
  198. PageCount: req.Pagination.PageSize,
  199. ReturnType: "Map",
  200. Checked: req.ErrorCheck,
  201. ParamMaps: &model.UserMaterialsStatisticsParams{
  202. PastureId: fmt.Sprintf("%d", req.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. if groupPasture.PastureId > 0 {
  292. req.PastureId = int32(groupPasture.PastureId)
  293. }
  294. body := &model.PastureCommonRequest{
  295. Name: req.ApiName,
  296. Page: req.Pagination.Page,
  297. Offset: req.Pagination.PageOffset,
  298. PageCount: req.Pagination.PageSize,
  299. ReturnType: "Map",
  300. ParamMaps: &model.PriceStatisticsParams{
  301. PastureId: fmt.Sprintf("%d", req.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. times := ""
  335. if req.ClassNumber > 0 {
  336. times = fmt.Sprintf("%d", req.ClassNumber)
  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. body := &model.PastureCommonRequest{
  344. Name: req.ApiName,
  345. Page: req.Pagination.Page,
  346. Offset: req.Pagination.PageOffset,
  347. PageCount: req.Pagination.PageSize,
  348. ReturnType: "Map",
  349. ParamMaps: &model.FeedStatisticsParams{
  350. PastureId: fmt.Sprintf("%d", pastureId),
  351. StartTime: req.StartTime,
  352. StopTime: req.StartTime,
  353. Date: req.StartTime,
  354. FeedTName: req.FormulaTemplate,
  355. BarName: req.BarnName,
  356. CowClass: req.CattleCategoryName,
  357. Times: times,
  358. },
  359. }
  360. response := &model.PastureCommonResponse{
  361. Data: &model.PastureCommonData{},
  362. }
  363. groupPasture, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(pastureId), body, response)
  364. if err != nil {
  365. zaplog.Error("SearchFeedStatistics",
  366. zap.Any("pastureId", pastureId),
  367. zap.Any("url", model.UrlDataByName),
  368. zap.Any("body", body),
  369. zap.Any("response", response))
  370. return
  371. }
  372. if response.Code == http.StatusOK && response.Data.List != nil {
  373. if req.ApiName == "getFeedEfficiencySC" {
  374. feedStatisticsConversions := FeedStatisticsConversions(response.Data.List)
  375. feedStatisticsConversions.PastureName = groupPasture.Name
  376. res.Data.List[groupPasture.Name] = FeedStatisticsConversions(response.Data.List)
  377. } else {
  378. res.Data.List[groupPasture.Name] = response.Data.List
  379. }
  380. }
  381. }(v)
  382. }
  383. wg.Wait()
  384. return res, nil
  385. }
  386. // FeedStatisticsConversions 数据转换
  387. func FeedStatisticsConversions(req interface{}) *model.FeedStatisticsConversions {
  388. feedStatisticsList := &model.FeedStatisticsConversions{}
  389. if data, ok := req.([]interface{}); ok && len(data) > 0 {
  390. value := data[0]
  391. if v, yes := value.(map[string]interface{}); yes {
  392. if d, t := v["TMR干物质"]; t {
  393. feedStatisticsList.TmrDryMatter = d.(string)
  394. }
  395. if d, t := v["barname"]; t {
  396. feedStatisticsList.BarName = d.(string)
  397. }
  398. if d, t := v["产奶量"]; t {
  399. feedStatisticsList.MilkYield = d.(string)
  400. }
  401. if d, t := v["今日剩料量"]; t {
  402. feedStatisticsList.TodayLeftovers = d.(string)
  403. }
  404. if d, t := v["公斤奶饲料成本"]; t {
  405. feedStatisticsList.FeedCosts = d.(float64)
  406. }
  407. if d, t := v["剩料率"]; t {
  408. feedStatisticsList.LeftoverRate = d.(string)
  409. }
  410. if d, t := v["实际干物质采食量"]; t {
  411. feedStatisticsList.ActualDryMatterIntake = d.(string)
  412. }
  413. if d, t := v["实际成本"]; t {
  414. feedStatisticsList.ActualCost = d.(string)
  415. }
  416. if d, t := v["实际牛头数"]; t {
  417. feedStatisticsList.ActualNumberOfCattle = d.(string)
  418. }
  419. if d, t := v["应混料量"]; t {
  420. feedStatisticsList.AmountToBeMixed = d.(string)
  421. }
  422. if d, t := v["混料时间"]; t {
  423. feedStatisticsList.MixingTime = d.(string)
  424. }
  425. if d, t := v["牲畜类别"]; t {
  426. feedStatisticsList.CategoryCattleName = d.(string)
  427. }
  428. if d, t := v["理论干物质"]; t {
  429. feedStatisticsList.TheoreticalDryMatter = d.(string)
  430. }
  431. if d, t := v["转投剩料量"]; t {
  432. feedStatisticsList.LeftoverMaterial = d.(float64)
  433. }
  434. if d, t := v["配方干物质采食量"]; t {
  435. feedStatisticsList.FormulatedDryMatterIntake = d.(string)
  436. }
  437. if d, t := v["配方成本"]; t {
  438. feedStatisticsList.CostOfFormulation = d.(string)
  439. }
  440. if d, t := v["采食率"]; t {
  441. feedStatisticsList.FoodIntakeRate = d.(string)
  442. }
  443. if d, t := v["饲料转化率"]; t {
  444. feedStatisticsList.FeedConversionRatio = d.(float64)
  445. }
  446. }
  447. }
  448. return feedStatisticsList
  449. }
  450. // FeedChartStatistics 饲喂效率图表分析
  451. func (s *StoreEntry) FeedChartStatistics(ctx context.Context, req *operationPb.FeedChartStatisticsRequest) (*model.PastureCommonResponse, error) {
  452. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  453. if err != nil {
  454. return nil, xerr.WithStack(err)
  455. }
  456. if groupPasture.PastureId > 0 {
  457. req.PastureId = int32(groupPasture.PastureId)
  458. }
  459. body := &model.FeedChartParams{
  460. ParamMaps: &model.ParamMaps{
  461. PastureId: fmt.Sprintf("%d", req.PastureId),
  462. StartTime: req.StartTime,
  463. StopTime: req.StartTime,
  464. Status: req.Status,
  465. },
  466. }
  467. url, ok := model.UrlChart[req.ApiType]
  468. if !ok {
  469. return nil, xerr.Customf("错误的接口类型:%s", req.ApiType)
  470. }
  471. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  472. if _, err := s.PastureHttpClient(ctx, url, int64(req.PastureId), body, response); err != nil {
  473. return nil, xerr.WithStack(err)
  474. }
  475. return response, nil
  476. }
  477. // CowsAnalysis 饲喂效率-牛群评估
  478. func (s *StoreEntry) CowsAnalysis(ctx context.Context, req *operationPb.CowsAnalysisRequest) (*model.PastureCommonResponse, error) {
  479. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  480. if err != nil {
  481. return nil, xerr.WithStack(err)
  482. }
  483. if groupPasture.PastureId > 0 {
  484. req.PastureId = int32(groupPasture.PastureId)
  485. }
  486. body := &model.PastureCommonRequest{
  487. Name: req.ApiName,
  488. Page: req.Pagination.Page,
  489. Offset: req.Pagination.PageOffset,
  490. PageCount: req.Pagination.PageSize,
  491. ReturnType: "Map",
  492. ParamMaps: &model.MixFeedStatisticsParams{
  493. PastureId: fmt.Sprintf("%d", req.PastureId),
  494. StartTime: req.StartTime,
  495. StopTime: req.StartTime,
  496. },
  497. }
  498. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  499. if _, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  500. return nil, xerr.WithStack(err)
  501. }
  502. return response, nil
  503. }
  504. // SearchAccuracyAggStatistics 准确性分析-汇总分析
  505. func (s *StoreEntry) SearchAccuracyAggStatistics(ctx context.Context, req *operationPb.AccuracyAggStatisticsRequest) (*model.PastureCommonResponse, error) {
  506. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  507. if err != nil {
  508. return nil, xerr.WithStack(err)
  509. }
  510. if groupPasture.PastureId > 0 {
  511. req.PastureId = int32(groupPasture.PastureId)
  512. }
  513. body := &model.FeedChartParams{
  514. ParamMaps: &model.AccuracyAggParams{
  515. PastureId: fmt.Sprintf("%d", req.PastureId),
  516. StartTime: req.StartTime,
  517. StopTime: req.EndTime,
  518. FName: req.Fname,
  519. Sort: req.Sort,
  520. Status: req.Status,
  521. Times: req.Times,
  522. Genre: req.Genre,
  523. IsDate: req.Isdate,
  524. Hlwc1: req.Hlwc1,
  525. Hlwc2: req.Hlwc2,
  526. Hlzq1: req.Hlzq1,
  527. Hlzq2: req.Hlzq2,
  528. Hlzql1: req.Hlzql1,
  529. Hlzql2: req.Hlzql2,
  530. Slwc1: req.Slwc1,
  531. Slwc2: req.Slwc2,
  532. Slzq1: req.Slzq1,
  533. Slzq2: req.Slzq2,
  534. Slzql1: req.Slzql1,
  535. Slzql2: req.Slzql2,
  536. Error: req.IsError,
  537. },
  538. }
  539. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  540. if _, err := s.PastureHttpClient(ctx, model.UrlSummary, int64(req.PastureId), body, response); err != nil {
  541. return nil, xerr.WithStack(err)
  542. }
  543. return response, nil
  544. }
  545. // SearchMixFeedStatistics 准确性分析-混料统计
  546. func (s *StoreEntry) SearchMixFeedStatistics(ctx context.Context, req *operationPb.MixFeedStatisticsRequest) (*model.PastureCommonResponse, error) {
  547. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  548. if err != nil {
  549. return nil, xerr.WithStack(err)
  550. }
  551. if groupPasture.PastureId > 0 {
  552. req.PastureId = int32(groupPasture.PastureId)
  553. }
  554. times := ""
  555. if req.ClassNumber > 0 {
  556. times = fmt.Sprintf("%d", req.ClassNumber)
  557. }
  558. body := &model.PastureCommonRequest{
  559. Name: req.ApiName,
  560. Page: req.Pagination.Page,
  561. Offset: req.Pagination.PageOffset,
  562. PageCount: req.Pagination.PageSize,
  563. ReturnType: "Map",
  564. ParamMaps: &model.MixFeedStatisticsParams{
  565. PastureId: fmt.Sprintf("%d", req.PastureId),
  566. StartTime: req.StartTime,
  567. StopTime: req.StartTime,
  568. TmrTName: req.EquipmentName,
  569. ProjName: req.TrainNumber,
  570. Times: times,
  571. ButtonType: req.ButtonType,
  572. TempletName: req.FormulationName,
  573. Isuse: req.IsUse,
  574. Hlwc1: req.Hlwc1,
  575. Hlwc2: req.Hlwc2,
  576. Hlzq1: req.Hlzq1,
  577. Hlzq2: req.Hlzq2,
  578. Hlzql1: req.Hlzql1,
  579. Hlzql2: req.Hlzql2,
  580. Error: req.IsError,
  581. },
  582. }
  583. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  584. if _, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  585. return nil, xerr.WithStack(err)
  586. }
  587. return response, nil
  588. }
  589. // SearchSprinkleStatistics 准确性分析-撒料统计
  590. func (s *StoreEntry) SearchSprinkleStatistics(ctx context.Context, req *operationPb.SprinkleStatisticsRequest) (*model.PastureCommonResponse, error) {
  591. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  592. if err != nil {
  593. return nil, xerr.WithStack(err)
  594. }
  595. if groupPasture.PastureId > 0 {
  596. req.PastureId = int32(groupPasture.PastureId)
  597. }
  598. times := ""
  599. if req.ClassNumber > 0 {
  600. times = fmt.Sprintf("%d", req.ClassNumber)
  601. }
  602. body := &model.PastureCommonRequest{
  603. Name: req.ApiName,
  604. Page: req.Pagination.Page,
  605. Offset: req.Pagination.PageOffset,
  606. PageCount: req.Pagination.PageSize,
  607. ReturnType: "Map",
  608. ParamMaps: &model.SprinkleStatisticsParams{
  609. PastureId: fmt.Sprintf("%d", req.PastureId),
  610. StartTime: req.StartTime,
  611. StopTime: req.StartTime,
  612. TmrTName: req.EquipmentName,
  613. ProjName: req.TrainNumber,
  614. Times: times,
  615. ButtonType: req.ButtonType,
  616. TempletName: req.FormulationName,
  617. Isuse: req.IsUse,
  618. Fname: req.BarnName,
  619. Slwc1: req.Slwc1,
  620. Slwc2: req.Slwc2,
  621. Slzq2: req.Slzq2,
  622. Slzq1: req.Slzq1,
  623. Slzql1: req.Slzql1,
  624. Slzql2: req.Slzql2,
  625. Error: req.IsError,
  626. },
  627. }
  628. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  629. if _, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  630. return nil, xerr.WithStack(err)
  631. }
  632. return response, nil
  633. }
  634. // SearchProcessAnalysis 过程分析
  635. func (s *StoreEntry) SearchProcessAnalysis(ctx context.Context, req *operationPb.ProcessAnalysisRequest) (*model.PastureCommonResponse, error) {
  636. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  637. if err != nil {
  638. return nil, xerr.WithStack(err)
  639. }
  640. if groupPasture.PastureId > 0 {
  641. req.PastureId = int32(groupPasture.PastureId)
  642. }
  643. body := &model.PastureCommonRequest{
  644. Name: req.ApiName,
  645. Page: req.Pagination.Page,
  646. Offset: req.Pagination.PageOffset,
  647. PageCount: req.Pagination.PageSize,
  648. ReturnType: "Map",
  649. ParamMaps: &model.ProcessAnalysisParams{
  650. PastureId: fmt.Sprintf("%d", req.PastureId),
  651. StartTime: req.StartTime,
  652. StopTime: req.StartTime,
  653. TmrTName: req.TmrName,
  654. IsCompleted: "",
  655. LpPlanType: fmt.Sprintf("%d", req.PlanType),
  656. FClassId: req.MixFeedType,
  657. Hlzq1: req.Hlzq1,
  658. Hlzq2: req.Hlzq2,
  659. Hlwc1: req.Hlwc1,
  660. Hlwc2: req.Hlwc2,
  661. Slwc1: req.Slwc1,
  662. Slwc2: req.Slwc2,
  663. Slzq2: req.Slzq2,
  664. Slzq1: req.Slzq1,
  665. Error: req.ErrorRange,
  666. },
  667. }
  668. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  669. if _, err := s.PastureHttpClient(ctx, model.UrlProcess, int64(req.PastureId), body, response); err != nil {
  670. return nil, xerr.WithStack(err)
  671. }
  672. return response, nil
  673. }
  674. // GetDataByName 共同接口
  675. func (s *StoreEntry) GetDataByName(ctx context.Context, req *operationPb.GetDataByNameRequest) (*model.PastureCommonResponse, error) {
  676. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  677. if err != nil {
  678. return nil, xerr.WithStack(err)
  679. }
  680. if groupPasture.PastureId > 0 {
  681. req.PastureId = int32(groupPasture.PastureId)
  682. }
  683. body := &model.PastureCommonRequest{
  684. Name: req.ApiName,
  685. ParamMaps: &model.GetDataByNameParams{
  686. PastureId: fmt.Sprintf("%d", req.PastureId),
  687. StartTime: req.StartTime,
  688. StopTime: req.StartTime,
  689. },
  690. }
  691. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  692. if _, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  693. return nil, xerr.WithStack(err)
  694. }
  695. return response, nil
  696. }
  697. // GetTrainNumber 获取班次
  698. func (s *StoreEntry) GetTrainNumber(ctx context.Context, req *operationPb.TrainNumberRequest) (*operationPb.TrainNumberResponse, error) {
  699. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  700. if err != nil {
  701. return nil, xerr.WithStack(err)
  702. }
  703. if groupPasture.PastureId > 0 {
  704. req.PastureId = int32(groupPasture.PastureId)
  705. }
  706. body := &model.PastureCommonRequest{
  707. Name: req.ApiName,
  708. Page: req.Pagination.Page,
  709. Offset: req.Pagination.PageOffset,
  710. PageCount: req.Pagination.PageSize,
  711. ReturnType: "Map",
  712. ParamMaps: &model.TrainNumberParams{
  713. PastureId: fmt.Sprintf("%d", req.PastureId),
  714. InfoRName: req.InfoName,
  715. },
  716. }
  717. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  718. if _, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  719. return nil, xerr.WithStack(err)
  720. }
  721. result := &operationPb.TrainNumberResponse{
  722. Code: http.StatusOK,
  723. Msg: "ok",
  724. Data: &operationPb.TrainNumberData{List: make([]*operationPb.FormulaOptionEnum, 0)},
  725. }
  726. if response.Data.List == nil {
  727. return result, nil
  728. }
  729. b, _ := json.Marshal(response.Data.List)
  730. trainNumberList := make([]*model.TrainNumberList, 0)
  731. if err := json.Unmarshal(b, &trainNumberList); err != nil {
  732. return nil, xerr.WithStack(err)
  733. }
  734. formulaOption := make([]*operationPb.FormulaOptionEnum, 0)
  735. if len(trainNumberList) > 0 {
  736. infoValue := trainNumberList[0].InfoValue
  737. switch infoValue {
  738. case "1":
  739. formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{
  740. Value: 1,
  741. Label: "第一班",
  742. })
  743. case "2":
  744. formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{
  745. Value: 1,
  746. Label: "第一班",
  747. }, &operationPb.FormulaOptionEnum{
  748. Value: 2,
  749. Label: "第二班",
  750. })
  751. case "3":
  752. formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{
  753. Value: 1,
  754. Label: "第一班",
  755. }, &operationPb.FormulaOptionEnum{
  756. Value: 2,
  757. Label: "第二班",
  758. }, &operationPb.FormulaOptionEnum{
  759. Value: 3,
  760. Label: "第三班",
  761. })
  762. }
  763. }
  764. result.Data.List = formulaOption
  765. return result, nil
  766. }