Browse Source

prescription: 处方管理

ping 7 months ago
parent
commit
138bf28ac4

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20240729074839-92b2b1102385
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20240730101550-db8c0ea85f7e
 	gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/getsentry/sentry-go v0.23.0

+ 14 - 0
go.sum

@@ -52,6 +52,20 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20240729074149-7b30664e5821 h1:QbJFiz83
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240729074149-7b30664e5821/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240729074839-92b2b1102385 h1:qhfsmIovTOcKlqzaHoNzqhBuk4mwjN0pnQtG+W7BRbo=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240729074839-92b2b1102385/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240730020825-060b3ad80644 h1:rbHKuooh0QO4NNxXUI/Smtk7lt0T7GmlD5I9apdfaJU=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240730020825-060b3ad80644/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240730022208-2ba264fb9569 h1:F/G3ueJfqQ2+TG8Eo1zXz5AncfW2EeHWg2hcfrrNmHI=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240730022208-2ba264fb9569/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240730024415-20af7e52a080 h1:6rg7S76RZjfXQyXuuVwP7scGMrefMUekdUtSjcPfVk4=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240730024415-20af7e52a080/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240730092700-a684a5327b69 h1:YHeXzSbDUNSqs0AEkX7H20uofK/Fn51PdAPrvdwqKgE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240730092700-a684a5327b69/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240730095157-1e778ebd2d50 h1:SUALePGdSEqQbKOX8US4SPQtUT37cFbrpG/BUg8BKXA=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240730095157-1e778ebd2d50/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240730100339-3dcd352d754b h1:Txe128YoGBSkzzcj92DzQ0bCi/PiijyG0r1JOMxkIfo=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240730100339-3dcd352d754b/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240730101550-db8c0ea85f7e h1:1YSKaVoWu3mCvVpzRWs4jggQvSN9LD77OmeT2JoTBQU=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240730101550-db8c0ea85f7e/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015 h1:dfb5dRd57L2HKjdwLT93UFmPYFPOmEl56gtZmqcNnaE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015/go.mod h1:Fk4GYI/v0IK3XFrm1Gn+VkgCz5Y7mfswD5hsTJYOG6A=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

+ 0 - 143
http/handler/pasture/barn.go

@@ -399,146 +399,3 @@ func CreatedOrUpdateCowSource(c *gin.Context) {
 		Data: &operationPb.Success{Success: true},
 	})
 }
-
-func SearchDiseaseList(c *gin.Context) {
-	var req pasturePb.SearchDiseaseRequest
-	if err := ginutil.BindProto(c, &req); err != nil {
-		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
-		return
-	}
-
-	pagination := &pasturePb.PaginationModel{
-		Page:       int32(c.GetInt(middleware.Page)),
-		PageSize:   int32(c.GetInt(middleware.PageSize)),
-		PageOffset: int32(c.GetInt(middleware.PageOffset)),
-	}
-
-	res, err := middleware.Dependency(c).StoreEventHub.OpsService.SearchDiseaseList(c, &req, pagination)
-	if err != nil {
-		apierr.ClassifiedAbort(c, err)
-		return
-	}
-	ginutil.JSONResp(c, res)
-}
-
-func CreatedOrUpdateDisease(c *gin.Context) {
-	var req pasturePb.SearchDiseaseList
-	if err := ginutil.BindProto(c, &req); err != nil {
-		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
-		return
-	}
-
-	if err := valid.ValidateStruct(&req,
-		valid.Field(&req.Name, valid.Required),
-		valid.Field(&req.DiseaseTypeId, valid.Required),
-	); err != nil {
-		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
-		return
-	}
-
-	if err := middleware.BackendOperation(c).OpsService.CreateOrUpdateDisease(c, &req); err != nil {
-		apierr.ClassifiedAbort(c, err)
-		return
-	}
-
-	ginutil.JSONResp(c, &operationPb.CommonOK{
-		Code: http.StatusOK,
-		Msg:  "ok",
-		Data: &operationPb.Success{Success: true},
-	})
-}
-
-func SearchDiseaseTypeList(c *gin.Context) {
-	var req pasturePb.SearchNameRequest
-	if err := ginutil.BindProto(c, &req); err != nil {
-		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
-		return
-	}
-
-	pagination := &pasturePb.PaginationModel{
-		Page:       int32(c.GetInt(middleware.Page)),
-		PageSize:   int32(c.GetInt(middleware.PageSize)),
-		PageOffset: int32(c.GetInt(middleware.PageOffset)),
-	}
-
-	res, err := middleware.Dependency(c).StoreEventHub.OpsService.SearchDiseaseTypeList(c, &req, pagination)
-	if err != nil {
-		apierr.ClassifiedAbort(c, err)
-		return
-	}
-	ginutil.JSONResp(c, res)
-}
-
-func CreatedOrUpdateDiseaseType(c *gin.Context) {
-	var req pasturePb.SearchBaseConfigList
-	if err := ginutil.BindProto(c, &req); err != nil {
-		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
-		return
-	}
-
-	if err := valid.ValidateStruct(&req,
-		valid.Field(&req.Name, valid.Required),
-	); err != nil {
-		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
-		return
-	}
-
-	if err := middleware.BackendOperation(c).OpsService.CreateOrUpdateDiseaseType(c, &req); err != nil {
-		apierr.ClassifiedAbort(c, err)
-		return
-	}
-
-	ginutil.JSONResp(c, &operationPb.CommonOK{
-		Code: http.StatusOK,
-		Msg:  "ok",
-		Data: &operationPb.Success{Success: true},
-	})
-}
-
-func SearchPrescriptionList(c *gin.Context) {
-	var req pasturePb.SearchPrescriptionRequest
-	if err := ginutil.BindProto(c, &req); err != nil {
-		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
-		return
-	}
-
-	pagination := &pasturePb.PaginationModel{
-		Page:       int32(c.GetInt(middleware.Page)),
-		PageSize:   int32(c.GetInt(middleware.PageSize)),
-		PageOffset: int32(c.GetInt(middleware.PageOffset)),
-	}
-
-	res, err := middleware.Dependency(c).StoreEventHub.OpsService.SearchPrescriptionList(c, &req, pagination)
-	if err != nil {
-		apierr.ClassifiedAbort(c, err)
-		return
-	}
-	ginutil.JSONResp(c, res)
-}
-
-func CreatedOrUpdatePrescription(c *gin.Context) {
-	var req pasturePb.PrescriptionRequest
-	if err := ginutil.BindProto(c, &req); err != nil {
-		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
-		return
-	}
-
-	if err := valid.ValidateStruct(&req,
-		valid.Field(&req.Name, valid.Required),
-		valid.Field(&req.PrescriptionDrugsItem, valid.Required),
-	); err != nil {
-		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
-		return
-	}
-
-	if err := middleware.BackendOperation(c).OpsService.CreateOrUpdatePrescription(c, &req); err != nil {
-		apierr.ClassifiedAbort(c, err)
-		return
-	}
-
-	ginutil.JSONResp(c, &operationPb.CommonOK{
-		Code: http.StatusOK,
-		Msg:  "ok",
-		Data: &operationPb.Success{Success: true},
-	})
-}

+ 156 - 0
http/handler/pasture/prescription.go

@@ -0,0 +1,156 @@
+package pasture
+
+import (
+	"kpt-pasture/http/middleware"
+	"net/http"
+
+	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+	operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
+	"gitee.com/xuyiping_admin/pkg/apierr"
+	"gitee.com/xuyiping_admin/pkg/ginutil"
+	"gitee.com/xuyiping_admin/pkg/valid"
+	"github.com/gin-gonic/gin"
+)
+
+func SearchDiseaseList(c *gin.Context) {
+	var req pasturePb.SearchDiseaseRequest
+	if err := ginutil.BindProto(c, &req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	pagination := &pasturePb.PaginationModel{
+		Page:       int32(c.GetInt(middleware.Page)),
+		PageSize:   int32(c.GetInt(middleware.PageSize)),
+		PageOffset: int32(c.GetInt(middleware.PageOffset)),
+	}
+
+	res, err := middleware.Dependency(c).StoreEventHub.OpsService.SearchDiseaseList(c, &req, pagination)
+	if err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	ginutil.JSONResp(c, res)
+}
+
+func CreatedOrUpdateDisease(c *gin.Context) {
+	var req pasturePb.SearchDiseaseList
+	if err := ginutil.BindProto(c, &req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	if err := valid.ValidateStruct(&req,
+		valid.Field(&req.Name, valid.Required),
+		valid.Field(&req.DiseaseTypeId, valid.Required),
+	); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	if err := middleware.BackendOperation(c).OpsService.CreateOrUpdateDisease(c, &req); err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+
+	ginutil.JSONResp(c, &operationPb.CommonOK{
+		Code: http.StatusOK,
+		Msg:  "ok",
+		Data: &operationPb.Success{Success: true},
+	})
+}
+
+func SearchDiseaseTypeList(c *gin.Context) {
+	var req pasturePb.SearchNameRequest
+	if err := ginutil.BindProto(c, &req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	pagination := &pasturePb.PaginationModel{
+		Page:       int32(c.GetInt(middleware.Page)),
+		PageSize:   int32(c.GetInt(middleware.PageSize)),
+		PageOffset: int32(c.GetInt(middleware.PageOffset)),
+	}
+
+	res, err := middleware.Dependency(c).StoreEventHub.OpsService.SearchDiseaseTypeList(c, &req, pagination)
+	if err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	ginutil.JSONResp(c, res)
+}
+
+func CreatedOrUpdateDiseaseType(c *gin.Context) {
+	var req pasturePb.SearchBaseConfigList
+	if err := ginutil.BindProto(c, &req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	if err := valid.ValidateStruct(&req,
+		valid.Field(&req.Name, valid.Required),
+	); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	if err := middleware.BackendOperation(c).OpsService.CreateOrUpdateDiseaseType(c, &req); err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+
+	ginutil.JSONResp(c, &operationPb.CommonOK{
+		Code: http.StatusOK,
+		Msg:  "ok",
+		Data: &operationPb.Success{Success: true},
+	})
+}
+
+func SearchPrescriptionList(c *gin.Context) {
+	var req pasturePb.SearchPrescriptionRequest
+	if err := ginutil.BindProto(c, &req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	pagination := &pasturePb.PaginationModel{
+		Page:       int32(c.GetInt(middleware.Page)),
+		PageSize:   int32(c.GetInt(middleware.PageSize)),
+		PageOffset: int32(c.GetInt(middleware.PageOffset)),
+	}
+
+	res, err := middleware.Dependency(c).StoreEventHub.OpsService.SearchPrescriptionList(c, &req, pagination)
+	if err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	ginutil.JSONResp(c, res)
+}
+
+func CreatedOrUpdatePrescription(c *gin.Context) {
+	var req pasturePb.PrescriptionRequest
+	if err := ginutil.BindProto(c, &req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	if err := valid.ValidateStruct(&req,
+		valid.Field(&req.Name, valid.Required),
+		valid.Field(&req.DrugsList, valid.Required),
+	); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	if err := middleware.BackendOperation(c).OpsService.CreateOrUpdatePrescription(c, &req); err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+
+	ginutil.JSONResp(c, &operationPb.CommonOK{
+		Code: http.StatusOK,
+		Msg:  "ok",
+		Data: &operationPb.Success{Success: true},
+	})
+}

+ 45 - 27
model/prescription.go

@@ -1,8 +1,10 @@
 package model
 
 import (
-	"fmt"
-	"strings"
+	"encoding/json"
+
+	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
+	"go.uber.org/zap"
 
 	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 )
@@ -26,11 +28,12 @@ func (p *Prescription) TableName() string {
 	return "prescription"
 }
 
-func NewPrescription(req *pasturePb.PrescriptionRequest, useDays, meatExpiredDays, milkExpiredDays int32, operationId int64) *Prescription {
-	applicableDisease := ""
-	for _, v := range req.ApplicableDisease {
-		applicableDisease += fmt.Sprintf("%d", v) + "|"
-	}
+type ApplicableDisease struct {
+	DiseaseId   int64  `json:"disease_id"`
+	DiseaseName string `json:"disease_name"`
+}
+
+func NewPrescription(req *pasturePb.PrescriptionRequest, applicableDisease string, useDays, meatExpiredDays, milkExpiredDays int32, operationId int64) *Prescription {
 	return &Prescription{
 		Name:              req.Name,
 		ApplicableDisease: applicableDisease,
@@ -62,29 +65,44 @@ func (p PrescriptionSlice) ToPB(userList []*SystemUser, prescriptionDrugsList []
 				continue
 			}
 			drugsList = append(drugsList, &pasturePb.PrescriptionDrugsList{
-				Id:       int32(d.Id),
-				DrugsId:  int32(d.DrugsId),
-				Unit:     d.Unit,
-				UnitName: d.UnitName,
-				Dosages:  d.Dosages,
-				UseDays:  d.UseDays,
+				Id:        int32(d.Id),
+				DrugsId:   int32(d.DrugsId),
+				DrugsName: d.DrugsName,
+				Unit:      d.Unit,
+				UnitName:  d.UnitName,
+				Dosages:   d.Dosages,
+				UseDays:   d.UseDays,
+				Specs:     d.Specs,
 			})
 		}
+		applicableDiseaseList := make([]*ApplicableDisease, 0)
+		err := json.Unmarshal([]byte(v.ApplicableDisease), &applicableDiseaseList)
+		if err != nil {
+			zaplog.Error("PrescriptionSliceToPB", zap.Any("err", err))
+		}
+		applicableDiseaseNames := make([]string, 0)
+		applicableDiseaseIds := make([]int32, 0)
+		for _, a := range applicableDiseaseList {
+			applicableDiseaseNames = append(applicableDiseaseNames, a.DiseaseName)
+			applicableDiseaseIds = append(applicableDiseaseIds, int32(a.DiseaseId))
+		}
 		res[i] = &pasturePb.SearchPrescriptionList{
-			Id:                int32(v.Id),
-			Name:              v.Name,
-			ApplicableDisease: strings.Split(v.ApplicableDisease, "|"),
-			UseDays:           v.UseDays,
-			UseCount:          v.UseCount,
-			MeatExpiredDays:   v.MeatExpiredDays,
-			MilkExpiredDays:   v.MilkExpiredDays,
-			IsShow:            v.IsShow,
-			Remarks:           v.Remarks,
-			OperationId:       int32(v.OperationId),
-			OperationName:     operationName,
-			CreatedAt:         int32(v.CreatedAt),
-			UpdatedAt:         int32(v.UpdatedAt),
-			DrugsList:         drugsList,
+			Id:                     int32(v.Id),
+			Name:                   v.Name,
+			ApplicableDisease:      v.ApplicableDisease,
+			ApplicableDiseaseIds:   applicableDiseaseIds,
+			ApplicableDiseaseNames: applicableDiseaseNames,
+			UseDays:                v.UseDays,
+			UseCount:               v.UseCount,
+			MeatExpiredDays:        v.MeatExpiredDays,
+			MilkExpiredDays:        v.MilkExpiredDays,
+			IsShow:                 v.IsShow,
+			Remarks:                v.Remarks,
+			OperationId:            int32(v.OperationId),
+			OperationName:          operationName,
+			CreatedAt:              int32(v.CreatedAt),
+			UpdatedAt:              int32(v.UpdatedAt),
+			DrugsList:              drugsList,
 		}
 	}
 	return res

+ 6 - 3
model/prescription_drugs.go

@@ -6,10 +6,12 @@ type PrescriptionDrugs struct {
 	Id             int64                 `json:"id"`
 	PrescriptionId int64                 `json:"prescription_id"`
 	DrugsId        int64                 `json:"drugs_id"`
+	DrugsName      string                `json:"drugs_name"`
 	UseDays        int32                 `json:"use_days"`
 	Dosages        int32                 `json:"dosages"`
 	Unit           pasturePb.Unit_Kind   `json:"unit"`
 	UnitName       string                `json:"unit_name"`
+	Specs          string                `json:"specs"`
 	IsShow         pasturePb.IsShow_Kind `json:"is_show"`
 	CreatedAt      int64                 `json:"created_at"`
 	UpdatedAt      int64                 `json:"updated_at"`
@@ -20,16 +22,17 @@ func (p *PrescriptionDrugs) TableName() string {
 }
 
 func NewPrescriptionDrugs(prescriptionId int64, req *pasturePb.PrescriptionRequest) []*PrescriptionDrugs {
-	res := make([]*PrescriptionDrugs, len(req.PrescriptionDrugsItem))
-	for i, v := range req.PrescriptionDrugsItem {
-
+	res := make([]*PrescriptionDrugs, len(req.DrugsList))
+	for i, v := range req.DrugsList {
 		res[i] = &PrescriptionDrugs{
 			PrescriptionId: prescriptionId,
 			DrugsId:        int64(v.DrugsId),
+			DrugsName:      v.DrugsName,
 			UseDays:        v.UseDays,
 			Dosages:        v.Dosages,
 			Unit:           v.Unit,
 			UnitName:       v.UnitName,
+			Specs:          v.Specs,
 			IsShow:         pasturePb.IsShow_Ok,
 		}
 	}

+ 0 - 233
module/backend/pasture.go

@@ -7,7 +7,6 @@ import (
 	"fmt"
 	"kpt-pasture/model"
 	"net/http"
-	"strings"
 
 	"gitee.com/xuyiping_admin/pkg/xerr"
 	"gorm.io/gorm"
@@ -502,235 +501,3 @@ func (s *StoreEntry) SemeTimeDetail(ctx context.Context, semeTimeId int64) (*pas
 		},
 	}, nil
 }
-
-func (s *StoreEntry) SearchDiseaseList(ctx context.Context, req *pasturePb.SearchDiseaseRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchDiseaseResponse, error) {
-	diseaseList := make([]*model.Disease, 0)
-	var count int64 = 0
-
-	pref := s.DB.Model(new(model.Disease))
-	if req.Name != "" {
-		pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
-	}
-
-	if req.DiseaseTypeId > 0 {
-		pref.Where("disease_type = ?", req.DiseaseTypeId)
-	}
-
-	if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
-		Find(&diseaseList).Error; err != nil {
-		return nil, xerr.WithStack(err)
-	}
-
-	systemUserList, _ := s.SystemUserList(ctx)
-	diseaseTypeList, _ := s.DiseaseTypeList(ctx)
-
-	return &pasturePb.SearchDiseaseResponse{
-		Code:    http.StatusOK,
-		Message: "ok",
-		Data: &pasturePb.SearchDiseaseData{
-			List:     model.DiseaseSlice(diseaseList).ToPB(systemUserList, diseaseTypeList),
-			Total:    int32(count),
-			PageSize: pagination.PageSize,
-			Page:     pagination.Page,
-		},
-	}, nil
-}
-
-func (s *StoreEntry) CreateOrUpdateDisease(ctx context.Context, req *pasturePb.SearchDiseaseList) error {
-	currUser, _ := s.GetCurrentSystemUser(ctx)
-
-	if req.Id > 0 {
-		barn := &model.Disease{Id: int64(req.Id)}
-		if err := s.DB.Model(&model.Disease{}).First(barn).Error; err != nil {
-			if !errors.Is(err, gorm.ErrRecordNotFound) {
-				return xerr.WithStack(err)
-			}
-		}
-	}
-	if err := s.DB.Model(&model.Disease{}).Where(map[string]interface{}{
-		"id": req.Id,
-	}).Assign(map[string]interface{}{
-		"disease_type": req.DiseaseTypeId,
-		"name":         req.Name,
-		"symptoms":     strings.Join(req.Symptoms, "|"),
-		"remarks":      req.Remarks,
-		"is_show":      pasturePb.IsShow_Ok,
-		"operation_id": currUser.Id,
-	}).FirstOrCreate(&model.Disease{}).Error; err != nil {
-		return xerr.WithStack(err)
-	}
-	return nil
-}
-
-func (s *StoreEntry) SearchDiseaseTypeList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) {
-	diseaseTypeList := make([]*model.ConfigDiseaseType, 0)
-	var count int64 = 0
-
-	pref := s.DB.Model(new(model.ConfigDiseaseType))
-	if req.Name != "" {
-		pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
-	}
-
-	if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
-		Find(&diseaseTypeList).Error; err != nil {
-		return nil, xerr.WithStack(err)
-	}
-
-	return &pasturePb.SearchBaseConfigResponse{
-		Code:    http.StatusOK,
-		Message: "ok",
-		Data: &pasturePb.SearchBaseConfigData{
-			List:     model.ConfigDiseaseTypeSlice(diseaseTypeList).ToPB(),
-			Total:    int32(count),
-			PageSize: pagination.PageSize,
-			Page:     pagination.Page,
-		},
-	}, nil
-}
-
-func (s *StoreEntry) CreateOrUpdateDiseaseType(ctx context.Context, req *pasturePb.SearchBaseConfigList) error {
-	if req.Id > 0 {
-		barn := &model.ConfigDiseaseType{Id: int64(req.Id)}
-		if err := s.DB.Model(&model.ConfigDiseaseType{}).First(barn).Error; err != nil {
-			if !errors.Is(err, gorm.ErrRecordNotFound) {
-				return xerr.WithStack(err)
-			}
-		}
-	}
-
-	if err := s.DB.Model(&model.ConfigDiseaseType{}).Where(map[string]interface{}{
-		"id": req.Id,
-	}).Assign(map[string]interface{}{
-		"name":    req.Name,
-		"remarks": req.Remarks,
-		"is_show": pasturePb.IsShow_Ok,
-	}).FirstOrCreate(&model.ConfigDiseaseType{}).Error; err != nil {
-		return xerr.WithStack(err)
-	}
-	return nil
-}
-
-func (s *StoreEntry) SearchPrescriptionList(ctx context.Context, req *pasturePb.SearchPrescriptionRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchPrescriptionResponse, error) {
-	prescriptionList := make([]*model.Prescription, 0)
-	var count int64 = 0
-
-	pref := s.DB.Model(new(model.Prescription))
-	if req.Name != "" {
-		pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
-	}
-
-	if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
-		Find(&prescriptionList).Error; err != nil {
-		return nil, xerr.WithStack(err)
-	}
-
-	prescriptionIds := make([]int64, 0)
-	for _, prescription := range prescriptionList {
-		prescriptionIds = append(prescriptionIds, prescription.Id)
-	}
-
-	prescriptionDrugs := make([]*model.PrescriptionDrugs, 0)
-	if len(prescriptionIds) > 0 {
-		if err := s.DB.Model(new(model.PrescriptionDrugs)).Where("prescription_id in (?)", prescriptionIds).
-			Where("is_show = ? ", pasturePb.IsShow_Ok).
-			Find(&prescriptionDrugs).Error; err != nil {
-			return nil, xerr.WithStack(err)
-		}
-	}
-
-	systemUserList, _ := s.SystemUserList(ctx)
-	return &pasturePb.SearchPrescriptionResponse{
-		Code:    http.StatusOK,
-		Message: "ok",
-		Data: &pasturePb.SearchPrescriptionData{
-			List:     model.PrescriptionSlice(prescriptionList).ToPB(systemUserList, prescriptionDrugs),
-			Total:    int32(count),
-			PageSize: pagination.PageSize,
-			Page:     pagination.Page,
-		},
-	}, nil
-}
-
-func (s *StoreEntry) CreateOrUpdatePrescription(ctx context.Context, req *pasturePb.PrescriptionRequest) error {
-	currUser, _ := s.GetCurrentSystemUser(ctx)
-	var maxUseDays, maxMeatExpiredDays, maxMilkExpiredDays int32 = 0, 0, 0
-	for _, v := range req.PrescriptionDrugsItem {
-		if v.DrugsId <= 0 {
-			return xerr.Customf("错误处方药品的数据")
-		}
-		if v.UseDays <= 0 {
-			return xerr.Customf("错误处方药品使用天数")
-		}
-		if v.UseDays > maxUseDays {
-			maxUseDays = v.UseDays
-		}
-
-		drugs, err := s.DrugsById(ctx, int64(v.DrugsId))
-		if err != nil {
-			return xerr.WithStack(err)
-		}
-
-		if drugs.MeatExpiredDays > maxMeatExpiredDays {
-			maxMeatExpiredDays = drugs.MeatExpiredDays
-		}
-
-		if drugs.MilkExpiredDays > maxMilkExpiredDays {
-			maxMilkExpiredDays = drugs.MilkExpiredDays
-		}
-	}
-	newPrescription := model.NewPrescription(req, maxUseDays, maxMeatExpiredDays, maxMilkExpiredDays, currUser.Id)
-
-	if req.Id > 0 {
-		prescription := &model.Prescription{Id: int64(req.Id)}
-		if err := s.DB.Model(&model.Prescription{}).First(prescription).Error; err != nil {
-			if !errors.Is(err, gorm.ErrRecordNotFound) {
-				return xerr.WithStack(err)
-			}
-		}
-		if err := s.DB.Transaction(func(tx *gorm.DB) error {
-			if err := tx.Model(&model.Prescription{}).Where(map[string]interface{}{
-				"id": req.Id,
-			}).Updates(map[string]interface{}{
-				"name":               newPrescription.Name,
-				"applicable_disease": newPrescription.ApplicableDisease,
-				"use_days":           newPrescription.UseDays,
-				"meat_expired_days":  newPrescription.MeatExpiredDays,
-				"milk_expired_days":  newPrescription.MilkExpiredDays,
-				"remarks":            newPrescription.Remarks,
-			}).Error; err != nil {
-				return xerr.WithStack(err)
-			}
-
-			if err := tx.Model(&model.PrescriptionDrugs{}).Where("prescription_id", req.Id).Delete(&model.PrescriptionDrugs{}).Error; err != nil {
-				return xerr.WithStack(err)
-			}
-			// 创建处方药品
-			newPrescriptionDrugs := model.NewPrescriptionDrugs(int64(req.Id), req)
-			if err := tx.Create(&newPrescriptionDrugs).Error; err != nil {
-				return xerr.WithStack(err)
-			}
-
-			return nil
-		}); err != nil {
-			return xerr.WithStack(err)
-		}
-		return nil
-	}
-
-	// 创建处方
-	if err := s.DB.Transaction(func(tx *gorm.DB) error {
-		// 创建处方
-		if err := tx.Create(&newPrescription).Error; err != nil {
-			return xerr.WithStack(err)
-		}
-		// 创建处方药品
-		newPrescriptionDrugs := model.NewPrescriptionDrugs(newPrescription.Id, req)
-		if err := tx.Create(&newPrescriptionDrugs).Error; err != nil {
-			return xerr.WithStack(err)
-		}
-		return nil
-	}); err != nil {
-		return xerr.WithStack(err)
-	}
-	return nil
-}

+ 264 - 0
module/backend/prescription.go

@@ -0,0 +1,264 @@
+package backend
+
+import (
+	"context"
+	"encoding/json"
+	"errors"
+	"fmt"
+	"kpt-pasture/model"
+	"net/http"
+	"strings"
+
+	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+	"gitee.com/xuyiping_admin/pkg/xerr"
+	"gorm.io/gorm"
+)
+
+func (s *StoreEntry) SearchDiseaseList(ctx context.Context, req *pasturePb.SearchDiseaseRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchDiseaseResponse, error) {
+	diseaseList := make([]*model.Disease, 0)
+	var count int64 = 0
+
+	pref := s.DB.Model(new(model.Disease))
+	if req.Name != "" {
+		pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
+	}
+
+	if req.DiseaseTypeId > 0 {
+		pref.Where("disease_type = ?", req.DiseaseTypeId)
+	}
+
+	if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
+		Find(&diseaseList).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+
+	systemUserList, _ := s.SystemUserList(ctx)
+	diseaseTypeList, _ := s.DiseaseTypeList(ctx)
+
+	return &pasturePb.SearchDiseaseResponse{
+		Code:    http.StatusOK,
+		Message: "ok",
+		Data: &pasturePb.SearchDiseaseData{
+			List:     model.DiseaseSlice(diseaseList).ToPB(systemUserList, diseaseTypeList),
+			Total:    int32(count),
+			PageSize: pagination.PageSize,
+			Page:     pagination.Page,
+		},
+	}, nil
+}
+
+func (s *StoreEntry) CreateOrUpdateDisease(ctx context.Context, req *pasturePb.SearchDiseaseList) error {
+	currUser, _ := s.GetCurrentSystemUser(ctx)
+
+	if req.Id > 0 {
+		barn := &model.Disease{Id: int64(req.Id)}
+		if err := s.DB.Model(&model.Disease{}).First(barn).Error; err != nil {
+			if !errors.Is(err, gorm.ErrRecordNotFound) {
+				return xerr.WithStack(err)
+			}
+		}
+	}
+	if err := s.DB.Model(&model.Disease{}).Where(map[string]interface{}{
+		"id": req.Id,
+	}).Assign(map[string]interface{}{
+		"disease_type": req.DiseaseTypeId,
+		"name":         req.Name,
+		"symptoms":     strings.Join(req.Symptoms, "|"),
+		"remarks":      req.Remarks,
+		"is_show":      pasturePb.IsShow_Ok,
+		"operation_id": currUser.Id,
+	}).FirstOrCreate(&model.Disease{}).Error; err != nil {
+		return xerr.WithStack(err)
+	}
+	return nil
+}
+
+func (s *StoreEntry) SearchDiseaseTypeList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) {
+	diseaseTypeList := make([]*model.ConfigDiseaseType, 0)
+	var count int64 = 0
+
+	pref := s.DB.Model(new(model.ConfigDiseaseType))
+	if req.Name != "" {
+		pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
+	}
+
+	if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
+		Find(&diseaseTypeList).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+
+	return &pasturePb.SearchBaseConfigResponse{
+		Code:    http.StatusOK,
+		Message: "ok",
+		Data: &pasturePb.SearchBaseConfigData{
+			List:     model.ConfigDiseaseTypeSlice(diseaseTypeList).ToPB(),
+			Total:    int32(count),
+			PageSize: pagination.PageSize,
+			Page:     pagination.Page,
+		},
+	}, nil
+}
+
+func (s *StoreEntry) CreateOrUpdateDiseaseType(ctx context.Context, req *pasturePb.SearchBaseConfigList) error {
+	if req.Id > 0 {
+		barn := &model.ConfigDiseaseType{Id: int64(req.Id)}
+		if err := s.DB.Model(&model.ConfigDiseaseType{}).First(barn).Error; err != nil {
+			if !errors.Is(err, gorm.ErrRecordNotFound) {
+				return xerr.WithStack(err)
+			}
+		}
+	}
+
+	if err := s.DB.Model(&model.ConfigDiseaseType{}).Where(map[string]interface{}{
+		"id": req.Id,
+	}).Assign(map[string]interface{}{
+		"name":    req.Name,
+		"remarks": req.Remarks,
+		"is_show": pasturePb.IsShow_Ok,
+	}).FirstOrCreate(&model.ConfigDiseaseType{}).Error; err != nil {
+		return xerr.WithStack(err)
+	}
+	return nil
+}
+
+func (s *StoreEntry) SearchPrescriptionList(ctx context.Context, req *pasturePb.SearchPrescriptionRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchPrescriptionResponse, error) {
+	prescriptionList := make([]*model.Prescription, 0)
+	var count int64 = 0
+
+	pref := s.DB.Model(new(model.Prescription))
+	if req.Name != "" {
+		pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
+	}
+
+	if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
+		Find(&prescriptionList).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+
+	prescriptionIds := make([]int64, 0)
+	for _, prescription := range prescriptionList {
+		prescriptionIds = append(prescriptionIds, prescription.Id)
+	}
+
+	prescriptionDrugs := make([]*model.PrescriptionDrugs, 0)
+	if len(prescriptionIds) > 0 {
+		if err := s.DB.Model(new(model.PrescriptionDrugs)).Where("prescription_id in (?)", prescriptionIds).
+			Where("is_show = ? ", pasturePb.IsShow_Ok).
+			Find(&prescriptionDrugs).Error; err != nil {
+			return nil, xerr.WithStack(err)
+		}
+	}
+
+	systemUserList, _ := s.SystemUserList(ctx)
+	return &pasturePb.SearchPrescriptionResponse{
+		Code:    http.StatusOK,
+		Message: "ok",
+		Data: &pasturePb.SearchPrescriptionData{
+			List:     model.PrescriptionSlice(prescriptionList).ToPB(systemUserList, prescriptionDrugs),
+			Total:    int32(count),
+			PageSize: pagination.PageSize,
+			Page:     pagination.Page,
+		},
+	}, nil
+}
+
+func (s *StoreEntry) CreateOrUpdatePrescription(ctx context.Context, req *pasturePb.PrescriptionRequest) error {
+	currUser, _ := s.GetCurrentSystemUser(ctx)
+	var maxUseDays, maxMeatExpiredDays, maxMilkExpiredDays int32 = 0, 0, 0
+	for _, v := range req.DrugsList {
+		if v.DrugsId <= 0 {
+			return xerr.Customf("错误处方药品的数据")
+		}
+		if v.UseDays <= 0 {
+			return xerr.Customf("错误处方药品使用天数")
+		}
+		if v.UseDays > maxUseDays {
+			maxUseDays = v.UseDays
+		}
+
+		drugs, err := s.DrugsById(ctx, int64(v.DrugsId))
+		if err != nil {
+			return xerr.WithStack(err)
+		}
+
+		if drugs.MeatExpiredDays > maxMeatExpiredDays {
+			maxMeatExpiredDays = drugs.MeatExpiredDays
+		}
+
+		if drugs.MilkExpiredDays > maxMilkExpiredDays {
+			maxMilkExpiredDays = drugs.MilkExpiredDays
+		}
+	}
+
+	applicableDisease := ""
+	if len(req.ApplicableDiseaseIds) > 0 {
+		diseaseList, err := s.DiseaseListByIds(ctx, req.ApplicableDiseaseIds)
+		if err != nil {
+			return xerr.WithStack(err)
+		}
+		applicableDiseaseList := make([]*model.ApplicableDisease, 0)
+		for _, v := range diseaseList {
+			applicableDiseaseList = append(applicableDiseaseList, &model.ApplicableDisease{
+				DiseaseId:   v.Id,
+				DiseaseName: v.Name,
+			})
+		}
+		b, _ := json.Marshal(applicableDiseaseList)
+		applicableDisease = string(b)
+	}
+	newPrescription := model.NewPrescription(req, applicableDisease, maxUseDays, maxMeatExpiredDays, maxMilkExpiredDays, currUser.Id)
+
+	if req.Id > 0 {
+		prescription := &model.Prescription{Id: int64(req.Id)}
+		if err := s.DB.Model(&model.Prescription{}).First(prescription).Error; err != nil {
+			if !errors.Is(err, gorm.ErrRecordNotFound) {
+				return xerr.WithStack(err)
+			}
+		}
+		if err := s.DB.Transaction(func(tx *gorm.DB) error {
+			if err := tx.Model(&model.Prescription{}).Where(map[string]interface{}{
+				"id": req.Id,
+			}).Updates(map[string]interface{}{
+				"name":               newPrescription.Name,
+				"applicable_disease": newPrescription.ApplicableDisease,
+				"use_days":           newPrescription.UseDays,
+				"meat_expired_days":  newPrescription.MeatExpiredDays,
+				"milk_expired_days":  newPrescription.MilkExpiredDays,
+				"remarks":            newPrescription.Remarks,
+			}).Error; err != nil {
+				return xerr.WithStack(err)
+			}
+
+			if err := tx.Model(&model.PrescriptionDrugs{}).Where("prescription_id", req.Id).Delete(&model.PrescriptionDrugs{}).Error; err != nil {
+				return xerr.WithStack(err)
+			}
+			// 创建处方药品
+			newPrescriptionDrugs := model.NewPrescriptionDrugs(int64(req.Id), req)
+			if err := tx.Create(&newPrescriptionDrugs).Error; err != nil {
+				return xerr.WithStack(err)
+			}
+
+			return nil
+		}); err != nil {
+			return xerr.WithStack(err)
+		}
+		return nil
+	}
+
+	// 创建处方
+	if err := s.DB.Transaction(func(tx *gorm.DB) error {
+		// 创建处方
+		if err := tx.Create(&newPrescription).Error; err != nil {
+			return xerr.WithStack(err)
+		}
+		// 创建处方药品
+		newPrescriptionDrugs := model.NewPrescriptionDrugs(newPrescription.Id, req)
+		if err := tx.Create(&newPrescriptionDrugs).Error; err != nil {
+			return xerr.WithStack(err)
+		}
+		return nil
+	}); err != nil {
+		return xerr.WithStack(err)
+	}
+	return nil
+}

+ 8 - 0
module/backend/sql.go

@@ -130,3 +130,11 @@ func (s *StoreEntry) DrugsById(ctx context.Context, id int64) (*model.Drugs, err
 	}
 	return drugs, nil
 }
+
+func (s *StoreEntry) DiseaseListByIds(ctx context.Context, ids []int32) ([]*model.Disease, error) {
+	diseaseList := make([]*model.Disease, 0)
+	if err := s.DB.Where("id IN ?", ids).Find(&diseaseList).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	return diseaseList, nil
+}