prescription.go 17 KB

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