statistic_service.go 26 KB

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