123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- package backend
- import (
- "context"
- "errors"
- "fmt"
- "kpt-pasture/model"
- "net/http"
- "strconv"
- "strings"
- 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 {
- currentUser, _ := s.GetCurrentSystemUser(ctx)
- semeTime := model.NewSameTime(currentUser, req)
- if req.Id > 0 {
- if err := s.DB.Model(new(model.SameTime)).Where("id = ?", req.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) {
- semeTimeList := make([]*model.SameTime, 0)
- var count int64 = 0
- pref := s.DB.Model(new(model.SameTime))
- 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,
- Message: "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 {
- sameTime := &model.SameTime{
- Id: id,
- }
- if err := s.DB.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) {
- 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)).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("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,
- Message: "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 {
- 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.GetDrugsById(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 {
- applicableDiseaseIds := make([]string, 0)
- for _, v := range req.ApplicableDiseaseIds {
- applicableDiseaseIds = append(applicableDiseaseIds, strconv.Itoa(int(v)))
- }
- applicableDisease = strings.Join(applicableDiseaseIds, ",")
- }
- newPrescription := model.NewPrescription(req, applicableDisease, maxUseDays, maxMeatExpiredDays, maxMilkExpiredDays, currUser)
- 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(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(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) 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
- err error
- systemUser *model.SystemUser
- )
- if req.ImmunizationPlanId > 0 {
- otherImmunization, err := s.GetImmunizationById(ctx, int64(req.ImmunizationPlanId))
- if err != nil {
- return xerr.WithStack(err)
- }
- req.ImmunizationPlanName = otherImmunization.Name
- }
- if req.Id > 0 {
- immunization, err = s.GetImmunizationById(ctx, int64(req.Id))
- if err != nil {
- return xerr.WithStack(err)
- }
- systemUser, _ = s.GetSystemUserById(ctx, immunization.OperationId)
- } 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,
- "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 {
- 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
- }
|