prescription.go 16 KB

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