statistic_service.go 28 KB

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