prescription.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package backend
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "kpt-pasture/model"
  8. "net/http"
  9. "strings"
  10. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  11. "gitee.com/xuyiping_admin/pkg/xerr"
  12. "gorm.io/gorm"
  13. )
  14. func (s *StoreEntry) SearchDiseaseList(ctx context.Context, req *pasturePb.SearchDiseaseRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchDiseaseResponse, error) {
  15. diseaseList := make([]*model.Disease, 0)
  16. var count int64 = 0
  17. pref := s.DB.Model(new(model.Disease))
  18. if req.Name != "" {
  19. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  20. }
  21. if req.DiseaseTypeId > 0 {
  22. pref.Where("disease_type = ?", req.DiseaseTypeId)
  23. }
  24. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  25. Find(&diseaseList).Error; err != nil {
  26. return nil, xerr.WithStack(err)
  27. }
  28. systemUserList, _ := s.SystemUserList(ctx)
  29. diseaseTypeList, _ := s.DiseaseTypeList(ctx)
  30. return &pasturePb.SearchDiseaseResponse{
  31. Code: http.StatusOK,
  32. Message: "ok",
  33. Data: &pasturePb.SearchDiseaseData{
  34. List: model.DiseaseSlice(diseaseList).ToPB(systemUserList, diseaseTypeList),
  35. Total: int32(count),
  36. PageSize: pagination.PageSize,
  37. Page: pagination.Page,
  38. },
  39. }, nil
  40. }
  41. func (s *StoreEntry) CreateOrUpdateDisease(ctx context.Context, req *pasturePb.SearchDiseaseList) error {
  42. currUser, _ := s.GetCurrentSystemUser(ctx)
  43. if req.Id > 0 {
  44. barn := &model.Disease{Id: int64(req.Id)}
  45. if err := s.DB.Model(&model.Disease{}).First(barn).Error; err != nil {
  46. if !errors.Is(err, gorm.ErrRecordNotFound) {
  47. return xerr.WithStack(err)
  48. }
  49. }
  50. }
  51. if err := s.DB.Model(&model.Disease{}).Where(map[string]interface{}{
  52. "id": req.Id,
  53. }).Assign(map[string]interface{}{
  54. "disease_type": req.DiseaseTypeId,
  55. "name": req.Name,
  56. "symptoms": strings.Join(req.Symptoms, "|"),
  57. "remarks": req.Remarks,
  58. "is_show": pasturePb.IsShow_Ok,
  59. "operation_id": currUser.Id,
  60. }).FirstOrCreate(&model.Disease{}).Error; err != nil {
  61. return xerr.WithStack(err)
  62. }
  63. return nil
  64. }
  65. func (s *StoreEntry) SearchDiseaseTypeList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) {
  66. diseaseTypeList := make([]*model.ConfigDiseaseType, 0)
  67. var count int64 = 0
  68. pref := s.DB.Model(new(model.ConfigDiseaseType))
  69. if req.Name != "" {
  70. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  71. }
  72. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  73. Find(&diseaseTypeList).Error; err != nil {
  74. return nil, xerr.WithStack(err)
  75. }
  76. return &pasturePb.SearchBaseConfigResponse{
  77. Code: http.StatusOK,
  78. Message: "ok",
  79. Data: &pasturePb.SearchBaseConfigData{
  80. List: model.ConfigDiseaseTypeSlice(diseaseTypeList).ToPB(),
  81. Total: int32(count),
  82. PageSize: pagination.PageSize,
  83. Page: pagination.Page,
  84. },
  85. }, nil
  86. }
  87. func (s *StoreEntry) CreateOrUpdateDiseaseType(ctx context.Context, req *pasturePb.SearchBaseConfigList) error {
  88. if req.Id > 0 {
  89. barn := &model.ConfigDiseaseType{Id: int64(req.Id)}
  90. if err := s.DB.Model(&model.ConfigDiseaseType{}).First(barn).Error; err != nil {
  91. if !errors.Is(err, gorm.ErrRecordNotFound) {
  92. return xerr.WithStack(err)
  93. }
  94. }
  95. }
  96. if err := s.DB.Model(&model.ConfigDiseaseType{}).Where(map[string]interface{}{
  97. "id": req.Id,
  98. }).Assign(map[string]interface{}{
  99. "name": req.Name,
  100. "remarks": req.Remarks,
  101. "is_show": pasturePb.IsShow_Ok,
  102. }).FirstOrCreate(&model.ConfigDiseaseType{}).Error; err != nil {
  103. return xerr.WithStack(err)
  104. }
  105. return nil
  106. }
  107. func (s *StoreEntry) SearchPrescriptionList(ctx context.Context, req *pasturePb.SearchPrescriptionRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchPrescriptionResponse, error) {
  108. prescriptionList := make([]*model.Prescription, 0)
  109. var count int64 = 0
  110. pref := s.DB.Model(new(model.Prescription))
  111. if req.Name != "" {
  112. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  113. }
  114. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  115. Find(&prescriptionList).Error; err != nil {
  116. return nil, xerr.WithStack(err)
  117. }
  118. prescriptionIds := make([]int64, 0)
  119. for _, prescription := range prescriptionList {
  120. prescriptionIds = append(prescriptionIds, prescription.Id)
  121. }
  122. prescriptionDrugs := make([]*model.PrescriptionDrugs, 0)
  123. if len(prescriptionIds) > 0 {
  124. if err := s.DB.Model(new(model.PrescriptionDrugs)).Where("prescription_id in (?)", prescriptionIds).
  125. Where("is_show = ? ", pasturePb.IsShow_Ok).
  126. Find(&prescriptionDrugs).Error; err != nil {
  127. return nil, xerr.WithStack(err)
  128. }
  129. }
  130. systemUserList, _ := s.SystemUserList(ctx)
  131. return &pasturePb.SearchPrescriptionResponse{
  132. Code: http.StatusOK,
  133. Message: "ok",
  134. Data: &pasturePb.SearchPrescriptionData{
  135. List: model.PrescriptionSlice(prescriptionList).ToPB(systemUserList, prescriptionDrugs),
  136. Total: int32(count),
  137. PageSize: pagination.PageSize,
  138. Page: pagination.Page,
  139. },
  140. }, nil
  141. }
  142. func (s *StoreEntry) CreateOrUpdatePrescription(ctx context.Context, req *pasturePb.PrescriptionRequest) error {
  143. currUser, _ := s.GetCurrentSystemUser(ctx)
  144. var maxUseDays, maxMeatExpiredDays, maxMilkExpiredDays int32 = 0, 0, 0
  145. for _, v := range req.DrugsList {
  146. if v.DrugsId <= 0 {
  147. return xerr.Customf("错误处方药品的数据")
  148. }
  149. if v.UseDays <= 0 {
  150. return xerr.Customf("错误处方药品使用天数")
  151. }
  152. if v.UseDays > maxUseDays {
  153. maxUseDays = v.UseDays
  154. }
  155. drugs, err := s.DrugsById(ctx, int64(v.DrugsId))
  156. if err != nil {
  157. return xerr.WithStack(err)
  158. }
  159. if drugs.MeatExpiredDays > maxMeatExpiredDays {
  160. maxMeatExpiredDays = drugs.MeatExpiredDays
  161. }
  162. if drugs.MilkExpiredDays > maxMilkExpiredDays {
  163. maxMilkExpiredDays = drugs.MilkExpiredDays
  164. }
  165. }
  166. applicableDisease := ""
  167. if len(req.ApplicableDiseaseIds) > 0 {
  168. diseaseList, err := s.DiseaseListByIds(ctx, req.ApplicableDiseaseIds)
  169. if err != nil {
  170. return xerr.WithStack(err)
  171. }
  172. applicableDiseaseList := make([]*model.ApplicableDisease, 0)
  173. for _, v := range diseaseList {
  174. applicableDiseaseList = append(applicableDiseaseList, &model.ApplicableDisease{
  175. DiseaseId: v.Id,
  176. DiseaseName: v.Name,
  177. })
  178. }
  179. b, _ := json.Marshal(applicableDiseaseList)
  180. applicableDisease = string(b)
  181. }
  182. newPrescription := model.NewPrescription(req, applicableDisease, maxUseDays, maxMeatExpiredDays, maxMilkExpiredDays, currUser.Id)
  183. if req.Id > 0 {
  184. prescription := &model.Prescription{Id: int64(req.Id)}
  185. if err := s.DB.Model(&model.Prescription{}).First(prescription).Error; err != nil {
  186. if !errors.Is(err, gorm.ErrRecordNotFound) {
  187. return xerr.WithStack(err)
  188. }
  189. }
  190. if err := s.DB.Transaction(func(tx *gorm.DB) error {
  191. if err := tx.Model(&model.Prescription{}).Where(map[string]interface{}{
  192. "id": req.Id,
  193. }).Updates(map[string]interface{}{
  194. "name": newPrescription.Name,
  195. "applicable_disease": newPrescription.ApplicableDisease,
  196. "use_days": newPrescription.UseDays,
  197. "meat_expired_days": newPrescription.MeatExpiredDays,
  198. "milk_expired_days": newPrescription.MilkExpiredDays,
  199. "remarks": newPrescription.Remarks,
  200. }).Error; err != nil {
  201. return xerr.WithStack(err)
  202. }
  203. if err := tx.Model(&model.PrescriptionDrugs{}).Where("prescription_id", req.Id).Delete(&model.PrescriptionDrugs{}).Error; err != nil {
  204. return xerr.WithStack(err)
  205. }
  206. // 创建处方药品
  207. newPrescriptionDrugs := model.NewPrescriptionDrugs(int64(req.Id), req)
  208. if err := tx.Create(&newPrescriptionDrugs).Error; err != nil {
  209. return xerr.WithStack(err)
  210. }
  211. return nil
  212. }); err != nil {
  213. return xerr.WithStack(err)
  214. }
  215. return nil
  216. }
  217. // 创建处方
  218. if err := s.DB.Transaction(func(tx *gorm.DB) error {
  219. // 创建处方
  220. if err := tx.Create(&newPrescription).Error; err != nil {
  221. return xerr.WithStack(err)
  222. }
  223. // 创建处方药品
  224. newPrescriptionDrugs := model.NewPrescriptionDrugs(newPrescription.Id, req)
  225. if err := tx.Create(&newPrescriptionDrugs).Error; err != nil {
  226. return xerr.WithStack(err)
  227. }
  228. return nil
  229. }); err != nil {
  230. return xerr.WithStack(err)
  231. }
  232. return nil
  233. }