prescription.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. package backend
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "kpt-pasture/model"
  7. "net/http"
  8. "strconv"
  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) CreateOrUpdateSameTime(ctx context.Context, req *pasturePb.SearchSameTimeList) error {
  15. currentUser, _ := s.GetCurrentSystemUser(ctx)
  16. semeTime := model.NewSameTime(currentUser, req)
  17. if req.Id > 0 {
  18. if err := s.DB.Model(new(model.SameTime)).Where("id = ?", req.Id).Updates(map[string]interface{}{
  19. "name": semeTime.Name,
  20. "week_type": semeTime.WeekType,
  21. "cow_type": semeTime.CowType,
  22. "postpartum_days_start": semeTime.PostpartumDaysStart,
  23. "postpartum_days_end": semeTime.PostpartumDaysEnd,
  24. "collate_nodes": semeTime.CollateNodes,
  25. "is_show": semeTime.IsShow,
  26. "remarks": semeTime.Remarks,
  27. }).Error; err != nil {
  28. return xerr.WithStack(err)
  29. }
  30. } else {
  31. if err := s.DB.Create(&semeTime).Error; err != nil {
  32. return xerr.WithStack(err)
  33. }
  34. }
  35. return nil
  36. }
  37. func (s *StoreEntry) SearchSameTimeList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SameTimeResponse, error) {
  38. semeTimeList := make([]*model.SameTime, 0)
  39. var count int64 = 0
  40. pref := s.DB.Model(new(model.SameTime))
  41. if req.Name != "" {
  42. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  43. }
  44. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  45. Find(&semeTimeList).Error; err != nil {
  46. return nil, xerr.WithStack(err)
  47. }
  48. weekMap := s.WeekMap()
  49. sameTimeCowTypeMap := s.SameTimeCowTypeMap()
  50. systemUser, _ := s.SystemUserList(ctx)
  51. return &pasturePb.SameTimeResponse{
  52. Code: http.StatusOK,
  53. Message: "ok",
  54. Data: &pasturePb.SearchSameTimeData{
  55. List: model.SameTimeSlice(semeTimeList).ToPB(weekMap, sameTimeCowTypeMap, systemUser),
  56. Total: int32(count),
  57. PageSize: pagination.PageSize,
  58. Page: pagination.Page,
  59. },
  60. }, nil
  61. }
  62. func (s *StoreEntry) SameTimeIsShow(ctx context.Context, id int64) error {
  63. sameTime := &model.SameTime{
  64. Id: id,
  65. }
  66. if err := s.DB.First(sameTime).Error; err != nil {
  67. if errors.Is(err, gorm.ErrRecordNotFound) {
  68. return xerr.Custom("该同期数据不存在")
  69. }
  70. return xerr.WithStack(err)
  71. }
  72. isShow := pasturePb.IsShow_No
  73. if sameTime.IsShow == pasturePb.IsShow_No {
  74. isShow = pasturePb.IsShow_Ok
  75. }
  76. if err := s.DB.Model(sameTime).Update("is_show", isShow).Error; err != nil {
  77. return xerr.WithStack(err)
  78. }
  79. return nil
  80. }
  81. func (s *StoreEntry) SearchDiseaseList(ctx context.Context, req *pasturePb.SearchDiseaseRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchDiseaseResponse, error) {
  82. diseaseList := make([]*model.Disease, 0)
  83. var count int64 = 0
  84. pref := s.DB.Model(new(model.Disease))
  85. if req.Name != "" {
  86. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  87. }
  88. if req.DiseaseTypeId > 0 {
  89. pref.Where("disease_type = ?", req.DiseaseTypeId)
  90. }
  91. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  92. Find(&diseaseList).Error; err != nil {
  93. return nil, xerr.WithStack(err)
  94. }
  95. systemUserList, _ := s.SystemUserList(ctx)
  96. diseaseTypeList, _ := s.DiseaseTypeList(ctx)
  97. return &pasturePb.SearchDiseaseResponse{
  98. Code: http.StatusOK,
  99. Message: "ok",
  100. Data: &pasturePb.SearchDiseaseData{
  101. List: model.DiseaseSlice(diseaseList).ToPB(systemUserList, diseaseTypeList),
  102. Total: int32(count),
  103. PageSize: pagination.PageSize,
  104. Page: pagination.Page,
  105. },
  106. }, nil
  107. }
  108. func (s *StoreEntry) CreateOrUpdateDisease(ctx context.Context, req *pasturePb.SearchDiseaseList) error {
  109. currUser, _ := s.GetCurrentSystemUser(ctx)
  110. if req.Id > 0 {
  111. barn := &model.Disease{Id: int64(req.Id)}
  112. if err := s.DB.Model(&model.Disease{}).First(barn).Error; err != nil {
  113. if !errors.Is(err, gorm.ErrRecordNotFound) {
  114. return xerr.WithStack(err)
  115. }
  116. }
  117. }
  118. if err := s.DB.Model(&model.Disease{}).Where(map[string]interface{}{
  119. "id": req.Id,
  120. }).Assign(map[string]interface{}{
  121. "disease_type": req.DiseaseTypeId,
  122. "name": req.Name,
  123. "symptoms": strings.Join(req.Symptoms, "|"),
  124. "remarks": req.Remarks,
  125. "is_show": pasturePb.IsShow_Ok,
  126. "operation_id": currUser.Id,
  127. }).FirstOrCreate(&model.Disease{}).Error; err != nil {
  128. return xerr.WithStack(err)
  129. }
  130. return nil
  131. }
  132. func (s *StoreEntry) SearchDiseaseTypeList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) {
  133. diseaseTypeList := make([]*model.ConfigDiseaseType, 0)
  134. var count int64 = 0
  135. pref := s.DB.Model(new(model.ConfigDiseaseType))
  136. if req.Name != "" {
  137. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  138. }
  139. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  140. Find(&diseaseTypeList).Error; err != nil {
  141. return nil, xerr.WithStack(err)
  142. }
  143. return &pasturePb.SearchBaseConfigResponse{
  144. Code: http.StatusOK,
  145. Message: "ok",
  146. Data: &pasturePb.SearchBaseConfigData{
  147. List: model.ConfigDiseaseTypeSlice(diseaseTypeList).ToPB(),
  148. Total: int32(count),
  149. PageSize: pagination.PageSize,
  150. Page: pagination.Page,
  151. },
  152. }, nil
  153. }
  154. func (s *StoreEntry) CreateOrUpdateDiseaseType(ctx context.Context, req *pasturePb.SearchBaseConfigList) error {
  155. if req.Id > 0 {
  156. barn := &model.ConfigDiseaseType{Id: int64(req.Id)}
  157. if err := s.DB.Model(&model.ConfigDiseaseType{}).First(barn).Error; err != nil {
  158. if !errors.Is(err, gorm.ErrRecordNotFound) {
  159. return xerr.WithStack(err)
  160. }
  161. }
  162. }
  163. if err := s.DB.Model(&model.ConfigDiseaseType{}).Where(map[string]interface{}{
  164. "id": req.Id,
  165. }).Assign(map[string]interface{}{
  166. "name": req.Name,
  167. "remarks": req.Remarks,
  168. "is_show": pasturePb.IsShow_Ok,
  169. }).FirstOrCreate(&model.ConfigDiseaseType{}).Error; err != nil {
  170. return xerr.WithStack(err)
  171. }
  172. return nil
  173. }
  174. func (s *StoreEntry) SearchPrescriptionList(ctx context.Context, req *pasturePb.SearchPrescriptionRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchPrescriptionResponse, error) {
  175. prescriptionList := make([]*model.Prescription, 0)
  176. var count int64 = 0
  177. pref := s.DB.Model(new(model.Prescription)).Where("is_show = ?", pasturePb.IsShow_Ok)
  178. if req.Name != "" {
  179. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  180. }
  181. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  182. Find(&prescriptionList).Error; err != nil {
  183. return nil, xerr.WithStack(err)
  184. }
  185. prescriptionIds := make([]int32, 0)
  186. for _, prescription := range prescriptionList {
  187. prescriptionIds = append(prescriptionIds, prescription.Id)
  188. }
  189. prescriptionDrugs := make([]*model.PrescriptionDrugs, 0)
  190. if len(prescriptionIds) > 0 {
  191. if err := s.DB.Model(new(model.PrescriptionDrugs)).Where("prescription_id in (?)", prescriptionIds).
  192. Where("is_show = ? ", pasturePb.IsShow_Ok).
  193. Find(&prescriptionDrugs).Error; err != nil {
  194. return nil, xerr.WithStack(err)
  195. }
  196. }
  197. diseaseMaps, _ := s.DiseaseMaps(ctx)
  198. return &pasturePb.SearchPrescriptionResponse{
  199. Code: http.StatusOK,
  200. Message: "ok",
  201. Data: &pasturePb.SearchPrescriptionData{
  202. List: model.PrescriptionSlice(prescriptionList).ToPB(prescriptionDrugs, diseaseMaps),
  203. Total: int32(count),
  204. PageSize: pagination.PageSize,
  205. Page: pagination.Page,
  206. },
  207. }, nil
  208. }
  209. func (s *StoreEntry) CreateOrUpdatePrescription(ctx context.Context, req *pasturePb.PrescriptionRequest) error {
  210. currUser, _ := s.GetCurrentSystemUser(ctx)
  211. var maxUseDays, maxMeatExpiredDays, maxMilkExpiredDays int32 = 0, 0, 0
  212. for _, v := range req.DrugsList {
  213. if v.DrugsId <= 0 {
  214. return xerr.Customf("错误处方药品的数据")
  215. }
  216. if v.UseDays <= 0 {
  217. return xerr.Customf("错误处方药品使用天数")
  218. }
  219. if v.UseDays > maxUseDays {
  220. maxUseDays = v.UseDays
  221. }
  222. drugs, err := s.GetDrugsById(ctx, int64(v.DrugsId))
  223. if err != nil {
  224. return xerr.WithStack(err)
  225. }
  226. if drugs.MeatExpiredDays > maxMeatExpiredDays {
  227. maxMeatExpiredDays = drugs.MeatExpiredDays
  228. }
  229. if drugs.MilkExpiredDays > maxMilkExpiredDays {
  230. maxMilkExpiredDays = drugs.MilkExpiredDays
  231. }
  232. }
  233. applicableDisease := ""
  234. if len(req.ApplicableDiseaseIds) > 0 {
  235. applicableDiseaseIds := make([]string, 0)
  236. for _, v := range req.ApplicableDiseaseIds {
  237. applicableDiseaseIds = append(applicableDiseaseIds, strconv.Itoa(int(v)))
  238. }
  239. applicableDisease = strings.Join(applicableDiseaseIds, ",")
  240. }
  241. newPrescription := model.NewPrescription(req, applicableDisease, maxUseDays, maxMeatExpiredDays, maxMilkExpiredDays, currUser)
  242. if req.Id > 0 {
  243. prescription := &model.Prescription{Id: req.Id}
  244. if err := s.DB.Model(&model.Prescription{}).First(prescription).Error; err != nil {
  245. if !errors.Is(err, gorm.ErrRecordNotFound) {
  246. return xerr.WithStack(err)
  247. }
  248. }
  249. if err := s.DB.Transaction(func(tx *gorm.DB) error {
  250. if err := tx.Model(&model.Prescription{}).Where(map[string]interface{}{
  251. "id": req.Id,
  252. }).Updates(map[string]interface{}{
  253. "name": newPrescription.Name,
  254. "applicable_disease": newPrescription.ApplicableDisease,
  255. "use_days": newPrescription.UseDays,
  256. "meat_expired_days": newPrescription.MeatExpiredDays,
  257. "milk_expired_days": newPrescription.MilkExpiredDays,
  258. "remarks": newPrescription.Remarks,
  259. }).Error; err != nil {
  260. return xerr.WithStack(err)
  261. }
  262. if err := tx.Model(new(model.PrescriptionDrugs)).Where("prescription_id", req.Id).
  263. Update("is_show", pasturePb.IsShow_No).Error; err != nil {
  264. return xerr.WithStack(err)
  265. }
  266. // 创建处方药品
  267. newPrescriptionDrugs := model.NewPrescriptionDrugs(req.Id, req.DrugsList)
  268. if err := tx.Create(&newPrescriptionDrugs).Error; err != nil {
  269. return xerr.WithStack(err)
  270. }
  271. return nil
  272. }); err != nil {
  273. return xerr.WithStack(err)
  274. }
  275. return nil
  276. }
  277. // 创建处方
  278. if err := s.DB.Transaction(func(tx *gorm.DB) error {
  279. // 创建处方
  280. if err := tx.Create(&newPrescription).Error; err != nil {
  281. return xerr.WithStack(err)
  282. }
  283. // 创建处方药品
  284. newPrescriptionDrugs := model.NewPrescriptionDrugs(newPrescription.Id, req.DrugsList)
  285. if err := tx.Create(&newPrescriptionDrugs).Error; err != nil {
  286. return xerr.WithStack(err)
  287. }
  288. return nil
  289. }); err != nil {
  290. return xerr.WithStack(err)
  291. }
  292. return nil
  293. }
  294. func (s *StoreEntry) ImmunizationList(ctx context.Context, req *pasturePb.ImmunizationRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchImmunizationResponse, error) {
  295. immunizationList := make([]*model.ImmunizationPlan, 0)
  296. var count int64 = 0
  297. pref := s.DB.Model(new(model.ImmunizationPlan))
  298. if req.Name != "" {
  299. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  300. }
  301. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  302. Find(&immunizationList).Error; err != nil {
  303. return nil, xerr.WithStack(err)
  304. }
  305. CowTypeOptions := s.ImmunizationCowTypeEnumList("")
  306. conditionsOptions := s.ImmunizationConditionsEnumList("")
  307. return &pasturePb.SearchImmunizationResponse{
  308. Code: http.StatusOK,
  309. Message: "ok",
  310. Data: &pasturePb.SearchImmunizationData{
  311. List: model.ImmunizationPlanSlice(immunizationList).ToPB(CowTypeOptions, conditionsOptions),
  312. Total: int32(count),
  313. PageSize: pagination.PageSize,
  314. Page: pagination.Page,
  315. },
  316. }, nil
  317. }
  318. func (s *StoreEntry) CreatedOrUpdateImmunization(ctx context.Context, req *pasturePb.ImmunizationRequest) error {
  319. var (
  320. immunization *model.ImmunizationPlan
  321. err error
  322. systemUser *model.SystemUser
  323. )
  324. if req.ImmunizationPlanId > 0 {
  325. otherImmunization, err := s.GetImmunizationById(ctx, int64(req.ImmunizationPlanId))
  326. if err != nil {
  327. return xerr.WithStack(err)
  328. }
  329. req.ImmunizationPlanName = otherImmunization.Name
  330. }
  331. if req.Id > 0 {
  332. immunization, err = s.GetImmunizationById(ctx, int64(req.Id))
  333. if err != nil {
  334. return xerr.WithStack(err)
  335. }
  336. systemUser, _ = s.GetSystemUserById(ctx, immunization.OperationId)
  337. } else {
  338. systemUser, _ = s.GetCurrentSystemUser(ctx)
  339. }
  340. immunization = model.NewImmunizationPlan(systemUser, req)
  341. if err = s.DB.Model(&model.ImmunizationPlan{}).Where(map[string]interface{}{
  342. "id": req.Id,
  343. }).Assign(map[string]interface{}{
  344. "name": immunization.Name,
  345. "cow_type": immunization.CowType,
  346. "conditions": immunization.Conditions,
  347. "value": immunization.Value,
  348. "immunization_plan_id": immunization.ImmunizationPlanId,
  349. "immunization_plan_name": immunization.ImmunizationPlanName,
  350. "is_show": immunization.IsShow,
  351. "operation_id": immunization.OperationId,
  352. "operation_name": immunization.OperationName,
  353. }).FirstOrCreate(&model.ImmunizationPlan{}).Error; err != nil {
  354. return xerr.WithStack(err)
  355. }
  356. return nil
  357. }
  358. func (s *StoreEntry) ImmunizationIsShow(ctx context.Context, id int64) error {
  359. immunizationPlan := &model.ImmunizationPlan{
  360. Id: id,
  361. }
  362. if err := s.DB.First(immunizationPlan).Error; err != nil {
  363. if errors.Is(err, gorm.ErrRecordNotFound) {
  364. return xerr.Custom("该免疫计划不存在")
  365. }
  366. return xerr.WithStack(err)
  367. }
  368. isShow := pasturePb.IsShow_No
  369. if immunizationPlan.IsShow == pasturePb.IsShow_No {
  370. isShow = pasturePb.IsShow_Ok
  371. }
  372. if err := s.DB.Model(immunizationPlan).Update("is_show", isShow).Error; err != nil {
  373. return xerr.WithStack(err)
  374. }
  375. return nil
  376. }