feed_template.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package group
  2. import (
  3. "context"
  4. "net/http"
  5. "tmr-watch/models"
  6. "tmr-watch/module"
  7. feedPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/pasture"
  8. )
  9. func SearchFeedTemplateList(ctx context.Context, req *feedPb.SearchFeedTemplateRequest) (*feedPb.SearchFeedTemplateResponse, error) {
  10. feedTemplateList, total, err := module.NewSearchFeedTemplateList(ctx, req)
  11. if err != nil {
  12. return nil, err
  13. }
  14. feedDetailVersionLogList, err := module.FeedDetailVersionLogList(ctx)
  15. if err != nil {
  16. return nil, err
  17. }
  18. res := &feedPb.SearchFeedTemplateResponse{
  19. Code: http.StatusOK,
  20. Msg: "ok",
  21. Data: &feedPb.FeedTemplateData{
  22. Page: req.Pagination.Page,
  23. PageSize: req.Pagination.PageSize,
  24. Total: int32(total),
  25. List: models.FeedTemplateSlice(feedTemplateList).ToPB(feedDetailVersionLogList),
  26. },
  27. }
  28. return res, nil
  29. }
  30. func GetFeetTemplateVersion(ctx context.Context, pastureId, feedTemplateId int) (*feedPb.GetFeetTemplateVersionResponse, error) {
  31. feedTemplateData, err := module.SearchFeedTemplateById(ctx, int64(pastureId), int64(feedTemplateId))
  32. if err != nil {
  33. return nil, err
  34. }
  35. historyFeedTemplateDetailList, err := module.SearchFeedTemplateDetailListByFeedTemplateId(ctx, int64(pastureId), int64(feedTemplateId))
  36. if err != nil {
  37. return nil, err
  38. }
  39. newFeedTemplateDetailVersion, err := module.GetFeedDetailVersionByDesc(ctx, int64(pastureId), feedTemplateData.GroupDataId)
  40. if err != nil {
  41. return nil, err
  42. }
  43. newFeedTemplateDetailVersionLogList, err := module.SearchFeedDetailVersionLogById(ctx, int64(pastureId), feedTemplateData.GroupDataId, newFeedTemplateDetailVersion.Id)
  44. if err != nil {
  45. return nil, err
  46. }
  47. res := &feedPb.GetFeetTemplateVersionResponse{
  48. Code: http.StatusOK,
  49. Msg: "ok",
  50. Data: &feedPb.FeetTemplateVersionData{
  51. Version: int32(newFeedTemplateDetailVersion.Id),
  52. List: make([]*feedPb.FeetTemplateVersionList, 0),
  53. },
  54. }
  55. // 比例两个配方饲料的不同
  56. for _, n := range newFeedTemplateDetailVersionLogList {
  57. // 更新
  58. for _, v := range historyFeedTemplateDetailList {
  59. if v.FName == n.ForageName {
  60. res.Data.List = append(res.Data.List, &feedPb.FeetTemplateVersionList{
  61. ForageName: v.FName,
  62. ForageGroupNameOld: n.ForageName,
  63. ForageGroupName: v.FeedGroup,
  64. ForageNameOld: n.ForageGroupName,
  65. Weight: float32(v.LWeight),
  66. WeightOld: float32(n.Weight),
  67. StirDelay: v.AutoSecond,
  68. StirDelayOld: n.StirDelay,
  69. AllowError: v.Deviation,
  70. AllowErrorOld: n.AllowError,
  71. Sort: v.Sort,
  72. SortOld: n.Sort,
  73. IsLockCowCountRatio: v.IsLockCount,
  74. IsLockCowCountRatioOld: int32(n.IsLockCowCountRatio),
  75. IsModify: v.IsModify,
  76. Status: feedPb.VersionStatus_UPDATE,
  77. })
  78. }
  79. }
  80. // 新增
  81. var insertInfo bool
  82. for _, v := range historyFeedTemplateDetailList {
  83. if v.FName == n.ForageName {
  84. insertInfo = true
  85. }
  86. }
  87. if !insertInfo {
  88. res.Data.List = append(res.Data.List, &feedPb.FeetTemplateVersionList{
  89. ForageName: n.ForageName,
  90. ForageGroupName: n.ForageGroupName,
  91. Weight: float32(n.Weight),
  92. StirDelay: n.StirDelay,
  93. AllowError: n.AllowError,
  94. Sort: n.Sort,
  95. IsLockCowCountRatio: int32(n.IsLockCowCountRatio),
  96. Status: feedPb.VersionStatus_ADD,
  97. })
  98. }
  99. }
  100. // 删除
  101. for _, v := range historyFeedTemplateDetailList {
  102. var deleteInfo bool
  103. for _, n := range newFeedTemplateDetailVersionLogList {
  104. if v.FName == n.ForageName {
  105. deleteInfo = true
  106. break
  107. }
  108. }
  109. if !deleteInfo {
  110. res.Data.List = append(res.Data.List, &feedPb.FeetTemplateVersionList{
  111. ForageName: v.FName,
  112. ForageGroupName: v.FeedGroup,
  113. Weight: float32(v.LWeight),
  114. StirDelay: v.AutoSecond,
  115. AllowError: v.Deviation,
  116. Sort: v.Sort,
  117. IsLockCowCountRatio: int32(v.IsLockCount),
  118. Status: feedPb.VersionStatus_DELETE,
  119. })
  120. }
  121. }
  122. return res, nil
  123. }
  124. func FeetTemplateUpgradeNewVersion(ctx context.Context, pastureId, feedTemplateId int) error {
  125. if err := module.FeetTemplateUpgradeNewVersion(ctx, int64(pastureId), int64(feedTemplateId)); err != nil {
  126. return err
  127. } else {
  128. return nil
  129. }
  130. }