sql.go 12 KB

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