dashboard_service.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. package backend
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "kpt-tmr-group/model"
  7. "kpt-tmr-group/pkg/logger/zaplog"
  8. "kpt-tmr-group/pkg/tool"
  9. "kpt-tmr-group/pkg/xerr"
  10. operationPb "kpt-tmr-group/proto/go/backend/operation"
  11. "net/http"
  12. "sort"
  13. "sync"
  14. "time"
  15. "go.uber.org/multierr"
  16. "go.uber.org/zap"
  17. )
  18. const compareTime = 10 * 60
  19. // PasturePrefAnalysisData PasturePrefExecTimeData PastureSprinkleFeedTime TODO 后期三个函数封装一下
  20. func (s *StoreEntry) PasturePrefAnalysisData(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (map[int64]*model.PastureAnalysisAccuracyData, error) {
  21. res := make(map[int64]*model.PastureAnalysisAccuracyData, 0)
  22. wg := sync.WaitGroup{}
  23. wg.Add(len(req.PastureIds))
  24. for _, pastureId := range req.PastureIds {
  25. go func(pId int32) {
  26. defer wg.Done()
  27. response := &model.PastureAnalysisAccuracyResponse{}
  28. groupPasture, err := s.GetGroupPastureListById(ctx, int64(pId))
  29. if err != nil {
  30. zaplog.Error("PasturePrefAnalysisData",
  31. zap.Any("GetGroupPastureListById", err),
  32. zap.Any("pId", pId))
  33. return
  34. }
  35. body := &model.DashboardAccuracyRequest{
  36. PastureId: int32(groupPasture.PastureId),
  37. FeedFormulaId: req.FeedFormulaId,
  38. CattleParentCategoryId: int32(req.CattleParentCategoryId),
  39. StartDate: req.StartDate,
  40. EndDate: req.EndDate,
  41. }
  42. _, err = s.PastureHttpClient(ctx, model.DashboardAccuracyUrl, int64(pId), body, response)
  43. if err != nil {
  44. zaplog.Error("DistributeFeedFormula",
  45. zap.String("url", model.DashboardAccuracyUrl),
  46. zap.Any("pasture", groupPasture), zap.Any("body", body),
  47. zap.Any("err", err), zap.Any("response", response))
  48. b, _ := json.Marshal(body)
  49. resB, _ := json.Marshal(response)
  50. pastureDataLog := model.NewPastureDataLog(groupPasture.Id, PastureDataLogType["FeedFormula_Distribute"], model.DashboardAccuracyUrl, string(b), string(resB))
  51. s.DB.Create(pastureDataLog)
  52. return
  53. }
  54. if response.Code != http.StatusOK {
  55. zaplog.Error("DistributeFeedFormula-response",
  56. zap.String("url", model.DashboardAccuracyUrl),
  57. zap.Any("pasture", groupPasture), zap.Any("body", body),
  58. zap.Any("response", response), zap.Any("response", response))
  59. return
  60. }
  61. res[groupPasture.Id] = response.Data
  62. }(pastureId)
  63. }
  64. wg.Wait()
  65. return res, nil
  66. }
  67. func (s *StoreEntry) PasturePrefExecTimeData(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (map[string]*model.ExecTimeData, error) {
  68. res := make(map[string]*model.ExecTimeData, 0)
  69. wg := sync.WaitGroup{}
  70. wg.Add(len(req.PastureIds))
  71. for _, pastureId := range req.PastureIds {
  72. go func(pId int32) {
  73. defer wg.Done()
  74. groupPasture, err := s.GetGroupPastureListById(ctx, int64(pId))
  75. if err != nil {
  76. return
  77. }
  78. response := &model.PastureExecTimeData{}
  79. body := &model.DashboardAccuracyRequest{
  80. PastureId: int32(groupPasture.PastureId),
  81. FeedFormulaId: req.FeedFormulaId,
  82. CattleParentCategoryId: int32(req.CattleParentCategoryId),
  83. StartDate: req.StartDate,
  84. EndDate: req.EndDate,
  85. }
  86. if _, err = s.PastureHttpClient(ctx, model.DashboardExecTimeUrl, int64(pId), body, response); err != nil {
  87. zaplog.Error("PasturePrefExecTimeData",
  88. zap.String("url", model.DashboardExecTimeUrl),
  89. zap.Any("pasture", groupPasture), zap.Any("body", body),
  90. zap.Any("err", err), zap.Any("response", response))
  91. b, _ := json.Marshal(body)
  92. resB, _ := json.Marshal(response)
  93. pastureDataLog := model.NewPastureDataLog(groupPasture.Id, PastureDataLogType["PasturePrefExecTimeData"], model.DashboardExecTimeUrl, string(b), string(resB))
  94. s.DB.Create(pastureDataLog)
  95. return
  96. }
  97. if response.Code != http.StatusOK {
  98. zaplog.Error("PasturePrefExecTimeData-response",
  99. zap.String("url", model.DashboardExecTimeUrl),
  100. zap.Any("pasture", groupPasture), zap.Any("body", body),
  101. zap.Any("err", err), zap.Any("response", response))
  102. return
  103. }
  104. res[groupPasture.Name] = response.Data
  105. }(pastureId)
  106. }
  107. wg.Wait()
  108. return res, nil
  109. }
  110. func (s *StoreEntry) PastureSprinkleFeedTime(ctx context.Context, req *operationPb.SprinkleFeedTimeRequest) (map[string][]*model.SprinkleStatisticsDataList, error) {
  111. res := make(map[string][]*model.SprinkleStatisticsDataList, 0)
  112. wg := sync.WaitGroup{}
  113. wg.Add(len(req.PastureIds))
  114. var muError error
  115. for _, pasture := range req.PastureIds {
  116. go func(pId int32) {
  117. defer wg.Done()
  118. groupPasture, err := s.GetGroupPastureListById(ctx, int64(pId))
  119. if err != nil {
  120. zaplog.Error("PastureSprinkleFeedTime", zap.Any("GetGroupPastureListById", err))
  121. return
  122. }
  123. response := &model.PastureSprinkleStatisticsDataList{}
  124. body := &model.DashboardAccuracyRequest{
  125. PastureId: int32(groupPasture.PastureId),
  126. FeedFormulaId: req.FeedFormulaId,
  127. StartDate: req.StartDate,
  128. EndDate: req.EndDate,
  129. }
  130. if _, err = s.PastureHttpClient(ctx, model.DashboardSprinkleFeedTimeUrl, int64(pId), body, response); err != nil {
  131. muError = multierr.Append(muError, err)
  132. zaplog.Error("PastureSprinkleFeedTime",
  133. zap.String("url", model.DashboardSprinkleFeedTimeUrl),
  134. zap.Any("pasture", groupPasture), zap.Any("body", body),
  135. zap.Any("err", err), zap.Any("response", response))
  136. b, _ := json.Marshal(body)
  137. resB, _ := json.Marshal(response)
  138. pastureDataLog := model.NewPastureDataLog(groupPasture.Id, PastureDataLogType["PasturePrefExecTimeData"], model.DashboardSprinkleFeedTimeUrl, string(b), string(resB))
  139. s.DB.Create(pastureDataLog)
  140. }
  141. if response.Code != http.StatusOK {
  142. muError = multierr.Append(muError, xerr.Custom(response.Msg))
  143. }
  144. res[groupPasture.Name] = response.Data
  145. }(pasture)
  146. }
  147. wg.Wait()
  148. return res, nil
  149. }
  150. func (s *StoreEntry) SearchAnalysisAccuracy(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.SearchAnalysisAccuracyResponse, error) {
  151. res := &model.SearchAnalysisAccuracyResponse{
  152. Code: http.StatusOK,
  153. Msg: "ok",
  154. Data: &model.AnalysisAccuracyData{
  155. Chart: &model.Chart{},
  156. Table: &model.Table{
  157. TitleList: make([]*model.TableList, 0),
  158. DataList: &model.DataList{},
  159. },
  160. },
  161. }
  162. res.Data.Table.TitleList = append(res.Data.Table.TitleList, &model.TableList{
  163. Name: "title",
  164. Value: "牧场",
  165. })
  166. pastureAnalysisAccuracy, err := s.PasturePrefAnalysisData(ctx, req)
  167. if err != nil {
  168. return nil, xerr.WithStack(err)
  169. }
  170. mixedFodderAccurateRatio, mixedFodderCorrectRatio, sprinkleFodderAccurateRatio, sprinkleFodderCorrectRatio :=
  171. &model.CommonValueRatio{}, &model.CommonValueRatio{}, &model.CommonValueRatio{}, &model.CommonValueRatio{}
  172. maTitleValueList, mcTitleValueList, saTitleValueList, scTitleValueList := make([]float64, 0), make([]float64, 0), make([]float64, 0), make([]float64, 0)
  173. dayTimeList := tool.TimeBetween(req.StartDate, req.EndDate)
  174. for pastureId, data := range pastureAnalysisAccuracy {
  175. groupPasture, err := s.GetGroupPastureListById(ctx, pastureId)
  176. if err != nil {
  177. zaplog.Error("SearchAnalysisAccuracy GetGroupPastureListById",
  178. zap.Any("pastureId", pastureId), zap.Any("error", err))
  179. continue
  180. }
  181. if data == nil {
  182. continue
  183. }
  184. mixedFodderAccurateRatioDataList := make([]string, 0)
  185. for _, v := range data.MixedFodderAccurateRatio {
  186. mixedFodderAccurateRatioDataList = append(mixedFodderAccurateRatioDataList, fmt.Sprintf("%.2f", v.Ratio))
  187. if len(mixedFodderAccurateRatio.DateDay) < len(dayTimeList) {
  188. mixedFodderAccurateRatio.DateDay = append(mixedFodderAccurateRatio.DateDay, v.DayTime)
  189. }
  190. maTitleValueList = append(maTitleValueList, v.Ratio)
  191. }
  192. mixedFodderAccurateRatio.DataList = append(mixedFodderAccurateRatio.DataList, mixedFodderAccurateRatioDataList)
  193. mixedFodderAccurateRatio.PastureIds = append(mixedFodderAccurateRatio.PastureIds, int32(pastureId))
  194. mixedFodderAccurateRatio.PastureName = append(mixedFodderAccurateRatio.PastureName, groupPasture.Name)
  195. mixedFodderCorrectRatioDataList := make([]string, 0)
  196. for _, v := range data.MixedFodderCorrectRatio {
  197. mixedFodderCorrectRatioDataList = append(mixedFodderCorrectRatioDataList, fmt.Sprintf("%.2f", v.Ratio))
  198. if len(mixedFodderCorrectRatio.DateDay) < len(dayTimeList) {
  199. mixedFodderCorrectRatio.DateDay = append(mixedFodderCorrectRatio.DateDay, v.DayTime)
  200. }
  201. mcTitleValueList = append(mcTitleValueList, v.Ratio)
  202. }
  203. mixedFodderCorrectRatio.DataList = append(mixedFodderCorrectRatio.DataList, mixedFodderCorrectRatioDataList)
  204. mixedFodderCorrectRatio.PastureIds = append(mixedFodderCorrectRatio.PastureIds, int32(pastureId))
  205. mixedFodderCorrectRatio.PastureName = append(mixedFodderCorrectRatio.PastureName, groupPasture.Name)
  206. sprinkleFodderRatioDataList := make([]string, 0)
  207. for _, v := range data.SprinkleFodderAccurateRatio {
  208. sprinkleFodderRatioDataList = append(sprinkleFodderRatioDataList, fmt.Sprintf("%.2f", v.Ratio))
  209. if len(sprinkleFodderAccurateRatio.DateDay) < len(dayTimeList) {
  210. sprinkleFodderAccurateRatio.DateDay = append(sprinkleFodderAccurateRatio.DateDay, v.DayTime)
  211. }
  212. saTitleValueList = append(saTitleValueList, v.Ratio)
  213. }
  214. sprinkleFodderAccurateRatio.DataList = append(sprinkleFodderAccurateRatio.DataList, sprinkleFodderRatioDataList)
  215. sprinkleFodderAccurateRatio.PastureIds = append(sprinkleFodderAccurateRatio.PastureIds, int32(pastureId))
  216. sprinkleFodderAccurateRatio.PastureName = append(sprinkleFodderAccurateRatio.PastureName, groupPasture.Name)
  217. sprinkleFodderCorrectRatioDataList := make([]string, 0)
  218. for _, v := range data.SprinkleFodderCorrectRatio {
  219. sprinkleFodderCorrectRatioDataList = append(sprinkleFodderCorrectRatioDataList, fmt.Sprintf("%.2f", v.Ratio))
  220. if len(sprinkleFodderCorrectRatio.DateDay) < len(dayTimeList) {
  221. sprinkleFodderCorrectRatio.DateDay = append(sprinkleFodderCorrectRatio.DateDay, v.DayTime)
  222. }
  223. scTitleValueList = append(scTitleValueList, v.Ratio)
  224. }
  225. sprinkleFodderCorrectRatio.DataList = append(sprinkleFodderCorrectRatio.DataList, sprinkleFodderCorrectRatioDataList)
  226. sprinkleFodderCorrectRatio.PastureIds = append(sprinkleFodderCorrectRatio.PastureIds, int32(pastureId))
  227. sprinkleFodderCorrectRatio.PastureName = append(sprinkleFodderCorrectRatio.PastureName, groupPasture.Name)
  228. }
  229. pastureTopDataList, err := s.TopPasture(ctx, req)
  230. if err != nil {
  231. zaplog.Error("SearchAnalysisAccuracy", zap.Any("TopPasture", err), zap.Any("request", req))
  232. return nil, xerr.WithStack(err)
  233. }
  234. sort.Float64s(maTitleValueList)
  235. mixedFodderAccurateRatio.MaxValue = fmt.Sprintf("%.2f", maTitleValueList[len(maTitleValueList)-1])
  236. mixedFodderAccurateRatio.MinValue = fmt.Sprintf("%.2f", maTitleValueList[0])
  237. mixedFodderAccurateRatio.MiddleValue = fmt.Sprintf("%.2f", tool.Median(maTitleValueList))
  238. mixedFodderAccurateRatio.TopOneName = pastureTopDataList.Data.MixedFodderAccurateRatio[0].PastureName
  239. sort.Float64s(mcTitleValueList)
  240. mixedFodderCorrectRatio.MaxValue = fmt.Sprintf("%.2f", mcTitleValueList[len(mcTitleValueList)-1])
  241. mixedFodderCorrectRatio.MinValue = fmt.Sprintf("%.2f", mcTitleValueList[0])
  242. mixedFodderCorrectRatio.MiddleValue = fmt.Sprintf("%.2f", tool.Median(mcTitleValueList))
  243. mixedFodderCorrectRatio.TopOneName = pastureTopDataList.Data.MixedFodderCorrectRatio[0].PastureName
  244. sort.Float64s(saTitleValueList)
  245. sprinkleFodderAccurateRatio.MaxValue = fmt.Sprintf("%.2f", saTitleValueList[len(saTitleValueList)-1])
  246. sprinkleFodderAccurateRatio.MinValue = fmt.Sprintf("%.2f", saTitleValueList[0])
  247. sprinkleFodderAccurateRatio.MiddleValue = fmt.Sprintf("%.2f", tool.Median(saTitleValueList))
  248. sprinkleFodderAccurateRatio.TopOneName = pastureTopDataList.Data.SprinkleFodderAccurateRatio[0].PastureName
  249. sort.Float64s(scTitleValueList)
  250. sprinkleFodderCorrectRatio.MaxValue = fmt.Sprintf("%.2f", scTitleValueList[len(scTitleValueList)-1])
  251. sprinkleFodderCorrectRatio.MinValue = fmt.Sprintf("%.2f", scTitleValueList[0])
  252. sprinkleFodderCorrectRatio.MiddleValue = fmt.Sprintf("%.2f", tool.Median(scTitleValueList))
  253. sprinkleFodderCorrectRatio.TopOneName = pastureTopDataList.Data.SprinkleFodderCorrectRatio[0].PastureName
  254. chart := &model.Chart{
  255. MixedFodderAccurateRatio: mixedFodderAccurateRatio,
  256. MixedFodderCorrectRatio: mixedFodderCorrectRatio,
  257. SprinkleFodderAccurateRatio: sprinkleFodderAccurateRatio,
  258. SprinkleFodderCorrectRatio: sprinkleFodderCorrectRatio,
  259. }
  260. res.Data.Chart = chart
  261. res.Data.Table = s.TitleList(ctx, pastureAnalysisAccuracy)
  262. return res, nil
  263. }
  264. // TopPasture 牧场排名
  265. func (s *StoreEntry) TopPasture(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.GetPastureTopResponse, error) {
  266. dashboardTopData := &model.PastureTop{
  267. MixedFodderAccurateRatio: make([]*model.PastureTopData, 0),
  268. MixedFodderCorrectRatio: make([]*model.PastureTopData, 0),
  269. SprinkleFodderAccurateRatio: make([]*model.PastureTopData, 0),
  270. SprinkleFodderCorrectRatio: make([]*model.PastureTopData, 0),
  271. }
  272. res := &model.GetPastureTopResponse{
  273. Code: http.StatusOK,
  274. Msg: "ok",
  275. Data: nil,
  276. }
  277. analysisAccuracy, err := s.PasturePrefAnalysisData(ctx, req)
  278. if err != nil {
  279. return nil, xerr.WithStack(err)
  280. }
  281. for pastureId, data := range analysisAccuracy {
  282. groupPasture, err := s.GetGroupPastureListById(ctx, pastureId)
  283. if err != nil {
  284. zaplog.Error("TopPasture", zap.Any("GetGroupPastureListById", pastureId), zap.Any("err", err))
  285. continue
  286. }
  287. if data == nil {
  288. continue
  289. }
  290. allMaRatio, allMcRatio, allSaRatio, allScRatio := 0.0, 0.0, 0.0, 0.0
  291. for _, v := range data.MixedFodderAccurateRatio {
  292. allMaRatio += v.Ratio
  293. }
  294. dashboardTopData.MixedFodderAccurateRatio = append(dashboardTopData.MixedFodderAccurateRatio, &model.PastureTopData{
  295. PastureName: groupPasture.Name,
  296. Ratio: tool.Decimal(allMaRatio / float64(len(data.MixedFodderAccurateRatio))),
  297. })
  298. for _, v := range data.MixedFodderCorrectRatio {
  299. allMcRatio += v.Ratio
  300. }
  301. dashboardTopData.MixedFodderCorrectRatio = append(dashboardTopData.MixedFodderCorrectRatio, &model.PastureTopData{
  302. PastureName: groupPasture.Name,
  303. Ratio: tool.Decimal(allMaRatio / float64(len(data.MixedFodderCorrectRatio))),
  304. })
  305. for _, v := range data.SprinkleFodderAccurateRatio {
  306. allSaRatio += v.Ratio
  307. }
  308. dashboardTopData.SprinkleFodderAccurateRatio = append(dashboardTopData.SprinkleFodderAccurateRatio, &model.PastureTopData{
  309. PastureName: groupPasture.Name,
  310. Ratio: tool.Decimal(allSaRatio / float64(len(data.SprinkleFodderAccurateRatio))),
  311. })
  312. for _, v := range data.SprinkleFodderCorrectRatio {
  313. allScRatio += v.Ratio
  314. }
  315. dashboardTopData.SprinkleFodderCorrectRatio = append(dashboardTopData.SprinkleFodderCorrectRatio, &model.PastureTopData{
  316. PastureName: groupPasture.Name,
  317. Ratio: tool.Decimal(allScRatio / float64(len(data.SprinkleFodderCorrectRatio))),
  318. })
  319. }
  320. sort.Slice(dashboardTopData.MixedFodderAccurateRatio, func(i, j int) bool {
  321. return dashboardTopData.MixedFodderAccurateRatio[i].Ratio > dashboardTopData.MixedFodderAccurateRatio[j].Ratio
  322. })
  323. sort.Slice(dashboardTopData.MixedFodderCorrectRatio, func(i, j int) bool {
  324. return dashboardTopData.MixedFodderCorrectRatio[i].Ratio > dashboardTopData.MixedFodderCorrectRatio[j].Ratio
  325. })
  326. sort.Slice(dashboardTopData.SprinkleFodderAccurateRatio, func(i, j int) bool {
  327. return dashboardTopData.SprinkleFodderAccurateRatio[i].Ratio > dashboardTopData.SprinkleFodderAccurateRatio[j].Ratio
  328. })
  329. sort.Slice(dashboardTopData.SprinkleFodderCorrectRatio, func(i, j int) bool {
  330. return dashboardTopData.SprinkleFodderCorrectRatio[i].Ratio > dashboardTopData.SprinkleFodderCorrectRatio[j].Ratio
  331. })
  332. if req.DashboardTopType > 0 {
  333. switch req.DashboardTopType {
  334. case operationPb.DashboardTopType_MIXED_ACCURATE:
  335. dashboardTopData.MixedFodderAccurateRatio = dashboardTopRand(req, dashboardTopData.MixedFodderAccurateRatio)
  336. case operationPb.DashboardTopType_MIXED_CORRECT:
  337. dashboardTopData.MixedFodderCorrectRatio = dashboardTopRand(req, dashboardTopData.MixedFodderCorrectRatio)
  338. case operationPb.DashboardTopType_SPRINKLE_ACCURATE:
  339. dashboardTopData.SprinkleFodderAccurateRatio = dashboardTopRand(req, dashboardTopData.SprinkleFodderAccurateRatio)
  340. case operationPb.DashboardTopType_Sprinkle_CORRECT:
  341. dashboardTopData.SprinkleFodderCorrectRatio = dashboardTopRand(req, dashboardTopData.SprinkleFodderCorrectRatio)
  342. }
  343. }
  344. res.Data = dashboardTopData
  345. return res, nil
  346. }
  347. func (s *StoreEntry) TitleList(ctx context.Context, pastureAnalysisList map[int64]*model.PastureAnalysisAccuracyData) *model.Table {
  348. res := &model.Table{
  349. TitleList: make([]*model.TableList, 0),
  350. DataList: &model.DataList{
  351. MixedFodderAccurateRatio: make([]map[string]string, 0),
  352. MixedFodderCorrectRatio: make([]map[string]string, 0),
  353. SprinkleFodderAccurateRatio: make([]map[string]string, 0),
  354. SprinkleFodderCorrectRatio: make([]map[string]string, 0),
  355. },
  356. }
  357. for pastureId, data := range pastureAnalysisList {
  358. groupPasture, err := s.GetGroupPastureListById(ctx, pastureId)
  359. if err != nil {
  360. zaplog.Info("TitleList", zap.Any("GetGroupPastureListById", pastureId), zap.Any("err", err))
  361. continue
  362. }
  363. if len(res.TitleList) <= len(data.MixedFodderAccurateRatio) {
  364. res.TitleList = append(res.TitleList, &model.TableList{
  365. Name: "title",
  366. Value: "牧场",
  367. })
  368. }
  369. maMap := map[string]string{
  370. "title": groupPasture.Name,
  371. }
  372. for i, v := range data.MixedFodderCorrectRatio {
  373. maMap[fmt.Sprintf("data%d", i+1)] = fmt.Sprintf("%.2f", v.Ratio)
  374. if len(res.TitleList) <= len(data.MixedFodderAccurateRatio) {
  375. res.TitleList = append(res.TitleList, &model.TableList{
  376. Name: fmt.Sprintf("data%d", i+1),
  377. Value: v.DayTime,
  378. })
  379. }
  380. }
  381. res.DataList.MixedFodderAccurateRatio = append(res.DataList.MixedFodderAccurateRatio, maMap)
  382. mcMap := map[string]string{
  383. "title": groupPasture.Name,
  384. }
  385. for i, v := range data.MixedFodderCorrectRatio {
  386. mcMap[fmt.Sprintf("data%d", i+1)] = fmt.Sprintf("%.2f", v.Ratio)
  387. }
  388. res.DataList.MixedFodderCorrectRatio = append(res.DataList.MixedFodderCorrectRatio, mcMap)
  389. saMap := map[string]string{
  390. "title": groupPasture.Name,
  391. }
  392. for i, v := range data.SprinkleFodderAccurateRatio {
  393. saMap[fmt.Sprintf("data%d", i+1)] = fmt.Sprintf("%.2f", v.Ratio)
  394. }
  395. res.DataList.SprinkleFodderAccurateRatio = append(res.DataList.SprinkleFodderAccurateRatio, saMap)
  396. scMap := map[string]string{
  397. "title": groupPasture.Name,
  398. }
  399. for i, v := range data.SprinkleFodderCorrectRatio {
  400. scMap[fmt.Sprintf("data%d", i+1)] = fmt.Sprintf("%.2f", v.Ratio)
  401. }
  402. res.DataList.SprinkleFodderCorrectRatio = append(res.DataList.SprinkleFodderCorrectRatio, scMap)
  403. }
  404. return res
  405. }
  406. func (s *StoreEntry) ExecutionTime(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.ExecTimeResponse, error) {
  407. res := &model.ExecTimeResponse{
  408. Code: http.StatusOK,
  409. Msg: "ok",
  410. Data: &model.ExecTimeDataList{
  411. Chart: &model.ExecTimeDataListChart{
  412. Title: make([]string, 0),
  413. AddFeedTime: make([][]string, 0),
  414. SprinkleTime: make([][]string, 0),
  415. StirTime: make([][]string, 0),
  416. },
  417. TableList: make([]map[string]string, 0),
  418. },
  419. }
  420. pastureExecTime, err := s.PasturePrefExecTimeData(ctx, req)
  421. if err != nil {
  422. return nil, xerr.WithStack(err)
  423. }
  424. for pastureName, execTime := range pastureExecTime {
  425. res.Data.Chart.Title = append(res.Data.Chart.Title, pastureName)
  426. addFeedTimeStr, sprinkleTimeStr, stirTimeStr := make([]string, 0), make([]string, 0), make([]string, 0)
  427. if execTime != nil {
  428. addFeedTimeStr = append(addFeedTimeStr, execTime.AddFeedTime.MaxValue, execTime.AddFeedTime.UpMiddleValue,
  429. execTime.AddFeedTime.MiddleValue, execTime.AddFeedTime.DownMiddleValue, execTime.AddFeedTime.MinValue)
  430. sprinkleTimeStr = append(sprinkleTimeStr, execTime.SprinkleTime.MaxValue, execTime.SprinkleTime.UpMiddleValue,
  431. execTime.SprinkleTime.MiddleValue, execTime.SprinkleTime.DownMiddleValue, execTime.SprinkleTime.MinValue)
  432. stirTimeStr = append(stirTimeStr, execTime.StirTime.MaxValue, execTime.StirTime.UpMiddleValue,
  433. execTime.StirTime.MiddleValue, execTime.StirTime.DownMiddleValue, execTime.StirTime.MinValue)
  434. }
  435. res.Data.Chart.AddFeedTime = append(res.Data.Chart.AddFeedTime, addFeedTimeStr)
  436. res.Data.Chart.SprinkleTime = append(res.Data.Chart.SprinkleTime, sprinkleTimeStr)
  437. res.Data.Chart.StirTime = append(res.Data.Chart.StirTime, stirTimeStr)
  438. if execTime == nil {
  439. continue
  440. }
  441. tableList := map[string]string{
  442. "title": pastureName,
  443. "add_feed_time": "加料时间",
  444. "add_feed_time_max_value": execTime.AddFeedTime.MaxValue,
  445. "add_feed_time_up_middle_value": execTime.AddFeedTime.UpMiddleValue,
  446. "add_feed_time_middle_value": execTime.AddFeedTime.MiddleValue,
  447. "add_feed_time_down_middle_value": execTime.AddFeedTime.DownMiddleValue,
  448. "add_feed_time_min_value": execTime.AddFeedTime.MinValue,
  449. "sprinkle_time": "撒料时间",
  450. "sprinkle_time_max_value": execTime.SprinkleTime.MaxValue,
  451. "sprinkle_time_up_middle_value": execTime.SprinkleTime.UpMiddleValue,
  452. "sprinkle_time_middle_value": execTime.SprinkleTime.MiddleValue,
  453. "sprinkle_time_down_middle_value": execTime.SprinkleTime.DownMiddleValue,
  454. "sprinkle_time_min_value": execTime.SprinkleTime.MinValue,
  455. "stir_time": "搅拌延迟时间",
  456. "stir_time_max_value": execTime.StirTime.MaxValue,
  457. "stir_time_up_middle_value": execTime.StirTime.UpMiddleValue,
  458. "stir_time_middle_value": execTime.StirTime.MiddleValue,
  459. "stir_time_down_middle_value": execTime.StirTime.DownMiddleValue,
  460. "stir_time_min_value": execTime.StirTime.MinValue,
  461. }
  462. res.Data.TableList = append(res.Data.TableList, tableList)
  463. }
  464. return res, nil
  465. }
  466. func (s *StoreEntry) SprinkleFeedTime(ctx context.Context, req *operationPb.SprinkleFeedTimeRequest) (*model.SprinkleFeedTimeResponse, error) {
  467. res := &model.SprinkleFeedTimeResponse{
  468. Code: http.StatusOK,
  469. Msg: "ok",
  470. Data: &model.SprinkleFeedTimeData{
  471. Chart: &model.SprinkleFeedTimeChart{
  472. Title: make([]string, 0),
  473. SprinkleNumberList: make([][]int32, 0),
  474. },
  475. TableList: make([]*model.SprinkleFeedTimeTable, 0),
  476. },
  477. }
  478. pastureSprinkleDataList, err := s.PastureSprinkleFeedTime(ctx, req)
  479. if err != nil {
  480. return nil, xerr.WithStack(err)
  481. }
  482. tableList := make([]*model.SprinkleFeedTimeTable, 0)
  483. infoSprinkleNumber, errorSprinkleNumber := make([]int32, 0), make([]int32, 0)
  484. for pastureName, data := range pastureSprinkleDataList {
  485. sprinkleFeedTimeList := make(map[int32]map[int32][]int64, 0)
  486. for _, v := range data {
  487. tableList = append(tableList, &model.SprinkleFeedTimeTable{
  488. PastureName: pastureName,
  489. BarnName: v.FName,
  490. ClassNumber: fmt.Sprintf("%d", v.Times),
  491. RealitySprinkleFeedTime: tool.TimeSub(v.InTime, v.ProcessTime),
  492. })
  493. realityTime := tool.TimeSub(v.InTime, v.ProcessTime)
  494. realityTimeUnix, _ := time.Parse(model.LayoutTime, realityTime)
  495. if sprinkleFeedTimeList[v.FBarId] == nil {
  496. sprinkleFeedTimeList[v.FBarId] = make(map[int32][]int64, 0)
  497. }
  498. sprinkleFeedTimeList[v.FBarId][v.Times] = append(sprinkleFeedTimeList[v.FBarId][v.Times], realityTimeUnix.Unix())
  499. }
  500. res.Data.Chart.Title = append(res.Data.Chart.Title, pastureName)
  501. infoNumber, errNumber := sprinkleExecTimeAnalysis(sprinkleFeedTimeList)
  502. infoSprinkleNumber = append(infoSprinkleNumber, infoNumber)
  503. errorSprinkleNumber = append(errorSprinkleNumber, errNumber)
  504. }
  505. res.Data.Chart.SprinkleNumberList = append(res.Data.Chart.SprinkleNumberList, infoSprinkleNumber, errorSprinkleNumber)
  506. res.Data.TableList = tableList
  507. return res, nil
  508. }
  509. func (s *StoreEntry) FeedMixedAndTmrName(ctx context.Context, req *operationPb.MixedCategoryTmrName) (*model.PastureCommonResponse, error) {
  510. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  511. if err != nil {
  512. return nil, xerr.WithStack(err)
  513. }
  514. pastureId := req.PastureId
  515. if groupPasture.PastureId > 0 {
  516. pastureId = int32(groupPasture.PastureId)
  517. }
  518. body := &model.PastureCommonRequest{
  519. Name: req.ApiName,
  520. ReturnType: "Map",
  521. ParamMaps: &model.MixedCategoryTmrNameParams{
  522. PastureId: fmt.Sprintf("%d", pastureId),
  523. StartTime: req.StartTime,
  524. EndTime: req.EndTime,
  525. },
  526. }
  527. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  528. if _, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  529. return nil, xerr.WithStack(err)
  530. }
  531. return response, nil
  532. }
  533. func sprinkleExecTimeAnalysis(sprinkleFeedTimeList map[int32]map[int32][]int64) (int32, int32) {
  534. var infoSprinkleNumber, errorSprinkleNumber int32 = 0, 0
  535. if len(sprinkleFeedTimeList) <= 0 {
  536. return infoSprinkleNumber, errorSprinkleNumber
  537. } else {
  538. for _, value := range sprinkleFeedTimeList {
  539. for _, execTimeList := range value {
  540. middleValue := tool.MedianInt64(execTimeList)
  541. for _, v := range execTimeList {
  542. if v >= middleValue-int64(compareTime) && v <= middleValue+int64(compareTime) {
  543. infoSprinkleNumber += 1
  544. } else {
  545. errorSprinkleNumber += 1
  546. }
  547. }
  548. }
  549. }
  550. }
  551. return infoSprinkleNumber, errorSprinkleNumber
  552. }
  553. func dashboardTopRand(req *operationPb.SearchAnalysisAccuracyRequest, data []*model.PastureTopData) []*model.PastureTopData {
  554. if req.TopRandStart < 0 || req.TopRandEnd < 0 || req.TopRandStart > req.TopRandEnd {
  555. return data
  556. }
  557. res := make([]*model.PastureTopData, 0)
  558. for _, v := range data {
  559. if v.Ratio >= float64(req.TopRandStart) && v.Ratio <= float64(req.TopRandEnd) {
  560. res = append(res, v)
  561. }
  562. }
  563. return res
  564. }