event_base.go 10 KB

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