statistic_service.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. package backend
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "kpt-tmr-group/model"
  8. "kpt-tmr-group/pkg/logger/zaplog"
  9. "kpt-tmr-group/pkg/xerr"
  10. operationPb "kpt-tmr-group/proto/go/backend/operation"
  11. "net/http"
  12. "strconv"
  13. "strings"
  14. "github.com/xuri/excelize/v2"
  15. "go.uber.org/zap"
  16. )
  17. type PastureClientHandler func(ctx context.Context, pastureId int64, body interface{}) error
  18. // type eventHandler func(ev map[string]interface{}, openID string, appID string, enterpriseID int, cts int64, conn redis.Conn) error
  19. // PastureDetailById 获取指定牧场详情
  20. func (s *StoreEntry) PastureDetailById(ctx context.Context, pastureId int64) (*model.GroupPasture, error) {
  21. result := &model.GroupPasture{Id: pastureId}
  22. if err := s.DB.Where("is_delete = ? and is_show = ?", operationPb.IsShow_OK, operationPb.IsShow_OK).First(result).Error; err != nil {
  23. return nil, xerr.WithStack(err)
  24. }
  25. return result, nil
  26. }
  27. func (s *StoreEntry) PastureHttpClient(ctx context.Context, apiUrl string, pastureId int64, body, response interface{}) error {
  28. pastureDetail, err := s.PastureDetailById(ctx, pastureId)
  29. if err != nil {
  30. zaplog.Error("SearchFormulaEstimateList", zap.Any("Err", err), zap.Int64("pastureId", pastureId))
  31. return xerr.Customf("该牧场数据错误,Err:%s", err)
  32. }
  33. pastureClient := model.NewPastureClient(pastureDetail)
  34. url := fmt.Sprintf("%s/%s", pastureDetail.Domain, apiUrl)
  35. result, err := pastureClient.DoPost(url, body)
  36. if err != nil {
  37. return xerr.WithStack(err)
  38. }
  39. zaplog.Info("PastureHttpClient", zap.String("url", url), zap.Any("request", body), zap.String("response", string(result)))
  40. if err = json.Unmarshal(result, response); err != nil {
  41. return xerr.WithStack(err)
  42. }
  43. return nil
  44. }
  45. // SearchFormulaEstimateList 配方评估
  46. func (s *StoreEntry) SearchFormulaEstimateList(ctx context.Context, req *operationPb.SearchFormulaEstimateRequest) (*model.PastureCommonResponse, error) {
  47. body := &model.PastureCommonRequest{
  48. Name: req.ApiName,
  49. Page: req.Pagination.Page,
  50. Offset: req.Pagination.PageOffset,
  51. PageCount: req.Pagination.PageSize,
  52. ReturnType: "Map",
  53. ParamMaps: &model.FormulaEstimateParams{
  54. PastureId: fmt.Sprintf("%d", req.PastureId),
  55. StartTime: req.StartTime,
  56. StopTime: req.EndTime,
  57. Search: fmt.Sprintf("%d", req.SearchType),
  58. TempletId: fmt.Sprintf("%d", req.TemplateId),
  59. Barid: fmt.Sprintf("%d", req.BarnId),
  60. },
  61. }
  62. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  63. if err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  64. return nil, xerr.WithStack(err)
  65. }
  66. return response, nil
  67. }
  68. // SearchInventoryStatistics 库存管理-库存统计
  69. func (s *StoreEntry) SearchInventoryStatistics(ctx context.Context, req *operationPb.SearchInventoryStatisticsRequest) (*model.PastureCommonResponse, error) {
  70. body := &model.PastureCommonRequest{
  71. Name: req.ApiName,
  72. Page: req.Pagination.Page,
  73. Offset: req.Pagination.PageOffset,
  74. PageCount: req.Pagination.PageSize,
  75. ReturnType: "Map",
  76. ParamMaps: &model.InventoryStatisticsParams{
  77. PastureId: fmt.Sprintf("%d", req.PastureId),
  78. StartTime: req.StartTime,
  79. StopTime: req.EndTime,
  80. FeedName: req.FeedName,
  81. },
  82. }
  83. response := &model.PastureCommonResponse{
  84. Data: &model.PastureCommonData{
  85. List: make([]*model.InventoryStatisticsList, 0),
  86. },
  87. }
  88. if err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  89. return nil, xerr.WithStack(err)
  90. }
  91. return response, nil
  92. }
  93. // InventoryStatisticsExcelExport 库存管理-库存统计-报表导出
  94. func (s *StoreEntry) InventoryStatisticsExcelExport(ctx context.Context, req *operationPb.SearchInventoryStatisticsRequest) (*bytes.Buffer, error) {
  95. result, err := s.SearchInventoryStatistics(ctx, req)
  96. if err != nil {
  97. return nil, xerr.WithStack(err)
  98. }
  99. b, _ := json.Marshal(result.Data.List)
  100. inventoryStatisticsList := make([]*model.InventoryStatisticsList, 0)
  101. if err = json.Unmarshal(b, &inventoryStatisticsList); err != nil {
  102. return nil, xerr.Customf("牧场端返回数据错误")
  103. }
  104. file := excelize.NewFile()
  105. defer file.Close()
  106. streamWriter, err := file.NewStreamWriter(model.DefaultSheetName)
  107. if err != nil {
  108. return nil, xerr.WithStack(err)
  109. }
  110. // 表头
  111. titles := map[string][]interface{}{
  112. "A1": {"饲料名称", "期初", nil, "用料", nil, nil, nil, "期末"},
  113. "A2": {nil, "期初库存(kg)", "期初金额(元)", "入库重量(kg)", "系统出库重量(kg)", "人工用料重量(kg)", "损耗重量", "期末库存(kg)", "期末金额(kg)"},
  114. }
  115. for cell, values := range titles {
  116. if err = streamWriter.SetRow(cell, values); err != nil {
  117. return nil, xerr.WithStack(err)
  118. }
  119. }
  120. for i, item := range inventoryStatisticsList {
  121. cell, err := excelize.CoordinatesToCellName(1, i+3)
  122. if err != nil {
  123. zaplog.Error("InventoryStatisticsExcelExport CoordinatesToCellName", zap.Any("Err", err))
  124. continue
  125. }
  126. row := make([]interface{}, 0)
  127. row = append(row, item.FeedName, item.StartSum, item.StartPrice, item.LaidSum, item.UseSumXT, item.UseSumRG, item.UseSumXH, item.StopSum, item.StopPrice)
  128. if err = streamWriter.SetRow(cell, row); err != nil {
  129. return nil, xerr.WithStack(err)
  130. }
  131. }
  132. hvCell := map[string]string{
  133. "A1": "A2",
  134. "B1": "C1",
  135. "D1": "G1",
  136. "H1": "I1",
  137. }
  138. // 合并单元格
  139. for h, v := range hvCell {
  140. if err = streamWriter.MergeCell(h, v); err != nil {
  141. return nil, xerr.WithStack(err)
  142. }
  143. }
  144. // 修改样式
  145. /*style1, err := file.NewStyle(&excelize.Style{
  146. Fill: excelize.Fill{Type: "pattern", Color: []string{"#DFEBF6"}, Pattern: 1},
  147. Alignment: &excelize.Alignment{Horizontal: "center"},
  148. })
  149. if err != nil {
  150. return nil, xerr.WithStack(err)
  151. }
  152. style1, err := file.NewStyle(&excelize.Style{
  153. //Fill: excelize.Fill{Type: "pattern", Color: []string{"#DFEBF6"}, Pattern: 1},
  154. Alignment: &excelize.Alignment{Horizontal: "center"},
  155. })
  156. if err != nil {
  157. return nil, xerr.WithStack(err)
  158. }
  159. if err = file.SetCellStyle(model.DefaultSheetName, "A1", "A1", style1); err != nil {
  160. return nil, xerr.WithStack(err)
  161. }*/
  162. if err = streamWriter.Flush(); err != nil {
  163. return nil, xerr.WithStack(err)
  164. }
  165. return file.WriteToBuffer()
  166. }
  167. // SearchUserMaterialsStatistics 库存管理-用料分析
  168. func (s *StoreEntry) SearchUserMaterialsStatistics(ctx context.Context, req *operationPb.SearchUserMaterialsStatisticsRequest) (*model.PastureCommonResponse, error) {
  169. body := &model.PastureCommonRequest{
  170. Name: req.ApiName,
  171. Page: req.Pagination.Page,
  172. Offset: req.Pagination.PageOffset,
  173. PageCount: req.Pagination.PageSize,
  174. ReturnType: "Map",
  175. Checked: req.ErrorCheck,
  176. ParamMaps: &model.UserMaterialsStatisticsParams{
  177. PastureId: fmt.Sprintf("%d", req.PastureId),
  178. StartTime: req.StartTime,
  179. StopTime: req.EndTime,
  180. FeedName: req.FeedName,
  181. Typea: fmt.Sprintf("%d", req.TypeCheck),
  182. },
  183. }
  184. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{
  185. List: &model.UserMaterialsList{},
  186. }}
  187. if err := s.PastureHttpClient(ctx, model.UrlReportForm, int64(req.PastureId), body, response); err != nil {
  188. return nil, xerr.WithStack(err)
  189. }
  190. return response, nil
  191. }
  192. func (s *StoreEntry) UserMaterialsStatisticsExcelExport(ctx context.Context, req *operationPb.SearchUserMaterialsStatisticsRequest) (*bytes.Buffer, error) {
  193. result, err := s.SearchUserMaterialsStatistics(ctx, req)
  194. if err != nil {
  195. return nil, xerr.WithStack(err)
  196. }
  197. b, _ := json.Marshal(result.Data.List)
  198. userMaterialsList := &model.UserMaterialsList{}
  199. if err = json.Unmarshal(b, userMaterialsList); err != nil {
  200. return nil, xerr.Customf("牧场端返回数据错误")
  201. }
  202. file := excelize.NewFile()
  203. defer file.Close()
  204. streamWriter, err := file.NewStreamWriter(model.DefaultSheetName)
  205. if err != nil {
  206. return nil, xerr.WithStack(err)
  207. }
  208. // 表头
  209. excelValuesList := map[string][]interface{}{}
  210. /*"A1": {
  211. "饲料名称", "需加料", nil, "Y国产燕麦草55", nil, "自制高产Ⅱ精补料1#", nil, "Y苜蓿M150", nil, "自制酵贮康1#", nil,
  212. "Y麸皮^大片", nil, "Y甜菜粕颗粒", nil, "3Y青贮玉米", nil, "围产牛补充预混合饲料1#", nil, "5Y青贮玉米", nil, "Y羊草", nil,
  213. "Y国产燕麦草60", nil, "Y苜蓿M170", nil, "Y苜蓿Z130", nil, "1Y青贮玉米", nil, "水", nil, "自制干奶精补料1#", nil,
  214. "自制初产精补料1#", nil, "自制高产精补料1#", nil, "自制低产精补料1#", nil, "Y青贮玉米^棒子", nil, "Y压片玉米", nil,
  215. "自制围产精补料1#", nil, "自制青年精补料1#", nil, "合计",
  216. },*/
  217. for _, data2 := range userMaterialsList.Data2 {
  218. excelValuesList["A1"] = append(excelValuesList["A1"], data2.Label)
  219. for _, c := range data2.Children {
  220. excelValuesList["A2"] = append(excelValuesList["A2"], c.Label)
  221. }
  222. }
  223. for _, data2 := range userMaterialsList.Data2 {
  224. for i, data1 := range userMaterialsList.Data1 {
  225. index := fmt.Sprintf("A%d", i+3)
  226. //value := tool.GetTargetByValueForTag(data1, c.Prop, "json")
  227. data1Map, ok := data1.(map[string]interface{})
  228. fmt.Println("======excelValuesList======", ok, data1Map, index, data2)
  229. // excelValuesList[index] = append(excelValuesList[index], data1Map[c.Prop])
  230. }
  231. }
  232. vv, _ := json.Marshal(excelValuesList)
  233. fmt.Println("======excelValuesList======", string(vv))
  234. /*for cell, values := range excelValuesList {
  235. if err = streamWriter.SetRow(cell, values); err != nil {
  236. return nil, xerr.WithStack(err)
  237. }
  238. }
  239. hvCell := map[string]string{
  240. "B1": "C1",
  241. "D1": "E1",
  242. "F1": "G1",
  243. "H1": "R1",
  244. "J1": "K1",
  245. "L1": "M1",
  246. "N1": "O1",
  247. "P1": "Q1",
  248. "R1": "S1",
  249. "T1": "U1",
  250. "V1": "W1",
  251. "Z1": "AA1",
  252. }
  253. // 合并单元格
  254. for h, v := range hvCell {
  255. if err = streamWriter.MergeCell(h, v); err != nil {
  256. return nil, xerr.WithStack(err)
  257. }
  258. }*/
  259. if err = streamWriter.Flush(); err != nil {
  260. return nil, xerr.WithStack(err)
  261. }
  262. return file.WriteToBuffer()
  263. }
  264. // SearchPriceStatistics 库存管理-价格分析
  265. func (s *StoreEntry) SearchPriceStatistics(ctx context.Context, req *operationPb.SearchPriceStatisticsRequest) (*model.PastureCommonResponse, error) {
  266. body := &model.PastureCommonRequest{
  267. Name: req.ApiName,
  268. Page: req.Pagination.Page,
  269. Offset: req.Pagination.PageOffset,
  270. PageCount: req.Pagination.PageSize,
  271. ReturnType: "Map",
  272. ParamMaps: &model.PriceStatisticsParams{
  273. PastureId: fmt.Sprintf("%d", req.PastureId),
  274. StartTime: req.StartTime,
  275. StopTime: req.EndTime,
  276. FeedName: req.FeedName,
  277. },
  278. }
  279. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  280. if err := s.PastureHttpClient(ctx, model.UrlReportForm, int64(req.PastureId), body, response); err != nil {
  281. return nil, xerr.WithStack(err)
  282. }
  283. return response, nil
  284. }
  285. // SearchFeedStatistics 饲喂效率-效率统计
  286. func (s *StoreEntry) SearchFeedStatistics(ctx context.Context, req *operationPb.SearchFeedStatisticsRequest) (*model.PastureCommonResponse, error) {
  287. body := &model.PastureCommonRequest{
  288. Name: req.ApiName,
  289. Page: req.Pagination.Page,
  290. Offset: req.Pagination.PageOffset,
  291. PageCount: req.Pagination.PageSize,
  292. ReturnType: "Map",
  293. ParamMaps: &model.FeedStatisticsParams{
  294. PastureId: fmt.Sprintf("%d", req.PastureId),
  295. StartTime: req.StartTime,
  296. StopTime: req.StartTime,
  297. Date: req.StartTime,
  298. FeedTName: req.FormulaTemplate,
  299. BarName: req.BarnName,
  300. CowClass: req.CattleCategoryName,
  301. Times: fmt.Sprintf("%d", req.ClassNumber),
  302. },
  303. }
  304. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  305. if err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  306. return nil, xerr.WithStack(err)
  307. }
  308. return response, nil
  309. }
  310. // FeedChartStatistics 饲喂效率图表分析
  311. func (s *StoreEntry) FeedChartStatistics(ctx context.Context, req *operationPb.FeedChartStatisticsRequest) (*model.PastureCommonResponse, error) {
  312. body := &model.FeedChartParams{
  313. ParamMaps: &model.ParamMaps{
  314. PastureId: fmt.Sprintf("%d", req.PastureId),
  315. StartTime: req.StartTime,
  316. StopTime: req.StartTime,
  317. Status: req.Status,
  318. },
  319. }
  320. url, ok := model.UrlChart[req.ApiType]
  321. if !ok {
  322. return nil, xerr.Customf("错误的接口类型:%s", req.ApiType)
  323. }
  324. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  325. if err := s.PastureHttpClient(ctx, url, int64(req.PastureId), body, response); err != nil {
  326. return nil, xerr.WithStack(err)
  327. }
  328. return response, nil
  329. }
  330. // CowsAnalysis 饲喂效率-牛群评估
  331. func (s *StoreEntry) CowsAnalysis(ctx context.Context, req *operationPb.CowsAnalysisRequest) (*model.PastureCommonResponse, error) {
  332. body := &model.PastureCommonRequest{
  333. Name: req.ApiName,
  334. Page: req.Pagination.Page,
  335. Offset: req.Pagination.PageOffset,
  336. PageCount: req.Pagination.PageSize,
  337. ReturnType: "Map",
  338. ParamMaps: &model.MixFeedStatisticsParams{
  339. PastureId: fmt.Sprintf("%d", req.PastureId),
  340. StartTime: req.StartTime,
  341. StopTime: req.StartTime,
  342. },
  343. }
  344. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  345. if err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  346. return nil, xerr.WithStack(err)
  347. }
  348. return response, nil
  349. }
  350. // SearchAccuracyAggStatistics 准确性分析-汇总分析
  351. func (s *StoreEntry) SearchAccuracyAggStatistics(ctx context.Context, req *operationPb.AccuracyAggStatisticsRequest) (*model.PastureCommonResponse, error) {
  352. body := &model.FeedChartParams{
  353. ParamMaps: &model.AccuracyAggParams{
  354. PastureId: fmt.Sprintf("%d", req.PastureId),
  355. StartTime: req.StartTime,
  356. StopTime: req.EndTime,
  357. FName: req.Fname,
  358. Sort: req.Sort,
  359. Status: req.Status,
  360. Times: req.Times,
  361. Genre: req.Genre,
  362. IsDate: req.Isdate,
  363. Hlwc1: req.Hlwc1,
  364. Hlwc2: req.Hlwc2,
  365. Hlzq1: req.Hlzq1,
  366. Hlzq2: req.Hlzq2,
  367. Hlzql1: req.Hlzql1,
  368. Hlzql2: req.Hlzql2,
  369. Slwc1: req.Slwc1,
  370. Slwc2: req.Slwc2,
  371. Slzq1: req.Slzq1,
  372. Slzq2: req.Slzq2,
  373. Slzql1: req.Slzql1,
  374. Slzql2: req.Slzql2,
  375. Error: req.IsError,
  376. },
  377. }
  378. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  379. if err := s.PastureHttpClient(ctx, model.UrlSummary, int64(req.PastureId), body, response); err != nil {
  380. return nil, xerr.WithStack(err)
  381. }
  382. return response, nil
  383. }
  384. // SearchMixFeedStatistics 准确性分析-混料统计
  385. func (s *StoreEntry) SearchMixFeedStatistics(ctx context.Context, req *operationPb.MixFeedStatisticsRequest) (*model.PastureCommonResponse, error) {
  386. times := ""
  387. if req.ClassNumber > 0 {
  388. times = fmt.Sprintf("%d", req.ClassNumber)
  389. }
  390. body := &model.PastureCommonRequest{
  391. Name: req.ApiName,
  392. Page: req.Pagination.Page,
  393. Offset: req.Pagination.PageOffset,
  394. PageCount: req.Pagination.PageSize,
  395. ReturnType: "Map",
  396. ParamMaps: &model.MixFeedStatisticsParams{
  397. PastureId: fmt.Sprintf("%d", req.PastureId),
  398. StartTime: req.StartTime,
  399. StopTime: req.StartTime,
  400. TmrTName: req.EquipmentName,
  401. ProjName: req.TrainNumber,
  402. Times: times,
  403. ButtonType: req.ButtonType,
  404. TempletName: req.FormulationName,
  405. Isuse: req.IsUse,
  406. Hlwc1: req.Hlwc1,
  407. Hlwc2: req.Hlwc2,
  408. Hlzq1: req.Hlzq1,
  409. Hlzq2: req.Hlzq2,
  410. Hlzql1: req.Hlzql1,
  411. Hlzql2: req.Hlzql2,
  412. Error: req.IsError,
  413. },
  414. }
  415. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  416. if err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  417. return nil, xerr.WithStack(err)
  418. }
  419. return response, nil
  420. }
  421. // SearchSprinkleStatistics 准确性分析-撒料统计
  422. func (s *StoreEntry) SearchSprinkleStatistics(ctx context.Context, req *operationPb.SprinkleStatisticsRequest) (*model.PastureCommonResponse, error) {
  423. times := ""
  424. if req.ClassNumber > 0 {
  425. times = fmt.Sprintf("%d", req.ClassNumber)
  426. }
  427. body := &model.PastureCommonRequest{
  428. Name: req.ApiName,
  429. Page: req.Pagination.Page,
  430. Offset: req.Pagination.PageOffset,
  431. PageCount: req.Pagination.PageSize,
  432. ReturnType: "Map",
  433. ParamMaps: &model.SprinkleStatisticsParams{
  434. PastureId: fmt.Sprintf("%d", req.PastureId),
  435. StartTime: req.StartTime,
  436. StopTime: req.StartTime,
  437. TmrTName: req.EquipmentName,
  438. ProjName: req.TrainNumber,
  439. Times: times,
  440. ButtonType: req.ButtonType,
  441. TempletName: req.FormulationName,
  442. Isuse: req.IsUse,
  443. Fname: req.BarnName,
  444. Slwc1: req.Slwc1,
  445. Slwc2: req.Slwc2,
  446. Slzq2: req.Slzq2,
  447. Slzq1: req.Slzq1,
  448. Slzql1: req.Slzql1,
  449. Slzql2: req.Slzql2,
  450. Error: req.IsError,
  451. },
  452. }
  453. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  454. if err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  455. return nil, xerr.WithStack(err)
  456. }
  457. return response, nil
  458. }
  459. // SearchProcessAnalysis 过程分析
  460. func (s *StoreEntry) SearchProcessAnalysis(ctx context.Context, req *operationPb.ProcessAnalysisRequest) (*model.PastureCommonResponse, error) {
  461. body := &model.PastureCommonRequest{
  462. Name: req.ApiName,
  463. Page: req.Pagination.Page,
  464. Offset: req.Pagination.PageOffset,
  465. PageCount: req.Pagination.PageSize,
  466. ReturnType: "Map",
  467. ParamMaps: &model.ProcessAnalysisParams{
  468. PastureId: fmt.Sprintf("%d", req.PastureId),
  469. StartTime: req.StartTime,
  470. StopTime: req.StartTime,
  471. TmrTName: req.TmrName,
  472. IsCompleted: "",
  473. LpPlanType: fmt.Sprintf("%d", req.PlanType),
  474. FClassId: req.MixFeedType,
  475. Hlzq1: req.Hlzq1,
  476. Hlzq2: req.Hlzq2,
  477. Hlwc1: req.Hlwc1,
  478. Hlwc2: req.Hlwc2,
  479. Slwc1: req.Slwc1,
  480. Slwc2: req.Slwc2,
  481. Slzq2: req.Slzq2,
  482. Slzq1: req.Slzq1,
  483. Error: req.ErrorRange,
  484. },
  485. }
  486. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  487. if err := s.PastureHttpClient(ctx, model.UrlProcess, int64(req.PastureId), body, response); err != nil {
  488. return nil, xerr.WithStack(err)
  489. }
  490. return response, nil
  491. }
  492. func (s *StoreEntry) SearchAnalysisAccuracy(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.SearchAnalysisAccuracyResponse, error) {
  493. dataList := &model.CommonValueRatio{
  494. MaxValue: "",
  495. MiddleValue: "",
  496. MinValue: "",
  497. PastureName: make([]string, 0),
  498. DateDay: make([]string, 0),
  499. DataList: make([][]string, 0),
  500. PastureIds: make([]int32, 0),
  501. }
  502. res := &model.SearchAnalysisAccuracyResponse{
  503. Code: http.StatusOK,
  504. Msg: "ok",
  505. Data: &model.AnalysisAccuracyData{
  506. Chart: &model.Chart{
  507. MixedFodderAccurateRatio: dataList,
  508. MixedFodderCorrectRatio: dataList,
  509. SprinkleFodderAccurateRatio: dataList,
  510. SprinkleFodderCorrectRatio: dataList,
  511. },
  512. Table: &model.Table{
  513. TitleList: make([]*model.TableList, 0),
  514. DataList: make([]*interface{}, 0),
  515. },
  516. },
  517. }
  518. res.Data.Table.TitleList = append(res.Data.Table.TitleList, &model.TableList{
  519. Name: "title",
  520. Value: "牧场",
  521. })
  522. analysisAccuracy := make([]*model.OptionsAnalysisAccuracy, 0)
  523. pref := s.DB.Model(new(model.AnalysisAccuracy))
  524. if req.EndDate != "" && req.StartDate != "" {
  525. pref.Where("date_day BETWEEN ? AND ?", req.StartDate, req.EndDate)
  526. }
  527. if req.CattleParentCategoryId > 0 {
  528. pref.Where("cattle_parent_category_id = ?", req.CattleParentCategoryId)
  529. }
  530. if req.FeedFormulaId > 0 {
  531. pref.Where("feed_formula_id = ?", req.FeedFormulaId)
  532. }
  533. if len(req.PastureIds) > 0 {
  534. pref.Where("pasture_id IN ?", req.PastureIds)
  535. }
  536. if err := pref.Select("pasture_id,pasture_name,date_day,sum(iweight) as all_iweight,sum(lweight) as all_lweight,sum(oweight) as all_oweight,sum(actual_weight_minus) as all_actual_weight_minus,sum(allow_ratio) as all_allow_ratio,sum(alweight) as all_alweight").
  537. Group("pasture_id,pasture_name,date_day").Order("pasture_name,date_day").Find(&analysisAccuracy).Debug().Error; err != nil {
  538. return nil, xerr.WithStack(err)
  539. }
  540. mixedFodderAccurateRatio := make([]float64, 0)
  541. mapPastureName := make(map[string]bool, 0)
  542. mapDateDay := make(map[string]bool, 0)
  543. mapRatio := make(map[string]bool, 0)
  544. tableList := make([]*model.TableList, 0)
  545. for k, v := range analysisAccuracy {
  546. if _, ok := mapPastureName[v.PastureName]; !ok {
  547. res.Data.Chart.MixedFodderAccurateRatio.PastureName = append(res.Data.Chart.MixedFodderAccurateRatio.PastureName, v.PastureName)
  548. res.Data.Chart.MixedFodderAccurateRatio.PastureIds = append(res.Data.Chart.MixedFodderAccurateRatio.PastureIds, int32(v.PastureId))
  549. mapPastureName[v.PastureName] = true
  550. }
  551. dataDayFormat := strings.TrimRight(v.DateDay, "T00:00:00+08:00")
  552. if _, ok := mapDateDay[v.DateDay]; !ok {
  553. res.Data.Chart.MixedFodderAccurateRatio.DateDay = append(res.Data.Chart.MixedFodderAccurateRatio.DateDay, dataDayFormat)
  554. mapDateDay[v.DateDay] = true
  555. }
  556. valueRatio1 := float64(v.AllIweight/v.AllLweight) / 100.0
  557. if _, ok := mapRatio[v.DateDay]; !ok {
  558. valueRatio := make([]string, 0)
  559. for _, a := range analysisAccuracy {
  560. if a.DateDay == v.DateDay {
  561. valueRatio = append(valueRatio, strconv.FormatFloat(float64(a.AllIweight/a.AllLweight)/100.0, 'f', 2, 64))
  562. }
  563. }
  564. res.Data.Chart.MixedFodderAccurateRatio.DataList = append(res.Data.Chart.MixedFodderAccurateRatio.DataList, valueRatio)
  565. mapRatio[v.DateDay] = true
  566. tableList = append(tableList, &model.TableList{
  567. Name: fmt.Sprintf("date%d", k),
  568. Value: dataDayFormat,
  569. })
  570. }
  571. mixedFodderAccurateRatio = append(mixedFodderAccurateRatio, valueRatio1)
  572. }
  573. mixedFodderAccurateRatioMaxValue, mixedFodderAccurateRatioMiddleValue, mixedFodderAccurateRatioMinValue := calculateRatio(mixedFodderAccurateRatio)
  574. res.Data.Chart.MixedFodderAccurateRatio.MaxValue = strconv.FormatFloat(mixedFodderAccurateRatioMaxValue, 'f', 2, 64)
  575. res.Data.Chart.MixedFodderAccurateRatio.MiddleValue = strconv.FormatFloat(mixedFodderAccurateRatioMiddleValue, 'f', 2, 64)
  576. res.Data.Chart.MixedFodderAccurateRatio.MinValue = strconv.FormatFloat(mixedFodderAccurateRatioMinValue, 'f', 2, 64)
  577. res.Data.Table.TitleList = append(res.Data.Table.TitleList, tableList...)
  578. return res, nil
  579. }
  580. func calculateRatio(res []float64) (float64, float64, float64) {
  581. var maxValue, middleValue, minValue, allValue float64 = 0, 0, 0, 0
  582. for _, v := range res {
  583. if v > maxValue {
  584. maxValue = v
  585. }
  586. if v <= maxValue {
  587. minValue = v
  588. }
  589. allValue += v
  590. }
  591. middleValue = allValue / float64(len(res))
  592. return maxValue, middleValue, minValue
  593. }