event_base.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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([]*pasturePb.EventEnterData, 0)
  36. var count int64 = 0
  37. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventEnter).TableName())).
  38. Select(`a.id,a.birth_at,a.weaning_at,a.dry_milk_at,a.cow_source_id,a.remarks,a.mating_at,a.lact,
  39. a.breed_status,a.mother_number as ,a.cow_kind,a.cow_id,a.ear_number,a.sex,a.created_at,a.updated_at,a.enter_at,
  40. a.weight / 100 as weight,a.price / 100 as price,b.name as breed_status_name,c.name as cow_source_name,d.name as cow_type_name,
  41. e.name as cow_kind_name,f.name as pen_name,g.name as operation_name,h.name as staff_member_name`).
  42. Joins(fmt.Sprintf("JOIN %s AS b ON a.breed_status = b.id", new(model.ConfigBreedStatus).TableName())).
  43. Joins(fmt.Sprintf("JOIN %s AS c on a.cow_source_id = c.id", new(model.ConfigCowSource).TableName())).
  44. Joins(fmt.Sprintf("JOIN %s AS d on a.cow_type = d.id", new(model.ConfigCowType).TableName())).
  45. Joins(fmt.Sprintf("JOIN %s AS e ON a.cow_kind = e.id", new(model.ConfigCowKind).TableName())).
  46. Joins(fmt.Sprintf("JOIN %s AS f on a.pen_id = f.id", new(model.Pen).TableName())).
  47. Joins(fmt.Sprintf("JOIN %s AS g on a.operation_id = g.id", new(model.SystemUser).TableName())).
  48. Joins(fmt.Sprintf("JOIN %s AS h on a.staff_member_id = h.id", new(model.SystemUser).TableName()))
  49. if len(req.CowId) > 0 {
  50. cowIds := strings.Split(req.CowId, ",")
  51. pref.Where("a.cow_id IN ?", cowIds)
  52. }
  53. if err := pref.Order("a.id desc").
  54. Count(&count).Limit(int(pagination.PageSize)).
  55. Offset(int(pagination.PageOffset)).
  56. Find(&eventEnterList).Error; err != nil {
  57. return nil, xerr.WithStack(err)
  58. }
  59. return &pasturePb.SearchEnterEventResponse{
  60. Code: http.StatusOK,
  61. Message: "ok",
  62. Data: &pasturePb.SearchEnterEventData{
  63. List: eventEnterList,
  64. Total: int32(count),
  65. PageSize: pagination.PageSize,
  66. Page: pagination.Page,
  67. },
  68. }, nil
  69. }
  70. func (s *StoreEntry) CreateEnter(ctx context.Context, req *pasturePb.EventEnterData) error {
  71. if err := s.DB.Transaction(func(tx *gorm.DB) error {
  72. newCowData := model.NewCow(req)
  73. if err := tx.Create(newCowData).Error; err != nil {
  74. return xerr.WithStack(err)
  75. }
  76. operationUser, _ := s.GetCurrentSystemUser(ctx)
  77. if err := tx.Create(model.NewEventEnter(newCowData.Id, operationUser.Id, req)).Error; err != nil {
  78. return xerr.WithStack(err)
  79. }
  80. return nil
  81. }); err != nil {
  82. return xerr.WithStack(err)
  83. }
  84. return nil
  85. }
  86. func (s *StoreEntry) GroupTransferList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchTransferGroupEventResponse, error) {
  87. eventGroupTransferList := make([]*pasturePb.EventTransferGroupData, 0)
  88. var count int64 = 0
  89. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventTransferGroup).TableName())).
  90. Select(`a.id,a.cow_id,a.transfer_in_pen_id,a.transfer_out_pen_id,a.lact,a.remarks,a.transfer_reason_id,
  91. a.transfer_date,a.created_at,a.staff_member_id,b.name as transfer_in_pen_name,c.name as transfer_out_pen_name,
  92. d.name as transfer_reason_name,e.name as staff_member_name,f.lact,f.ear_number`).
  93. Joins(fmt.Sprintf("JOIN %s AS b ON a.transfer_in_pen_id = b.id", new(model.Pen).TableName())).
  94. Joins(fmt.Sprintf("JOIN %s AS c on a.transfer_out_pen_id = c.id", new(model.Pen).TableName())).
  95. Joins(fmt.Sprintf("JOIN %s AS d on a.transfer_reason_id = d.id", new(model.ConfigTransferPenReason).TableName())).
  96. Joins(fmt.Sprintf("JOIN %s AS e ON a.staff_member_id = e.id", new(model.SystemUser).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. system, _ := s.GetCurrentSystemUser(ctx)
  121. res := make([]*model.EventTransferGroup, 0)
  122. for _, v := range req.Body {
  123. cow, err := s.GetCowInfoByCowId(ctx, int64(v.CowId))
  124. if err != nil {
  125. return xerr.WithStack(err)
  126. }
  127. // 转去栏舍和当前栏舍相同,则不处理
  128. if cow.PenId == v.TransferInPenId {
  129. continue
  130. }
  131. pen, err := s.GetPenById(ctx, int64(v.TransferInPenId))
  132. if err != nil {
  133. return xerr.WithStack(err)
  134. }
  135. transferReasonData, err := s.GetTransferReasonInfo(ctx, int64(v.TransferReasonId))
  136. if err != nil {
  137. return xerr.WithStack(err)
  138. }
  139. staffMemberData, err := s.GetSystemUserById(ctx, int64(v.StaffMemberId))
  140. if err != nil {
  141. return xerr.WithStack(err)
  142. }
  143. res = append(res, &model.EventTransferGroup{
  144. CowId: int64(v.CowId),
  145. TransferInPenId: int64(v.TransferInPenId),
  146. TransferOutPenId: pen.Id,
  147. Lact: cow.Lact,
  148. DayAge: cow.GetDayAge(),
  149. TransferDate: v.TransferDate,
  150. TransferReasonId: transferReasonData.Id,
  151. Remarks: v.Remarks,
  152. StaffMemberId: staffMemberData.Id,
  153. OperationId: system.Id,
  154. })
  155. }
  156. if len(res) <= 0 {
  157. return nil
  158. }
  159. if err := s.DB.Transaction(func(tx *gorm.DB) error {
  160. if err := tx.Create(res).Error; err != nil {
  161. return xerr.WithStack(err)
  162. }
  163. for _, v := range res {
  164. cow, err := s.GetCowInfoByCowId(ctx, v.CowId)
  165. if err != nil {
  166. return xerr.WithStack(err)
  167. }
  168. if err = s.DB.Model(cow).Update("pen_id", v.TransferInPenId).Error; err != nil {
  169. return xerr.WithStack(err)
  170. }
  171. }
  172. return nil
  173. }); err != nil {
  174. return xerr.WithStack(err)
  175. }
  176. return nil
  177. }
  178. func (s *StoreEntry) BodyScoreList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBodyScoreEventResponse, error) {
  179. bodyScoreList := make([]*pasturePb.BodyScoreList, 0)
  180. var count int64 = 0
  181. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventBodyScore).TableName())).
  182. 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,
  183. a.updated_at,a.staff_member_id,a.operation_id,b.name as staff_member_name,c.name as operation_name`).
  184. Joins(fmt.Sprintf("JOIN %s AS b on a.staff_member_id = b.id", new(model.SystemUser).TableName())).
  185. Joins(fmt.Sprintf("JOIN %s AS c on a.operation_id = c.id", new(model.SystemUser).TableName()))
  186. if len(req.CowId) > 0 {
  187. cowIds := strings.Split(req.CowId, ",")
  188. pref.Where("a.cow_id IN ?", cowIds)
  189. }
  190. if err := pref.Order("a.id desc").
  191. Count(&count).Limit(int(pagination.PageSize)).
  192. Offset(int(pagination.PageOffset)).
  193. Find(&bodyScoreList).Error; err != nil {
  194. return nil, xerr.WithStack(err)
  195. }
  196. return &pasturePb.SearchBodyScoreEventResponse{
  197. Code: http.StatusOK,
  198. Message: "ok",
  199. Data: &pasturePb.SearchBodyScoreData{
  200. List: bodyScoreList,
  201. Total: int32(count),
  202. PageSize: pagination.PageSize,
  203. Page: pagination.Page,
  204. },
  205. }, nil
  206. }
  207. func (s *StoreEntry) CreateBodyScore(ctx context.Context, req *pasturePb.BodyScoreEventRequest) error {
  208. if len(req.CowId) <= 0 {
  209. return xerr.Custom("请选择相关牛只")
  210. }
  211. currentSystemUser, _ := s.GetCurrentSystemUser(ctx)
  212. //cowList := strings.Split(req.CowId, ",")
  213. bodyScourEvent := make([]*model.EventBodyScore, 0)
  214. cowList, err := s.ParseCowIds(ctx, req.CowId)
  215. if err != nil {
  216. return xerr.WithStack(err)
  217. }
  218. for _, cow := range cowList {
  219. bodyScourEvent = append(bodyScourEvent, model.NewEventBodyScore(cow, currentSystemUser.Id, req))
  220. }
  221. if len(bodyScourEvent) <= 0 {
  222. return nil
  223. }
  224. return s.DB.Create(bodyScourEvent).Error
  225. }
  226. func (s *StoreEntry) WeightList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchWeightEventResponse, error) {
  227. weightList := make([]*pasturePb.SearchWeightList, 0)
  228. var count int64 = 0
  229. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventWeight).TableName())).
  230. 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,
  231. a.updated_at,a.staff_member_id,a.operation_id,b.name as staff_member_name,c.name as operation_name`).
  232. Joins(fmt.Sprintf("JOIN %s AS b on a.staff_member_id = b.id", new(model.SystemUser).TableName())).
  233. Joins(fmt.Sprintf("JOIN %s AS c on a.operation_id = c.id", new(model.SystemUser).TableName()))
  234. if len(req.CowId) > 0 {
  235. cowIds := strings.Split(req.CowId, ",")
  236. pref.Where("a.cow_id IN ?", cowIds)
  237. }
  238. if err := pref.Order("a.id desc").
  239. Count(&count).Limit(int(pagination.PageSize)).
  240. Offset(int(pagination.PageOffset)).
  241. Find(&weightList).Error; err != nil {
  242. return nil, xerr.WithStack(err)
  243. }
  244. return &pasturePb.SearchWeightEventResponse{
  245. Code: http.StatusOK,
  246. Message: "ok",
  247. Data: &pasturePb.SearchWeightData{
  248. List: weightList,
  249. Total: int32(count),
  250. PageSize: pagination.PageSize,
  251. Page: pagination.Page,
  252. },
  253. }, nil
  254. }
  255. func (s *StoreEntry) WeightCreate(ctx context.Context, req *pasturePb.EventWeight) error {
  256. if len(req.CowId) <= 0 {
  257. return xerr.Custom("请选择相关牛只")
  258. }
  259. currentSystemUser, _ := s.GetCurrentSystemUser(ctx)
  260. weightEvent := make([]*model.EventWeight, 0)
  261. for _, item := range req.WeightItems {
  262. cow, err := s.GetCowInfoByCowId(ctx, int64(item.CowId))
  263. if err != nil {
  264. return xerr.WithStack(err)
  265. }
  266. var weight = int32(item.Weight * 100)
  267. weightEvent = append(weightEvent, model.NewEventWeight(cow, currentSystemUser, weight, req))
  268. }
  269. if len(weightEvent) <= 0 {
  270. return nil
  271. }
  272. if err := s.DB.Transaction(func(tx *gorm.DB) error {
  273. for _, item := range weightEvent {
  274. if err := tx.Model(new(model.Cow)).Where("id = ?", item.CowId).Updates(map[string]interface{}{
  275. "last_weight_at": item.WeightAt,
  276. "current_weight": item.Weight,
  277. }).Error; err != nil {
  278. return xerr.WithStack(err)
  279. }
  280. }
  281. if err := tx.Create(weightEvent).Error; err != nil {
  282. return xerr.WithStack(err)
  283. }
  284. return nil
  285. }); err != nil {
  286. return xerr.WithStack(err)
  287. }
  288. return nil
  289. }