event_breed.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "kpt-pasture/util"
  7. "net/http"
  8. "strings"
  9. "time"
  10. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  11. "go.uber.org/zap"
  12. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  13. "gitee.com/xuyiping_admin/pkg/xerr"
  14. "gorm.io/gorm"
  15. )
  16. func (s *StoreEntry) CalvingList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchLavingEventResponse, error) {
  17. userModel, err := s.GetUserModel(ctx)
  18. if err != nil {
  19. return nil, xerr.WithStack(err)
  20. }
  21. lavingList := make([]*model.EventCalvingList, 0)
  22. var count int64 = 0
  23. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCalving).TableName())).
  24. Select(`a.*,b.name as pen_name,c.name as staff_member_name`).
  25. Joins(fmt.Sprintf("JOIN %s AS b on a.pen_id = b.id", new(model.Pen).TableName())).
  26. Joins(fmt.Sprintf("JOIN %s AS c on a.operation_id = c.id", new(model.SystemUser).TableName())).
  27. Where("a.pasture_id = ?", userModel.AppPasture.Id).Where("a.status = ?", pasturePb.IsShow_No)
  28. if len(req.CowId) > 0 {
  29. cowIds := strings.Split(req.CowId, ",")
  30. pref.Where("a.cow_id IN ?", cowIds)
  31. }
  32. if req.EarNumber != "" {
  33. pref.Where("a.ear_number = ?", req.EarNumber)
  34. }
  35. if err = pref.Order("a.id desc").
  36. Count(&count).Limit(int(pagination.PageSize)).
  37. Offset(int(pagination.PageOffset)).
  38. Find(&lavingList).Error; err != nil {
  39. return nil, xerr.WithStack(err)
  40. }
  41. calvingIds := make([]int64, 0)
  42. for _, v := range lavingList {
  43. calvingIds = append(calvingIds, v.Id)
  44. }
  45. calvingCalfList := make([]*model.CalvingCalf, 0)
  46. if err = s.DB.Model(new(model.CalvingCalf)).
  47. Where("calving_id IN ?", calvingIds).
  48. Find(&calvingCalfList).Error; err != nil {
  49. return nil, xerr.WithStack(err)
  50. }
  51. return &pasturePb.SearchLavingEventResponse{
  52. Code: http.StatusOK,
  53. Msg: "ok",
  54. Data: &pasturePb.SearchLavingData{
  55. List: model.EventCalvingListSlice(lavingList).ToPB(calvingCalfList),
  56. Total: int32(count),
  57. PageSize: pagination.PageSize,
  58. Page: pagination.Page,
  59. },
  60. }, nil
  61. }
  62. func (s *StoreEntry) CalvingCreate(ctx context.Context, req *pasturePb.EventCalving) (err error) {
  63. userModel, err := s.GetUserModel(ctx)
  64. if err != nil {
  65. return xerr.WithStack(err)
  66. }
  67. cow, err := s.GetCowInfoByEarNumber(ctx, userModel.AppPasture.Id, req.EarNumber)
  68. if err != nil {
  69. zaplog.Error("CalvingCreate", zap.Any("cow_id", req.CowId), zap.Any("err", err))
  70. return xerr.Custom("请选择相关牛只")
  71. }
  72. if len(req.CalfItemList) != int(req.ChildNumber) {
  73. return xerr.Custom("犊牛信息与产子数不相符")
  74. }
  75. if cow.IsPregnant != pasturePb.IsShow_Ok || cow.BreedStatus != pasturePb.BreedStatus_Pregnant {
  76. return xerr.Custom("该母牛未配种")
  77. }
  78. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  79. if err != nil {
  80. return xerr.Customf("获取操作人员信息失败: %s", err.Error())
  81. }
  82. req.OperationName = operationUser.Name
  83. penMap := s.PenMap(ctx, userModel.AppPasture.Id)
  84. newEventCalving := &model.EventCalving{}
  85. if err = s.DB.Model(new(model.EventCalving)).
  86. Where("cow_id = ?", cow.Id).
  87. Where("lact = ?", cow.Lact).
  88. Where("status = ?", pasturePb.IsShow_No).
  89. First(newEventCalving).Error; err != nil {
  90. return xerr.Custom("该母牛信息不存在")
  91. }
  92. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  93. // 更新产犊事件表
  94. newEventCalving.EventUpdate(operationUser, userModel.SystemUser, req, cow)
  95. if err = tx.Model(new(model.EventCalving)).
  96. Select("operation_id", "operation_name", "message_id", "message_name", "reality_day", "day_age", "pregnancy_age",
  97. "bull_number", "calving_level", "child_number", "status", "is_inducing_childbirth", "pen_id", "dystocia_reason", "remarks").
  98. Where("id = ?", newEventCalving.Id).
  99. Updates(newEventCalving).Error; err != nil {
  100. return xerr.WithStack(err)
  101. }
  102. for _, calf := range req.CalfItemList {
  103. // 犊牛信息
  104. newCalvingCalf := model.NewEventCalvingCalf(userModel.AppPasture.Id, newEventCalving.Id, int64(req.CalvingAt), cow, penMap, calf)
  105. if err = tx.Create(newCalvingCalf).Error; err != nil {
  106. return xerr.WithStack(err)
  107. }
  108. if calf.IsLive == pasturePb.IsShow_No || calf.IsAdoption == pasturePb.IsShow_No {
  109. continue
  110. }
  111. newCalfCow := model.NewCalfCow(cow, newCalvingCalf)
  112. if err = tx.Create(newCalfCow).Error; err != nil {
  113. return xerr.WithStack(err)
  114. }
  115. if err = tx.Model(new(model.CalvingCalf)).Where("id = ?", newCalvingCalf.Id).Update("cow_id", newCalfCow.Id).Error; err != nil {
  116. return xerr.WithStack(err)
  117. }
  118. // 犊牛日志
  119. cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, newCalfCow, pasturePb.EventType_Birth, calf)
  120. if err = tx.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
  121. return xerr.WithStack(err)
  122. }
  123. }
  124. // 更新母牛信息
  125. cow.EventCalvingUpdate(int64(req.CalvingAt))
  126. if err = tx.Model(cow).
  127. Select("calving_age", "mating_times", "pregnancy_age", "lact", "breed_status", "is_pregnant", "last_calving_at").
  128. Where("id = ?", cow.Id).
  129. Updates(cow).Error; err != nil {
  130. return xerr.WithStack(err)
  131. }
  132. // 母牛事件日志
  133. cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cow, pasturePb.EventType_Calving, req)
  134. if err = tx.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
  135. return xerr.WithStack(err)
  136. }
  137. return nil
  138. }); err != nil {
  139. return xerr.WithStack(err)
  140. }
  141. return nil
  142. }
  143. func (s *StoreEntry) SameTimeCreate(ctx context.Context, req *pasturePb.EventSameTime) (err error) {
  144. userModel, err := s.GetUserModel(ctx)
  145. if err != nil {
  146. return xerr.WithStack(err)
  147. }
  148. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  149. if err != nil {
  150. return xerr.WithStack(err)
  151. }
  152. req.OperationName = operationUser.Name
  153. eventCowSameTime, err := s.GetEventCowSameTimeByCowId(ctx, userModel.AppPasture.Id, int64(req.CowId))
  154. if err != nil {
  155. zaplog.Error("SameTimeCreate", zap.Any("err", err), zap.Any("req", req))
  156. return xerr.Customf("异常数据")
  157. }
  158. drugs := &model.Drugs{}
  159. if drugs, err = s.GetDrugsById(ctx, userModel.AppPasture.Id, int64(req.DrugsId)); err != nil {
  160. zaplog.Error("SameTimeCreate", zap.Any("err", err), zap.Any("req", req))
  161. return xerr.Customf("该药品不存在: %d", req.DrugsId)
  162. }
  163. req.DrugsName = drugs.Name
  164. eventCowSameTime.EventUpdate(drugs, req.Usage, req.Remarks, operationUser, userModel.SystemUser)
  165. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  166. if err = tx.Model(new(model.EventCowSameTime)).
  167. Select("status", "drugs_id", "unit", "usage", "remarks", "operation_id", "operation_name").
  168. Where("id = ?", eventCowSameTime.Id).
  169. Updates(eventCowSameTime).Error; err != nil {
  170. return xerr.WithStack(err)
  171. }
  172. // 记录牛只事件日志
  173. cow, _ := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, eventCowSameTime.CowId)
  174. cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cow, pasturePb.EventType_Seme_Time, req)
  175. if err = s.DB.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
  176. return xerr.WithStack(err)
  177. }
  178. return nil
  179. }); err != nil {
  180. }
  181. return nil
  182. }
  183. func (s *StoreEntry) SameTimeBatch(ctx context.Context, req *pasturePb.EventSameTimeBatch) (err error) {
  184. userModel, err := s.GetUserModel(ctx)
  185. if err != nil {
  186. return xerr.WithStack(err)
  187. }
  188. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  189. if err != nil {
  190. return xerr.Customf("异常数据")
  191. }
  192. req.OperationName = operationUser.Name
  193. drugs := &model.Drugs{}
  194. if drugs, err = s.GetDrugsById(ctx, userModel.AppPasture.Id, int64(req.DrugsId)); err != nil {
  195. zaplog.Error("SameTimeBatch", zap.Any("err", err), zap.Any("req", req))
  196. return xerr.Customf("该药物不存在: %d", req.DrugsId)
  197. }
  198. req.DrugsName = drugs.Name
  199. nowTime := time.Now().Format(model.LayoutDate2)
  200. eventCowSameTimeList := make([]*model.EventCowSameTime, 0)
  201. for _, v := range req.CowIds {
  202. eventCowSameTime, err := s.GetEventCowSameTimeByCowId(ctx, userModel.AppPasture.Id, int64(v))
  203. if err != nil {
  204. zaplog.Error("SameTimeCreate", zap.Any("err", err), zap.Any("req", req))
  205. return xerr.WithStack(err)
  206. }
  207. if time.Unix(eventCowSameTime.PlanDay, 0).Format(model.LayoutDate2) != nowTime {
  208. return xerr.Customf("该牛只不是今日计划: %d", eventCowSameTime.EarNumber)
  209. }
  210. eventCowSameTimeList = append(eventCowSameTimeList, eventCowSameTime)
  211. }
  212. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  213. for _, v := range eventCowSameTimeList {
  214. // 更新SameTime
  215. v.EventUpdate(drugs, req.Usage, req.Remarks, userModel.SystemUser, operationUser)
  216. if err = tx.Model(new(model.EventCowSameTime)).
  217. Select("status", "drugs_id", "unit", "usage", "remarks", "operation_id", "operation_name").
  218. Where("id = ?", v.Id).
  219. Updates(v).Error; err != nil {
  220. return xerr.WithStack(err)
  221. }
  222. cow, _ := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, v.CowId)
  223. logItem := &pasturePb.EventSameTime{
  224. Id: int32(v.Id),
  225. CowId: int32(v.CowId),
  226. SameTimeId: int32(v.SameTimeId),
  227. SameTimeType: v.SameTimeType,
  228. SameTimeTypeName: v.SameTimeName,
  229. DrugsId: int32(v.DrugsId),
  230. DrugsName: drugs.Name,
  231. Usage: req.Usage,
  232. OperationId: int32(operationUser.Id),
  233. OperationName: operationUser.Name,
  234. SameTimeAt: req.SameTimeAt,
  235. }
  236. cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cow, pasturePb.EventType_Seme_Time, logItem)
  237. if err = tx.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
  238. return xerr.WithStack(err)
  239. }
  240. }
  241. return nil
  242. }); err != nil {
  243. return xerr.WithStack(err)
  244. }
  245. return nil
  246. }
  247. func (s *StoreEntry) SameTimeList(
  248. ctx context.Context,
  249. req *pasturePb.SearchEventRequest,
  250. pagination *pasturePb.PaginationModel,
  251. ) (*pasturePb.SearchSameTimeResponse, error) {
  252. userModel, err := s.GetUserModel(ctx)
  253. if err != nil {
  254. return nil, xerr.WithStack(err)
  255. }
  256. cowSameTimeList := make([]*model.EventCowSameTime, 0)
  257. var count int64 = 0
  258. pref := s.DB.Table(new(model.EventCowSameTime).TableName()).Where("pasture_id = ?", userModel.AppPasture.Id)
  259. if req.CowId != "" {
  260. cowIds := strings.Split(req.CowId, ",")
  261. pref.Where("cow_id IN ?", cowIds)
  262. }
  263. if req.EarNumber != "" {
  264. pref.Where("ear_number = ?", req.EarNumber)
  265. }
  266. if err = pref.Count(&count).
  267. Limit(int(pagination.PageSize)).
  268. Offset(int(pagination.PageOffset)).
  269. Find(&cowSameTimeList).Error; err != nil {
  270. return nil, xerr.WithStack(err)
  271. }
  272. sameTimeTypeMap := s.SameTimeTypeMap()
  273. return &pasturePb.SearchSameTimeResponse{
  274. Code: http.StatusOK,
  275. Msg: "ok",
  276. Data: &pasturePb.SameTimeData{
  277. List: model.EventCowSameTimeSlice(cowSameTimeList).ToPB(sameTimeTypeMap),
  278. Total: int32(count),
  279. PageSize: pagination.PageSize,
  280. Page: pagination.Page,
  281. },
  282. }, nil
  283. }
  284. func (s *StoreEntry) EstrusList(ctx context.Context, req *pasturePb.EstrusItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchEventEstrusResponse, error) {
  285. userModel, err := s.GetUserModel(ctx)
  286. if err != nil {
  287. return nil, xerr.WithStack(err)
  288. }
  289. estrusList := make([]*model.EventEstrus, 0)
  290. var count int64 = 0
  291. pref := s.DB.Table(new(model.EventEstrus).TableName()).
  292. Where("is_show = ?", pasturePb.IsShow_Ok).
  293. Where("pasture_id = ?", userModel.AppPasture.Id).
  294. Where("expose_estrus_type = ?", pasturePb.ExposeEstrusType_Natural_Estrus)
  295. if len(req.EarNumbers) > 0 {
  296. earNumberString := ""
  297. for _, e := range req.EarNumbers {
  298. earNumberString += fmt.Sprintf("%s,", e)
  299. }
  300. pref.Where("ear_number IN (?)", strings.TrimRight(earNumberString, ","))
  301. }
  302. if req.EarNumber != "" {
  303. pref.Where("ear_number = ?", req.EarNumber)
  304. }
  305. if req.Level > 0 {
  306. pref.Where("level = ?", req.Level)
  307. }
  308. if len(req.PenIds) > 0 {
  309. penIds := strings.Split(util.ArrayInt32ToStrings(req.PenIds, ","), ",")
  310. pref.Where("pen_id IN ?", penIds)
  311. }
  312. if err = pref.Order("id desc").
  313. Count(&count).Limit(int(pagination.PageSize)).
  314. Offset(int(pagination.PageOffset)).
  315. Find(&estrusList).Error; err != nil {
  316. return nil, xerr.WithStack(err)
  317. }
  318. return &pasturePb.SearchEventEstrusResponse{
  319. Code: http.StatusOK,
  320. Msg: "ok",
  321. Data: &pasturePb.SearchEventEstrusData{
  322. List: model.EventEstrusSlice(estrusList).ToPB(),
  323. Total: int32(count),
  324. PageSize: pagination.PageSize,
  325. Page: pagination.Page,
  326. },
  327. }, nil
  328. }
  329. func (s *StoreEntry) EstrusBatchMating(ctx context.Context, req *pasturePb.EventNaturalEstrusBatch) (err error) {
  330. userModel, err := s.GetUserModel(ctx)
  331. if err != nil {
  332. return xerr.WithStack(err)
  333. }
  334. eventEstrusList, eventMatingList, err := s.EstrusCheckDataCheck(ctx, userModel, req.Items)
  335. if err != nil {
  336. return xerr.WithStack(err)
  337. }
  338. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  339. if len(eventMatingList) > 0 {
  340. if err = tx.Model(new(model.EventMating)).Create(eventMatingList).Error; err != nil {
  341. return xerr.WithStack(err)
  342. }
  343. }
  344. if len(eventEstrusList) > 0 {
  345. if err = tx.Model(new(model.EventEstrus)).Create(eventEstrusList).Error; err != nil {
  346. return xerr.WithStack(err)
  347. }
  348. }
  349. // 记录事件日志
  350. for _, v := range eventEstrusList {
  351. cow, _ := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, v.CowId)
  352. // 更新牛最近发情时间
  353. cow.EstrusUpdate(v.RealityDay)
  354. if err = tx.Model(cow).
  355. Select("last_estrus_at").
  356. Where("id = ?", cow.Id).
  357. Updates(cow).Error; err != nil {
  358. return xerr.WithStack(err)
  359. }
  360. cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cow, pasturePb.EventType_Estrus, v)
  361. if err = tx.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
  362. return xerr.WithStack(err)
  363. }
  364. }
  365. return nil
  366. }); err != nil {
  367. return xerr.WithStack(err)
  368. }
  369. return nil
  370. }