system_basic.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package backend
  2. import (
  3. "context"
  4. "kpt-pasture/model"
  5. "net/http"
  6. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  7. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  8. "gitee.com/xuyiping_admin/pkg/xerr"
  9. "go.uber.org/zap"
  10. )
  11. func (s *StoreEntry) SystemBasicList(ctx context.Context) (*pasturePb.SearchBaseDataConfigResponse, error) {
  12. userModel, err := s.GetUserModel(ctx)
  13. if err != nil {
  14. return nil, xerr.WithStack(err)
  15. }
  16. systemBasicList := make([]*model.SystemBasic, 0)
  17. if err = s.DB.Model(new(model.SystemBasic)).
  18. Where("pasture_id = ?", userModel.AppPasture.Id).
  19. Where("is_show = ?", pasturePb.IsShow_Ok).Find(&systemBasicList).Error; err != nil {
  20. return nil, xerr.WithStack(err)
  21. }
  22. return &pasturePb.SearchBaseDataConfigResponse{
  23. Code: http.StatusOK,
  24. Msg: "ok",
  25. Data: &pasturePb.SearchBaseDataConfigData{
  26. List: model.SystemBasicSlice(systemBasicList).ToPb(),
  27. Total: int32(len(systemBasicList)),
  28. },
  29. }, nil
  30. }
  31. func (s *StoreEntry) SystemBasicEdit(ctx context.Context, req *pasturePb.BaseDataConfigBatch) error {
  32. userModel, err := s.GetUserModel(ctx)
  33. if err != nil {
  34. return xerr.WithStack(err)
  35. }
  36. for _, v := range req.Item {
  37. systemBasic := &model.SystemBasic{Id: v.Id}
  38. if err = s.DB.Model(systemBasic).
  39. Where("is_show = ?", pasturePb.IsShow_Ok).
  40. Where("pasture_id = ?", userModel.AppPasture.Id).
  41. First(systemBasic).Error; err != nil {
  42. zaplog.Error("SystemBasicEdit", zap.Any("err", err), zap.Any("req", req))
  43. return xerr.Customf("该配置信息不存在: %d", v.Id)
  44. }
  45. systemBasic.EditUpdate(v.MinValue, v.MaxValue, v.WeekValue, userModel.SystemUser)
  46. if err = s.DB.Model(systemBasic).
  47. Select("min_value", "max_value", "week_value", "operation_id", "operation_name").
  48. Updates(systemBasic).Error; err != nil {
  49. zaplog.Error("SystemBasicEdit", zap.Any("err", err), zap.Any("req", req))
  50. return xerr.WithStack(err)
  51. }
  52. }
  53. return nil
  54. }
  55. func (s *StoreEntry) FindSystemBasic(ctx context.Context, pastureId int64, name string) (*pasturePb.BaseDataConfig, error) {
  56. systemBasic := &model.SystemBasic{}
  57. if err := s.DB.Model(new(model.SystemBasic)).
  58. Where("pasture_id = ?", pastureId).
  59. Where("name = ?", name).
  60. Where("is_show = ?", pasturePb.IsShow_Ok).
  61. Find(&systemBasic).Error; err != nil {
  62. return nil, xerr.WithStack(err)
  63. }
  64. return &pasturePb.BaseDataConfig{
  65. Id: systemBasic.Id,
  66. Name: systemBasic.Name,
  67. CategoryId: systemBasic.CategoryId,
  68. CategoryName: systemBasic.CategoryName,
  69. MinValue: systemBasic.MinValue,
  70. MaxValue: systemBasic.MinValue,
  71. ValueType: systemBasic.ValueType,
  72. WeekValue: systemBasic.WeekValue,
  73. }, nil
  74. }