statistic_service.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. }
  455. }
  456. return feedStatisticsList
  457. }
  458. // FeedChartStatistics 饲喂效率图表分析
  459. func (s *StoreEntry) FeedChartStatistics(ctx context.Context, req *operationPb.FeedChartStatisticsRequest) (*model.PastureCommonResponse, error) {
  460. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  461. if err != nil {
  462. return nil, xerr.WithStack(err)
  463. }
  464. pastureId := req.PastureId
  465. if groupPasture.PastureId > 0 {
  466. pastureId = int32(groupPasture.PastureId)
  467. }
  468. body := &model.FeedChartParams{
  469. ParamMaps: &model.ParamMaps{
  470. PastureId: fmt.Sprintf("%d", pastureId),
  471. StartTime: req.StartTime,
  472. StopTime: req.StartTime,
  473. Status: req.Status,
  474. },
  475. }
  476. url, ok := model.UrlChart[req.ApiType]
  477. if !ok {
  478. return nil, xerr.Customf("错误的接口类型:%s", req.ApiType)
  479. }
  480. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  481. if _, err = s.PastureHttpClient(ctx, url, int64(req.PastureId), body, response); err != nil {
  482. return nil, xerr.WithStack(err)
  483. }
  484. return response, nil
  485. }
  486. // CowsAnalysis 饲喂效率-牛群评估
  487. func (s *StoreEntry) CowsAnalysis(ctx context.Context, req *operationPb.CowsAnalysisRequest) (*model.PastureCommonResponse, error) {
  488. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  489. if err != nil {
  490. return nil, xerr.WithStack(err)
  491. }
  492. pastureId := req.PastureId
  493. if groupPasture.PastureId > 0 {
  494. pastureId = int32(groupPasture.PastureId)
  495. }
  496. body := &model.PastureCommonRequest{
  497. Name: req.ApiName,
  498. Page: req.Pagination.Page,
  499. Offset: req.Pagination.PageOffset,
  500. PageCount: req.Pagination.PageSize,
  501. ReturnType: "Map",
  502. ParamMaps: &model.MixFeedStatisticsParams{
  503. PastureId: fmt.Sprintf("%d", pastureId),
  504. StartTime: req.StartTime,
  505. StopTime: req.StartTime,
  506. },
  507. }
  508. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  509. if _, err = s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  510. return nil, xerr.WithStack(err)
  511. }
  512. return response, nil
  513. }
  514. // SearchAccuracyAggStatistics 准确性分析-汇总分析
  515. func (s *StoreEntry) SearchAccuracyAggStatistics(ctx context.Context, req *operationPb.AccuracyAggStatisticsRequest) (*model.PastureCommonResponse, error) {
  516. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  517. if err != nil {
  518. return nil, xerr.WithStack(err)
  519. }
  520. pastureId := req.PastureId
  521. if groupPasture.PastureId > 0 {
  522. pastureId = int32(groupPasture.PastureId)
  523. }
  524. body := &model.FeedChartParams{
  525. ParamMaps: &model.AccuracyAggParams{
  526. PastureId: fmt.Sprintf("%d", pastureId),
  527. StartTime: req.StartTime,
  528. StopTime: req.EndTime,
  529. FName: req.Fname,
  530. Sort: req.Sort,
  531. Status: req.Status,
  532. Times: req.Times,
  533. Genre: req.Genre,
  534. IsDate: req.Isdate,
  535. Hlwc1: req.Hlwc1,
  536. Hlwc2: req.Hlwc2,
  537. Hlzq1: req.Hlzq1,
  538. Hlzq2: req.Hlzq2,
  539. Hlzql1: req.Hlzql1,
  540. Hlzql2: req.Hlzql2,
  541. Slwc1: req.Slwc1,
  542. Slwc2: req.Slwc2,
  543. Slzq1: req.Slzq1,
  544. Slzq2: req.Slzq2,
  545. Slzql1: req.Slzql1,
  546. Slzql2: req.Slzql2,
  547. Error: req.IsError,
  548. },
  549. }
  550. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  551. if _, err = s.PastureHttpClient(ctx, model.UrlSummary, int64(req.PastureId), body, response); err != nil {
  552. return nil, xerr.WithStack(err)
  553. }
  554. return response, nil
  555. }
  556. // SearchMixFeedStatistics 准确性分析-混料统计
  557. func (s *StoreEntry) SearchMixFeedStatistics(ctx context.Context, req *operationPb.MixFeedStatisticsRequest) (*model.PastureCommonResponse, error) {
  558. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  559. if err != nil {
  560. return nil, xerr.WithStack(err)
  561. }
  562. pastureId := req.PastureId
  563. if groupPasture.PastureId > 0 {
  564. pastureId = int32(groupPasture.PastureId)
  565. }
  566. times := ""
  567. if req.ClassNumber > 0 {
  568. times = fmt.Sprintf("%d", req.ClassNumber)
  569. }
  570. body := &model.PastureCommonRequest{
  571. Name: req.ApiName,
  572. Page: req.Pagination.Page,
  573. Offset: req.Pagination.PageOffset,
  574. PageCount: req.Pagination.PageSize,
  575. ReturnType: "Map",
  576. ParamMaps: &model.MixFeedStatisticsParams{
  577. PastureId: fmt.Sprintf("%d", pastureId),
  578. StartTime: req.StartTime,
  579. StopTime: req.StartTime,
  580. TmrTName: req.EquipmentName,
  581. ProjName: req.TrainNumber,
  582. Times: times,
  583. ButtonType: req.ButtonType,
  584. TempletName: req.FormulationName,
  585. Isuse: req.IsUse,
  586. Hlwc1: req.Hlwc1,
  587. Hlwc2: req.Hlwc2,
  588. Hlzq1: req.Hlzq1,
  589. Hlzq2: req.Hlzq2,
  590. Hlzql1: req.Hlzql1,
  591. Hlzql2: req.Hlzql2,
  592. Error: req.IsError,
  593. },
  594. }
  595. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  596. if _, err = s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  597. return nil, xerr.WithStack(err)
  598. }
  599. return response, nil
  600. }
  601. // SearchSprinkleStatistics 准确性分析-撒料统计
  602. func (s *StoreEntry) SearchSprinkleStatistics(ctx context.Context, req *operationPb.SprinkleStatisticsRequest) (*model.PastureCommonResponse, error) {
  603. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  604. if err != nil {
  605. return nil, xerr.WithStack(err)
  606. }
  607. pastureId := req.PastureId
  608. if groupPasture.PastureId > 0 {
  609. pastureId = int32(groupPasture.PastureId)
  610. }
  611. times := ""
  612. if req.ClassNumber > 0 {
  613. times = fmt.Sprintf("%d", req.ClassNumber)
  614. }
  615. body := &model.PastureCommonRequest{
  616. Name: req.ApiName,
  617. Page: req.Pagination.Page,
  618. Offset: req.Pagination.PageOffset,
  619. PageCount: req.Pagination.PageSize,
  620. ReturnType: "Map",
  621. ParamMaps: &model.SprinkleStatisticsParams{
  622. PastureId: fmt.Sprintf("%d", pastureId),
  623. StartTime: req.StartTime,
  624. StopTime: req.StartTime,
  625. TmrTName: req.EquipmentName,
  626. ProjName: req.TrainNumber,
  627. Times: times,
  628. ButtonType: req.ButtonType,
  629. TempletName: req.FormulationName,
  630. Isuse: req.IsUse,
  631. Fname: req.BarnName,
  632. Slwc1: req.Slwc1,
  633. Slwc2: req.Slwc2,
  634. Slzq2: req.Slzq2,
  635. Slzq1: req.Slzq1,
  636. Slzql1: req.Slzql1,
  637. Slzql2: req.Slzql2,
  638. Error: req.IsError,
  639. },
  640. }
  641. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  642. if _, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  643. return nil, xerr.WithStack(err)
  644. }
  645. return response, nil
  646. }
  647. // SearchProcessAnalysis 过程分析
  648. func (s *StoreEntry) SearchProcessAnalysis(ctx context.Context, req *operationPb.ProcessAnalysisRequest) (*model.PastureCommonResponse, error) {
  649. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  650. if err != nil {
  651. return nil, xerr.WithStack(err)
  652. }
  653. pastureId := req.PastureId
  654. if groupPasture.PastureId > 0 {
  655. pastureId = int32(groupPasture.PastureId)
  656. }
  657. body := &model.PastureCommonRequest{
  658. Name: req.ApiName,
  659. Page: req.Pagination.Page,
  660. Offset: req.Pagination.PageOffset,
  661. PageCount: req.Pagination.PageSize,
  662. ReturnType: "Map",
  663. ParamMaps: &model.ProcessAnalysisParams{
  664. PastureId: fmt.Sprintf("%d", pastureId),
  665. StartTime: req.StartTime,
  666. StopTime: req.StartTime,
  667. TmrTName: req.TmrName,
  668. IsCompleted: "",
  669. LpPlanType: fmt.Sprintf("%d", req.PlanType),
  670. FClassId: req.MixFeedType,
  671. Hlzq1: req.Hlzq1,
  672. Hlzq2: req.Hlzq2,
  673. Hlwc1: req.Hlwc1,
  674. Hlwc2: req.Hlwc2,
  675. Slwc1: req.Slwc1,
  676. Slwc2: req.Slwc2,
  677. Slzq2: req.Slzq2,
  678. Slzq1: req.Slzq1,
  679. Error: req.ErrorRange,
  680. },
  681. }
  682. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  683. if _, err = s.PastureHttpClient(ctx, model.UrlProcess, int64(req.PastureId), body, response); err != nil {
  684. return nil, xerr.WithStack(err)
  685. }
  686. return response, nil
  687. }
  688. func (s *StoreEntry) AnalysisMixedSprinkleDetail(ctx context.Context, req *operationPb.ProcessMixedSprinkleDetailRequest) (*model.PastureCommonResponse, error) {
  689. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  690. if err != nil {
  691. return nil, xerr.WithStack(err)
  692. }
  693. pastureId := req.PastureId
  694. if groupPasture.PastureId > 0 {
  695. pastureId = int32(groupPasture.PastureId)
  696. }
  697. body := &model.PastureCommonRequest{
  698. Name: req.ApiName,
  699. Page: req.Pagination.Page,
  700. Offset: req.Pagination.PageOffset,
  701. PageCount: req.Pagination.PageSize,
  702. ReturnType: "Map",
  703. ParamMaps: &model.MixedSprinkleDetailRequest{
  704. PastureId: fmt.Sprintf("%d", pastureId),
  705. FClassId: req.FclassId,
  706. PId: req.Pid,
  707. Id: req.Id,
  708. },
  709. }
  710. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  711. apiURl := model.UrlDataByName
  712. if req.ApiName == "getprocessAnalysisTB" {
  713. apiURl = model.UrlReportForm
  714. }
  715. if _, err = s.PastureHttpClient(ctx, apiURl, int64(req.PastureId), body, response); err != nil {
  716. return nil, xerr.WithStack(err)
  717. }
  718. return response, nil
  719. }
  720. // GetDataByName 共同接口
  721. func (s *StoreEntry) GetDataByName(ctx context.Context, req *operationPb.GetDataByNameRequest) (*model.PastureCommonResponse, error) {
  722. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  723. if err != nil {
  724. return nil, xerr.WithStack(err)
  725. }
  726. pastureId := req.PastureId
  727. if groupPasture.PastureId > 0 {
  728. pastureId = int32(groupPasture.PastureId)
  729. }
  730. body := &model.PastureCommonRequest{
  731. Name: req.ApiName,
  732. ParamMaps: &model.GetDataByNameParams{
  733. PastureId: fmt.Sprintf("%d", pastureId),
  734. StartTime: req.StartTime,
  735. StopTime: req.StartTime,
  736. },
  737. }
  738. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  739. if _, err = s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  740. return nil, xerr.WithStack(err)
  741. }
  742. return response, nil
  743. }
  744. // GetTrainNumber 获取班次
  745. func (s *StoreEntry) GetTrainNumber(ctx context.Context, req *operationPb.TrainNumberRequest) (*operationPb.TrainNumberResponse, error) {
  746. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  747. if err != nil {
  748. return nil, xerr.WithStack(err)
  749. }
  750. pastureId := req.PastureId
  751. if groupPasture.PastureId > 0 {
  752. pastureId = int32(groupPasture.PastureId)
  753. }
  754. body := &model.PastureCommonRequest{
  755. Name: req.ApiName,
  756. Page: req.Pagination.Page,
  757. Offset: req.Pagination.PageOffset,
  758. PageCount: req.Pagination.PageSize,
  759. ReturnType: "Map",
  760. ParamMaps: &model.TrainNumberParams{
  761. PastureId: fmt.Sprintf("%d", pastureId),
  762. InfoRName: req.InfoName,
  763. },
  764. }
  765. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  766. if _, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  767. return nil, xerr.WithStack(err)
  768. }
  769. result := &operationPb.TrainNumberResponse{
  770. Code: http.StatusOK,
  771. Msg: "ok",
  772. Data: &operationPb.TrainNumberData{List: make([]*operationPb.FormulaOptionEnum, 0)},
  773. }
  774. if response.Data.List == nil {
  775. return result, nil
  776. }
  777. b, _ := json.Marshal(response.Data.List)
  778. trainNumberList := make([]*model.TrainNumberList, 0)
  779. if err := json.Unmarshal(b, &trainNumberList); err != nil {
  780. return nil, xerr.WithStack(err)
  781. }
  782. formulaOption := make([]*operationPb.FormulaOptionEnum, 0)
  783. if len(trainNumberList) > 0 {
  784. infoValue := trainNumberList[0].InfoValue
  785. switch infoValue {
  786. case "1":
  787. formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{
  788. Value: 1,
  789. Label: "第一班",
  790. })
  791. case "2":
  792. formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{
  793. Value: 1,
  794. Label: "第一班",
  795. }, &operationPb.FormulaOptionEnum{
  796. Value: 2,
  797. Label: "第二班",
  798. })
  799. case "3":
  800. formulaOption = append(formulaOption, &operationPb.FormulaOptionEnum{
  801. Value: 1,
  802. Label: "第一班",
  803. }, &operationPb.FormulaOptionEnum{
  804. Value: 2,
  805. Label: "第二班",
  806. }, &operationPb.FormulaOptionEnum{
  807. Value: 3,
  808. Label: "第三班",
  809. })
  810. }
  811. }
  812. result.Data.List = formulaOption
  813. return result, nil
  814. }