dashboard_service.go 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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[int64][]*model.SprinkleStatisticsDataList, map[int64]*model.GroupPasture, error) {
  110. res, pastureList := make(map[int64][]*model.SprinkleStatisticsDataList), make(map[int64]*model.GroupPasture, 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.Id] = response.Data
  144. pastureList[groupPasture.Id] = groupPasture
  145. }(pasture)
  146. }
  147. wg.Wait()
  148. return res, pastureList, nil
  149. }
  150. // SearchAnalysisAccuracy 准确性分析
  151. func (s *StoreEntry) SearchAnalysisAccuracy(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.SearchAnalysisAccuracyResponse1, error) {
  152. res := &model.SearchAnalysisAccuracyResponse1{
  153. Code: http.StatusOK,
  154. Msg: "ok",
  155. Data: &model.AnalysisAccuracyData1{
  156. Table: &model.Table{
  157. TitleList: make([]*model.TableList, 0),
  158. DataList: &model.DataList{},
  159. },
  160. Chart: nil,
  161. },
  162. }
  163. res.Data.Table.TitleList = append(res.Data.Table.TitleList, &model.TableList{
  164. Name: "title",
  165. Value: "牧场",
  166. })
  167. pastureAnalysisAccuracy, err := s.PasturePrefAnalysisData(ctx, req)
  168. if err != nil {
  169. return nil, xerr.WithStack(err)
  170. }
  171. zaplog.Info("SearchAnalysisAccuracy", zap.Any("pastureAnalysisAccuracy", pastureAnalysisAccuracy))
  172. res.Data.Table = s.TitleList(ctx, pastureAnalysisAccuracy)
  173. dashboardTopData, err := s.DashboardTopPasture(ctx, req)
  174. if err != nil {
  175. return nil, xerr.WithStack(err)
  176. }
  177. dashboardTopData1 := &model.Chart1{
  178. MixedFodderAccurateRatio: &model.PastureTopData1{
  179. Title: make([]string, 0),
  180. Ratio: make([]float64, 0),
  181. },
  182. MixedFodderCorrectRatio: &model.PastureTopData1{
  183. Title: make([]string, 0),
  184. Ratio: make([]float64, 0),
  185. },
  186. SprinkleFodderAccurateRatio: &model.PastureTopData1{
  187. Title: make([]string, 0),
  188. Ratio: make([]float64, 0),
  189. },
  190. SprinkleFodderCorrectRatio: &model.PastureTopData1{
  191. Title: make([]string, 0),
  192. Ratio: make([]float64, 0),
  193. },
  194. }
  195. pastureDayTimeRatioList := make(map[operationPb.DashboardTopType_Kind][]*model.PastureDayTimeRatio, 0)
  196. for pastureId, data := range pastureAnalysisAccuracy {
  197. pastureInfo, err := s.GetGroupPastureListById(ctx, pastureId)
  198. if err != nil {
  199. zaplog.Error("SearchAnalysisAccuracy GetGroupPastureListById",
  200. zap.Any("pastureId", pastureId), zap.Any("error", err))
  201. continue
  202. }
  203. if data == nil {
  204. continue
  205. }
  206. for _, v := range data.MixedFodderAccurateRatio {
  207. pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_ACCURATE] =
  208. append(pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_ACCURATE],
  209. &model.PastureDayTimeRatio{
  210. PastureId: pastureId,
  211. PastureName: pastureInfo.Name,
  212. DayTime: v.DayTime,
  213. Ratio: v.Ratio,
  214. })
  215. }
  216. for _, v := range data.MixedFodderCorrectRatio {
  217. pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_CORRECT] =
  218. append(pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_CORRECT],
  219. &model.PastureDayTimeRatio{
  220. PastureId: pastureId,
  221. PastureName: pastureInfo.Name,
  222. DayTime: v.DayTime,
  223. Ratio: v.Ratio,
  224. })
  225. }
  226. for _, v := range data.SprinkleFodderAccurateRatio {
  227. pastureDayTimeRatioList[operationPb.DashboardTopType_SPRINKLE_ACCURATE] =
  228. append(pastureDayTimeRatioList[operationPb.DashboardTopType_SPRINKLE_ACCURATE],
  229. &model.PastureDayTimeRatio{
  230. PastureId: pastureId,
  231. PastureName: pastureInfo.Name,
  232. DayTime: v.DayTime,
  233. Ratio: v.Ratio,
  234. })
  235. }
  236. for _, v := range data.SprinkleFodderCorrectRatio {
  237. pastureDayTimeRatioList[operationPb.DashboardTopType_Sprinkle_CORRECT] =
  238. append(pastureDayTimeRatioList[operationPb.DashboardTopType_Sprinkle_CORRECT],
  239. &model.PastureDayTimeRatio{
  240. PastureId: pastureId,
  241. PastureName: pastureInfo.Name,
  242. DayTime: v.DayTime,
  243. Ratio: v.Ratio,
  244. })
  245. }
  246. }
  247. zaplog.Info("SearchAnalysisAccuracyBefore", zap.Any("pastureDayTimeRatioList", pastureDayTimeRatioList))
  248. pastureTopDataList, err := s.TopPasture(ctx, req)
  249. if err != nil {
  250. zaplog.Error("SearchAnalysisAccuracy", zap.Any("TopPasture", err), zap.Any("request", req))
  251. return nil, xerr.WithStack(err)
  252. }
  253. // 排序 最大值。最小值。中位数
  254. sort.Slice(pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_ACCURATE], func(i, j int) bool {
  255. return pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_ACCURATE][i].Ratio < pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_ACCURATE][j].Ratio
  256. })
  257. sort.Slice(pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_CORRECT], func(i, j int) bool {
  258. return pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_CORRECT][i].Ratio < pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_CORRECT][j].Ratio
  259. })
  260. sort.Slice(pastureDayTimeRatioList[operationPb.DashboardTopType_SPRINKLE_ACCURATE], func(i, j int) bool {
  261. return pastureDayTimeRatioList[operationPb.DashboardTopType_SPRINKLE_ACCURATE][i].Ratio < pastureDayTimeRatioList[operationPb.DashboardTopType_SPRINKLE_ACCURATE][j].Ratio
  262. })
  263. sort.Slice(pastureDayTimeRatioList[operationPb.DashboardTopType_Sprinkle_CORRECT], func(i, j int) bool {
  264. return pastureDayTimeRatioList[operationPb.DashboardTopType_Sprinkle_CORRECT][i].Ratio < pastureDayTimeRatioList[operationPb.DashboardTopType_Sprinkle_CORRECT][j].Ratio
  265. })
  266. zaplog.Info("SearchAnalysisAccuracyAfter", zap.Any("pastureDayTimeRatioList", pastureDayTimeRatioList))
  267. // 混料准确率
  268. dashboardTopData1.MixedFodderAccurateRatio.MaxValue = fmt.Sprintf("%.2f", pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_ACCURATE][len(pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_ACCURATE])-1].Ratio)
  269. dashboardTopData1.MixedFodderAccurateRatio.MinValue = fmt.Sprintf("%.2f", pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_ACCURATE][0].Ratio)
  270. dashboardTopData1.MixedFodderAccurateRatio.MiddleValue = fmt.Sprintf("%.2f", getPastureDayTimeRatioMiddleValue(pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_ACCURATE]))
  271. dashboardTopData1.MixedFodderAccurateRatio.TopOneName = pastureTopDataList.Data.MixedFodderAccurateRatio[0].PastureName
  272. // 混料正确率
  273. dashboardTopData1.MixedFodderCorrectRatio.MaxValue = fmt.Sprintf("%.2f", pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_CORRECT][len(pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_CORRECT])-1].Ratio)
  274. dashboardTopData1.MixedFodderCorrectRatio.MinValue = fmt.Sprintf("%.2f", pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_CORRECT][0].Ratio)
  275. dashboardTopData1.MixedFodderCorrectRatio.MiddleValue = fmt.Sprintf("%.2f", getPastureDayTimeRatioMiddleValue(pastureDayTimeRatioList[operationPb.DashboardTopType_MIXED_CORRECT]))
  276. dashboardTopData1.MixedFodderCorrectRatio.TopOneName = pastureTopDataList.Data.MixedFodderCorrectRatio[0].PastureName
  277. // 散料准确率
  278. dashboardTopData1.SprinkleFodderAccurateRatio.MaxValue = fmt.Sprintf("%.2f", pastureDayTimeRatioList[operationPb.DashboardTopType_SPRINKLE_ACCURATE][len(pastureDayTimeRatioList[operationPb.DashboardTopType_SPRINKLE_ACCURATE])-1].Ratio)
  279. dashboardTopData1.SprinkleFodderAccurateRatio.MinValue = fmt.Sprintf("%.2f", pastureDayTimeRatioList[operationPb.DashboardTopType_SPRINKLE_ACCURATE][0].Ratio)
  280. dashboardTopData1.SprinkleFodderAccurateRatio.MiddleValue = fmt.Sprintf("%.2f", getPastureDayTimeRatioMiddleValue(pastureDayTimeRatioList[operationPb.DashboardTopType_SPRINKLE_ACCURATE]))
  281. dashboardTopData1.SprinkleFodderAccurateRatio.TopOneName = pastureTopDataList.Data.SprinkleFodderAccurateRatio[0].PastureName
  282. // 散料正确率
  283. dashboardTopData1.SprinkleFodderCorrectRatio.MaxValue = fmt.Sprintf("%.2f", pastureDayTimeRatioList[operationPb.DashboardTopType_Sprinkle_CORRECT][len(pastureDayTimeRatioList[operationPb.DashboardTopType_Sprinkle_CORRECT])-1].Ratio)
  284. dashboardTopData1.SprinkleFodderCorrectRatio.MinValue = fmt.Sprintf("%.2f", pastureDayTimeRatioList[operationPb.DashboardTopType_Sprinkle_CORRECT][0].Ratio)
  285. dashboardTopData1.SprinkleFodderCorrectRatio.MiddleValue = fmt.Sprintf("%.2f", getPastureDayTimeRatioMiddleValue(pastureDayTimeRatioList[operationPb.DashboardTopType_Sprinkle_CORRECT]))
  286. dashboardTopData1.SprinkleFodderCorrectRatio.TopOneName = pastureTopDataList.Data.SprinkleFodderCorrectRatio[0].PastureName
  287. zaplog.Info("SearchAnalysisAccuracyAfter", zap.Any("dashboardTopData", dashboardTopData))
  288. for _, v := range dashboardTopData.MixedFodderCorrectRatio {
  289. dashboardTopData1.MixedFodderCorrectRatio.Title = append(dashboardTopData1.MixedFodderCorrectRatio.Title, v.PastureName)
  290. dashboardTopData1.MixedFodderCorrectRatio.Ratio = append(dashboardTopData1.MixedFodderCorrectRatio.Ratio, v.Ratio)
  291. }
  292. for _, v := range dashboardTopData.MixedFodderAccurateRatio {
  293. dashboardTopData1.MixedFodderAccurateRatio.Title = append(dashboardTopData1.MixedFodderAccurateRatio.Title, v.PastureName)
  294. dashboardTopData1.MixedFodderAccurateRatio.Ratio = append(dashboardTopData1.MixedFodderAccurateRatio.Ratio, v.Ratio)
  295. }
  296. for _, v := range dashboardTopData.SprinkleFodderAccurateRatio {
  297. dashboardTopData1.SprinkleFodderAccurateRatio.Title = append(dashboardTopData1.SprinkleFodderAccurateRatio.Title, v.PastureName)
  298. dashboardTopData1.SprinkleFodderAccurateRatio.Ratio = append(dashboardTopData1.SprinkleFodderAccurateRatio.Ratio, v.Ratio)
  299. }
  300. for _, v := range dashboardTopData.SprinkleFodderCorrectRatio {
  301. dashboardTopData1.SprinkleFodderCorrectRatio.Title = append(dashboardTopData1.SprinkleFodderCorrectRatio.Title, v.PastureName)
  302. dashboardTopData1.SprinkleFodderCorrectRatio.Ratio = append(dashboardTopData1.SprinkleFodderCorrectRatio.Ratio, v.Ratio)
  303. }
  304. res.Data.Chart = dashboardTopData1
  305. return res, nil
  306. }
  307. func (s *StoreEntry) SearchAnalysisAccuracyOld(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.SearchAnalysisAccuracyResponse, error) {
  308. res := &model.SearchAnalysisAccuracyResponse{
  309. Code: http.StatusOK,
  310. Msg: "ok",
  311. Data: &model.AnalysisAccuracyData{
  312. Chart: &model.Chart{},
  313. Table: &model.Table{
  314. TitleList: make([]*model.TableList, 0),
  315. DataList: &model.DataList{},
  316. },
  317. },
  318. }
  319. res.Data.Table.TitleList = append(res.Data.Table.TitleList, &model.TableList{
  320. Name: "title",
  321. Value: "牧场",
  322. })
  323. pastureAnalysisAccuracy, err := s.PasturePrefAnalysisData(ctx, req)
  324. if err != nil {
  325. return nil, xerr.WithStack(err)
  326. }
  327. mixedFodderAccurateRatio, mixedFodderCorrectRatio, sprinkleFodderAccurateRatio, sprinkleFodderCorrectRatio :=
  328. &model.CommonValueRatio{}, &model.CommonValueRatio{}, &model.CommonValueRatio{}, &model.CommonValueRatio{}
  329. maTitleValueList, mcTitleValueList, saTitleValueList, scTitleValueList := make([]float64, 0), make([]float64, 0), make([]float64, 0), make([]float64, 0)
  330. dayTimeList := tool.TimeBetween(req.StartDate, req.EndDate)
  331. for pastureId, data := range pastureAnalysisAccuracy {
  332. groupPasture, err := s.GetGroupPastureListById(ctx, pastureId)
  333. if err != nil {
  334. zaplog.Error("SearchAnalysisAccuracy GetGroupPastureListById",
  335. zap.Any("pastureId", pastureId), zap.Any("error", err))
  336. continue
  337. }
  338. if data == nil {
  339. continue
  340. }
  341. mixedFodderAccurateRatioDataList := make([]string, 0)
  342. for _, v := range data.MixedFodderAccurateRatio {
  343. mixedFodderAccurateRatioDataList = append(mixedFodderAccurateRatioDataList, fmt.Sprintf("%.2f", v.Ratio))
  344. if len(mixedFodderAccurateRatio.DateDay) < len(dayTimeList) {
  345. mixedFodderAccurateRatio.DateDay = append(mixedFodderAccurateRatio.DateDay, v.DayTime)
  346. }
  347. maTitleValueList = append(maTitleValueList, v.Ratio)
  348. }
  349. mixedFodderAccurateRatio.DataList = append(mixedFodderAccurateRatio.DataList, mixedFodderAccurateRatioDataList)
  350. mixedFodderAccurateRatio.PastureIds = append(mixedFodderAccurateRatio.PastureIds, int32(pastureId))
  351. mixedFodderAccurateRatio.PastureName = append(mixedFodderAccurateRatio.PastureName, groupPasture.Name)
  352. mixedFodderCorrectRatioDataList := make([]string, 0)
  353. for _, v := range data.MixedFodderCorrectRatio {
  354. mixedFodderCorrectRatioDataList = append(mixedFodderCorrectRatioDataList, fmt.Sprintf("%.2f", v.Ratio))
  355. if len(mixedFodderCorrectRatio.DateDay) < len(dayTimeList) {
  356. mixedFodderCorrectRatio.DateDay = append(mixedFodderCorrectRatio.DateDay, v.DayTime)
  357. }
  358. mcTitleValueList = append(mcTitleValueList, v.Ratio)
  359. }
  360. mixedFodderCorrectRatio.DataList = append(mixedFodderCorrectRatio.DataList, mixedFodderCorrectRatioDataList)
  361. mixedFodderCorrectRatio.PastureIds = append(mixedFodderCorrectRatio.PastureIds, int32(pastureId))
  362. mixedFodderCorrectRatio.PastureName = append(mixedFodderCorrectRatio.PastureName, groupPasture.Name)
  363. sprinkleFodderRatioDataList := make([]string, 0)
  364. for _, v := range data.SprinkleFodderAccurateRatio {
  365. sprinkleFodderRatioDataList = append(sprinkleFodderRatioDataList, fmt.Sprintf("%.2f", v.Ratio))
  366. if len(sprinkleFodderAccurateRatio.DateDay) < len(dayTimeList) {
  367. sprinkleFodderAccurateRatio.DateDay = append(sprinkleFodderAccurateRatio.DateDay, v.DayTime)
  368. }
  369. saTitleValueList = append(saTitleValueList, v.Ratio)
  370. }
  371. sprinkleFodderAccurateRatio.DataList = append(sprinkleFodderAccurateRatio.DataList, sprinkleFodderRatioDataList)
  372. sprinkleFodderAccurateRatio.PastureIds = append(sprinkleFodderAccurateRatio.PastureIds, int32(pastureId))
  373. sprinkleFodderAccurateRatio.PastureName = append(sprinkleFodderAccurateRatio.PastureName, groupPasture.Name)
  374. sprinkleFodderCorrectRatioDataList := make([]string, 0)
  375. for _, v := range data.SprinkleFodderCorrectRatio {
  376. sprinkleFodderCorrectRatioDataList = append(sprinkleFodderCorrectRatioDataList, fmt.Sprintf("%.2f", v.Ratio))
  377. if len(sprinkleFodderCorrectRatio.DateDay) < len(dayTimeList) {
  378. sprinkleFodderCorrectRatio.DateDay = append(sprinkleFodderCorrectRatio.DateDay, v.DayTime)
  379. }
  380. scTitleValueList = append(scTitleValueList, v.Ratio)
  381. }
  382. sprinkleFodderCorrectRatio.DataList = append(sprinkleFodderCorrectRatio.DataList, sprinkleFodderCorrectRatioDataList)
  383. sprinkleFodderCorrectRatio.PastureIds = append(sprinkleFodderCorrectRatio.PastureIds, int32(pastureId))
  384. sprinkleFodderCorrectRatio.PastureName = append(sprinkleFodderCorrectRatio.PastureName, groupPasture.Name)
  385. }
  386. pastureTopDataList, err := s.TopPasture(ctx, req)
  387. if err != nil {
  388. zaplog.Error("SearchAnalysisAccuracy", zap.Any("TopPasture", err), zap.Any("request", req))
  389. return nil, xerr.WithStack(err)
  390. }
  391. sort.Float64s(maTitleValueList)
  392. mixedFodderAccurateRatio.MaxValue = fmt.Sprintf("%.2f", maTitleValueList[len(maTitleValueList)-1])
  393. mixedFodderAccurateRatio.MinValue = fmt.Sprintf("%.2f", maTitleValueList[0])
  394. mixedFodderAccurateRatio.MiddleValue = fmt.Sprintf("%.2f", tool.Median(maTitleValueList))
  395. mixedFodderAccurateRatio.TopOneName = pastureTopDataList.Data.MixedFodderAccurateRatio[0].PastureName
  396. sort.Float64s(mcTitleValueList)
  397. mixedFodderCorrectRatio.MaxValue = fmt.Sprintf("%.2f", mcTitleValueList[len(mcTitleValueList)-1])
  398. mixedFodderCorrectRatio.MinValue = fmt.Sprintf("%.2f", mcTitleValueList[0])
  399. mixedFodderCorrectRatio.MiddleValue = fmt.Sprintf("%.2f", tool.Median(mcTitleValueList))
  400. mixedFodderCorrectRatio.TopOneName = pastureTopDataList.Data.MixedFodderCorrectRatio[0].PastureName
  401. sort.Float64s(saTitleValueList)
  402. sprinkleFodderAccurateRatio.MaxValue = fmt.Sprintf("%.2f", saTitleValueList[len(saTitleValueList)-1])
  403. sprinkleFodderAccurateRatio.MinValue = fmt.Sprintf("%.2f", saTitleValueList[0])
  404. sprinkleFodderAccurateRatio.MiddleValue = fmt.Sprintf("%.2f", tool.Median(saTitleValueList))
  405. sprinkleFodderAccurateRatio.TopOneName = pastureTopDataList.Data.SprinkleFodderAccurateRatio[0].PastureName
  406. sort.Float64s(scTitleValueList)
  407. sprinkleFodderCorrectRatio.MaxValue = fmt.Sprintf("%.2f", scTitleValueList[len(scTitleValueList)-1])
  408. sprinkleFodderCorrectRatio.MinValue = fmt.Sprintf("%.2f", scTitleValueList[0])
  409. sprinkleFodderCorrectRatio.MiddleValue = fmt.Sprintf("%.2f", tool.Median(scTitleValueList))
  410. sprinkleFodderCorrectRatio.TopOneName = pastureTopDataList.Data.SprinkleFodderCorrectRatio[0].PastureName
  411. chart := &model.Chart{
  412. MixedFodderAccurateRatio: mixedFodderAccurateRatio,
  413. MixedFodderCorrectRatio: mixedFodderCorrectRatio,
  414. SprinkleFodderAccurateRatio: sprinkleFodderAccurateRatio,
  415. SprinkleFodderCorrectRatio: sprinkleFodderCorrectRatio,
  416. }
  417. res.Data.Chart = chart
  418. res.Data.Table = s.TitleList(ctx, pastureAnalysisAccuracy)
  419. return res, nil
  420. }
  421. // TopPasture 牧场排名
  422. func (s *StoreEntry) TopPasture(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.GetPastureTopResponse, error) {
  423. res := &model.GetPastureTopResponse{
  424. Code: http.StatusOK,
  425. Msg: "ok",
  426. Data: nil,
  427. }
  428. dashboardTopData, err := s.DashboardTopPasture(ctx, req)
  429. if err != nil {
  430. return nil, xerr.WithStack(err)
  431. }
  432. res.Data = dashboardTopData
  433. return res, nil
  434. }
  435. func (s *StoreEntry) DashboardTopPasture(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.PastureTop, error) {
  436. dashboardTopData := &model.PastureTop{
  437. MixedFodderAccurateRatio: make([]*model.PastureTopData, 0),
  438. MixedFodderCorrectRatio: make([]*model.PastureTopData, 0),
  439. SprinkleFodderAccurateRatio: make([]*model.PastureTopData, 0),
  440. SprinkleFodderCorrectRatio: make([]*model.PastureTopData, 0),
  441. }
  442. analysisAccuracy, err := s.PasturePrefAnalysisData(ctx, req)
  443. if err != nil {
  444. return nil, xerr.WithStack(err)
  445. }
  446. for pastureId, data := range analysisAccuracy {
  447. groupPasture, err := s.GetGroupPastureListById(ctx, pastureId)
  448. if err != nil {
  449. zaplog.Error("TopPasture", zap.Any("GetGroupPastureListById", pastureId), zap.Any("err", err))
  450. continue
  451. }
  452. if data == nil {
  453. continue
  454. }
  455. allMaRatio, allMcRatio, allSaRatio, allScRatio := 0.0, 0.0, 0.0, 0.0
  456. for _, v := range data.MixedFodderAccurateRatio {
  457. allMaRatio += v.Ratio
  458. }
  459. dashboardTopData.MixedFodderAccurateRatio = append(dashboardTopData.MixedFodderAccurateRatio, &model.PastureTopData{
  460. PastureName: groupPasture.Name,
  461. Ratio: tool.Decimal(allMaRatio / float64(len(data.MixedFodderAccurateRatio))),
  462. })
  463. for _, v := range data.MixedFodderCorrectRatio {
  464. allMcRatio += v.Ratio
  465. }
  466. dashboardTopData.MixedFodderCorrectRatio = append(dashboardTopData.MixedFodderCorrectRatio, &model.PastureTopData{
  467. PastureName: groupPasture.Name,
  468. Ratio: tool.Decimal(allMaRatio / float64(len(data.MixedFodderCorrectRatio))),
  469. })
  470. for _, v := range data.SprinkleFodderAccurateRatio {
  471. allSaRatio += v.Ratio
  472. }
  473. dashboardTopData.SprinkleFodderAccurateRatio = append(dashboardTopData.SprinkleFodderAccurateRatio, &model.PastureTopData{
  474. PastureName: groupPasture.Name,
  475. Ratio: tool.Decimal(allSaRatio / float64(len(data.SprinkleFodderAccurateRatio))),
  476. })
  477. for _, v := range data.SprinkleFodderCorrectRatio {
  478. allScRatio += v.Ratio
  479. }
  480. dashboardTopData.SprinkleFodderCorrectRatio = append(dashboardTopData.SprinkleFodderCorrectRatio, &model.PastureTopData{
  481. PastureName: groupPasture.Name,
  482. Ratio: tool.Decimal(allScRatio / float64(len(data.SprinkleFodderCorrectRatio))),
  483. })
  484. }
  485. sort.Slice(dashboardTopData.MixedFodderAccurateRatio, func(i, j int) bool {
  486. return dashboardTopData.MixedFodderAccurateRatio[i].Ratio > dashboardTopData.MixedFodderAccurateRatio[j].Ratio
  487. })
  488. sort.Slice(dashboardTopData.MixedFodderCorrectRatio, func(i, j int) bool {
  489. return dashboardTopData.MixedFodderCorrectRatio[i].Ratio > dashboardTopData.MixedFodderCorrectRatio[j].Ratio
  490. })
  491. sort.Slice(dashboardTopData.SprinkleFodderAccurateRatio, func(i, j int) bool {
  492. return dashboardTopData.SprinkleFodderAccurateRatio[i].Ratio > dashboardTopData.SprinkleFodderAccurateRatio[j].Ratio
  493. })
  494. sort.Slice(dashboardTopData.SprinkleFodderCorrectRatio, func(i, j int) bool {
  495. return dashboardTopData.SprinkleFodderCorrectRatio[i].Ratio > dashboardTopData.SprinkleFodderCorrectRatio[j].Ratio
  496. })
  497. if req.DashboardTopType > 0 {
  498. switch req.DashboardTopType {
  499. case operationPb.DashboardTopType_MIXED_ACCURATE:
  500. dashboardTopData.MixedFodderAccurateRatio = dashboardTopRand(req, dashboardTopData.MixedFodderAccurateRatio)
  501. case operationPb.DashboardTopType_MIXED_CORRECT:
  502. dashboardTopData.MixedFodderCorrectRatio = dashboardTopRand(req, dashboardTopData.MixedFodderCorrectRatio)
  503. case operationPb.DashboardTopType_SPRINKLE_ACCURATE:
  504. dashboardTopData.SprinkleFodderAccurateRatio = dashboardTopRand(req, dashboardTopData.SprinkleFodderAccurateRatio)
  505. case operationPb.DashboardTopType_Sprinkle_CORRECT:
  506. dashboardTopData.SprinkleFodderCorrectRatio = dashboardTopRand(req, dashboardTopData.SprinkleFodderCorrectRatio)
  507. }
  508. }
  509. return dashboardTopData, nil
  510. }
  511. func (s *StoreEntry) TitleList(ctx context.Context, pastureAnalysisList map[int64]*model.PastureAnalysisAccuracyData) *model.Table {
  512. res := &model.Table{
  513. TitleList: make([]*model.TableList, 0),
  514. DataList: &model.DataList{
  515. MixedFodderAccurateRatio: make([]map[string]string, 0),
  516. MixedFodderCorrectRatio: make([]map[string]string, 0),
  517. SprinkleFodderAccurateRatio: make([]map[string]string, 0),
  518. SprinkleFodderCorrectRatio: make([]map[string]string, 0),
  519. },
  520. }
  521. for pastureId, data := range pastureAnalysisList {
  522. groupPasture, err := s.GetGroupPastureListById(ctx, pastureId)
  523. if err != nil {
  524. zaplog.Info("TitleList", zap.Any("GetGroupPastureListById", pastureId), zap.Any("err", err))
  525. continue
  526. }
  527. if len(res.TitleList) <= len(data.MixedFodderAccurateRatio) {
  528. res.TitleList = append(res.TitleList, &model.TableList{
  529. Name: "title",
  530. Value: "牧场",
  531. })
  532. }
  533. maMap := map[string]string{
  534. "title": groupPasture.Name,
  535. }
  536. for i, v := range data.MixedFodderCorrectRatio {
  537. maMap[fmt.Sprintf("data%d", i+1)] = fmt.Sprintf("%.2f", v.Ratio)
  538. if len(res.TitleList) <= len(data.MixedFodderAccurateRatio) {
  539. res.TitleList = append(res.TitleList, &model.TableList{
  540. Name: fmt.Sprintf("data%d", i+1),
  541. Value: v.DayTime,
  542. })
  543. }
  544. }
  545. res.DataList.MixedFodderAccurateRatio = append(res.DataList.MixedFodderAccurateRatio, maMap)
  546. mcMap := map[string]string{
  547. "title": groupPasture.Name,
  548. }
  549. for i, v := range data.MixedFodderCorrectRatio {
  550. mcMap[fmt.Sprintf("data%d", i+1)] = fmt.Sprintf("%.2f", v.Ratio)
  551. }
  552. res.DataList.MixedFodderCorrectRatio = append(res.DataList.MixedFodderCorrectRatio, mcMap)
  553. saMap := map[string]string{
  554. "title": groupPasture.Name,
  555. }
  556. for i, v := range data.SprinkleFodderAccurateRatio {
  557. saMap[fmt.Sprintf("data%d", i+1)] = fmt.Sprintf("%.2f", v.Ratio)
  558. }
  559. res.DataList.SprinkleFodderAccurateRatio = append(res.DataList.SprinkleFodderAccurateRatio, saMap)
  560. scMap := map[string]string{
  561. "title": groupPasture.Name,
  562. }
  563. for i, v := range data.SprinkleFodderCorrectRatio {
  564. scMap[fmt.Sprintf("data%d", i+1)] = fmt.Sprintf("%.2f", v.Ratio)
  565. }
  566. res.DataList.SprinkleFodderCorrectRatio = append(res.DataList.SprinkleFodderCorrectRatio, scMap)
  567. }
  568. return res
  569. }
  570. func (s *StoreEntry) ExecutionTime(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.ExecTimeResponse, error) {
  571. res := &model.ExecTimeResponse{
  572. Code: http.StatusOK,
  573. Msg: "ok",
  574. Data: &model.ExecTimeDataList{
  575. Chart: &model.ExecTimeDataListChart{
  576. Title: make([]string, 0),
  577. AddFeedTime: make([][]string, 0),
  578. SprinkleTime: make([][]string, 0),
  579. StirTime: make([][]string, 0),
  580. },
  581. TableList: make([]map[string]string, 0),
  582. },
  583. }
  584. pastureExecTime, err := s.PasturePrefExecTimeData(ctx, req)
  585. if err != nil {
  586. return nil, xerr.WithStack(err)
  587. }
  588. for pastureName, execTime := range pastureExecTime {
  589. res.Data.Chart.Title = append(res.Data.Chart.Title, pastureName)
  590. addFeedTimeStr, sprinkleTimeStr, stirTimeStr := make([]string, 0), make([]string, 0), make([]string, 0)
  591. if execTime != nil {
  592. addFeedTimeStr = append(addFeedTimeStr, execTime.AddFeedTime.MaxValue, execTime.AddFeedTime.UpMiddleValue,
  593. execTime.AddFeedTime.MiddleValue, execTime.AddFeedTime.DownMiddleValue, execTime.AddFeedTime.MinValue)
  594. sprinkleTimeStr = append(sprinkleTimeStr, execTime.SprinkleTime.MaxValue, execTime.SprinkleTime.UpMiddleValue,
  595. execTime.SprinkleTime.MiddleValue, execTime.SprinkleTime.DownMiddleValue, execTime.SprinkleTime.MinValue)
  596. stirTimeStr = append(stirTimeStr, execTime.StirTime.MaxValue, execTime.StirTime.UpMiddleValue,
  597. execTime.StirTime.MiddleValue, execTime.StirTime.DownMiddleValue, execTime.StirTime.MinValue)
  598. }
  599. res.Data.Chart.AddFeedTime = append(res.Data.Chart.AddFeedTime, addFeedTimeStr)
  600. res.Data.Chart.SprinkleTime = append(res.Data.Chart.SprinkleTime, sprinkleTimeStr)
  601. res.Data.Chart.StirTime = append(res.Data.Chart.StirTime, stirTimeStr)
  602. if execTime == nil {
  603. continue
  604. }
  605. tableList := map[string]string{
  606. "title": pastureName,
  607. "add_feed_time": "加料时间",
  608. "add_feed_time_max_value": execTime.AddFeedTime.MaxValue,
  609. "add_feed_time_up_middle_value": execTime.AddFeedTime.UpMiddleValue,
  610. "add_feed_time_middle_value": execTime.AddFeedTime.MiddleValue,
  611. "add_feed_time_down_middle_value": execTime.AddFeedTime.DownMiddleValue,
  612. "add_feed_time_min_value": execTime.AddFeedTime.MinValue,
  613. "sprinkle_time": "撒料时间",
  614. "sprinkle_time_max_value": execTime.SprinkleTime.MaxValue,
  615. "sprinkle_time_up_middle_value": execTime.SprinkleTime.UpMiddleValue,
  616. "sprinkle_time_middle_value": execTime.SprinkleTime.MiddleValue,
  617. "sprinkle_time_down_middle_value": execTime.SprinkleTime.DownMiddleValue,
  618. "sprinkle_time_min_value": execTime.SprinkleTime.MinValue,
  619. "stir_time": "搅拌延迟时间",
  620. "stir_time_max_value": execTime.StirTime.MaxValue,
  621. "stir_time_up_middle_value": execTime.StirTime.UpMiddleValue,
  622. "stir_time_middle_value": execTime.StirTime.MiddleValue,
  623. "stir_time_down_middle_value": execTime.StirTime.DownMiddleValue,
  624. "stir_time_min_value": execTime.StirTime.MinValue,
  625. }
  626. res.Data.TableList = append(res.Data.TableList, tableList)
  627. }
  628. return res, nil
  629. }
  630. func (s *StoreEntry) SprinkleFeedTime(ctx context.Context, req *operationPb.SprinkleFeedTimeRequest) (*model.SprinkleFeedTimeResponse, error) {
  631. res := &model.SprinkleFeedTimeResponse{
  632. Code: http.StatusOK,
  633. Msg: "ok",
  634. Data: &model.SprinkleFeedTimeData{
  635. Chart: &model.SprinkleFeedTimeChart{
  636. Title: make([]string, 0),
  637. SprinkleNumberList: make([][]int32, 0),
  638. },
  639. TableList: make([]*model.SprinkleFeedTimeTable, 0),
  640. },
  641. }
  642. pastureSprinkleDataList, pastureList, err := s.PastureSprinkleFeedTime(ctx, req)
  643. if err != nil {
  644. return nil, xerr.WithStack(err)
  645. }
  646. tableList := make([]*model.SprinkleFeedTimeTable, 0)
  647. infoSprinkleNumber, errorSprinkleNumber, pastureIds := make([]int32, 0), make([]int32, 0), make([]int, 0)
  648. for pastureId, _ := range pastureSprinkleDataList {
  649. pastureIds = append(pastureIds, int(pastureId))
  650. }
  651. sort.Ints(pastureIds)
  652. pastureInfo := &model.GroupPasture{}
  653. for _, pastureId := range pastureIds {
  654. if pastureData, ok := pastureList[int64(pastureId)]; ok {
  655. pastureInfo = pastureData
  656. }
  657. if data, ok := pastureSprinkleDataList[int64(pastureId)]; ok {
  658. sprinkleFeedTimeList := make(map[int32]map[int32][]int64, 0)
  659. for _, v := range data {
  660. tableList = append(tableList, &model.SprinkleFeedTimeTable{
  661. PastureName: pastureInfo.Name,
  662. BarnName: v.FName,
  663. ClassNumber: fmt.Sprintf("%d", v.Times),
  664. RealitySprinkleFeedTime: tool.TimeSub(v.InTime, v.ProcessTime),
  665. })
  666. realityTime := tool.TimeSub(v.InTime, v.ProcessTime)
  667. realityTimeUnix, _ := time.Parse(model.LayoutTime, realityTime)
  668. if sprinkleFeedTimeList[v.FBarId] == nil {
  669. sprinkleFeedTimeList[v.FBarId] = make(map[int32][]int64, 0)
  670. }
  671. sprinkleFeedTimeList[v.FBarId][v.Times] = append(sprinkleFeedTimeList[v.FBarId][v.Times], realityTimeUnix.Unix())
  672. }
  673. res.Data.Chart.Title = append(res.Data.Chart.Title, pastureInfo.Name)
  674. infoNumber, errNumber := sprinkleExecTimeAnalysis(sprinkleFeedTimeList)
  675. infoSprinkleNumber = append(infoSprinkleNumber, infoNumber)
  676. errorSprinkleNumber = append(errorSprinkleNumber, errNumber)
  677. }
  678. }
  679. res.Data.Chart.SprinkleNumberList = append(res.Data.Chart.SprinkleNumberList, infoSprinkleNumber, errorSprinkleNumber)
  680. res.Data.TableList = tableList
  681. return res, nil
  682. }
  683. func (s *StoreEntry) FeedMixedAndTmrName(ctx context.Context, req *operationPb.MixedCategoryTmrName) (*model.PastureCommonResponse, error) {
  684. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  685. if err != nil {
  686. return nil, xerr.WithStack(err)
  687. }
  688. pastureId := req.PastureId
  689. if groupPasture.PastureId > 0 {
  690. pastureId = int32(groupPasture.PastureId)
  691. }
  692. body := &model.PastureCommonRequest{
  693. Name: req.ApiName,
  694. ReturnType: "Map",
  695. ParamMaps: &model.MixedCategoryTmrNameParams{
  696. PastureId: fmt.Sprintf("%d", pastureId),
  697. StartTime: req.StartTime,
  698. EndTime: req.EndTime,
  699. },
  700. }
  701. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  702. if err = s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  703. return nil, xerr.WithStack(err)
  704. }
  705. return response, nil
  706. }
  707. func (s *StoreEntry) FeedTemplateHistory(ctx context.Context, req *operationPb.FeedTemplateHistoryRequest) (*model.PastureFeedTemplateHistoryResponse, error) {
  708. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  709. if err != nil {
  710. return nil, xerr.WithStack(err)
  711. }
  712. pastureId := req.PastureId
  713. if groupPasture.PastureId > 0 {
  714. pastureId = int32(groupPasture.PastureId)
  715. }
  716. body := &model.PastureFeedTemplateHistoryRequest{
  717. PastureId: fmt.Sprintf("%d", pastureId),
  718. FTid: int64(req.Ftid),
  719. StartDate: req.StartTime,
  720. EndDate: req.EndTime,
  721. }
  722. response := &model.PastureFeedTemplateHistoryResponse{}
  723. if err = s.PastureHttpClient(ctx, model.UrlFeedTemplateHistory, int64(req.PastureId), body, response); err != nil {
  724. return nil, xerr.WithStack(err)
  725. }
  726. return response, nil
  727. }
  728. func (s *StoreEntry) BarnHistory(ctx context.Context, req *operationPb.BarnHistoryRequest) (*model.PastureBarnHistoryResponse, error) {
  729. _, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  730. if err != nil {
  731. return nil, xerr.WithStack(err)
  732. }
  733. body := &model.PastureBarnHistoryRequest{
  734. BarName: req.BarnName,
  735. StartDate: req.StartTime,
  736. EndDate: req.EndTime,
  737. }
  738. response := &model.PastureBarnHistoryResponse{}
  739. if err = s.PastureHttpClient(ctx, model.UrlBarnHistory, int64(req.PastureId), body, response); err != nil {
  740. return nil, xerr.WithStack(err)
  741. }
  742. return response, nil
  743. }
  744. func (s *StoreEntry) SpillageAllHistory(ctx context.Context, req *operationPb.BarnHistoryRequest) (*model.PastureBarnHistoryResponse, error) {
  745. groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  746. if err != nil {
  747. return nil, xerr.WithStack(err)
  748. }
  749. pastureId := req.PastureId
  750. if groupPasture.PastureId > 0 {
  751. pastureId = int32(groupPasture.PastureId)
  752. }
  753. body := &model.PastureBarnHistoryRequest{
  754. PastureId: fmt.Sprintf("%d", pastureId),
  755. BarName: req.BarnName,
  756. StartDate: req.StartTime,
  757. EndDate: req.EndTime,
  758. }
  759. response := &model.PastureBarnHistoryResponse{}
  760. if err = s.PastureHttpClient(ctx, model.UrlSpillageAllHistory, int64(req.PastureId), body, response); err != nil {
  761. return nil, xerr.WithStack(err)
  762. }
  763. return response, nil
  764. }
  765. func sprinkleExecTimeAnalysis(sprinkleFeedTimeList map[int32]map[int32][]int64) (int32, int32) {
  766. var infoSprinkleNumber, errorSprinkleNumber int32 = 0, 0
  767. if len(sprinkleFeedTimeList) <= 0 {
  768. return infoSprinkleNumber, errorSprinkleNumber
  769. } else {
  770. for _, value := range sprinkleFeedTimeList {
  771. for _, execTimeList := range value {
  772. middleValue := tool.MedianInt64(execTimeList)
  773. for _, v := range execTimeList {
  774. if v >= middleValue-int64(compareTime) && v <= middleValue+int64(compareTime) {
  775. infoSprinkleNumber += 1
  776. } else {
  777. errorSprinkleNumber += 1
  778. }
  779. }
  780. }
  781. }
  782. }
  783. return infoSprinkleNumber, errorSprinkleNumber
  784. }
  785. func dashboardTopRand(req *operationPb.SearchAnalysisAccuracyRequest, data []*model.PastureTopData) []*model.PastureTopData {
  786. if req.TopRandStart < 0 || req.TopRandEnd < 0 || req.TopRandStart > req.TopRandEnd {
  787. return data
  788. }
  789. res := make([]*model.PastureTopData, 0)
  790. for _, v := range data {
  791. if v.Ratio >= float64(req.TopRandStart) && v.Ratio <= float64(req.TopRandEnd) {
  792. res = append(res, v)
  793. }
  794. }
  795. return res
  796. }
  797. func getPastureDayTimeRatioMiddleValue(pastureDayTimeRatio []*model.PastureDayTimeRatio) float64 {
  798. if len(pastureDayTimeRatio) <= 0 {
  799. return 0
  800. }
  801. ratioList := make([]float64, 0)
  802. for _, v := range pastureDayTimeRatio {
  803. ratioList = append(ratioList, v.Ratio)
  804. }
  805. sort.Float64s(ratioList)
  806. return tool.Median(ratioList)
  807. }