dashboard_service.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. mTopOneName := ""
  174. dayTimeList := tool.TimeBetween(req.StartDate, req.EndDate)
  175. for pastureId, data := range pastureAnalysisAccuracy {
  176. groupPasture, err := s.GetGroupPastureListById(ctx, pastureId)
  177. if err != nil {
  178. zaplog.Error("SearchAnalysisAccuracy GetGroupPastureListById",
  179. zap.Any("pastureId", pastureId), zap.Any("error", err))
  180. continue
  181. }
  182. if data == nil {
  183. continue
  184. }
  185. mixedFodderAccurateRatioDataList := make([]string, 0)
  186. for _, v := range data.MixedFodderAccurateRatio {
  187. mixedFodderAccurateRatioDataList = append(mixedFodderAccurateRatioDataList, fmt.Sprintf("%.2f", v.Ratio))
  188. if len(mixedFodderAccurateRatio.DateDay) < len(dayTimeList) {
  189. mixedFodderAccurateRatio.DateDay = append(mixedFodderAccurateRatio.DateDay, v.DayTime)
  190. }
  191. maTitleValueList = append(maTitleValueList, v.Ratio)
  192. }
  193. mixedFodderAccurateRatio.DataList = append(mixedFodderAccurateRatio.DataList, mixedFodderAccurateRatioDataList)
  194. mixedFodderAccurateRatio.PastureIds = append(mixedFodderAccurateRatio.PastureIds, int32(pastureId))
  195. mixedFodderAccurateRatio.PastureName = append(mixedFodderAccurateRatio.PastureName, groupPasture.Name)
  196. mixedFodderCorrectRatioDataList := make([]string, 0)
  197. for _, v := range data.MixedFodderCorrectRatio {
  198. mixedFodderCorrectRatioDataList = append(mixedFodderCorrectRatioDataList, fmt.Sprintf("%.2f", v.Ratio))
  199. if len(mixedFodderCorrectRatio.DateDay) < len(dayTimeList) {
  200. mixedFodderCorrectRatio.DateDay = append(mixedFodderCorrectRatio.DateDay, v.DayTime)
  201. }
  202. mcTitleValueList = append(mcTitleValueList, v.Ratio)
  203. }
  204. mixedFodderCorrectRatio.DataList = append(mixedFodderCorrectRatio.DataList, mixedFodderCorrectRatioDataList)
  205. mixedFodderCorrectRatio.PastureIds = append(mixedFodderCorrectRatio.PastureIds, int32(pastureId))
  206. mixedFodderCorrectRatio.PastureName = append(mixedFodderCorrectRatio.PastureName, groupPasture.Name)
  207. sprinkleFodderRatioDataList := make([]string, 0)
  208. for _, v := range data.SprinkleFodderAccurateRatio {
  209. sprinkleFodderRatioDataList = append(sprinkleFodderRatioDataList, fmt.Sprintf("%.2f", v.Ratio))
  210. if len(sprinkleFodderAccurateRatio.DateDay) < len(dayTimeList) {
  211. sprinkleFodderAccurateRatio.DateDay = append(sprinkleFodderAccurateRatio.DateDay, v.DayTime)
  212. }
  213. saTitleValueList = append(saTitleValueList, v.Ratio)
  214. }
  215. sprinkleFodderAccurateRatio.DataList = append(sprinkleFodderAccurateRatio.DataList, sprinkleFodderRatioDataList)
  216. sprinkleFodderAccurateRatio.PastureIds = append(sprinkleFodderAccurateRatio.PastureIds, int32(pastureId))
  217. sprinkleFodderAccurateRatio.PastureName = append(sprinkleFodderAccurateRatio.PastureName, groupPasture.Name)
  218. sprinkleFodderCorrectRatioDataList := make([]string, 0)
  219. for _, v := range data.SprinkleFodderCorrectRatio {
  220. sprinkleFodderCorrectRatioDataList = append(sprinkleFodderCorrectRatioDataList, fmt.Sprintf("%.2f", v.Ratio))
  221. if len(sprinkleFodderCorrectRatio.DateDay) < len(dayTimeList) {
  222. sprinkleFodderCorrectRatio.DateDay = append(sprinkleFodderCorrectRatio.DateDay, v.DayTime)
  223. }
  224. scTitleValueList = append(scTitleValueList, v.Ratio)
  225. }
  226. sprinkleFodderCorrectRatio.DataList = append(sprinkleFodderCorrectRatio.DataList, sprinkleFodderCorrectRatioDataList)
  227. sprinkleFodderCorrectRatio.PastureIds = append(sprinkleFodderCorrectRatio.PastureIds, int32(pastureId))
  228. sprinkleFodderCorrectRatio.PastureName = append(sprinkleFodderCorrectRatio.PastureName, groupPasture.Name)
  229. }
  230. sort.Float64s(maTitleValueList)
  231. mixedFodderAccurateRatio.MaxValue = fmt.Sprintf("%.2f", maTitleValueList[len(maTitleValueList)-1])
  232. mixedFodderAccurateRatio.MinValue = fmt.Sprintf("%.2f", maTitleValueList[0])
  233. mixedFodderAccurateRatio.MiddleValue = fmt.Sprintf("%.2f", tool.Median(maTitleValueList))
  234. mixedFodderAccurateRatio.TopOneName = mTopOneName
  235. sort.Float64s(mcTitleValueList)
  236. mixedFodderCorrectRatio.MaxValue = fmt.Sprintf("%.2f", mcTitleValueList[len(mcTitleValueList)-1])
  237. mixedFodderCorrectRatio.MinValue = fmt.Sprintf("%.2f", mcTitleValueList[0])
  238. mixedFodderCorrectRatio.MiddleValue = fmt.Sprintf("%.2f", tool.Median(mcTitleValueList))
  239. mixedFodderCorrectRatio.TopOneName = mTopOneName
  240. sort.Float64s(saTitleValueList)
  241. sprinkleFodderAccurateRatio.MaxValue = fmt.Sprintf("%.2f", saTitleValueList[len(saTitleValueList)-1])
  242. sprinkleFodderAccurateRatio.MinValue = fmt.Sprintf("%.2f", saTitleValueList[0])
  243. sprinkleFodderAccurateRatio.MiddleValue = fmt.Sprintf("%.2f", tool.Median(saTitleValueList))
  244. sprinkleFodderAccurateRatio.TopOneName = mTopOneName
  245. sort.Float64s(scTitleValueList)
  246. sprinkleFodderCorrectRatio.MaxValue = fmt.Sprintf("%.2f", scTitleValueList[len(scTitleValueList)-1])
  247. sprinkleFodderCorrectRatio.MinValue = fmt.Sprintf("%.2f", scTitleValueList[0])
  248. sprinkleFodderCorrectRatio.MiddleValue = fmt.Sprintf("%.2f", tool.Median(scTitleValueList))
  249. sprinkleFodderCorrectRatio.TopOneName = mTopOneName
  250. chart := &model.Chart{
  251. MixedFodderAccurateRatio: mixedFodderAccurateRatio,
  252. MixedFodderCorrectRatio: mixedFodderCorrectRatio,
  253. SprinkleFodderAccurateRatio: sprinkleFodderAccurateRatio,
  254. SprinkleFodderCorrectRatio: sprinkleFodderCorrectRatio,
  255. }
  256. res.Data.Chart = chart
  257. res.Data.Table = s.TitleList(ctx, pastureAnalysisAccuracy)
  258. return res, nil
  259. }
  260. // TopPasture 牧场排名
  261. func (s *StoreEntry) TopPasture(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.GetPastureTopResponse, error) {
  262. dashboardTopData := &model.PastureTop{
  263. MixedFodderAccurateRatio: make([]*model.PastureTopData, 0),
  264. MixedFodderCorrectRatio: make([]*model.PastureTopData, 0),
  265. SprinkleFodderAccurateRatio: make([]*model.PastureTopData, 0),
  266. SprinkleFodderCorrectRatio: make([]*model.PastureTopData, 0),
  267. }
  268. res := &model.GetPastureTopResponse{
  269. Code: http.StatusOK,
  270. Msg: "ok",
  271. Data: nil,
  272. }
  273. analysisAccuracy, err := s.PasturePrefAnalysisData(ctx, req)
  274. if err != nil {
  275. return nil, xerr.WithStack(err)
  276. }
  277. for pastureId, data := range analysisAccuracy {
  278. groupPasture, err := s.GetGroupPastureListById(ctx, pastureId)
  279. if err != nil {
  280. zaplog.Error("TopPasture", zap.Any("GetGroupPastureListById", pastureId), zap.Any("err", err))
  281. continue
  282. }
  283. if data == nil {
  284. continue
  285. }
  286. allMaRatio, allMcRatio, allSaRatio, allScRatio := 0.0, 0.0, 0.0, 0.0
  287. for _, v := range data.MixedFodderAccurateRatio {
  288. allMaRatio += v.Ratio
  289. }
  290. dashboardTopData.MixedFodderAccurateRatio = append(dashboardTopData.MixedFodderAccurateRatio, &model.PastureTopData{
  291. PastureName: groupPasture.Name,
  292. Ratio: tool.Decimal(allMaRatio / float64(len(data.MixedFodderAccurateRatio))),
  293. })
  294. for _, v := range data.MixedFodderCorrectRatio {
  295. allMcRatio += v.Ratio
  296. }
  297. dashboardTopData.MixedFodderCorrectRatio = append(dashboardTopData.MixedFodderCorrectRatio, &model.PastureTopData{
  298. PastureName: groupPasture.Name,
  299. Ratio: tool.Decimal(allMaRatio / float64(len(data.MixedFodderCorrectRatio))),
  300. })
  301. for _, v := range data.SprinkleFodderAccurateRatio {
  302. allSaRatio += v.Ratio
  303. }
  304. dashboardTopData.SprinkleFodderAccurateRatio = append(dashboardTopData.SprinkleFodderAccurateRatio, &model.PastureTopData{
  305. PastureName: groupPasture.Name,
  306. Ratio: tool.Decimal(allSaRatio / float64(len(data.SprinkleFodderAccurateRatio))),
  307. })
  308. for _, v := range data.SprinkleFodderCorrectRatio {
  309. allScRatio += v.Ratio
  310. }
  311. dashboardTopData.SprinkleFodderCorrectRatio = append(dashboardTopData.SprinkleFodderCorrectRatio, &model.PastureTopData{
  312. PastureName: groupPasture.Name,
  313. Ratio: tool.Decimal(allScRatio / float64(len(data.SprinkleFodderCorrectRatio))),
  314. })
  315. }
  316. sort.Slice(dashboardTopData.MixedFodderAccurateRatio, func(i, j int) bool {
  317. return dashboardTopData.MixedFodderAccurateRatio[i].Ratio > dashboardTopData.MixedFodderAccurateRatio[j].Ratio
  318. })
  319. sort.Slice(dashboardTopData.MixedFodderCorrectRatio, func(i, j int) bool {
  320. return dashboardTopData.MixedFodderCorrectRatio[i].Ratio > dashboardTopData.MixedFodderCorrectRatio[j].Ratio
  321. })
  322. sort.Slice(dashboardTopData.SprinkleFodderAccurateRatio, func(i, j int) bool {
  323. return dashboardTopData.SprinkleFodderAccurateRatio[i].Ratio > dashboardTopData.SprinkleFodderAccurateRatio[j].Ratio
  324. })
  325. sort.Slice(dashboardTopData.SprinkleFodderCorrectRatio, func(i, j int) bool {
  326. return dashboardTopData.SprinkleFodderCorrectRatio[i].Ratio > dashboardTopData.SprinkleFodderCorrectRatio[j].Ratio
  327. })
  328. if req.DashboardTopType > 0 {
  329. switch req.DashboardTopType {
  330. case operationPb.DashboardTopType_MIXED_ACCURATE:
  331. dashboardTopData.MixedFodderAccurateRatio = dashboardTopRand(req, dashboardTopData.MixedFodderAccurateRatio)
  332. case operationPb.DashboardTopType_MIXED_CORRECT:
  333. dashboardTopData.MixedFodderCorrectRatio = dashboardTopRand(req, dashboardTopData.MixedFodderCorrectRatio)
  334. case operationPb.DashboardTopType_SPRINKLE_ACCURATE:
  335. dashboardTopData.SprinkleFodderAccurateRatio = dashboardTopRand(req, dashboardTopData.SprinkleFodderAccurateRatio)
  336. case operationPb.DashboardTopType_Sprinkle_CORRECT:
  337. dashboardTopData.SprinkleFodderCorrectRatio = dashboardTopRand(req, dashboardTopData.SprinkleFodderCorrectRatio)
  338. }
  339. }
  340. res.Data = dashboardTopData
  341. return res, nil
  342. }
  343. func (s *StoreEntry) TitleList(ctx context.Context, pastureAnalysisList map[int64]*model.PastureAnalysisAccuracyData) *model.Table {
  344. res := &model.Table{
  345. TitleList: make([]*model.TableList, 0),
  346. DataList: &model.DataList{
  347. MixedFodderAccurateRatio: make([]map[string]string, 0),
  348. MixedFodderCorrectRatio: make([]map[string]string, 0),
  349. SprinkleFodderAccurateRatio: make([]map[string]string, 0),
  350. SprinkleFodderCorrectRatio: make([]map[string]string, 0),
  351. },
  352. }
  353. for pastureId, data := range pastureAnalysisList {
  354. groupPasture, err := s.GetGroupPastureListById(ctx, pastureId)
  355. if err != nil {
  356. zaplog.Info("TitleList", zap.Any("GetGroupPastureListById", pastureId), zap.Any("err", err))
  357. continue
  358. }
  359. if len(res.TitleList) <= len(data.MixedFodderAccurateRatio) {
  360. res.TitleList = append(res.TitleList, &model.TableList{
  361. Name: "title",
  362. Value: "牧场",
  363. })
  364. }
  365. maMap := map[string]string{
  366. "title": groupPasture.Name,
  367. }
  368. for i, v := range data.MixedFodderCorrectRatio {
  369. maMap[fmt.Sprintf("data%d", i+1)] = fmt.Sprintf("%.2f", v.Ratio)
  370. if len(res.TitleList) <= len(data.MixedFodderAccurateRatio) {
  371. res.TitleList = append(res.TitleList, &model.TableList{
  372. Name: fmt.Sprintf("data%d", i+1),
  373. Value: v.DayTime,
  374. })
  375. }
  376. }
  377. res.DataList.MixedFodderAccurateRatio = append(res.DataList.MixedFodderAccurateRatio, maMap)
  378. mcMap := map[string]string{
  379. "title": groupPasture.Name,
  380. }
  381. for i, v := range data.MixedFodderCorrectRatio {
  382. mcMap[fmt.Sprintf("data%d", i+1)] = fmt.Sprintf("%.2f", v.Ratio)
  383. }
  384. res.DataList.MixedFodderCorrectRatio = append(res.DataList.MixedFodderCorrectRatio, mcMap)
  385. saMap := map[string]string{
  386. "title": groupPasture.Name,
  387. }
  388. for i, v := range data.SprinkleFodderAccurateRatio {
  389. saMap[fmt.Sprintf("data%d", i+1)] = fmt.Sprintf("%.2f", v.Ratio)
  390. }
  391. res.DataList.SprinkleFodderAccurateRatio = append(res.DataList.SprinkleFodderAccurateRatio, saMap)
  392. scMap := map[string]string{
  393. "title": groupPasture.Name,
  394. }
  395. for i, v := range data.SprinkleFodderCorrectRatio {
  396. scMap[fmt.Sprintf("data%d", i+1)] = fmt.Sprintf("%.2f", v.Ratio)
  397. }
  398. res.DataList.SprinkleFodderCorrectRatio = append(res.DataList.SprinkleFodderCorrectRatio, scMap)
  399. }
  400. return res
  401. }
  402. func (s *StoreEntry) ExecutionTime(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.ExecTimeResponse, error) {
  403. res := &model.ExecTimeResponse{
  404. Code: http.StatusOK,
  405. Msg: "ok",
  406. Data: &model.ExecTimeDataList{
  407. Chart: &model.ExecTimeDataListChart{
  408. Title: make([]string, 0),
  409. AddFeedTime: make([][]string, 0),
  410. SprinkleTime: make([][]string, 0),
  411. StirTime: make([][]string, 0),
  412. },
  413. TableList: make([]map[string]string, 0),
  414. },
  415. }
  416. pastureExecTime, err := s.PasturePrefExecTimeData(ctx, req)
  417. if err != nil {
  418. return nil, xerr.WithStack(err)
  419. }
  420. for pastureName, execTime := range pastureExecTime {
  421. res.Data.Chart.Title = append(res.Data.Chart.Title, pastureName)
  422. addFeedTimeStr, sprinkleTimeStr, stirTimeStr := make([]string, 0), make([]string, 0), make([]string, 0)
  423. if execTime != nil {
  424. addFeedTimeStr = append(addFeedTimeStr, execTime.AddFeedTime.MaxValue, execTime.AddFeedTime.UpMiddleValue,
  425. execTime.AddFeedTime.MiddleValue, execTime.AddFeedTime.DownMiddleValue, execTime.AddFeedTime.MinValue)
  426. sprinkleTimeStr = append(sprinkleTimeStr, execTime.SprinkleTime.MaxValue, execTime.SprinkleTime.UpMiddleValue,
  427. execTime.SprinkleTime.MiddleValue, execTime.SprinkleTime.DownMiddleValue, execTime.SprinkleTime.MinValue)
  428. stirTimeStr = append(stirTimeStr, execTime.StirTime.MaxValue, execTime.StirTime.UpMiddleValue,
  429. execTime.StirTime.MiddleValue, execTime.StirTime.DownMiddleValue, execTime.StirTime.MinValue)
  430. }
  431. res.Data.Chart.AddFeedTime = append(res.Data.Chart.AddFeedTime, addFeedTimeStr)
  432. res.Data.Chart.SprinkleTime = append(res.Data.Chart.SprinkleTime, sprinkleTimeStr)
  433. res.Data.Chart.StirTime = append(res.Data.Chart.StirTime, stirTimeStr)
  434. if execTime == nil {
  435. continue
  436. }
  437. tableList := map[string]string{
  438. "title": pastureName,
  439. "add_feed_time": "加料时间",
  440. "add_feed_time_max_value": execTime.AddFeedTime.MaxValue,
  441. "add_feed_time_up_middle_value": execTime.AddFeedTime.UpMiddleValue,
  442. "add_feed_time_middle_value": execTime.AddFeedTime.MiddleValue,
  443. "add_feed_time_down_middle_value": execTime.AddFeedTime.DownMiddleValue,
  444. "add_feed_time_min_value": execTime.AddFeedTime.MinValue,
  445. "sprinkle_time": "撒料时间",
  446. "sprinkle_time_max_value": execTime.SprinkleTime.MaxValue,
  447. "sprinkle_time_up_middle_value": execTime.SprinkleTime.UpMiddleValue,
  448. "sprinkle_time_middle_value": execTime.SprinkleTime.MiddleValue,
  449. "sprinkle_time_down_middle_value": execTime.SprinkleTime.DownMiddleValue,
  450. "sprinkle_time_min_value": execTime.SprinkleTime.MinValue,
  451. "stir_time": "搅拌延迟时间",
  452. "stir_time_max_value": execTime.StirTime.MaxValue,
  453. "stir_time_up_middle_value": execTime.StirTime.UpMiddleValue,
  454. "stir_time_middle_value": execTime.StirTime.MiddleValue,
  455. "stir_time_down_middle_value": execTime.StirTime.DownMiddleValue,
  456. "stir_time_min_value": execTime.StirTime.MinValue,
  457. }
  458. res.Data.TableList = append(res.Data.TableList, tableList)
  459. }
  460. return res, nil
  461. }
  462. func (s *StoreEntry) SprinkleFeedTime(ctx context.Context, req *operationPb.SprinkleFeedTimeRequest) (*model.SprinkleFeedTimeResponse, error) {
  463. res := &model.SprinkleFeedTimeResponse{
  464. Code: http.StatusOK,
  465. Msg: "ok",
  466. Data: &model.SprinkleFeedTimeData{
  467. Chart: &model.SprinkleFeedTimeChart{
  468. Title: make([]string, 0),
  469. SprinkleNumberList: make([][]int32, 0),
  470. },
  471. TableList: make([]*model.SprinkleFeedTimeTable, 0),
  472. },
  473. }
  474. pastureSprinkleDataList, err := s.PastureSprinkleFeedTime(ctx, req)
  475. if err != nil {
  476. return nil, xerr.WithStack(err)
  477. }
  478. tableList := make([]*model.SprinkleFeedTimeTable, 0)
  479. infoSprinkleNumber, errorSprinkleNumber := make([]int32, 0), make([]int32, 0)
  480. for pastureName, data := range pastureSprinkleDataList {
  481. sprinkleFeedTimeList := make(map[int32]map[int32][]int64, 0)
  482. for _, v := range data {
  483. tableList = append(tableList, &model.SprinkleFeedTimeTable{
  484. PastureName: pastureName,
  485. BarnName: v.FName,
  486. ClassNumber: fmt.Sprintf("%d", v.Times),
  487. RealitySprinkleFeedTime: tool.TimeSub(v.InTime, v.ProcessTime),
  488. })
  489. realityTime := tool.TimeSub(v.InTime, v.ProcessTime)
  490. realityTimeUnix, _ := time.Parse(model.LayoutTime, realityTime)
  491. if sprinkleFeedTimeList[v.FBarId] == nil {
  492. sprinkleFeedTimeList[v.FBarId] = make(map[int32][]int64, 0)
  493. }
  494. sprinkleFeedTimeList[v.FBarId][v.Times] = append(sprinkleFeedTimeList[v.FBarId][v.Times], realityTimeUnix.Unix())
  495. }
  496. res.Data.Chart.Title = append(res.Data.Chart.Title, pastureName)
  497. infoNumber, errNumber := sprinkleExecTimeAnalysis(sprinkleFeedTimeList)
  498. infoSprinkleNumber = append(infoSprinkleNumber, infoNumber)
  499. errorSprinkleNumber = append(errorSprinkleNumber, errNumber)
  500. }
  501. res.Data.Chart.SprinkleNumberList = append(res.Data.Chart.SprinkleNumberList, infoSprinkleNumber, errorSprinkleNumber)
  502. res.Data.TableList = tableList
  503. return res, nil
  504. }
  505. func (s *StoreEntry) FeedMixedAndTmrName(ctx context.Context, req *operationPb.MixedCategoryTmrName) (*model.PastureCommonResponse, error) {
  506. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  507. if err != nil {
  508. return nil, xerr.WithStack(err)
  509. }
  510. pastureId := req.PastureId
  511. if groupPasture.PastureId > 0 {
  512. pastureId = int32(groupPasture.PastureId)
  513. }
  514. body := &model.PastureCommonRequest{
  515. Name: req.ApiName,
  516. ReturnType: "Map",
  517. ParamMaps: &model.MixedCategoryTmrNameParams{
  518. PastureId: fmt.Sprintf("%d", pastureId),
  519. StartTime: req.StartTime,
  520. EndTime: req.EndTime,
  521. },
  522. }
  523. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  524. if _, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  525. return nil, xerr.WithStack(err)
  526. }
  527. return response, nil
  528. }
  529. func sprinkleExecTimeAnalysis(sprinkleFeedTimeList map[int32]map[int32][]int64) (int32, int32) {
  530. var infoSprinkleNumber, errorSprinkleNumber int32 = 0, 0
  531. if len(sprinkleFeedTimeList) <= 0 {
  532. return infoSprinkleNumber, errorSprinkleNumber
  533. } else {
  534. for _, value := range sprinkleFeedTimeList {
  535. for _, execTimeList := range value {
  536. middleValue := tool.MedianInt64(execTimeList)
  537. for _, v := range execTimeList {
  538. if v >= middleValue-int64(compareTime) && v <= middleValue+int64(compareTime) {
  539. infoSprinkleNumber += 1
  540. } else {
  541. errorSprinkleNumber += 1
  542. }
  543. }
  544. }
  545. }
  546. }
  547. return infoSprinkleNumber, errorSprinkleNumber
  548. }
  549. func dashboardTopRand(req *operationPb.SearchAnalysisAccuracyRequest, data []*model.PastureTopData) []*model.PastureTopData {
  550. if req.TopRandStart < 0 || req.TopRandEnd < 0 || req.TopRandStart > req.TopRandEnd {
  551. return data
  552. }
  553. res := make([]*model.PastureTopData, 0)
  554. for _, v := range data {
  555. if float64(req.TopRandStart) <= v.Ratio || v.Ratio <= float64(req.TopRandEnd) {
  556. res = append(res, v)
  557. }
  558. }
  559. return res
  560. }