dashboard_service.go 25 KB

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