Yi 1 mēnesi atpakaļ
vecāks
revīzija
d8eeffc505

+ 13 - 0
http/handler/test.go

@@ -115,3 +115,16 @@ func NeckRingOriginalAsync(c *gin.Context) {
 		Data: &operationPb.Success{Success: true},
 	})
 }
+
+func PastureInit(c *gin.Context) {
+	if err := middleware.BackendOperation(c).OpsService.PastureInit(c); err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+
+	ginutil.JSONResp(c, &operationPb.CommonOK{
+		Code: http.StatusOK,
+		Msg:  "ok",
+		Data: &operationPb.Success{Success: true},
+	})
+}

+ 1 - 0
http/route/test_api.go

@@ -18,5 +18,6 @@ func TestAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 		testRoute.POST("/cow/pen/update", handler.UpdateCowPen)
 		testRoute.GET("/data/warning", handler.DataWarning)
 		testRoute.GET("/neck_ring_original/sync", handler.NeckRingOriginalAsync)
+		testRoute.GET("/pasture/init", handler.PastureInit)
 	}
 }

+ 20 - 0
model/data_waring.go

@@ -15,6 +15,7 @@ type DataWarning struct {
 	PastureId         int64                          `json:"pastureId"`
 	UserId            int64                          `json:"userId"`
 	Kind              pasturePb.DataWarningType_Kind `json:"kind"`
+	KindName          string                         `json:"kindName"`
 	Name              string                         `json:"name"`
 	Description       string                         `json:"description"`
 	DataValue         string                         `json:"dataValue"`
@@ -29,6 +30,23 @@ func (d *DataWarning) TableName() string {
 	return "data_warning"
 }
 
+func DataWarningInitData(pastureId int64, dataWarningList []*pasturePb.ConfigOptionsList) []*DataWarning {
+	res := make([]*DataWarning, 0)
+	for _, v := range dataWarningList {
+		res = append(res,
+			NewDataWarning(
+				pastureId,
+				DefaultUserId,
+				pasturePb.DataWarningType_Kind(v.Value),
+				pasturePb.IsShow_Ok,
+				&DataWarning{
+					Name:        v.Label,
+					Description: v.Props,
+				}))
+	}
+	return res
+}
+
 func (d *DataWarning) GetWarningColumn() (headers map[string]string, headerSort []string, err error) {
 	switch d.Kind {
 	case pasturePb.DataWarningType_Sale_Standard:
@@ -181,10 +199,12 @@ func NewDataWarningList(pastureId, userId int64, req []*pasturePb.WarningDataSet
 }
 
 func NewDataWarning(pastureId, userId int64, Kind pasturePb.DataWarningType_Kind, isShow pasturePb.IsShow_Kind, defaultDataWarning *DataWarning) *DataWarning {
+	kindName := pasturePb.DataWarningType_Kind_name[int32(Kind)]
 	return &DataWarning{
 		PastureId:   pastureId,
 		UserId:      userId,
 		Kind:        Kind,
+		KindName:    kindName,
 		Name:        defaultDataWarning.Name,
 		Description: defaultDataWarning.Description,
 		IsShow:      isShow,

+ 113 - 0
model/data_warning_items.go

@@ -1,6 +1,8 @@
 package model
 
 import (
+	"fmt"
+
 	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 )
 
@@ -23,6 +25,117 @@ func (d *DataWarningItems) TableName() string {
 	return "data_warning_items"
 }
 
+var DefaultDataWarningItemsMap = map[pasturePb.DataWarningType_Kind][]*pasturePb.WarningDataSet{
+	pasturePb.DataWarningType_Sale_Standard: {
+		{
+			GroupId:   1,
+			FieldName: "admission_age",
+			FieldDesc: "在群天数",
+			Operator:  ">=",
+			Value:     "100",
+			IsShow:    pasturePb.IsShow_Ok,
+		},
+		{
+			GroupId:   1,
+			FieldName: "current_weight",
+			FieldDesc: "体重",
+			Operator:  ">=",
+			Value:     "700",
+			IsShow:    pasturePb.IsShow_Ok,
+		},
+	},
+	pasturePb.DataWarningType_UnPregnant_Mating_Time: {
+		{
+			GroupId:   1,
+			FieldName: "mating_times",
+			FieldDesc: "配次",
+			Operator:  ">",
+			Value:     "2",
+			IsShow:    pasturePb.IsShow_Ok,
+		},
+	},
+	pasturePb.DataWarningType_Abortion_Time: {
+		{
+			GroupId:   1,
+			FieldName: "abortion_times",
+			FieldDesc: "流产次数",
+			Operator:  ">",
+			Value:     "3",
+			IsShow:    pasturePb.IsShow_Ok,
+		},
+	},
+	pasturePb.DataWarningType_Over_Age_UnPaired_Young: {
+		{
+			GroupId:   1,
+			FieldName: "day_age",
+			FieldDesc: "月龄",
+			Operator:  ">=",
+			Value:     "150",
+			IsShow:    pasturePb.IsShow_Ok,
+		},
+	},
+	pasturePb.DataWarningType_Over_Age_UnPregnant_Young: {
+		{
+			GroupId:   1,
+			FieldName: "day_age",
+			FieldDesc: "月龄",
+			Operator:  ">=",
+			Value:     "660",
+			IsShow:    pasturePb.IsShow_Ok,
+		},
+		{
+			GroupId:   2,
+			FieldName: "cow_type",
+			FieldDesc: "牛只类型",
+			Operator:  "=",
+			Value:     fmt.Sprintf("%d", pasturePb.CowType_Fattening_Calf),
+			IsShow:    pasturePb.IsShow_Ok,
+		},
+		{
+			GroupId:   2,
+			FieldName: "cow_type",
+			FieldDesc: "牛只类型",
+			Operator:  "=",
+			Value:     fmt.Sprintf("%d", pasturePb.CowType_Reserve_Calf),
+			IsShow:    pasturePb.IsShow_Ok,
+		},
+		{
+			GroupId:   3,
+			FieldName: "is_pregnant",
+			FieldDesc: "是否怀孕",
+			Operator:  "=",
+			Value:     fmt.Sprintf("%d", pasturePb.IsShow_No),
+			IsShow:    pasturePb.IsShow_Ok,
+		},
+	},
+	pasturePb.DataWarningType_Over_Month_UnSale: {
+		{
+			GroupId:   1,
+			FieldName: "day_age",
+			FieldDesc: "月龄",
+			Operator:  ">=",
+			Value:     "540",
+			IsShow:    pasturePb.IsShow_Ok,
+		},
+		{
+			GroupId:   2,
+			FieldName: "purpose_kind",
+			FieldDesc: "用途",
+			Operator:  "=",
+			Value:     fmt.Sprintf("%d", pasturePb.Purpose_Fatten),
+			IsShow:    pasturePb.IsShow_Ok,
+		},
+	},
+}
+
+func DataWarningItemsInitData(pastureId int64, dataWarning *DataWarning) []*DataWarningItems {
+	dataWarningItems := make([]*DataWarningItems, 0)
+	for _, v := range DefaultDataWarningItemsMap[dataWarning.Kind] {
+		dataWarningItems = append(dataWarningItems, NewDataWarningItems(pastureId, DefaultUserId, dataWarning, v))
+	}
+	return dataWarningItems
+}
+
 func NewDataWarningItems(pastureId, userId int64, dataWarning *DataWarning, req *pasturePb.WarningDataSet) *DataWarningItems {
 	return &DataWarningItems{
 		PastureId: pastureId,

+ 78 - 0
model/neck_ring_configure.go

@@ -31,3 +31,81 @@ type NeckRingConfigure struct {
 func (s *NeckRingConfigure) TableName() string {
 	return "neck_ring_configure"
 }
+
+func NeckRingConfigureInit(pastureId int64) []*NeckRingConfigure {
+	res := make([]*NeckRingConfigure, 0)
+	res = append(res, &NeckRingConfigure{
+		PastureId: pastureId,
+		Name:      High,
+		Value:     50,
+		Remarks:   "高活动量最低有效值",
+		IsShow:    pasturePb.IsShow_Ok,
+	}, &NeckRingConfigure{
+		PastureId: pastureId,
+		Name:      Rumina,
+		Value:     5,
+		Remarks:   "反刍最低值",
+		IsShow:    pasturePb.IsShow_Ok,
+	}, &NeckRingConfigure{
+		PastureId: pastureId,
+		Name:      WeeklyActive,
+		Value:     3000,
+		Remarks:   "周活动量初始值(oriWeeklyAct)",
+		IsShow:    pasturePb.IsShow_Ok,
+	}, &NeckRingConfigure{
+		PastureId: pastureId,
+		Name:      XRuminaDisc,
+		Value:     3,
+		Remarks:   "变化趋势(使用的时候需要除以10)",
+		IsShow:    pasturePb.IsShow_Ok,
+	}, &NeckRingConfigure{
+		PastureId: pastureId,
+		Name:      XChangeDiscount,
+		Value:     3,
+		Remarks:   "活动量滤波系数(使用的时候需要除以10)",
+		IsShow:    pasturePb.IsShow_Ok,
+	}, &NeckRingConfigure{
+		PastureId: pastureId,
+		Name:      ActiveLow,
+		Value:     55,
+		Remarks:   "活动量低值",
+		IsShow:    pasturePb.IsShow_Ok,
+	}, &NeckRingConfigure{
+		PastureId: pastureId,
+		Name:      ActiveMiddle,
+		Value:     75,
+		Remarks:   "活动量中间值",
+		IsShow:    pasturePb.IsShow_Ok,
+	}, &NeckRingConfigure{
+		PastureId: pastureId,
+		Name:      ActiveHigh,
+		Value:     125,
+		Remarks:   "活动量高值",
+		IsShow:    pasturePb.IsShow_Ok,
+	}, &NeckRingConfigure{
+		PastureId: pastureId,
+		Name:      MaxHabit,
+		Value:     0,
+		Remarks:   "脖环数据更新",
+		IsShow:    pasturePb.IsShow_Ok,
+	}, &NeckRingConfigure{
+		PastureId: pastureId,
+		Name:      HealthWarning,
+		Value:     82,
+		Remarks:   "预警健康指数值",
+		IsShow:    pasturePb.IsShow_Ok,
+	}, &NeckRingConfigure{
+		PastureId: pastureId,
+		Name:      MinWeeklyActive,
+		Value:     1500,
+		Remarks:   "周最小活动量值",
+		IsShow:    pasturePb.IsShow_Ok,
+	}, &NeckRingConfigure{
+		PastureId: pastureId,
+		Name:      new(PenBehavior).TableName(),
+		Value:     0,
+		Remarks:   "栏舍行为数据",
+		IsShow:    pasturePb.IsShow_Ok,
+	})
+	return res
+}

+ 44 - 0
module/backend/config_data_extend.go

@@ -263,3 +263,47 @@ func (s *StoreEntry) MatingWindowPeriodEnumList(isAll string) []*pasturePb.Confi
 	})
 	return configOptions
 }
+
+func (s *StoreEntry) DataWaringTypeEnumList(isAll string) []*pasturePb.ConfigOptionsList {
+	configOptions := make([]*pasturePb.ConfigOptionsList, 0)
+	if isAll == model.IsAllYes {
+		configOptions = append(configOptions,
+			&pasturePb.ConfigOptionsList{
+				Value:    int32(0),
+				Label:    "全部",
+				Disabled: true,
+			})
+	}
+	configOptions = append(configOptions, &pasturePb.ConfigOptionsList{
+		Value:    int32(pasturePb.DataWarningType_Sale_Standard),
+		Label:    "出栏标准",
+		Disabled: true,
+		Props:    "达到出栏重量的牛只",
+	}, &pasturePb.ConfigOptionsList{
+		Value:    int32(pasturePb.DataWarningType_UnPregnant_Mating_Time),
+		Label:    ">6次未孕牛只",
+		Disabled: true,
+		Props:    "配次高于6次未孕牛只",
+	}, &pasturePb.ConfigOptionsList{
+		Value:    int32(pasturePb.DataWarningType_Abortion_Time),
+		Label:    ">2次流产牛只",
+		Disabled: true,
+		Props:    "本胎次内流产大于2次的牛只",
+	}, &pasturePb.ConfigOptionsList{
+		Value:    int32(pasturePb.DataWarningType_Over_Age_UnPaired_Young),
+		Label:    "超龄未配(青年牛)",
+		Disabled: true,
+		Props:    "超15个月未配青年牛",
+	}, &pasturePb.ConfigOptionsList{
+		Value:    int32(pasturePb.DataWarningType_Over_Age_UnPregnant_Young),
+		Label:    "超龄未孕(青年牛)",
+		Disabled: true,
+		Props:    "超22月未孕青年牛",
+	}, &pasturePb.ConfigOptionsList{
+		Value:    int32(pasturePb.DataWarningType_Over_Age_UnPregnant_Young),
+		Label:    "超18月未卖",
+		Disabled: true,
+		Props:    "饲养超过18个月未卖出牛只",
+	})
+	return configOptions
+}

+ 1 - 0
module/backend/interface.go

@@ -356,4 +356,5 @@ type TestService interface {
 	UpdateCowPen(ctx context.Context, pagination *pasturePb.PaginationModel) error
 	TestDataWaring(ctx context.Context, userId int64) error
 	NeckRingOriginalAsync(ctx context.Context, pastureId int64, pagination *pasturePb.PaginationModel) error
+	PastureInit(ctx context.Context) error
 }

+ 35 - 0
module/backend/test_service.go

@@ -126,3 +126,38 @@ func (s *StoreEntry) UpdateCowPen(ctx context.Context, pagination *pasturePb.Pag
 	}
 	return nil
 }
+
+func (s *StoreEntry) PastureInit(ctx context.Context) error {
+	userModel, err := s.GetUserModel(ctx)
+	if err != nil {
+		return xerr.WithStack(err)
+	}
+	pastureId := userModel.AppPasture.Id
+	dataWaringTypeEnumList := s.DataWaringTypeEnumList("all")
+	dataWarningList := model.DataWarningInitData(pastureId, dataWaringTypeEnumList)
+
+	if err = s.DB.Transaction(func(tx *gorm.DB) error {
+		for _, dataWarning := range dataWarningList {
+			if err = tx.Model(new(model.DataWarning)).
+				Create(dataWarning).Error; err != nil {
+				zaplog.Error("PastureInit-DataWarning", zap.Any("error", err))
+			}
+			dataWarningItemsList := model.DataWarningItemsInitData(pastureId, dataWarning)
+			if err = tx.Model(new(model.DataWarningItems)).
+				Create(dataWarningItemsList).Error; err != nil {
+				zaplog.Error("PastureInit-DataWarningItems", zap.Any("error", err))
+			}
+		}
+
+		nackRingConfigureList := model.NeckRingConfigureInit(pastureId)
+		if err = tx.Model(new(model.NeckRingConfigure)).
+			Create(nackRingConfigureList).Error; err != nil {
+			zaplog.Error("PastureInit-NeckRingConfigure", zap.Any("error", err))
+		}
+		return nil
+	}); err != nil {
+		return xerr.WithStack(err)
+	}
+
+	return nil
+}