dashboard_service.go 37 KB

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