dashboard_service.go 22 KB

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