dashboard_service.go 37 KB

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