prescription.go 18 KB

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