neck_ring_warning.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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),
  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).Where("a.is_show = ?", pasturePb.IsShow_Ok)
  87. if len(req.PenIds) > 0 {
  88. pref.Where("b.pen_id IN ?", req.PenIds)
  89. }
  90. if len(req.EarNumber) > 0 {
  91. pref.Where("a.ear_number = ?", req.EarNumber)
  92. }
  93. if req.Level > 0 {
  94. pref.Where("a.level = ?", req.Level)
  95. }
  96. var count int64
  97. if err = pref.Order("a.level DESC").
  98. Count(&count).
  99. Limit(int(pagination.PageSize)).
  100. Offset(int(pagination.PageOffset)).
  101. Find(&neckWaringHealthList).Error; err != nil {
  102. return nil, xerr.WithStack(err)
  103. }
  104. warningHealthLevelMap := s.WarningHealthLevelMap()
  105. cowMap := make(map[int64]*model.Cow)
  106. eventLogMap := make(map[int64]string)
  107. cowIds := make([]int64, 0)
  108. for _, v := range neckWaringHealthList {
  109. cowIds = append(cowIds, v.CowId)
  110. lastEventLog := s.GetCowLastEvent(userModel.AppPasture.Id, v.CowId, pasturePb.EventCategory_Breed)
  111. if lastEventLog != nil {
  112. eventLogMap[v.CowId] = lastEventLog.EventDescription
  113. }
  114. }
  115. if len(cowIds) > 0 {
  116. cowList, _ := s.GetCowInfoByCowIds(ctx, userModel.AppPasture.Id, cowIds)
  117. for _, cow := range cowList {
  118. cowMap[cow.Id] = cow
  119. }
  120. }
  121. return &pasturePb.HealthWarningResponse{
  122. Code: http.StatusOK,
  123. Msg: "ok",
  124. Data: &pasturePb.HealthWarningData{
  125. Total: int32(count),
  126. Page: pagination.Page,
  127. PageSize: pagination.PageSize,
  128. List: model.NeckRingHealthWarningSlice(neckWaringHealthList).ToPB(warningHealthLevelMap, cowMap, eventLogMap),
  129. },
  130. }, nil
  131. }
  132. func (s *StoreEntry) NeckRingNoEstrusBatch(ctx context.Context, req *pasturePb.NeckRingNoEstrusBatchRequest) error {
  133. userModel, err := s.GetUserModel(ctx)
  134. if err != nil {
  135. return xerr.WithStack(err)
  136. }
  137. if len(req.EarNumbers) <= 0 {
  138. return xerr.Custom("请选择牛号")
  139. }
  140. nowTime := time.Now().Local()
  141. startTime := nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2)
  142. endTime := nowTime.AddDate(0, 0, 1).Format(model.LayoutDate2)
  143. startTimeUnix := util.TimeParseLocalUnix(startTime)
  144. endTimeUnix := util.TimeParseLocalUnix(endTime)
  145. startTime = time.Unix(startTimeUnix, 0).Local().Format(model.LayoutTime)
  146. endTime = time.Unix(endTimeUnix, 0).Local().Format(model.LayoutTime)
  147. neckRingEstrusWarningList := make([]*model.NeckRingEstrusWarning, 0)
  148. if err = s.DB.Model(new(model.NeckRingEstrusWarning)).
  149. Where("pasture_id = ?", userModel.AppPasture.Id).
  150. Where("date_time BETWEEN ? AND ?", startTime, endTime).
  151. Where("ear_number IN ?", req.EarNumbers).
  152. Where("is_show = ?", pasturePb.IsShow_Ok).
  153. Find(&neckRingEstrusWarningList).Error; err != nil {
  154. return xerr.WithStack(err)
  155. }
  156. if len(neckRingEstrusWarningList) <= 0 {
  157. return nil
  158. }
  159. neckRingEstrusIds := make([]int64, 0)
  160. for _, v := range neckRingEstrusWarningList {
  161. neckRingEstrus := &model.NeckRingEstrus{}
  162. if err = s.DB.Model(new(model.NeckRingEstrus)).
  163. Where("pasture_id = ?", userModel.AppPasture.Id).
  164. Where("id = ?", v.NeckRingEstrusId).
  165. Where("is_show = ?", pasturePb.IsShow_Ok).
  166. First(neckRingEstrus).Error; err != nil {
  167. zaplog.Error("NeckRingNoEstrusBatch", zap.Any("err", err), zap.Any("neckRingEstrusWarning", v))
  168. return xerr.Customf("数据异常: %s", v.EarNumber)
  169. }
  170. neckRingEstrusIds = append(neckRingEstrusIds, neckRingEstrus.Id)
  171. }
  172. if len(neckRingEstrusIds) <= 0 {
  173. return nil
  174. }
  175. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  176. for _, id := range neckRingEstrusIds {
  177. if err = tx.Model(new(model.NeckRingEstrus)).
  178. Where("id = ?", id).
  179. Updates(map[string]interface{}{
  180. "check_result": pasturePb.CheckResult_Fail,
  181. "check_user_id": userModel.SystemUser.Id,
  182. "check_user_name": userModel.SystemUser.Name,
  183. "check_at": time.Now().Local().Unix(),
  184. "is_show": pasturePb.IsShow_No,
  185. }).Error; err != nil {
  186. return xerr.WithStack(err)
  187. }
  188. if err = tx.Model(new(model.NeckRingEstrusWarning)).
  189. Where("neck_ring_estrus_id = ?", id).
  190. Updates(map[string]interface{}{
  191. "is_show": pasturePb.IsShow_No,
  192. }).Error; err != nil {
  193. return xerr.WithStack(err)
  194. }
  195. }
  196. return nil
  197. }); err != nil {
  198. return xerr.WithStack(err)
  199. }
  200. return nil
  201. }
  202. func (s *StoreEntry) NeckRingNoDiseaseBatch(ctx context.Context, req *pasturePb.NeckRingNoEstrusBatchRequest) error {
  203. userModel, err := s.GetUserModel(ctx)
  204. if err != nil {
  205. return xerr.WithStack(err)
  206. }
  207. if len(req.EarNumbers) <= 0 {
  208. return xerr.Custom("请选择牛号")
  209. }
  210. nowTime := time.Now().Local()
  211. startTime := nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2)
  212. endTime := nowTime.AddDate(0, 0, 1).Format(model.LayoutDate2)
  213. startTimeUnix := util.TimeParseLocalUnix(startTime)
  214. endTimeUnix := util.TimeParseLocalUnix(endTime)
  215. startTime = time.Unix(startTimeUnix, 0).Local().Format(model.LayoutTime)
  216. endTime = time.Unix(endTimeUnix, 0).Local().Format(model.LayoutTime)
  217. neckRingHealthWarningList := make([]*model.NeckRingHealthWarning, 0)
  218. if err = s.DB.Model(new(model.NeckRingHealthWarning)).
  219. Where("pasture_id = ?", userModel.AppPasture.Id).
  220. Where("date_time BETWEEN ? AND ?", startTime, endTime).
  221. Where("ear_number IN ?", req.EarNumbers).
  222. Where("is_show = ?", pasturePb.IsShow_Ok).
  223. Find(&neckRingHealthWarningList).Error; err != nil {
  224. return xerr.WithStack(err)
  225. }
  226. if len(neckRingHealthWarningList) <= 0 {
  227. return nil
  228. }
  229. neckRingHealthIds := make([]int64, 0)
  230. for _, v := range neckRingHealthWarningList {
  231. neckRingHealth := &model.NeckRingHealth{}
  232. if err = s.DB.Model(new(model.NeckRingHealth)).
  233. Where("pasture_id = ?", userModel.AppPasture.Id).
  234. Where("id = ?", v.NeckRingHealthId).
  235. Where("is_show = ?", pasturePb.IsShow_Ok).
  236. First(neckRingHealth).Error; err != nil {
  237. zaplog.Error("NeckRingNoEstrusBatch", zap.Any("err", err), zap.Any("neckRingEstrusWarning", v))
  238. return xerr.Customf("数据异常: %s", v.EarNumber)
  239. }
  240. neckRingHealthIds = append(neckRingHealthIds, neckRingHealth.Id)
  241. }
  242. if len(neckRingHealthIds) <= 0 {
  243. return nil
  244. }
  245. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  246. for _, id := range neckRingHealthIds {
  247. if err = tx.Model(new(model.NeckRingHealth)).
  248. Where("id = ?", id).
  249. Updates(map[string]interface{}{
  250. "check_result": pasturePb.CheckResult_Fail,
  251. "check_user_id": userModel.SystemUser.Id,
  252. "check_user_name": userModel.SystemUser.Name,
  253. "check_at": time.Now().Local().Unix(),
  254. "is_show": pasturePb.IsShow_No,
  255. }).Error; err != nil {
  256. return xerr.WithStack(err)
  257. }
  258. if err = tx.Model(new(model.NeckRingHealthWarning)).
  259. Where("neck_ring_health_id = ?", id).
  260. Updates(map[string]interface{}{
  261. "is_show": pasturePb.IsShow_No,
  262. }).Error; err != nil {
  263. return xerr.WithStack(err)
  264. }
  265. }
  266. return nil
  267. }); err != nil {
  268. return xerr.WithStack(err)
  269. }
  270. return nil
  271. }
  272. func (s *StoreEntry) EstrusWarningQuery(ctx context.Context, pastureId int64) (*gorm.DB, error) {
  273. nowTime := time.Now().Local()
  274. startTime := time.Unix(util.TimeParseLocalUnix(nowTime.Format(model.LayoutDate2)), 0).Local().Format(model.LayoutTime)
  275. entTime := time.Unix(util.TimeParseLocalEndUnix(nowTime.AddDate(0, 0, 1).Local().Format(model.LayoutDate2)), 0).Format(model.LayoutTime)
  276. systemBasic, err := s.FindSystemBasic(ctx, pastureId, model.EstrusWaringDays)
  277. if err != nil {
  278. return nil, xerr.WithStack(err)
  279. }
  280. return s.DB.Table(fmt.Sprintf("%s as a", new(model.NeckRingEstrusWarning).TableName())).
  281. Joins(fmt.Sprintf("JOIN %s AS b on a.cow_id = b.id", new(model.Cow).TableName())).
  282. Where("b.last_mating_at < UNIX_TIMESTAMP(a.date_time)").
  283. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  284. Where("b.is_forbidden_mating = ?", pasturePb.IsShow_No).
  285. Where("a.level >= ?", pasturePb.EstrusLevel_Low).
  286. Where("a.pasture_id = ?", pastureId).
  287. Where("a.date_time BETWEEN ? AND ?", startTime, entTime).
  288. Where(s.DB.Where("b.last_mating_at < UNIX_TIMESTAMP(a.first_time)").
  289. Or(s.DB.Where("b.last_mating_at = ?", 0).
  290. Where("b.calving_age > ?", systemBasic.MinValue).
  291. Or("b.lact = ?", 0))).
  292. Where("a.is_show = ?", pasturePb.IsShow_Ok), nil
  293. }
  294. func (s *StoreEntry) AbortionWarningQuery(ctx context.Context, pastureId int64) (*gorm.DB, error) {
  295. return s.DB.Table(fmt.Sprintf("%s as a", new(model.NeckRingEstrusWarning).TableName())).
  296. Joins(fmt.Sprintf("JOIN %s AS b on a.cow_id = b.id", new(model.Cow).TableName())).
  297. Where("b.pregnancy_age BETWEEN ? AND ?", 1, 260).
  298. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  299. Where("b.is_pregnant = ?", pasturePb.IsShow_Ok).
  300. Where("a.level >= ?", pasturePb.EstrusLevel_Middle).
  301. Where("a.pasture_id = ?", pastureId).
  302. Where("a.is_show = ?", pasturePb.IsShow_Ok), nil
  303. }