123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587 |
- 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
- }
|