prescription.go 13 KB

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