prescription.go 17 KB

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