|
@@ -14,6 +14,98 @@ import (
|
|
|
"gorm.io/gorm"
|
|
|
)
|
|
|
|
|
|
+func (s *StoreEntry) CreateOrUpdateSemeTime(ctx context.Context, req *pasturePb.SemeTimeRequest) error {
|
|
|
+ currentUser, _ := s.GetCurrentSystemUser(ctx)
|
|
|
+ if err := s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
+ semeTime := model.NewEventSemeTime(currentUser, req)
|
|
|
+ semeTimeFlow := model.NewEventSemeTimeFlow(semeTime.Id, req)
|
|
|
+ if req.Form.Id > 0 {
|
|
|
+ if err := tx.Model(new(model.EventSemeTime)).Where("id = ?", req.Form.Id).Updates(semeTime).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ if err := tx.Model(new(model.EventSemeTimeFlow)).Where("seme_time_id = ?", req.Form.Id).Updates(semeTimeFlow).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if err := tx.Create(&semeTime).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ if err := tx.Create(&semeTimeFlow).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+ }); err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *StoreEntry) SearchSemeTimeList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SemeTimeEventResponse, error) {
|
|
|
+ semeTimeList := make([]*model.EventSemeTime, 0)
|
|
|
+ var count int64 = 0
|
|
|
+ pref := s.DB.Model(new(model.EventSemeTime)).
|
|
|
+ Where("is_delete = ?", pasturePb.IsShow_Ok)
|
|
|
+ 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(&semeTimeList).Error; err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ weekMap := s.WeekMap()
|
|
|
+ cowTypeMap := s.CowTypeMap()
|
|
|
+ systemUser, _ := s.SystemUserList(ctx)
|
|
|
+ return &pasturePb.SemeTimeEventResponse{
|
|
|
+ Code: http.StatusOK,
|
|
|
+ Message: "ok",
|
|
|
+ Data: &pasturePb.SearchSemeTimeData{
|
|
|
+ List: model.EventSemeTimeSlice(semeTimeList).ToPB(weekMap, cowTypeMap, systemUser),
|
|
|
+ Total: int32(count),
|
|
|
+ PageSize: pagination.PageSize,
|
|
|
+ Page: pagination.Page,
|
|
|
+ },
|
|
|
+ }, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *StoreEntry) SemeTimeDetail(ctx context.Context, semeTimeId int64) (*pasturePb.SemeTimeDetailResponse, error) {
|
|
|
+ semeTime := &model.EventSemeTime{Id: semeTimeId}
|
|
|
+ if err := s.DB.Model(new(model.EventSemeTime)).
|
|
|
+ Where("is_delete = ?", pasturePb.IsShow_Ok).First(semeTime).Error; err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ semeTimeFlow := &model.EventSemeTimeFlow{}
|
|
|
+ if err := s.DB.Model(new(model.EventSemeTimeFlow)).Where("seme_time_id = ?", semeTimeId).
|
|
|
+ First(&semeTimeFlow).Error; err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ graph := &pasturePb.SemeTimeGraphData{}
|
|
|
+ if err := json.Unmarshal([]byte(semeTimeFlow.OriginalFlowData), graph); err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ return &pasturePb.SemeTimeDetailResponse{
|
|
|
+ Code: http.StatusOK,
|
|
|
+ Message: "ok",
|
|
|
+ Data: &pasturePb.SemeTimeRequest{
|
|
|
+ Form: &pasturePb.SemeTimeFormData{
|
|
|
+ Id: int32(semeTime.Id),
|
|
|
+ Name: semeTime.Name,
|
|
|
+ WeekType: semeTime.WeekType,
|
|
|
+ PostpartumDays: semeTime.PostpartumDays,
|
|
|
+ CowType: semeTime.CowType,
|
|
|
+ Remarks: semeTime.Remarks,
|
|
|
+ },
|
|
|
+ Graph: graph,
|
|
|
+ },
|
|
|
+ }, 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
|
|
@@ -262,3 +354,87 @@ func (s *StoreEntry) CreateOrUpdatePrescription(ctx context.Context, req *pastur
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+func (s *StoreEntry) ImmunizationList(ctx context.Context, req *pasturePb.ImmunizationRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchImmunizationResponse, error) {
|
|
|
+ immunizationList := make([]*model.ImmunizationPlan, 0)
|
|
|
+ var count int64 = 0
|
|
|
+ pref := s.DB.Model(new(model.ImmunizationPlan))
|
|
|
+ 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(&immunizationList).Error; err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ CowTypeOptions := s.ImmunizationCowTypeEnumList()
|
|
|
+ conditionsOptions := s.ImmunizationConditionsEnumList()
|
|
|
+ return &pasturePb.SearchImmunizationResponse{
|
|
|
+ Code: http.StatusOK,
|
|
|
+ Message: "ok",
|
|
|
+ Data: &pasturePb.SearchImmunizationData{
|
|
|
+ List: model.ImmunizationPlanSlice(immunizationList).ToPB(CowTypeOptions, conditionsOptions),
|
|
|
+ Total: int32(count),
|
|
|
+ PageSize: pagination.PageSize,
|
|
|
+ Page: pagination.Page,
|
|
|
+ },
|
|
|
+ }, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *StoreEntry) CreatedOrUpdateImmunization(ctx context.Context, req *pasturePb.ImmunizationRequest) error {
|
|
|
+ var immunization *model.ImmunizationPlan
|
|
|
+ var err error
|
|
|
+ if req.Id > 0 {
|
|
|
+ immunization, err = s.ImmunizationById(ctx, int64(req.Id))
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ immunization.Name = req.Name
|
|
|
+ immunization.CowType = req.CowType
|
|
|
+ immunization.Conditions = req.Conditions
|
|
|
+ immunization.Value = int64(req.Value)
|
|
|
+ immunization.Value2 = int64(req.Value2)
|
|
|
+ immunization.IsShow = req.IsShow
|
|
|
+ } else {
|
|
|
+ systemUser, _ := s.GetCurrentSystemUser(ctx)
|
|
|
+ immunization = model.NewImmunizationPlan(systemUser, req)
|
|
|
+ }
|
|
|
+ if err = s.DB.Model(&model.ImmunizationPlan{}).Where(map[string]interface{}{
|
|
|
+ "id": req.Id,
|
|
|
+ }).Assign(map[string]interface{}{
|
|
|
+ "name": immunization.Name,
|
|
|
+ "cow_type": immunization.CowType,
|
|
|
+ "conditions": immunization.Conditions,
|
|
|
+ "value": immunization.Value,
|
|
|
+ "value2": immunization.Value2,
|
|
|
+ "is_show": immunization.IsShow,
|
|
|
+ "operation_id": immunization.OperationId,
|
|
|
+ "operation_name": immunization.OperationName,
|
|
|
+ }).FirstOrCreate(&model.ImmunizationPlan{}).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *StoreEntry) ImmunizationIsShow(ctx context.Context, id int64) error {
|
|
|
+ immunizationPlan := &model.ImmunizationPlan{
|
|
|
+ Id: id,
|
|
|
+ }
|
|
|
+ if err := s.DB.First(immunizationPlan).Error; err != nil {
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ return xerr.Custom("该免疫计划不存在")
|
|
|
+ }
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ isShow := pasturePb.IsShow_No
|
|
|
+ if immunizationPlan.IsShow == pasturePb.IsShow_No {
|
|
|
+ isShow = pasturePb.IsShow_Ok
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := s.DB.Model(immunizationPlan).Update("is_show", isShow).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|