ソースを参照

goods: 物品管理

Yi 4 ヶ月 前
コミット
b5e684a11b
2 ファイル変更39 行追加31 行削除
  1. 28 23
      model/drugs.go
  2. 11 8
      module/backend/goods.go

+ 28 - 23
model/drugs.go

@@ -5,15 +5,18 @@ import pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 type Drugs struct {
 	Id              int64                       `json:"id"`
 	Name            string                      `json:"name"`
-	CategoryId      pasturePb.DrugCategory_Kind `json:"category_id"`
+	CategoryId      pasturePb.DrugCategory_Kind `json:"categoryId"`
+	CategoryName    string                      `json:"categoryName"`
 	Producer        string                      `json:"producer"`
-	BatchNumber     string                      `json:"batch_number"`
-	ProductionAt    int64                       `json:"production_at"`
-	ExpirationAt    int64                       `json:"expiration_at"`
+	BatchNumber     string                      `json:"batchNumber"`
+	ProductionAt    int64                       `json:"productionAt"`
+	ExpirationAt    int64                       `json:"expirationAt"`
 	Unit            pasturePb.Unit_Kind         `json:"unit"`
+	UnitName        string                      `json:"unitName"`
 	Specs           string                      `json:"specs"`
 	Inventory       int32                       `json:"inventory"`
 	UsageMethod     pasturePb.DrugUsage_Kind    `json:"usage_method"`
+	UsageMethodName string                      `json:"usageMethodName"`
 	Price           int32                       `json:"price"`
 	MilkExpiredDays int32                       `json:"milk_expired_days"`
 	MeatExpiredDays int32                       `json:"meat_expired_days"`
@@ -30,44 +33,46 @@ func (d *Drugs) TableName() string {
 
 func NewDrugs(req *pasturePb.SearchDrugsList, currentUser *SystemUser) *Drugs {
 	return &Drugs{
-		Name:          req.Name,
-		CategoryId:    req.CategoryId,
-		Producer:      req.Producer,
-		BatchNumber:   req.BatchNumber,
-		ProductionAt:  int64(req.ProductionAt),
-		ExpirationAt:  int64(req.ExpirationAt),
-		Unit:          req.Unit,
-		Specs:         req.Specs,
-		Inventory:     req.Inventory,
-		UsageMethod:   req.Usage,
-		Price:         int32(req.Price * 100),
-		Remarks:       req.Remarks,
-		OperationId:   int32(currentUser.Id),
-		OperationName: currentUser.Name,
+		Name:            req.Name,
+		CategoryId:      req.CategoryId,
+		CategoryName:    req.CategoryName,
+		Producer:        req.Producer,
+		BatchNumber:     req.BatchNumber,
+		ProductionAt:    int64(req.ProductionAt),
+		ExpirationAt:    int64(req.ExpirationAt),
+		Unit:            req.Unit,
+		UnitName:        req.UnitName,
+		Specs:           req.Specs,
+		Inventory:       req.Inventory,
+		UsageMethod:     req.Usage,
+		UsageMethodName: req.UsageName,
+		Price:           int32(req.Price * 100),
+		Remarks:         req.Remarks,
+		OperationId:     int32(currentUser.Id),
+		OperationName:   currentUser.Name,
 	}
 }
 
 type DrugsSlice []*Drugs
 
-func (d DrugsSlice) ToPB(drugsCategoryMap map[pasturePb.DrugCategory_Kind]string,
-	unitMap map[pasturePb.Unit_Kind]string, drugUsageMap map[pasturePb.DrugUsage_Kind]string) []*pasturePb.SearchDrugsList {
+func (d DrugsSlice) ToPB() []*pasturePb.SearchDrugsList {
 	res := make([]*pasturePb.SearchDrugsList, len(d))
 	for i, v := range d {
 		res[i] = &pasturePb.SearchDrugsList{
 			Id:              int32(v.Id),
 			Name:            v.Name,
 			CategoryId:      v.CategoryId,
-			CategoryName:    drugsCategoryMap[v.CategoryId],
+			CategoryName:    v.CategoryName,
 			Producer:        v.Producer,
 			BatchNumber:     v.BatchNumber,
 			ProductionAt:    int32(v.ProductionAt),
 			ExpirationAt:    int32(v.ExpirationAt),
 			Unit:            v.Unit,
-			UnitName:        unitMap[v.Unit],
+			UnitName:        v.UnitName,
 			Specs:           v.Specs,
 			Inventory:       v.Inventory,
 			Usage:           v.UsageMethod,
-			UsageName:       drugUsageMap[v.UsageMethod],
+			UsageName:       v.UsageMethodName,
 			Price:           float32(v.Price) / 100,
 			MeatExpiredDays: v.MeatExpiredDays,
 			MilkExpiredDays: v.MilkExpiredDays,

+ 11 - 8
module/backend/goods.go

@@ -36,15 +36,11 @@ func (s *StoreEntry) DrugsList(ctx context.Context, req *pasturePb.SearchDrugsRe
 		return nil, xerr.WithStack(err)
 	}
 
-	drugsCategoryMap := s.DrugCategoryMaps()
-	unitMap := s.UnitMap()
-	drugUsageMap := s.DrugUsageMaps()
-
 	return &pasturePb.SearchDrugsResponse{
 		Code:    http.StatusOK,
 		Message: "ok",
 		Data: &pasturePb.SearchDrugsData{
-			List:     model.DrugsSlice(drugsList).ToPB(drugsCategoryMap, unitMap, drugUsageMap),
+			List:     model.DrugsSlice(drugsList).ToPB(),
 			Total:    int32(count),
 			PageSize: pagination.PageSize,
 			Page:     pagination.Page,
@@ -53,14 +49,21 @@ func (s *StoreEntry) DrugsList(ctx context.Context, req *pasturePb.SearchDrugsRe
 }
 
 func (s *StoreEntry) DrugsCreateOrUpdate(ctx context.Context, req *pasturePb.SearchDrugsList) error {
-	currentUser, _ := s.GetCurrentSystemUser(ctx)
+	currentUser, err := s.GetCurrentSystemUser(ctx)
+	if err != nil {
+		return xerr.Custom("登录人信息失效")
+	}
+	req.CategoryName = s.DrugCategoryMaps()[req.CategoryId]
+	req.UnitName = s.UnitMap()[req.Unit]
+	req.UnitName = s.DrugUsageMaps()[req.Usage]
+
 	newDrugs := model.NewDrugs(req, currentUser)
 	if req.Id <= 0 {
-		if err := s.DB.Create(newDrugs).Error; err != nil {
+		if err = s.DB.Create(newDrugs).Error; err != nil {
 			return xerr.WithStack(err)
 		}
 	} else {
-		if err := s.DB.Where("id = ?", req.Id).Updates(newDrugs).Error; err != nil {
+		if err = s.DB.Where("id = ?", req.Id).Updates(newDrugs).Error; err != nil {
 			return xerr.WithStack(err)
 		}
 	}