event_base.go 11 KB

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