sql.go 12 KB

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