milk_original_update_gea_more.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package crontab
  2. import (
  3. "kpt-pasture/model"
  4. "kpt-pasture/util"
  5. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  6. "go.uber.org/zap"
  7. )
  8. // UpdateMilkLoad2 (UPDATE milkweight m SET m.nattach=1 WHERE m.wid BETWEEN xdminwid AND xdmaxwid AND m.milkdate=xcurdate AND m.nattach=0 AND m.station=xvarName;)
  9. func (e *Entry) UpdateMilkLoad2(pastureId int64, milkClassConfig *MilkClassConfig, hall *model.MilkHall) {
  10. milkOriginalList := make([]*model.MilkOriginal, 0)
  11. if err := e.DB.Model(new(model.MilkOriginal)).
  12. Where("pasture_id = ?", pastureId).
  13. Where("nattach = ?", 0).
  14. Where("milk_hall_number = ?", hall.Name).
  15. Where("id BETWEEN ? AND ?", milkClassConfig.OldUpdateMaxId+1, milkClassConfig.CurrentMaxId).
  16. Find(&milkOriginalList).Error; err != nil {
  17. return
  18. }
  19. for _, v := range milkOriginalList {
  20. if err := e.DB.Model(new(model.MilkOriginal)).
  21. Where("id = ?", v.Id).
  22. Update("nattach", 1).Error; err != nil {
  23. zaplog.Error("UpdateMilkLoad2", zap.Any("err", err))
  24. return
  25. }
  26. }
  27. }
  28. // UpdateMilkLoad3 更新重复套杯牛只圈数
  29. func (e *Entry) UpdateMilkLoad3(pastureId int64, milkClassConfig *MilkClassConfig, hall *model.MilkHall) {
  30. milkOriginalList := make([]*model.MilkOriginal, 0)
  31. if err := e.DB.Model(new(model.MilkOriginal)).
  32. Where("pasture_id = ?", pastureId).
  33. Where("milk_hall_number = ?", hall.Name).
  34. Where("id BETWEEN ? AND ?", milkClassConfig.OldUpdateMaxId+1, milkClassConfig.CurrentMaxId).
  35. Order("shifts,detached_address,id").
  36. Find(&milkOriginalList).Error; err != nil {
  37. zaplog.Error("UpdateMilkLoad3", zap.Any("err", err))
  38. return
  39. }
  40. // 如果没有数据,直接返回
  41. if len(milkOriginalList) == 0 {
  42. return
  43. }
  44. // 初始化变量,模拟SQL中的变量
  45. //var currentShifts int32 = 6
  46. var currentAddress int64 = 0
  47. var currentLoad int32 = 1
  48. // 创建需要更新的记录映射
  49. updateMap := make(map[int64]int32)
  50. // 遍历排序后的记录,计算正确的load值
  51. for _, record := range milkOriginalList {
  52. // 如果nattach=1或者detached_address变化,则更新currentLoad
  53. if record.Nattach == 1 || currentAddress != record.DetachedAddress {
  54. currentLoad = record.Load
  55. }
  56. // 更新当前变量值
  57. //currentShifts = record.Shifts
  58. currentAddress = record.DetachedAddress
  59. // 如果nattach=2,则记录需要更新的load值
  60. if record.Nattach == 2 {
  61. updateMap[record.Id] = currentLoad
  62. }
  63. }
  64. // 更新数据库中的load字段
  65. for id, load := range updateMap {
  66. if err := e.DB.Model(new(model.MilkOriginal)).
  67. Where("id = ?", id).
  68. Update("load", load).Error; err != nil {
  69. zaplog.Error("UpdateMilkLoad3", zap.Any("err", err), zap.Any("id", id), zap.Any("load", load))
  70. return
  71. }
  72. }
  73. }
  74. // UpdateMilkCowIdResetZero 假定2.5分钟内套杯为重复套杯,3分钟外为其它牛只,清除错误牛号
  75. func (e *Entry) UpdateMilkCowIdResetZero(pastureId int64, milkClassConfig *MilkClassConfig, hall *model.MilkHall) {
  76. // 1. 首先查询重复套杯的牛只记录
  77. // 查询条件:同一牧场、同一奶厅、同一班次、同一脱杯地址、同一牛号有多条记录
  78. duplicateCowRecords := make([]*model.BaseRecords, 0)
  79. if err := e.DB.Model(new(model.MilkOriginal)).
  80. Select("milk_date, shifts, detached_address, cow_id, COUNT(0) as count").
  81. Where("pasture_id = ?", pastureId).
  82. Where("milk_hall_number = ?", hall.Name).
  83. Where("id BETWEEN ? AND ?", milkClassConfig.OldUpdateMaxId+1, milkClassConfig.CurrentMaxId).
  84. Where("cow_id > ?", 0).
  85. Group("milk_date, shifts, detached_address, cow_id").
  86. Having("count > ?", 1).
  87. Find(&duplicateCowRecords).Error; err != nil {
  88. zaplog.Error("UpdateMilkCowIdResetZero", zap.Any("err", err))
  89. return
  90. }
  91. // 如果没有重复套杯的记录,直接返回
  92. if len(duplicateCowRecords) == 0 {
  93. return
  94. }
  95. // 2. 查询需要处理的详细记录
  96. baseRecords := make([]*model.BaseRecords, 0)
  97. for _, record := range duplicateCowRecords {
  98. var records []*model.BaseRecords
  99. if err := e.DB.Model(new(model.MilkOriginal)).
  100. Select("id, attach_adjust_time, detached_address, shifts, ear_number, milk_date, recognition_time").
  101. Where("pasture_id = ?", pastureId).
  102. Where("milk_hall_number = ?", hall.Name).
  103. Where("milk_date = ?", record.MilkDate).
  104. Where("shifts = ?", record.Shifts).
  105. Where("detached_address = ?", record.DetachedAddress).
  106. Where("cow_id = ?", record.CowId).
  107. Where("id BETWEEN ? AND ?", milkClassConfig.OldUpdateMaxId+1, milkClassConfig.CurrentMaxId).
  108. Order("shifts, detached_address, cow_id, id").
  109. Find(&records).Error; err != nil {
  110. zaplog.Error("UpdateMilkCowIdResetZero", zap.Any("err", err))
  111. continue
  112. }
  113. baseRecords = append(baseRecords, records...)
  114. }
  115. // 如果没有详细记录,直接返回
  116. if len(baseRecords) == 0 {
  117. return
  118. }
  119. // 3. 初始化变量,模拟SQL中的变量
  120. var currentCowId int64 = 999999
  121. //var currentLoad int32 = 0
  122. var currentAddress int32 = 999
  123. var currentShifts int32 = 6
  124. var currentDetachTime string = "2001-01-01 01:01:01"
  125. // 4. 创建需要更新的记录映射
  126. updateMap := make(map[int64]bool)
  127. // 5. 遍历排序后的记录,计算需要清除牛号的记录
  128. for _, record := range baseRecords {
  129. // 解析时间
  130. attachAdjustTime, err := util.TimeParseLocal(model.LayoutTime, record.AttachAdjust)
  131. if err != nil {
  132. continue
  133. }
  134. detachTime, err := util.TimeParseLocal(model.LayoutTime, currentDetachTime)
  135. if err != nil {
  136. continue
  137. }
  138. // 计算时间差(秒)
  139. timeDiff := int64(attachAdjustTime.Sub(detachTime).Seconds())
  140. // 判断是否需要清除牛号
  141. // 条件:同一牛号、同一脱杯地址、同一班次,且时间差小于180秒(3分钟)
  142. if record.CowId == currentCowId && record.DetachedAddress == currentAddress && record.Shifts == currentShifts && timeDiff < 180 {
  143. // 这是重复套杯,不需要清除牛号
  144. } else if record.CowId == currentCowId {
  145. // 这是同一牛号但不同脱杯地址或班次,需要清除牛号
  146. updateMap[record.Id] = true
  147. }
  148. // 更新当前变量值
  149. currentCowId = record.CowId
  150. currentAddress = record.DetachedAddress
  151. currentShifts = record.Shifts
  152. currentDetachTime = record.AttachAdjust
  153. }
  154. // 6. 更新数据库中的记录,清除牛号
  155. for id := range updateMap {
  156. if err := e.DB.Model(new(model.MilkOriginal)).
  157. Where("id = ?", id).
  158. Updates(map[string]interface{}{
  159. "cow_id": 0,
  160. "ear_number": "",
  161. "pen_id": 0,
  162. "pen_name": "",
  163. }).Error; err != nil {
  164. zaplog.Error("UpdateMilkCowIdResetZero", zap.Any("err", err), zap.Any("id", id))
  165. return
  166. }
  167. }
  168. }