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