prescription.go 17 KB

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