package backend import ( "context" "errors" "fmt" "kpt-pasture/model" "net/http" "strconv" "strings" "gitee.com/xuyiping_admin/pkg/logger/zaplog" "go.uber.org/zap" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" "gitee.com/xuyiping_admin/pkg/xerr" "gorm.io/gorm" ) func (s *StoreEntry) CreateOrUpdateSameTime(ctx context.Context, req *pasturePb.SearchSameTimeList) error { userModel, err := s.GetUserModel(ctx) if err != nil { return xerr.WithStack(err) } semeTime := model.NewSameTime(userModel.AppPasture.Id, userModel.SystemUser, req) if req.Id > 0 { if err = s.DB.Model(new(model.SameTime)). Where("id = ?", req.Id). Where("pasture_id = ?", userModel.AppPasture.Id). Updates(map[string]interface{}{ "name": semeTime.Name, "week_type": semeTime.WeekType, "cow_type": semeTime.CowType, "postpartum_days_start": semeTime.PostpartumDaysStart, "postpartum_days_end": semeTime.PostpartumDaysEnd, "collate_nodes": semeTime.CollateNodes, "is_show": semeTime.IsShow, "remarks": semeTime.Remarks, }).Error; err != nil { return xerr.WithStack(err) } } else { if err = s.DB.Create(&semeTime).Error; err != nil { return xerr.WithStack(err) } } return nil } func (s *StoreEntry) SearchSameTimeList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SameTimeResponse, error) { userModel, err := s.GetUserModel(ctx) if err != nil { return nil, xerr.WithStack(err) } semeTimeList := make([]*model.SameTime, 0) var count int64 = 0 pref := s.DB.Model(new(model.SameTime)).Where("pasture_id = ?", userModel.AppPasture.Id) 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() sameTimeCowTypeMap := s.SameTimeCowTypeMap() systemUser, _ := s.SystemUserList(ctx) return &pasturePb.SameTimeResponse{ Code: http.StatusOK, Msg: "ok", Data: &pasturePb.SearchSameTimeData{ List: model.SameTimeSlice(semeTimeList).ToPB(weekMap, sameTimeCowTypeMap, systemUser), Total: int32(count), PageSize: pagination.PageSize, Page: pagination.Page, }, }, nil } func (s *StoreEntry) SameTimeIsShow(ctx context.Context, id int64) error { userModel, err := s.GetUserModel(ctx) if err != nil { return xerr.WithStack(err) } sameTime := &model.SameTime{} if err = s.DB.Model(new(model.SameTime)). Where("id = ?", id). Where("pasture_id = ?", userModel.AppPasture.Id). First(sameTime).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return xerr.Custom("该同期数据不存在") } return xerr.WithStack(err) } isShow := pasturePb.IsShow_No if sameTime.IsShow == pasturePb.IsShow_No { isShow = pasturePb.IsShow_Ok } if err = s.DB.Model(sameTime).Update("is_show", isShow).Error; err != nil { return xerr.WithStack(err) } return nil } func (s *StoreEntry) SearchDiseaseList(ctx context.Context, req *pasturePb.SearchDiseaseRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchDiseaseResponse, error) { userModel, err := s.GetUserModel(ctx) if err != nil { return nil, xerr.WithStack(err) } diseaseList := make([]*model.Disease, 0) var count int64 = 0 pref := s.DB.Model(new(model.Disease)).Where("pasture_id = ?", userModel.AppPasture.Id) 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, Msg: "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 { userModel, err := s.GetUserModel(ctx) if err != nil { return xerr.WithStack(err) } if req.Id > 0 { barn := &model.Disease{Id: int64(req.Id)} if err = s.DB.Model(new(model.Disease)).First(barn).Error; err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { return xerr.WithStack(err) } } } diseaseTypeList, _ := s.DiseaseTypeList(ctx) if req.DiseaseTypeId > 0 { for _, v := range diseaseTypeList { if int32(v.Id) == req.DiseaseTypeId { req.DiseaseTypeName = v.Name } } } if err = s.DB.Model(new(model.Disease)).Where(map[string]interface{}{ "id": req.Id, "pasture_id": userModel.AppPasture.Id, }).Assign(map[string]interface{}{ "disease_type": req.DiseaseTypeId, "disease_type_name": req.DiseaseTypeName, "name": req.Name, "symptoms": strings.Join(req.Symptoms, "|"), "remarks": req.Remarks, "is_show": pasturePb.IsShow_Ok, "operation_id": userModel.SystemUser.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) { userModel, err := s.GetUserModel(ctx) if err != nil { return nil, xerr.WithStack(err) } diseaseTypeList := make([]*model.ConfigDiseaseType, 0) var count int64 = 0 pref := s.DB.Model(new(model.ConfigDiseaseType)).Where("pasture_id = ?", userModel.AppPasture.Id) 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, Msg: "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 { userModel, err := s.GetUserModel(ctx) if err != nil { return xerr.WithStack(err) } if req.Id > 0 { barn := &model.ConfigDiseaseType{Id: int64(req.Id)} if err = s.DB.Model(new(model.ConfigDiseaseType)). Where("pasture_id = ?", userModel.AppPasture.Id). 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, "pasture_id": userModel.AppPasture.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) { userModel, err := s.GetUserModel(ctx) if err != nil { return nil, xerr.WithStack(err) } prescriptionList := make([]*model.Prescription, 0) var count int64 = 0 pref := s.DB.Model(new(model.Prescription)). Where("pasture_id = ?", userModel.AppPasture.Id). Where("is_show = ?", 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(&prescriptionList).Error; err != nil { return nil, xerr.WithStack(err) } prescriptionIds := make([]int32, 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("pasture_id = ?", userModel.AppPasture.Id). Where("prescription_id in (?)", prescriptionIds). Where("is_show = ? ", pasturePb.IsShow_Ok). Find(&prescriptionDrugs).Error; err != nil { return nil, xerr.WithStack(err) } } diseaseMaps, _ := s.DiseaseMaps(ctx) return &pasturePb.SearchPrescriptionResponse{ Code: http.StatusOK, Msg: "ok", Data: &pasturePb.SearchPrescriptionData{ List: model.PrescriptionSlice(prescriptionList).ToPB(prescriptionDrugs, diseaseMaps), Total: int32(count), PageSize: pagination.PageSize, Page: pagination.Page, }, }, nil } func (s *StoreEntry) CreateOrUpdatePrescription(ctx context.Context, req *pasturePb.PrescriptionRequest) error { userModel, err := s.GetUserModel(ctx) if err != nil { return xerr.WithStack(err) } 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.GetDrugsById(ctx, userModel.AppPasture.Id, 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 { applicableDiseaseIds := make([]string, 0) for _, v := range req.ApplicableDiseaseIds { applicableDiseaseIds = append(applicableDiseaseIds, strconv.Itoa(int(v))) } applicableDisease = strings.Join(applicableDiseaseIds, ",") } newPrescription := model.NewPrescription(userModel.AppPasture.Id, req, applicableDisease, maxUseDays, maxMeatExpiredDays, maxMilkExpiredDays, userModel.SystemUser) if req.Id > 0 { prescription := &model.Prescription{Id: 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(new(model.PrescriptionDrugs)).Where("prescription_id", req.Id). Update("is_show", pasturePb.IsShow_No).Error; err != nil { return xerr.WithStack(err) } // 创建处方药品 newPrescriptionDrugs := model.NewPrescriptionDrugs(userModel.AppPasture.Id, req.Id, req.DrugsList) 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(userModel.AppPasture.Id, newPrescription.Id, req.DrugsList) if err = tx.Create(&newPrescriptionDrugs).Error; err != nil { return xerr.WithStack(err) } return nil }); err != nil { return xerr.WithStack(err) } return nil } func (s *StoreEntry) PrescriptionDetail(ctx context.Context, id int64) (*pasturePb.PrescriptionDetailResponse, error) { userModel, err := s.GetUserModel(ctx) if err != nil { return nil, xerr.WithStack(err) } prescriptionData, err := s.GetPrescriptionById(ctx, userModel.AppPasture.Id, int32(id)) if err != nil { return nil, xerr.WithStack(err) } prescriptionDrugsList, err := s.PrescriptionDrugsByPrescriptionId(ctx, userModel.AppPasture.Id, int32(id)) if err != nil { return nil, xerr.WithStack(err) } return &pasturePb.PrescriptionDetailResponse{ Code: http.StatusOK, Msg: "ok", Data: &pasturePb.PrescriptionRequest{ Id: prescriptionData.Id, Name: prescriptionData.Name, ApplicableDiseaseIds: make([]int32, 0), IsShow: prescriptionData.IsShow, Remarks: prescriptionData.Remarks, DrugsList: model.PrescriptionDrugsSlice(prescriptionDrugsList).ToPB(), }, }, nil } func (s *StoreEntry) ImmunizationList(ctx context.Context, req *pasturePb.ImmunizationRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchImmunizationResponse, error) { userModel, err := s.GetUserModel(ctx) if err != nil { return nil, xerr.WithStack(err) } immunizationList := make([]*model.ImmunizationPlan, 0) var count int64 = 0 pref := s.DB.Model(new(model.ImmunizationPlan)). Where("pasture_id = ?", userModel.AppPasture.Id). Where("is_show = ?", 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(&immunizationList).Error; err != nil { return nil, xerr.WithStack(err) } CowTypeOptions := s.ImmunizationCowTypeEnumList("") conditionsOptions := s.ImmunizationConditionsEnumList("") return &pasturePb.SearchImmunizationResponse{ Code: http.StatusOK, Msg: "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 err error systemUser *model.SystemUser ) userModel, err := s.GetUserModel(ctx) if err != nil { return xerr.WithStack(err) } if req.Conditions == pasturePb.ImmunizationConditions_Other_Vaccine_After && req.ImmunizationPlanId <= 0 { return xerr.Custom("请选择具体的免疫计划") } if req.ImmunizationPlanId > 0 { otherImmunization, err := s.GetImmunizationById(ctx, userModel.AppPasture.Id, int64(req.ImmunizationPlanId)) if err != nil { return xerr.WithStack(err) } req.ImmunizationPlanName = otherImmunization.Name } if req.Id > 0 { immunization, err = s.GetImmunizationById(ctx, userModel.AppPasture.Id, int64(req.Id)) if err != nil { return xerr.WithStack(err) } systemUser, _ = s.GetSystemUserById(ctx, immunization.OperationId) } else { systemUser, _ = s.GetCurrentSystemUser(ctx) } immunization = model.NewImmunizationPlan(userModel.AppPasture.Id, systemUser, req) if err = s.DB.Model(&model.ImmunizationPlan{}).Where(map[string]interface{}{ "id": req.Id, "pasture_id": userModel.AppPasture.Id, }).Assign(map[string]interface{}{ "name": immunization.Name, "cow_type": immunization.CowType, "conditions": immunization.Conditions, "value": immunization.Value, "immunization_plan_id": immunization.ImmunizationPlanId, "immunization_plan_name": immunization.ImmunizationPlanName, "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 { userModel, err := s.GetUserModel(ctx) if err != nil { return xerr.WithStack(err) } immunizationPlan := &model.ImmunizationPlan{ Id: id, } if err = s.DB.Model(new(model.ImmunizationPlan)). Where("id = ?", id). Where("pasture_id = ?", userModel.AppPasture.Id). 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 } func (s *StoreEntry) SystemBasicList(ctx context.Context) (*pasturePb.SearchBaseDataConfigResponse, error) { userModel, err := s.GetUserModel(ctx) if err != nil { return nil, xerr.WithStack(err) } systemBasicList := make([]*model.SystemBasic, 0) if err = s.DB.Model(new(model.SystemBasic)). Where("pasture_id = ?", userModel.AppPasture.Id). Where("is_show = ?", pasturePb.IsShow_Ok).Find(&systemBasicList).Error; err != nil { return nil, xerr.WithStack(err) } return &pasturePb.SearchBaseDataConfigResponse{ Code: http.StatusOK, Msg: "ok", Data: &pasturePb.SearchBaseDataConfigData{ List: model.SystemBasicSlice(systemBasicList).ToPb(), Total: int32(len(systemBasicList)), }, }, nil } func (s *StoreEntry) SystemBasicEdit(ctx context.Context, req *pasturePb.BaseDataConfigBatch) error { userModel, err := s.GetUserModel(ctx) if err != nil { return xerr.WithStack(err) } for _, v := range req.Item { systemBasic := &model.SystemBasic{Id: v.Id} if err = s.DB.Model(systemBasic). Where("is_show = ?", pasturePb.IsShow_Ok). Where("pasture_id = ?", userModel.AppPasture.Id). First(systemBasic).Error; err != nil { zaplog.Error("SystemBasicEdit", zap.Any("err", err), zap.Any("req", req)) return xerr.Customf("该配置信息不存在: %d", v.Id) } systemBasic.EditUpdate(userModel.AppPasture.Id, v.MinValue, v.MaxValue, v.WeekValue, userModel.SystemUser) if err = s.DB.Model(systemBasic). Select("min_value", "max_value", "week_value", "operation_id", "operation_name"). Updates(systemBasic).Error; err != nil { zaplog.Error("SystemBasicEdit", zap.Any("err", err), zap.Any("req", req)) return xerr.WithStack(err) } } return nil }