|
@@ -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
|
|
|
}
|