dashboard_service.go 34 KB

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