neck_ring_warning.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "kpt-pasture/util"
  7. "net/http"
  8. "time"
  9. "go.uber.org/zap"
  10. "gorm.io/gorm"
  11. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  12. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  13. "gitee.com/xuyiping_admin/pkg/xerr"
  14. )
  15. func (s *StoreEntry) NeckRingWarningEstrusOrAbortionCowList(ctx context.Context, req *pasturePb.WarningItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EstrusResponse, error) {
  16. userModel, err := s.GetUserModel(ctx)
  17. if err != nil {
  18. return nil, xerr.WithStack(err)
  19. }
  20. var count int64
  21. neckRingEstrusList := make([]*model.NeckRingEstrusWarning, 0)
  22. var pref *gorm.DB
  23. switch req.Kind {
  24. case "abortion":
  25. pref, err = s.AbortionWarningQuery(ctx, userModel.AppPasture.Id)
  26. if err != nil {
  27. return nil, xerr.WithStack(err)
  28. }
  29. default:
  30. pref, err = s.EstrusWarningQuery(ctx, userModel.AppPasture.Id)
  31. if err != nil {
  32. return nil, xerr.WithStack(err)
  33. }
  34. }
  35. if len(req.EarNumber) > 0 {
  36. pref.Where("a.ear_number = ?", req.EarNumber)
  37. }
  38. if req.Level > 0 {
  39. pref.Where("a.level = ?", req.Level)
  40. }
  41. if len(req.PenIds) > 0 {
  42. pref.Where("b.pen_id IN ?", req.PenIds)
  43. }
  44. if err = pref.Order("a.level DESC").
  45. Count(&count).
  46. Limit(int(pagination.PageSize)).
  47. Offset(int(pagination.PageOffset)).
  48. Find(&neckRingEstrusList).Error; err != nil {
  49. return nil, xerr.WithStack(err)
  50. }
  51. cowMap := make(map[int64]*model.Cow)
  52. eventLogMap := make(map[int64]string)
  53. cowIds := make([]int64, 0)
  54. for _, v := range neckRingEstrusList {
  55. cowIds = append(cowIds, v.CowId)
  56. lastEventLog := s.GetCowLastEvent(userModel.AppPasture.Id, v.CowId, pasturePb.EventCategory_Breed)
  57. if lastEventLog != nil {
  58. eventLogMap[v.CowId] = lastEventLog.EventDescription
  59. }
  60. }
  61. if len(cowIds) > 0 {
  62. cowList, _ := s.GetCowInfoByCowIds(ctx, userModel.AppPasture.Id, cowIds)
  63. for _, cow := range cowList {
  64. cowMap[cow.Id] = cow
  65. }
  66. }
  67. return &pasturePb.EstrusResponse{
  68. Code: http.StatusOK,
  69. Msg: "ok",
  70. Data: &pasturePb.EstrusData{
  71. List: model.NeckRingEstrusWarningSlice(neckRingEstrusList).ToPB(cowMap, eventLogMap, req.MatingWindowPeriod),
  72. Total: int32(count),
  73. PageSize: pagination.PageSize,
  74. Page: pagination.Page,
  75. },
  76. }, nil
  77. }
  78. func (s *StoreEntry) NeckRingWarningHealthCowList(ctx context.Context, req *pasturePb.HealthWarningRequest, pagination *pasturePb.PaginationModel) (*pasturePb.HealthWarningResponse, error) {
  79. userModel, err := s.GetUserModel(ctx)
  80. if err != nil {
  81. return nil, xerr.WithStack(err)
  82. }
  83. neckWaringHealthList := make([]*model.NeckRingHealthWarning, 0)
  84. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.NeckRingHealthWarning).TableName())).
  85. Joins(fmt.Sprintf("JOIN cow AS b on a.cow_id = b.id")).
  86. Where("a.score >= ?", 0).
  87. Where("a.is_show = ?", pasturePb.IsShow_Ok).
  88. Where("a.pasture_id = ?", userModel.AppPasture.Id)
  89. if len(req.PenIds) > 0 {
  90. pref.Where("b.pen_id IN ?", req.PenIds)
  91. }
  92. if len(req.EarNumber) > 0 {
  93. pref.Where("a.ear_number = ?", req.EarNumber)
  94. }
  95. if req.Level > 0 {
  96. pref.Where("a.level = ?", req.Level)
  97. }
  98. var count int64
  99. if err = pref.Order("a.level DESC").
  100. Count(&count).
  101. Limit(int(pagination.PageSize)).
  102. Offset(int(pagination.PageOffset)).
  103. Find(&neckWaringHealthList).Error; err != nil {
  104. return nil, xerr.WithStack(err)
  105. }
  106. warningHealthLevelMap := s.WarningHealthLevelMap()
  107. cowMap := make(map[int64]*model.Cow)
  108. eventLogMap := make(map[int64]string)
  109. cowIds := make([]int64, 0)
  110. for _, v := range neckWaringHealthList {
  111. cowIds = append(cowIds, v.CowId)
  112. lastEventLog := s.GetCowLastEvent(userModel.AppPasture.Id, v.CowId, pasturePb.EventCategory_Breed)
  113. if lastEventLog != nil {
  114. eventLogMap[v.CowId] = lastEventLog.EventDescription
  115. }
  116. }
  117. if len(cowIds) > 0 {
  118. cowList, _ := s.GetCowInfoByCowIds(ctx, userModel.AppPasture.Id, cowIds)
  119. for _, cow := range cowList {
  120. cowMap[cow.Id] = cow
  121. }
  122. }
  123. return &pasturePb.HealthWarningResponse{
  124. Code: http.StatusOK,
  125. Msg: "ok",
  126. Data: &pasturePb.HealthWarningData{
  127. Total: int32(count),
  128. Page: pagination.Page,
  129. PageSize: pagination.PageSize,
  130. List: model.NeckRingHealthWarningSlice(neckWaringHealthList).ToPB(warningHealthLevelMap, cowMap, eventLogMap),
  131. },
  132. }, nil
  133. }
  134. func (s *StoreEntry) NeckRingNoEstrusBatch(ctx context.Context, req *pasturePb.NeckRingNoEstrusBatchRequest) error {
  135. userModel, err := s.GetUserModel(ctx)
  136. if err != nil {
  137. return xerr.WithStack(err)
  138. }
  139. if len(req.EarNumbers) <= 0 {
  140. return xerr.Custom("请选择牛号")
  141. }
  142. nowTime := time.Now().Local()
  143. startTime := nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2)
  144. endTime := nowTime.AddDate(0, 0, 1).Format(model.LayoutDate2)
  145. startTimeUnix := util.TimeParseLocalUnix(startTime)
  146. endTimeUnix := util.TimeParseLocalUnix(endTime)
  147. startTime = time.Unix(startTimeUnix, 0).Local().Format(model.LayoutTime)
  148. endTime = time.Unix(endTimeUnix, 0).Local().Format(model.LayoutTime)
  149. neckRingEstrusWarningList := make([]*model.NeckRingEstrusWarning, 0)
  150. if err = s.DB.Model(new(model.NeckRingEstrusWarning)).
  151. Where("pasture_id = ?", userModel.AppPasture.Id).
  152. Where("date_time BETWEEN ? AND ?", startTime, endTime).
  153. Where("ear_number IN ?", req.EarNumbers).
  154. Where("is_show = ?", pasturePb.IsShow_Ok).
  155. Find(&neckRingEstrusWarningList).Error; err != nil {
  156. return xerr.WithStack(err)
  157. }
  158. if len(neckRingEstrusWarningList) <= 0 {
  159. return nil
  160. }
  161. neckRingEstrusIds := make([]int64, 0)
  162. for _, v := range neckRingEstrusWarningList {
  163. neckRingEstrus := &model.NeckRingEstrus{}
  164. if err = s.DB.Model(new(model.NeckRingEstrus)).
  165. Where("pasture_id = ?", userModel.AppPasture.Id).
  166. Where("id = ?", v.NeckRingEstrusId).
  167. Where("is_show = ?", pasturePb.IsShow_Ok).
  168. First(neckRingEstrus).Error; err != nil {
  169. zaplog.Error("NeckRingNoEstrusBatch", zap.Any("err", err), zap.Any("neckRingEstrusWarning", v))
  170. return xerr.Customf("数据异常: %s", v.EarNumber)
  171. }
  172. neckRingEstrusIds = append(neckRingEstrusIds, neckRingEstrus.Id)
  173. }
  174. if len(neckRingEstrusIds) <= 0 {
  175. return nil
  176. }
  177. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  178. for _, id := range neckRingEstrusIds {
  179. if err = tx.Model(new(model.NeckRingEstrus)).
  180. Where("id = ?", id).
  181. Updates(map[string]interface{}{
  182. "check_result": pasturePb.CheckResult_Fail,
  183. "check_user_id": userModel.SystemUser.Id,
  184. "check_user_name": userModel.SystemUser.Name,
  185. "check_at": time.Now().Local().Unix(),
  186. "is_show": pasturePb.IsShow_No,
  187. }).Error; err != nil {
  188. return xerr.WithStack(err)
  189. }
  190. if err = tx.Model(new(model.NeckRingEstrusWarning)).
  191. Where("neck_ring_estrus_id = ?", id).
  192. Updates(map[string]interface{}{
  193. "is_show": pasturePb.IsShow_No,
  194. }).Error; err != nil {
  195. return xerr.WithStack(err)
  196. }
  197. }
  198. return nil
  199. }); err != nil {
  200. return xerr.WithStack(err)
  201. }
  202. return nil
  203. }
  204. func (s *StoreEntry) NeckRingNoDiseaseBatch(ctx context.Context, req *pasturePb.NeckRingNoEstrusBatchRequest) error {
  205. userModel, err := s.GetUserModel(ctx)
  206. if err != nil {
  207. return xerr.WithStack(err)
  208. }
  209. if len(req.EarNumbers) <= 0 {
  210. return xerr.Custom("请选择牛号")
  211. }
  212. nowTime := time.Now().Local()
  213. startTime := nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2)
  214. endTime := nowTime.AddDate(0, 0, 1).Format(model.LayoutDate2)
  215. startTimeUnix := util.TimeParseLocalUnix(startTime)
  216. endTimeUnix := util.TimeParseLocalUnix(endTime)
  217. startTime = time.Unix(startTimeUnix, 0).Local().Format(model.LayoutTime)
  218. endTime = time.Unix(endTimeUnix, 0).Local().Format(model.LayoutTime)
  219. neckRingHealthWarningList := make([]*model.NeckRingHealthWarning, 0)
  220. if err = s.DB.Model(new(model.NeckRingHealthWarning)).
  221. Where("pasture_id = ?", userModel.AppPasture.Id).
  222. Where("date_time BETWEEN ? AND ?", startTime, endTime).
  223. Where("ear_number IN ?", req.EarNumbers).
  224. Where("is_show = ?", pasturePb.IsShow_Ok).
  225. Find(&neckRingHealthWarningList).Error; err != nil {
  226. return xerr.WithStack(err)
  227. }
  228. if len(neckRingHealthWarningList) <= 0 {
  229. return nil
  230. }
  231. neckRingHealthIds := make([]int64, 0)
  232. for _, v := range neckRingHealthWarningList {
  233. neckRingHealth := &model.NeckRingHealth{}
  234. if err = s.DB.Model(new(model.NeckRingHealth)).
  235. Where("pasture_id = ?", userModel.AppPasture.Id).
  236. Where("id = ?", v.NeckRingHealthId).
  237. Where("is_show = ?", pasturePb.IsShow_Ok).
  238. First(neckRingHealth).Error; err != nil {
  239. zaplog.Error("NeckRingNoEstrusBatch", zap.Any("err", err), zap.Any("neckRingEstrusWarning", v))
  240. return xerr.Customf("数据异常: %s", v.EarNumber)
  241. }
  242. neckRingHealthIds = append(neckRingHealthIds, neckRingHealth.Id)
  243. }
  244. if len(neckRingHealthIds) <= 0 {
  245. return nil
  246. }
  247. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  248. for _, id := range neckRingHealthIds {
  249. if err = tx.Model(new(model.NeckRingHealth)).
  250. Where("id = ?", id).
  251. Updates(map[string]interface{}{
  252. "check_result": pasturePb.CheckResult_Fail,
  253. "check_user_id": userModel.SystemUser.Id,
  254. "check_user_name": userModel.SystemUser.Name,
  255. "check_at": time.Now().Local().Unix(),
  256. "is_show": pasturePb.IsShow_No,
  257. }).Error; err != nil {
  258. return xerr.WithStack(err)
  259. }
  260. if err = tx.Model(new(model.NeckRingHealthWarning)).
  261. Where("neck_ring_health_id = ?", id).
  262. Updates(map[string]interface{}{
  263. "is_show": pasturePb.IsShow_No,
  264. }).Error; err != nil {
  265. return xerr.WithStack(err)
  266. }
  267. }
  268. return nil
  269. }); err != nil {
  270. return xerr.WithStack(err)
  271. }
  272. return nil
  273. }
  274. func (s *StoreEntry) EstrusWarningQuery(ctx context.Context, pastureId int64) (*gorm.DB, error) {
  275. nowTime := time.Now().Local()
  276. startTime := time.Unix(util.TimeParseLocalUnix(nowTime.Format(model.LayoutDate2)), 0).Local().Format(model.LayoutTime)
  277. entTime := time.Unix(util.TimeParseLocalEndUnix(nowTime.AddDate(0, 0, 1).Local().Format(model.LayoutDate2)), 0).Format(model.LayoutTime)
  278. systemBasic, err := s.FindSystemBasic(ctx, pastureId, model.EstrusWaringDays)
  279. if err != nil {
  280. return nil, xerr.WithStack(err)
  281. }
  282. return s.DB.Table(fmt.Sprintf("%s as a", new(model.NeckRingEstrusWarning).TableName())).
  283. Joins(fmt.Sprintf("JOIN %s AS b on a.cow_id = b.id", new(model.Cow).TableName())).
  284. Where("b.last_mating_at < UNIX_TIMESTAMP(a.date_time)").
  285. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  286. Where("b.is_forbidden_mating = ?", pasturePb.IsShow_No).
  287. Where("a.level >= ?", pasturePb.EstrusLevel_Low).
  288. Where("a.pasture_id = ?", pastureId).
  289. Where("a.date_time BETWEEN ? AND ?", startTime, entTime).
  290. Where(s.DB.Where("b.last_mating_at < UNIX_TIMESTAMP(a.first_time)").
  291. Or(s.DB.Where("b.last_mating_at = ?", 0).
  292. Where("b.calving_age > ?", systemBasic.MinValue).
  293. Or("b.lact = ?", 0))).
  294. Where("a.is_show = ?", pasturePb.IsShow_Ok), nil
  295. }
  296. func (s *StoreEntry) AbortionWarningQuery(ctx context.Context, pastureId int64) (*gorm.DB, error) {
  297. return s.DB.Table(fmt.Sprintf("%s as a", new(model.NeckRingEstrusWarning).TableName())).
  298. Joins(fmt.Sprintf("JOIN %s AS b on a.cow_id = b.id", new(model.Cow).TableName())).
  299. Where("b.pregnancy_age BETWEEN ? AND ?", 1, 260).
  300. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  301. Where("b.is_pregnant = ?", pasturePb.IsShow_Ok).
  302. Where("a.level >= ?", pasturePb.EstrusLevel_Middle).
  303. Where("a.pasture_id = ?", pastureId).
  304. Where("a.is_show = ?", pasturePb.IsShow_Ok), nil
  305. }