event_base.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  10. "gitee.com/xuyiping_admin/pkg/xerr"
  11. "gorm.io/gorm"
  12. )
  13. func (s *StoreEntry) ParseCowIds(ctx context.Context, cowIds string) ([]*model.Cow, error) {
  14. if len(cowIds) == 0 {
  15. return nil, xerr.Custom("cow id is required")
  16. }
  17. cowIdStr := strings.Split(cowIds, ",")
  18. var cowIdInts = make([]int64, 0)
  19. for _, v := range cowIdStr {
  20. cowId, err := strconv.ParseInt(v, 10, 64)
  21. if err != nil {
  22. return nil, xerr.Customf("错误的牛号: %s", v)
  23. }
  24. cowIdInts = append(cowIdInts, cowId)
  25. }
  26. cowList, err := s.GetCowInfoByCowIds(ctx, cowIdInts)
  27. if err != nil {
  28. return nil, xerr.WithStack(err)
  29. }
  30. return cowList, nil
  31. }
  32. func (s *StoreEntry) EnterList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchEnterEventResponse, error) {
  33. eventEnterList := make([]*model.EventEnter, 0)
  34. var count int64 = 0
  35. pref := s.DB.Model(new(model.EventEnter))
  36. if len(req.CowId) > 0 {
  37. cowIds := strings.Split(req.CowId, ",")
  38. pref.Where("cow_id IN ?", cowIds)
  39. }
  40. if err := pref.Order("id desc").
  41. Count(&count).Limit(int(pagination.PageSize)).
  42. Offset(int(pagination.PageOffset)).
  43. Find(&eventEnterList).Error; err != nil {
  44. return nil, xerr.WithStack(err)
  45. }
  46. penMap := s.PenMap(ctx)
  47. breedStatusMap := s.CowBreedStatusMap()
  48. cowSourceMap := s.CowSourceMap()
  49. cowTypeMap := s.CowTypeMap()
  50. cowKindMap := s.CowKindMap()
  51. return &pasturePb.SearchEnterEventResponse{
  52. Code: http.StatusOK,
  53. Message: "ok",
  54. Data: &pasturePb.SearchEnterEventData{
  55. List: model.EventEnterSlice(eventEnterList).ToPB(penMap, breedStatusMap, cowSourceMap, cowTypeMap, cowKindMap),
  56. Total: int32(count),
  57. PageSize: pagination.PageSize,
  58. Page: pagination.Page,
  59. },
  60. }, nil
  61. }
  62. func (s *StoreEntry) CreateEnter(ctx context.Context, req *pasturePb.EventEnterRequest) error {
  63. currentUser, err := s.GetCurrentSystemUser(ctx)
  64. if err != nil || currentUser.Id <= 0 {
  65. return xerr.Customf("当前用户信息错误")
  66. }
  67. req.MessengerId = int32(currentUser.Id)
  68. req.MessengerName = currentUser.Name
  69. if req.OperationId > 0 {
  70. systemUser, _ := s.GetSystemUserById(ctx, int64(req.OperationId))
  71. req.OperationName = systemUser.Name
  72. }
  73. newCowData := model.NewCow(req)
  74. newEventEnter := model.NewEventEnter(newCowData.Id, req)
  75. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  76. if err = tx.Create(newCowData).Error; err != nil {
  77. return xerr.WithStack(err)
  78. }
  79. if err = tx.Create(newEventEnter).Error; err != nil {
  80. return xerr.WithStack(err)
  81. }
  82. return nil
  83. }); err != nil {
  84. return xerr.WithStack(err)
  85. }
  86. return nil
  87. }
  88. func (s *StoreEntry) GroupTransferList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchTransferGroupEventResponse, error) {
  89. eventGroupTransferList := make([]*pasturePb.EventTransferGroupData, 0)
  90. var count int64 = 0
  91. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventTransferGroup).TableName())).
  92. Select(`a.id,a.cow_id,a.pen_in_id as transfer_in_pen_id,a.pen_out_id as transfer_out_pen_id,a.lact,a.remarks,a.transfer_reason_id,a.transfer_reason_name,
  93. a.transfer_date,a.created_at,a.operation_id,a.operation_name,b.name as transfer_in_pen_name,c.name as transfer_out_pen_name,
  94. f.lact,f.ear_number`).
  95. Joins(fmt.Sprintf("JOIN %s AS b ON a.pen_in_id = b.id", new(model.Pen).TableName())).
  96. Joins(fmt.Sprintf("JOIN %s AS c on a.pen_out_id = c.id", new(model.Pen).TableName())).
  97. Joins(fmt.Sprintf("JOIN %s AS f ON a.cow_id = f.id", new(model.Cow).TableName()))
  98. if len(req.CowId) > 0 {
  99. cowIds := strings.Split(req.CowId, ",")
  100. pref.Where("a.cow_id IN ?", cowIds)
  101. }
  102. if err := pref.Order("a.id desc").Group("a.id").
  103. Count(&count).Limit(int(pagination.PageSize)).
  104. Offset(int(pagination.PageOffset)).
  105. Find(&eventGroupTransferList).Error; err != nil {
  106. return nil, xerr.WithStack(err)
  107. }
  108. return &pasturePb.SearchTransferGroupEventResponse{
  109. Code: http.StatusOK,
  110. Message: "ok",
  111. Data: &pasturePb.SearchTransferGroupEventData{
  112. List: eventGroupTransferList,
  113. Total: int32(count),
  114. PageSize: pagination.PageSize,
  115. Page: pagination.Page,
  116. },
  117. }, nil
  118. }
  119. func (s *StoreEntry) CreateGroupTransfer(ctx context.Context, req *pasturePb.TransferGroupEventRequest) error {
  120. currentUser, err := s.GetCurrentSystemUser(ctx)
  121. if err != nil || currentUser.Id <= 0 {
  122. return xerr.Customf("当前用户信息错误")
  123. }
  124. newEventTransferGroupList := make([]*model.EventTransferGroup, 0)
  125. for _, v := range req.Body {
  126. cow, err := s.GetCowInfoByCowId(ctx, int64(v.CowId))
  127. if err != nil {
  128. return xerr.WithStack(err)
  129. }
  130. // 转去栏舍和当前栏舍相同,则不处理
  131. if cow.PenId == v.TransferInPenId {
  132. continue
  133. }
  134. operationUser, err := s.GetSystemUserById(ctx, int64(v.OperationId))
  135. if err != nil {
  136. return xerr.WithStack(err)
  137. }
  138. newEventTransferGroup := model.NewEventTransferGroup(cow, v, currentUser, operationUser)
  139. newEventTransferGroupList = append(newEventTransferGroupList, newEventTransferGroup)
  140. }
  141. if len(newEventTransferGroupList) <= 0 {
  142. return nil
  143. }
  144. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  145. if err = tx.Create(newEventTransferGroupList).Error; err != nil {
  146. return xerr.WithStack(err)
  147. }
  148. for _, v := range newEventTransferGroupList {
  149. cow, err := s.GetCowInfoByCowId(ctx, v.CowId)
  150. if err != nil {
  151. return xerr.WithStack(err)
  152. }
  153. if err = s.DB.Model(cow).Update("pen_id", v.PenInId).Error; err != nil {
  154. return xerr.WithStack(err)
  155. }
  156. }
  157. return nil
  158. }); err != nil {
  159. return xerr.WithStack(err)
  160. }
  161. return nil
  162. }
  163. func (s *StoreEntry) BodyScoreList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBodyScoreEventResponse, error) {
  164. bodyScoreList := make([]*pasturePb.BodyScoreList, 0)
  165. var count int64 = 0
  166. pref := s.DB.Model(new(model.EventBodyScore)).Select("*,score as body_score")
  167. if len(req.CowId) > 0 {
  168. cowIds := strings.Split(req.CowId, ",")
  169. pref.Where("cow_id IN ?", cowIds)
  170. }
  171. if err := pref.Order("id desc").
  172. Count(&count).Limit(int(pagination.PageSize)).
  173. Offset(int(pagination.PageOffset)).
  174. Find(&bodyScoreList).Error; err != nil {
  175. return nil, xerr.WithStack(err)
  176. }
  177. return &pasturePb.SearchBodyScoreEventResponse{
  178. Code: http.StatusOK,
  179. Message: "ok",
  180. Data: &pasturePb.SearchBodyScoreData{
  181. List: bodyScoreList,
  182. Total: int32(count),
  183. PageSize: pagination.PageSize,
  184. Page: pagination.Page,
  185. },
  186. }, nil
  187. }
  188. func (s *StoreEntry) CreateBodyScore(ctx context.Context, req *pasturePb.BodyScoreEventRequest) error {
  189. if len(req.CowId) <= 0 {
  190. return xerr.Custom("请选择相关牛只")
  191. }
  192. currentSystemUser, err := s.GetCurrentSystemUser(ctx)
  193. if err != nil {
  194. return xerr.Customf("获取当前用户失败: %s", err.Error())
  195. }
  196. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  197. if err != nil {
  198. return xerr.WithStack(err)
  199. }
  200. req.OperationName = operationUser.Name
  201. bodyScourEvent := make([]*model.EventBodyScore, 0)
  202. cowList, err := s.ParseCowIds(ctx, req.CowId)
  203. if err != nil {
  204. return xerr.WithStack(err)
  205. }
  206. for _, cow := range cowList {
  207. bodyScourEvent = append(bodyScourEvent, model.NewEventBodyScore(cow, currentSystemUser, req))
  208. }
  209. if len(bodyScourEvent) <= 0 {
  210. return nil
  211. }
  212. return s.DB.Create(bodyScourEvent).Error
  213. }
  214. func (s *StoreEntry) WeightList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchWeightEventResponse, error) {
  215. weightList := make([]*pasturePb.SearchWeightList, 0)
  216. var count int64 = 0
  217. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventWeight).TableName())).
  218. Select(`a.id,a.cow_id,a.ear_number,a.weight / 100 as weight,a.lact,a.day_age,a.weight_at,a.remarks,a.created_at,
  219. a.updated_at,a.message_id,a.operation_id,a.message_name,a.operation_name`)
  220. if len(req.CowId) > 0 {
  221. cowIds := strings.Split(req.CowId, ",")
  222. pref.Where("a.cow_id IN ?", cowIds)
  223. }
  224. if err := pref.Order("a.id desc").
  225. Count(&count).Limit(int(pagination.PageSize)).
  226. Offset(int(pagination.PageOffset)).
  227. Find(&weightList).Error; err != nil {
  228. return nil, xerr.WithStack(err)
  229. }
  230. return &pasturePb.SearchWeightEventResponse{
  231. Code: http.StatusOK,
  232. Message: "ok",
  233. Data: &pasturePb.SearchWeightData{
  234. List: weightList,
  235. Total: int32(count),
  236. PageSize: pagination.PageSize,
  237. Page: pagination.Page,
  238. },
  239. }, nil
  240. }
  241. func (s *StoreEntry) WeightBatch(ctx context.Context, req *pasturePb.EventWeight) error {
  242. if len(req.WeightItems) <= 0 {
  243. return xerr.Custom("请选择相关牛只")
  244. }
  245. currentUser, err := s.GetCurrentSystemUser(ctx)
  246. if err != nil {
  247. return xerr.Customf("获取当前登录用户失败: %s", err.Error())
  248. }
  249. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  250. if err != nil {
  251. return xerr.WithStack(err)
  252. }
  253. req.OperationName = operationUser.Name
  254. weightEvent := make([]*model.EventWeight, 0)
  255. for _, item := range req.WeightItems {
  256. cow, err := s.GetCowInfoByCowId(ctx, int64(item.CowId))
  257. if err != nil {
  258. return xerr.WithStack(err)
  259. }
  260. var weight = int32(item.Weight * 100)
  261. weightEvent = append(weightEvent, model.NewEventWeight(cow, currentUser, weight, item.Height, req))
  262. }
  263. if len(weightEvent) <= 0 {
  264. return nil
  265. }
  266. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  267. for _, item := range weightEvent {
  268. if err = tx.Model(new(model.Cow)).
  269. Where("id = ?", item.CowId).
  270. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  271. Updates(map[string]interface{}{
  272. "last_second_weight_at": gorm.Expr("last_weight_at"),
  273. "last_second_weight": gorm.Expr("current_weight"),
  274. "last_weight_at": item.WeightAt,
  275. "current_weight": item.Weight,
  276. }).Error; err != nil {
  277. return xerr.WithStack(err)
  278. }
  279. }
  280. if err = tx.Create(weightEvent).Error; err != nil {
  281. return xerr.WithStack(err)
  282. }
  283. return nil
  284. }); err != nil {
  285. return xerr.WithStack(err)
  286. }
  287. return nil
  288. }