sql.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package crontab
  2. import (
  3. "errors"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "time"
  7. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  8. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  9. "gitee.com/xuyiping_admin/pkg/xerr"
  10. "go.uber.org/zap"
  11. "gorm.io/gorm"
  12. )
  13. func (e *Entry) FindPastureList() []*model.AppPastureList {
  14. res := make([]*model.AppPastureList, 0)
  15. if err := e.DB.Model(new(model.AppPastureList)).
  16. Where("is_show = ?", pasturePb.IsShow_Ok).
  17. Find(&res).Error; err != nil {
  18. zaplog.Error("FindPastureList error", zap.Any("err", err))
  19. return res
  20. }
  21. return res
  22. }
  23. func (e *Entry) GetCowById(cowId int64) (*model.Cow, error) {
  24. cowInfo := &model.Cow{}
  25. if err := e.DB.Model(new(model.Cow)).
  26. Where("id = ?", cowId).
  27. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  28. First(cowInfo).Error; err != nil {
  29. return nil, err
  30. }
  31. return cowInfo, nil
  32. }
  33. func (e *Entry) GetPenMapList(pastureId int64) (map[int32]*model.Pen, error) {
  34. penList := make([]*model.Pen, 0)
  35. if err := e.DB.Model(new(model.Pen)).
  36. Where("pasture_id = ?", pastureId).
  37. Where("is_delete = ?", pasturePb.IsShow_Ok).
  38. Find(&penList).Error; err != nil {
  39. return nil, xerr.WithStack(err)
  40. }
  41. penMap := make(map[int32]*model.Pen)
  42. for _, v := range penList {
  43. penMap[v.Id] = v
  44. }
  45. return penMap, nil
  46. }
  47. // GetBeforeThreeDaysCowEstrus 获取值得时间之前三天内最大发情记录
  48. func (e *Entry) GetBeforeThreeDaysCowEstrus(cowId int64, activeTime string) *model.NeckRingEstrus {
  49. neckRingEstrus := &model.NeckRingEstrus{}
  50. if err := e.DB.Model(new(model.NeckRingEstrus)).
  51. Select("MAX(max_high) as max_high,cow_id,MAX(day_high) as day_high").
  52. Select("MAX(IF(check_result=1,3,check_result)) AS check_result").
  53. Where("cow_id = ?", cowId).
  54. Where("active_time >= ?", activeTime).
  55. First(neckRingEstrus).Error; err != nil {
  56. return neckRingEstrus
  57. }
  58. return neckRingEstrus
  59. }
  60. // GetTwoEstrus 判断最近50天内是否存在发情记录(发情等级>=2),如果18~25天@xadjust21,如果36~50天@xadjust42
  61. func (e *Entry) GetTwoEstrus(pastureId, cowId int64, startActiveTime, endActiveTime string) *CowEstrus {
  62. newCowEstrus := &CowEstrus{}
  63. if err := e.DB.Model(new(model.NeckRingEstrus)).
  64. Select("cow_id,MAX(active_time) as active_date").
  65. Where("cow_id = ?", cowId).
  66. Where("pasture_id = ?", pastureId).
  67. Where("active_time BETWEEN ? AND ?", startActiveTime, endActiveTime).
  68. Where("active_level >= ?", pasturePb.EstrusLevel_Middle).
  69. First(newCowEstrus).Error; err != nil {
  70. return newCowEstrus
  71. }
  72. return newCowEstrus
  73. }
  74. func (e *Entry) FindCowInfoByCowId(cowId int64) *model.Cow {
  75. res := &model.Cow{}
  76. if err := e.DB.Model(new(model.Cow)).
  77. Where("id = ?", cowId).
  78. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  79. First(res).Error; err != nil {
  80. zaplog.Error("FindCowInfoByCowId", zap.Any("cowId", cowId), zap.Any("err", err))
  81. return nil
  82. }
  83. return res
  84. }
  85. func (e *Entry) IsExistEventEstrus(pastureId, cowId int64) *model.EventEstrus {
  86. res := &model.EventEstrus{}
  87. if err := e.DB.Model(new(model.EventEstrus)).
  88. Where("cow_id = ?", cowId).
  89. Where("pasture_id = ?", pastureId).
  90. Where("expose_estrus_type = ?", pasturePb.ExposeEstrusType_Neck_Ring).
  91. Where("is_show = ?", pasturePb.IsShow_Ok).
  92. Order("id DESC").
  93. First(res).Error; err != nil {
  94. return nil
  95. }
  96. return res
  97. }
  98. func (e *Entry) IsExistNeckActiveHabit(neckRingNumber, heatDate string, frameId int32) (*model.NeckActiveHabit, int64) {
  99. count := int64(0)
  100. neckActiveHabit := &model.NeckActiveHabit{}
  101. if err := e.DB.Model(new(model.NeckActiveHabit)).
  102. Where("neck_ring_number = ?", neckRingNumber).
  103. Where("heat_date = ?", heatDate).
  104. Where("frameid = ?", frameId).
  105. Count(&count).First(neckActiveHabit).Error; err != nil {
  106. return nil, 0
  107. }
  108. return neckActiveHabit, count
  109. }
  110. func (e *Entry) GetSystemNeckRingConfigure(pastureId int64) ([]*model.NeckRingConfigure, error) {
  111. res := make([]*model.NeckRingConfigure, 0)
  112. if err := e.DB.Model(new(model.NeckRingConfigure)).
  113. Where("pasture_id = ?", pastureId).
  114. Where("is_show = ?", pasturePb.IsShow_Ok).
  115. Find(&res).Error; err != nil {
  116. return nil, xerr.WithStack(err)
  117. }
  118. return res, nil
  119. }
  120. func (e *Entry) GetCowInfoByNeckRingNumber(pastureId int64, neckRingNumber string) *model.Cow {
  121. res := &model.Cow{}
  122. if err := e.DB.Model(new(model.Cow)).
  123. Where("pasture_id = ?", pastureId).
  124. Where("neck_ring_number = ?", neckRingNumber).
  125. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  126. First(res).Error; err != nil {
  127. return nil
  128. }
  129. return res
  130. }
  131. // GetMinIdByHeatDate 获取最小的id
  132. func (e *Entry) GetMinIdByHeatDate(heatDate string, defaultId int64) (int64, error) {
  133. xMinId := struct {
  134. Id int64
  135. }{}
  136. if err := e.DB.Model(new(model.NeckActiveHabit)).
  137. Select("MIN(id) as id").
  138. Where("heat_date = ?", heatDate).
  139. First(&xMinId).Error; err != nil {
  140. if errors.Is(err, gorm.ErrRecordNotFound) {
  141. xMinId.Id = defaultId
  142. } else {
  143. return 0, xerr.WithStack(err)
  144. }
  145. }
  146. return xMinId.Id, nil
  147. }
  148. func (e *Entry) FindFirstFilter(pastureId int64, neckRingNumber, heatDate string, frameId int32) *FirstFilterData {
  149. firstFilterData := &FirstFilterData{}
  150. if err := e.DB.Model(new(model.NeckActiveHabit)).
  151. Select("neck_ring_number", "filter_high", "filter_rumina", "filter_chew").
  152. Where("neck_ring_number = ?", neckRingNumber).
  153. Where("heat_date = ?", heatDate).
  154. Where("frameid = ?", frameId).
  155. Where("pasture_id = ?", pastureId).
  156. First(firstFilterData).Error; err != nil {
  157. zaplog.Error("FirstFilterUpdate",
  158. zap.Any("err", err),
  159. zap.Any("NeckRingNumber", neckRingNumber),
  160. zap.Any("heatDate", heatDate),
  161. zap.Any("frameId", frameId),
  162. )
  163. }
  164. return firstFilterData
  165. }
  166. func (e *Entry) FindWeekHabitData(pastureId int64, neckRingNumber, heatDate string, frameid int32, xToday *XToday) *WeekHabit {
  167. beginDayDate, _ := time.Parse(model.LayoutDate2, heatDate)
  168. before7DayDate := beginDayDate.AddDate(0, 0, -7).Format(model.LayoutDate2)
  169. before1DayDate := beginDayDate.AddDate(0, 0, -1).Format(model.LayoutDate2)
  170. weekHabitData := &WeekHabit{}
  171. if err := e.DB.Model(new(model.NeckActiveHabit)).
  172. Select(
  173. "neck_ring_number",
  174. "IF(COUNT(1)>=3, ROUND((SUM(filter_high) -MIN(filter_high) -MAX(filter_high))/ABS(COUNT(1) -2),0), -1) as week_high_habit",
  175. "IF(COUNT(1)>=3, ROUND((SUM(filter_rumina) -MIN(filter_rumina) -MAX(filter_rumina))/ABS(COUNT(1) -2),0), -1) as week_rumina_habit",
  176. "IF(COUNT(1)>=3, ROUND((SUM(filter_chew) -MIN(filter_chew) -MAX(filter_chew))/ABS(COUNT(1) -2),0), -1) as week_chew_habit",
  177. "ROUND(AVG(intake),0) as week_intake_habit",
  178. "ROUND(AVG(inactive),0) as week_inactive_habit",
  179. ).Where("pasture_id = ?", pastureId).
  180. Where("heat_date BETWEEN ? AND ?", before7DayDate, before1DayDate).
  181. Where("neck_ring_number = ? ", neckRingNumber).
  182. Where("frameid = ?", frameid).
  183. Where(e.DB.Where("high > ?", xToday.High).Or("rumina >= ?", xToday.Rumina)).
  184. Group("neck_ring_number").First(weekHabitData).Error; err != nil {
  185. zaplog.Error("WeeklyActiveAvgUpdate-1",
  186. zap.Any("error", err),
  187. zap.Any("neckRingNumber", neckRingNumber),
  188. zap.Any("frameId", frameid),
  189. zap.Any("heatDate", heatDate),
  190. zap.Any("xToday", xToday),
  191. )
  192. }
  193. return weekHabitData
  194. }
  195. func (e *Entry) FindSumHabitData(pastureId int64, neckRingNumber, heatDate string, frameid int32, xToday *XToday) *SumHabit {
  196. beginDayDate, _ := time.Parse(model.LayoutDate2, heatDate)
  197. before1DayDate := beginDayDate.AddDate(0, 0, -1).Format(model.LayoutDate2)
  198. activeTime := fmt.Sprintf("%s %02d:00:00", heatDate, frameid*2+1)
  199. activeStartTimeParse, _ := time.Parse(model.LayoutTime, activeTime)
  200. activeStartTime := activeStartTimeParse.Add(-23 * time.Hour).Format(model.LayoutTime)
  201. // 累计24小时数值
  202. sumHabitData := &SumHabit{}
  203. if err := e.DB.Model(new(model.NeckActiveHabit)).
  204. Select(
  205. "neck_ring_number",
  206. "IF(COUNT(1)>6, ROUND(AVG(filter_rumina)*12,0), 0) as sum_rumina",
  207. "IF(COUNT(1)>6, ROUND(AVG(intake)*12,0), 0) as sum_intake",
  208. "IF(COUNT(1)>6, ROUND(AVG(inactive)*12,0), 0) as sum_inactive",
  209. "IF(COUNT(1)>6, ROUND(AVG(active)*12,0), 0) as sum_active",
  210. "MAX(change_filter) as sum_max_high",
  211. fmt.Sprintf("MIN(IF(change_filter > %d, change_filter, %d)) as sum_min_high", model.DefaultChangeFilter, model.InitChangeFilter),
  212. fmt.Sprintf("MIN( CASE WHEN filter_chew > %d THEN filter_chew WHEN filter_rumina >= %d THEN filter_rumina ELSE 0 END) as sum_min_chew", model.DefaultChangeFilter, model.DefaultRuminaFilter),
  213. ).
  214. Where("pasture_id = ?", pastureId).
  215. Where("heat_date BETWEEN ? AND ?", before1DayDate, heatDate).
  216. Where("active_time BETWEEN ? AND ?", activeStartTime, activeTime).
  217. Where(e.DB.Where("high > ?", xToday.High).Or("rumina >= ?", xToday.Rumina)).
  218. Where("neck_ring_number = ?", neckRingNumber).
  219. Group("neck_ring_number").First(sumHabitData).Debug().Error; err != nil {
  220. zaplog.Error("WeeklyActiveAvgUpdate-2",
  221. zap.Any("error", err),
  222. zap.Any("neckRingNumber", neckRingNumber),
  223. zap.Any("frameId", frameid),
  224. zap.Any("heatDate", heatDate),
  225. zap.Any("xToday", xToday),
  226. )
  227. }
  228. return sumHabitData
  229. }
  230. func (e *Entry) FindBefore3DaysNeckActiveHabit(pastureId int64, neckRingNumber, heatDate string, frameid int32) *model.NeckActiveHabit {
  231. before3DaysNeckActiveHabit := &model.NeckActiveHabit{}
  232. beginDayDate, _ := time.Parse(model.LayoutDate2, heatDate)
  233. before3DayDate := beginDayDate.AddDate(0, 0, -3).Format(model.LayoutDate2)
  234. if err := e.DB.Model(new(model.NeckActiveHabit)).
  235. Select("sum_rumina", "sum_intake").
  236. Where("pasture_id = ?", pastureId).
  237. Where("neck_ring_number = ?", neckRingNumber).
  238. Where("heat_date = ?", before3DayDate).
  239. Where("frameid = ? ", frameid).
  240. First(before3DaysNeckActiveHabit).Error; err != nil {
  241. zaplog.Error("WeeklyActiveAvgUpdate-3",
  242. zap.Any("error", err),
  243. zap.Any("neckRingNumber", neckRingNumber),
  244. zap.Any("frameId", frameid),
  245. zap.Any("heatDate", heatDate),
  246. )
  247. }
  248. return before3DaysNeckActiveHabit
  249. }
  250. // FindNeckRingEstrusByFirstTimeEmpty 查询firstTime为空的数据
  251. func (e *Entry) FindNeckRingEstrusByFirstTimeEmpty(pastureId int64) []*model.NeckRingEstrus {
  252. neckRingEstrusList := make([]*model.NeckRingEstrus, 0)
  253. if err := e.DB.Model(new(model.NeckRingEstrus)).
  254. Where("first_time = ?", "").
  255. Where("is_show = ?", pasturePb.IsShow_Ok).
  256. Where("pasture_id = ?", pastureId).
  257. Find(&neckRingEstrusList).Error; err != nil {
  258. zaplog.Error("FindNeckRingEstrusFirstTime", zap.Any("err", err))
  259. }
  260. return neckRingEstrusList
  261. }