group.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package group
  2. import (
  3. "fmt"
  4. "sort"
  5. "tmr-watch/models"
  6. "tmr-watch/module"
  7. "tmr-watch/pkg/logger/zaplog"
  8. "tmr-watch/pkg/util"
  9. operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
  10. "go.uber.org/zap"
  11. )
  12. // DistributeFeedFormulaService 饲料配方下发牧场端
  13. func DistributeFeedFormulaService(req *operationPb.DistributeDataRequest) error {
  14. zaplog.Info("DistributeFeedFormulaService", zap.Any("request", req))
  15. feedTemplateList := make([]*models.FeedTemplate, 0)
  16. for _, b := range req.FeedFormulaList {
  17. feedTemplateList = append(feedTemplateList, models.NewFeedTemplateByGroup(int64(req.PastureId), b))
  18. }
  19. return module.DistributeFeedFormula(feedTemplateList)
  20. }
  21. // CancelDistributeFeedFormulaService 取消饲料配方下发牧场端
  22. func CancelDistributeFeedFormulaService(req *models.CancelDistributeFeedFormulaRequest) error {
  23. if len(req.PastureDataId) <= 0 {
  24. return nil
  25. }
  26. return module.CancelDistributeFeedFormula(req.PastureId, req.PastureDataId)
  27. }
  28. func FeedFormulaIsModifyService(req *models.PastureFeedFormulaIsModifyRequest) error {
  29. return module.FeedFormulaIsModify(req)
  30. }
  31. // FeedFormulaList 配方列表数据
  32. func FeedFormulaList(req *models.FeedListRequest) (*models.FeedFormulaData, error) {
  33. res := &models.FeedFormulaData{
  34. Total: 0,
  35. Page: req.Page,
  36. PageSize: req.PageSize,
  37. List: make([]*models.FeedTemplate, 0),
  38. }
  39. dataList, total, err := module.FeedTemplateList(req)
  40. if err != nil {
  41. zaplog.Error("FeedFormulaList", zap.Any("dataList", dataList), zap.Any("err", err))
  42. return nil, err
  43. }
  44. res.Total = int32(total)
  45. res.List = dataList
  46. return res, nil
  47. }
  48. // FeedList 饲料列表数据
  49. func FeedList(req *models.FeedListRequest) (*models.FeedData, error) {
  50. res := &models.FeedData{
  51. Total: 0,
  52. Page: req.Page,
  53. PageSize: req.PageSize,
  54. List: make([]*models.Feed, 0),
  55. }
  56. dataList, total, err := module.FeedList(req)
  57. if err != nil {
  58. zaplog.Error("FeedList", zap.Any("dataList", dataList), zap.Any("err", err))
  59. return nil, err
  60. }
  61. res.Total = int32(total)
  62. res.List = dataList
  63. return res, nil
  64. }
  65. // FeedTemplateDetailList 饲料配方详情列表数据
  66. func FeedTemplateDetailList(req *models.FeedListRequest) (*models.FeedDetailData, error) {
  67. res := &models.FeedDetailData{
  68. Total: 0,
  69. Page: req.Page,
  70. PageSize: req.PageSize,
  71. List: make([]*models.FeedTemplateDetail, 0),
  72. }
  73. dataList, total, err := module.FeedTemplateDetailList(req)
  74. if err != nil {
  75. zaplog.Error("FeedTemplateDetailList", zap.Any("dataList", dataList), zap.Any("err", err))
  76. return nil, err
  77. }
  78. res.Total = int32(total)
  79. res.List = dataList
  80. return res, nil
  81. }
  82. // AnalysisAccuracyService 首页准确率分析
  83. func AnalysisAccuracyService(req *models.AnalysisAccuracyRequest) (*models.AnalysisAccuracyResponse, error) {
  84. response := &models.AnalysisAccuracyResponse{
  85. MixedFodderAccurateRatio: make([]*models.PastureAnalysisAccuracyDataValue, 0),
  86. MixedFodderCorrectRatio: make([]*models.PastureAnalysisAccuracyDataValue, 0),
  87. SprinkleFodderAccurateRatio: make([]*models.PastureAnalysisAccuracyDataValue, 0),
  88. SprinkleFodderCorrectRatio: make([]*models.PastureAnalysisAccuracyDataValue, 0),
  89. }
  90. // 混料准确率和正确率
  91. mixedFodderDataList, err := module.MixedFodderData(req)
  92. if err != nil {
  93. return nil, err
  94. }
  95. // 撒料准确率和撒料正确率
  96. sprinkleFodderDataList, err := module.SprinkleFodderData(req)
  97. if err != nil {
  98. return nil, err
  99. }
  100. timeList := util.TimeBetween(req.StartDate, req.EndDate)
  101. for _, dayTime := range timeList {
  102. var mixedInfo, sprinkleInfo bool
  103. for _, mixed := range mixedFodderDataList {
  104. myDate := mixed.PlanTime.Format(util.LayoutDateFormat)
  105. if myDate != dayTime {
  106. continue
  107. }
  108. // 混料准确率
  109. mixedAccurateValue := &models.PastureAnalysisAccuracyDataValue{
  110. DayTime: dayTime,
  111. Ratio: mixed.MixedFodderAccurateRatio,
  112. }
  113. // 混料正确率
  114. mixedCorrectValue := &models.PastureAnalysisAccuracyDataValue{
  115. DayTime: dayTime,
  116. Ratio: mixed.MixedFodderCorrectRatio,
  117. }
  118. response.MixedFodderAccurateRatio = append(response.MixedFodderAccurateRatio, mixedAccurateValue)
  119. response.MixedFodderCorrectRatio = append(response.MixedFodderCorrectRatio, mixedCorrectValue)
  120. mixedInfo = true
  121. }
  122. if !mixedInfo {
  123. noInfo := &models.PastureAnalysisAccuracyDataValue{
  124. DayTime: dayTime,
  125. Ratio: 0,
  126. }
  127. response.MixedFodderAccurateRatio = append(response.MixedFodderAccurateRatio, noInfo)
  128. response.MixedFodderCorrectRatio = append(response.MixedFodderCorrectRatio, noInfo)
  129. }
  130. for _, sprinkle := range sprinkleFodderDataList {
  131. smyDate := sprinkle.PlanTime.Format(util.LayoutDateFormat)
  132. if smyDate != dayTime {
  133. continue
  134. }
  135. // 撒料准确率
  136. sprinkleAccurateValue := &models.PastureAnalysisAccuracyDataValue{
  137. DayTime: dayTime,
  138. Ratio: sprinkle.SprinkleFodderAccurateRatio,
  139. }
  140. // 撒料正确率
  141. sprinkleCorrectValue := &models.PastureAnalysisAccuracyDataValue{
  142. DayTime: dayTime,
  143. Ratio: sprinkle.SprinkleFodderCorrectRatio,
  144. }
  145. response.SprinkleFodderCorrectRatio = append(response.SprinkleFodderCorrectRatio, sprinkleCorrectValue)
  146. response.SprinkleFodderAccurateRatio = append(response.SprinkleFodderAccurateRatio, sprinkleAccurateValue)
  147. sprinkleInfo = true
  148. }
  149. if !sprinkleInfo {
  150. noInfo := &models.PastureAnalysisAccuracyDataValue{
  151. DayTime: dayTime,
  152. Ratio: 0,
  153. }
  154. response.SprinkleFodderAccurateRatio = append(response.SprinkleFodderAccurateRatio, noInfo)
  155. response.SprinkleFodderCorrectRatio = append(response.SprinkleFodderCorrectRatio, noInfo)
  156. }
  157. }
  158. return response, nil
  159. }
  160. // ProcessAnalysisService 过程分析-执行时间
  161. func ProcessAnalysisService(req *models.AnalysisAccuracyRequest) (*models.ProcessAnalysisResponse, error) {
  162. response := &models.ProcessAnalysisResponse{
  163. AddFeedTime: &models.ProcessAnalysisDataValue{},
  164. SprinkleTime: &models.ProcessAnalysisDataValue{},
  165. StirTime: &models.ProcessAnalysisDataValue{},
  166. }
  167. // 获取指定时间段内tmr设备列表
  168. tmrList, err := module.TMRList(req)
  169. if err != nil {
  170. return nil, err
  171. }
  172. addFeedList := make([]int, 0)
  173. sprinkleFeedList := make([]int, 0)
  174. stirDelayList := make([]int, 0)
  175. for _, v := range tmrList {
  176. mixedProcessTimeList, err := module.MixedProcessTimeList(req.PastureId, v.Id)
  177. if err != nil {
  178. zaplog.Error("ProcessAnalysisService", zap.Any("MixedProcessTimeList", err))
  179. } else {
  180. tmrMixAllTime := 0
  181. for _, t := range mixedProcessTimeList {
  182. tmrMixAllTime += util.TimeParseToMinutes(t.ProcessTime)
  183. }
  184. addFeedList = append(addFeedList, tmrMixAllTime)
  185. }
  186. sprinkleProcessTimeList, err := module.SprinkleProcessTimeList(req.PastureId, v.Id)
  187. if err != nil {
  188. zaplog.Error("ProcessAnalysisService", zap.Any("SprinkleProcessTimeList", err))
  189. } else {
  190. tmrSprinkleAllTime := 0
  191. for _, s := range sprinkleProcessTimeList {
  192. tmrSprinkleAllTime += util.TimeParseToMinutes(s.ProcessTime)
  193. }
  194. sprinkleFeedList = append(sprinkleFeedList, tmrSprinkleAllTime)
  195. }
  196. }
  197. if len(addFeedList) > 0 {
  198. sort.Ints(addFeedList)
  199. response.AddFeedTime.MaxValue = fmt.Sprintf("%.2f", float64(addFeedList[len(addFeedList)-1])/60)
  200. response.AddFeedTime.UpMiddleValue = fmt.Sprintf("%.2f", util.Median(addFeedList[len(addFeedList)/2+1:])/60)
  201. response.AddFeedTime.MiddleValue = fmt.Sprintf("%.2f", util.Median(addFeedList)/60)
  202. response.AddFeedTime.DownMiddleValue = fmt.Sprintf("%.2f", util.Median(addFeedList[0:len(addFeedList)/2])/60)
  203. response.AddFeedTime.MinValue = fmt.Sprintf("%.2f", float64(addFeedList[0])/60)
  204. }
  205. if len(sprinkleFeedList) > 0 {
  206. sort.Ints(sprinkleFeedList)
  207. response.SprinkleTime.MaxValue = fmt.Sprintf("%.2f", float64(sprinkleFeedList[len(sprinkleFeedList)-1])/60)
  208. response.SprinkleTime.UpMiddleValue = fmt.Sprintf("%.2f", util.Median(sprinkleFeedList[len(sprinkleFeedList)/2+1:])/60)
  209. response.SprinkleTime.MiddleValue = fmt.Sprintf("%.2f", util.Median(sprinkleFeedList)/60)
  210. response.SprinkleTime.DownMiddleValue = fmt.Sprintf("%.2f", util.Median(sprinkleFeedList[0:len(sprinkleFeedList)/2])/60)
  211. response.SprinkleTime.MinValue = fmt.Sprintf("%.2f", float64(sprinkleFeedList[0])/60)
  212. }
  213. if len(stirDelayList) > 0 {
  214. sort.Ints(stirDelayList)
  215. response.StirTime.MaxValue = fmt.Sprintf("%d", stirDelayList[len(stirDelayList)-1])
  216. response.StirTime.UpMiddleValue = fmt.Sprintf("%.2f", util.Median(stirDelayList[len(stirDelayList)/2+1:]))
  217. response.StirTime.MiddleValue = fmt.Sprintf("%.2f", util.Median(stirDelayList))
  218. response.StirTime.DownMiddleValue = fmt.Sprintf("%.2f", util.Median(stirDelayList[0:len(stirDelayList)/2]))
  219. response.StirTime.MinValue = fmt.Sprintf("%d", stirDelayList[0])
  220. }
  221. return response, nil
  222. }
  223. // SprinkleStatisticsService 撒料统计
  224. func SprinkleStatisticsService(req *models.SprinkleStatisticsRequest) ([]*models.SprinkleStatisticsDataList, error) {
  225. sprinkleDataList, err := module.SprinkleStatistics(req)
  226. if err != nil {
  227. return nil, err
  228. }
  229. return sprinkleDataList, nil
  230. }
  231. func AccountDistributionService(req *models.AccountDistributionRequest) error {
  232. return module.AccountDistribution(req)
  233. }
  234. func ForageCategoryDistributeService(req *models.CategoryRequest) error {
  235. feedClass := &models.FeedClass{
  236. PastureId: int64(req.PastureId),
  237. FCCode: req.Number,
  238. FCName: req.Name,
  239. Enable: req.IsShow,
  240. BigFeedClassId: req.ParentId,
  241. BigFeedClassName: req.ParentName,
  242. GroupId: int64(req.GroupId),
  243. }
  244. return module.ForageCategoryDistribute(feedClass)
  245. }
  246. func CattleCategoryDistributeService(req *models.CategoryRequest) error {
  247. cowClass := &models.CowClass{
  248. PastureId: int64(req.PastureId),
  249. ClassCode: req.Number,
  250. ClassName: req.Name,
  251. Enable: req.IsShow,
  252. ParentId: req.ParentId,
  253. ParentName: req.ParentName,
  254. GroupId: int64(req.GroupId),
  255. }
  256. return module.CattleCategoryDistribute(cowClass)
  257. }
  258. func CategoryDeleteService(keyWord string, req *models.CategoryDeleteRequest) error {
  259. switch keyWord {
  260. case "feed_delete":
  261. return module.ForageCategoryDelete(int64(req.PastureId), int64(req.GroupId))
  262. case "cow_delete":
  263. return module.CowCategoryDelete(int64(req.PastureId), int64(req.GroupId))
  264. }
  265. return nil
  266. }
  267. func FeedUsageService(req *models.FeedFormulaUsageRequest) (*models.FeedFormulaUsageResponse, error) {
  268. return module.FeedTemplateUsageDetail(req)
  269. }
  270. func FeedFormulaVersionService(pastureId int64, req *models.FeedFormulaUpdateVersionRequest) error {
  271. return module.FeedFormulaVersion(pastureId, req)
  272. }