dashboard_service.go 22 KB

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