瀏覽代碼

event: immuization 免疫事件

ping 3 月之前
父節點
當前提交
02c3c62ec8
共有 5 個文件被更改,包括 162 次插入8 次删除
  1. 1 1
      go.mod
  2. 4 0
      go.sum
  3. 11 0
      http/handler/event/event_health.go
  4. 50 6
      model/event_immunization_plan.go
  5. 96 1
      module/backend/event_base_more.go

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20250224101733-fe1933dd5c95
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20250224130629-8acdbff851e5
 	gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/eclipse/paho.mqtt.golang v1.4.3

+ 4 - 0
go.sum

@@ -242,6 +242,10 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20250224100909-32e2afe20cd1 h1:shMT9Jud
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250224100909-32e2afe20cd1/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250224101733-fe1933dd5c95 h1:rFUrO43EQWO8sCPkN0CEvxjpG3Zz2AiAin+ihdDlw7s=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250224101733-fe1933dd5c95/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250224130350-5bbe5630870a h1:wG/q1duur2eslYXjQqoR83EyN1x9cKvkP97+fDxXjBQ=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250224130350-5bbe5630870a/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250224130629-8acdbff851e5 h1:r11lo90UNSksUGhfhpxzOva2g91z9fFXk9TNhHBFvy8=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250224130629-8acdbff851e5/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b h1:w05MxH7yqveRlaRbxHhbif5YjPrJFodRPfOjYhXn7Zk=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b/go.mod h1:8tF25X6pE9WkFCczlNAC0K2mrjwKvhhp02I7o0HtDxY=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

+ 11 - 0
http/handler/event/event_health.go

@@ -227,6 +227,17 @@ func ImmunizationBatch(c *gin.Context) {
 		return
 	}
 
+	if err := valid.ValidateStruct(&req,
+		valid.Field(&req.PlanId, valid.Required),
+		valid.Field(&req.OperationId, valid.Required),
+		valid.Field(&req.CowIds, valid.Required),
+		valid.Field(&req.ImmunizationAt, valid.Required),
+		valid.Field(&req.Usage, valid.Required),
+	); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
 	if err := middleware.Dependency(c).StoreEventHub.OpsService.ImmunizationBatch(c, &req); err != nil {
 		apierr.ClassifiedAbort(c, err)
 		return

+ 50 - 6
model/event_immunization_plan.go

@@ -24,22 +24,42 @@ type EventImmunizationPlan struct {
 	RealityDay    int64                  `json:"realityDay"`
 	EndDay        int64                  `json:"endDay"`
 	Status        pasturePb.IsShow_Kind  `json:"status"`
-	OperationId   int64                  `json:"operationId"`
-	OperationName string                 `json:"operationName"`
 	DrugsId       int64                  `json:"drugsId"`
 	DrugsName     string                 `json:"drugsName"`
 	Unit          pasturePb.Unit_Kind    `json:"unit"`
 	UnitName      string                 `json:"unitName"`
 	Usage         int32                  `json:"usage"`
 	Remarks       string                 `json:"remarks"`
+	OperationId   int64                  `json:"operationId"`
+	OperationName string                 `json:"operationName"`
+	MessageId     int64                  `json:"messageId"`
+	MessageName   string                 `json:"messageName"`
 	CreatedAt     int64                  `json:"createdAt"`
 	UpdatedAt     int64                  `json:"updatedAt"`
 }
 
-func (c *EventImmunizationPlan) TableName() string {
+func (e *EventImmunizationPlan) TableName() string {
 	return "event_immunization_plan"
 }
 
+func (e *EventImmunizationPlan) EventUpdate(immunizationAt int64, cowInfo *Cow, drugs *Drugs, usage int32, operationUser, currUser *SystemUser, remark string) {
+	e.OperationId = operationUser.Id
+	e.OperationName = operationUser.Name
+	e.MessageId = currUser.Id
+	e.MessageName = currUser.Name
+	e.RealityDay = immunizationAt
+	e.Remarks = remark
+	e.Lact = cowInfo.Lact
+	e.DayAge = cowInfo.DayAge
+	e.EarNumber = cowInfo.EarNumber
+	e.Status = pasturePb.IsShow_Ok
+	e.DrugsId = drugs.Id
+	e.DrugsName = drugs.Name
+	e.Unit = drugs.Unit
+	e.UnitName = drugs.UnitName
+	e.Usage = usage
+}
+
 func NewEventImmunizationPlan(cow *Cow, immunizationPlan *ImmunizationPlan) *EventImmunizationPlan {
 	todayTime := time.Now().Format(LayoutDate2)
 	todayStartTime := util.TimeParseLocalUnix(todayTime)
@@ -72,9 +92,9 @@ func NewCowImmunizationPlanList(cowList []*Cow, immunizationPlan *ImmunizationPl
 
 type EventImmunizationPlanSlice []*EventImmunizationPlan
 
-func (I EventImmunizationPlanSlice) ToPB() []*pasturePb.ImmunizationItems {
-	res := make([]*pasturePb.ImmunizationItems, len(I))
-	for i, v := range I {
+func (e EventImmunizationPlanSlice) ToPB() []*pasturePb.ImmunizationItems {
+	res := make([]*pasturePb.ImmunizationItems, len(e))
+	for i, v := range e {
 		planDay := ""
 		if v.PlanDay > 0 {
 			planDay = time.Unix(v.PlanDay, 0).Format(LayoutDate2)
@@ -94,3 +114,27 @@ func (I EventImmunizationPlanSlice) ToPB() []*pasturePb.ImmunizationItems {
 	}
 	return res
 }
+
+func (e EventImmunizationPlanSlice) ToPB2() []*pasturePb.ImmunizationItem {
+	res := make([]*pasturePb.ImmunizationItem, len(e))
+	for i, v := range e {
+		res[i] = &pasturePb.ImmunizationItem{
+			Id:             int32(v.Id),
+			ImmunizationAt: int32(v.RealityDay),
+			OperationId:    int32(v.OperationId),
+			OperationName:  v.OperationName,
+			Remarks:        v.Remarks,
+			CowIds:         make([]int32, 0),
+			DrugsId:        int32(v.DrugsId),
+			DrugsName:      v.DrugsName,
+			Unit:           v.Unit,
+			UnitName:       v.UnitName,
+			Usage:          v.Usage,
+			PlanId:         int32(v.PlanId),
+			PlanName:       v.PlanName,
+			CowId:          int32(v.CowId),
+			EarNumber:      v.EarNumber,
+		}
+	}
+	return res
+}

+ 96 - 1
module/backend/event_base_more.go

@@ -247,13 +247,108 @@ func (s *StoreEntry) CowSaleList(ctx context.Context, req *pasturePb.EventCowSal
 }
 
 func (s *StoreEntry) ImmunizationList(ctx context.Context, req *pasturePb.SearchEventImmunizationRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchEventImmunizationResponse, error) {
+	userModel, err := s.GetUserModel(ctx)
+	if err != nil {
+		return nil, xerr.WithStack(err)
+	}
+
+	eventImmunizationList := make([]*model.EventImmunizationPlan, 0)
+	var count int64
+	pref := s.DB.Model(new(model.EventImmunizationPlan)).
+		Where("pasture_id = ?", userModel.AppPasture.Id).
+		Where("status = ?", pasturePb.IsShow_Ok)
+
+	if req.StartDayAt > 0 && req.EndDayAt > 0 && req.StartDayAt <= req.EndDayAt {
+		pref.Where("reality_day BETWEEN ? AND ?", req.StartDayAt, req.EndDayAt)
+	}
+
+	if len(req.CowIds) > 0 {
+		pref.Where("cow_id IN (?)", req.CowIds)
+	}
+
+	if err = pref.Order("plan_id ASC,reality_day DESC").
+		Count(&count).Limit(int(pagination.PageSize)).
+		Offset(int(pagination.PageOffset)).
+		Find(&eventImmunizationList).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+
 	return &pasturePb.SearchEventImmunizationResponse{
 		Code: http.StatusOK,
 		Msg:  "ok",
-		Data: nil,
+		Data: &pasturePb.EventImmunizationData{
+			List:     model.EventImmunizationPlanSlice(eventImmunizationList).ToPB2(),
+			Total:    int32(count),
+			PageSize: pagination.PageSize,
+			Page:     pagination.Page,
+		},
 	}, nil
 }
 
+// ImmunizationBatch 只能提交同一个免疫计划类型的数据
 func (s *StoreEntry) ImmunizationBatch(ctx context.Context, req *pasturePb.ImmunizationItem) error {
+	userModel, err := s.GetUserModel(ctx)
+	if err != nil {
+		return xerr.WithStack(err)
+	}
+
+	if len(req.CowIds) <= 0 {
+		return xerr.Custom("请选择相关牛只数据")
+	}
+
+	cowIds := make([]int64, len(req.CowIds))
+	for i, v := range req.CowIds {
+		cowIds[i] = int64(v)
+	}
+
+	eventImmunizationList := make([]*model.EventImmunizationPlan, 0)
+	if err = s.DB.Model(new(model.EventImmunizationPlan)).
+		Where("pasture_id = ?", userModel.AppPasture.Id).
+		Where("plan_id = ?", req.PlanId).
+		Where("cow_id IN (?)", cowIds).
+		Find(&eventImmunizationList).Error; err != nil {
+		return xerr.WithStack(err)
+	}
+
+	if len(eventImmunizationList) != len(req.CowIds) {
+		return xerr.Custom("数据异常")
+	}
+
+	drugsInfo := &model.Drugs{}
+	if err = s.DB.Model(new(model.Drugs)).
+		Where("pasture_id = ?", userModel.AppPasture.Id).
+		Where("id = ?", req.DrugsId).
+		First(drugsInfo).Error; err != nil {
+		return xerr.WithStack(err)
+	}
+
+	operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
+	if err != nil {
+		return xerr.WithStack(err)
+	}
+
+	if err = s.DB.Transaction(func(tx *gorm.DB) error {
+		for _, eventImmunization := range eventImmunizationList {
+			cowInfo, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, eventImmunization.CowId)
+			if err != nil {
+				return xerr.WithStack(err)
+			}
+
+			eventImmunization.EventUpdate(int64(req.ImmunizationAt), cowInfo, drugsInfo, req.Usage, operationUser, userModel.SystemUser, req.Remarks)
+			if err = tx.Model(new(model.EventImmunizationPlan)).
+				Select("ear_number", "lact", "day_age", "status", "operation_id", "operation_name", "message_id",
+					"message_name", "drugs_id", "drugs_name", "usage", "unit", "unit_name", "reality_day", "remarks").
+				Where("id = ?", eventImmunization.Id).
+				Updates(eventImmunization).Error; err != nil {
+				return xerr.WithStack(err)
+			}
+
+		}
+
+		return nil
+	}); err != nil {
+		return xerr.WithStack(err)
+	}
+
 	return nil
 }