s_local_life.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/sirupsen/logrus"
  6. "reflect"
  7. "youngee_b_api/model/common_model"
  8. "youngee_b_api/model/gorm_model"
  9. "youngee_b_api/util"
  10. )
  11. // GetFullLocalLifeList 查看所有本地生活列表
  12. func GetFullLocalLifeList(ctx context.Context, pageSize, pageNum int32, condition *common_model.SLocalLifeCondition) ([]*gorm_model.YounggeeLocalLifeInfo, int64, error) {
  13. db := GetReadDB(ctx)
  14. // 根据带货任务状态过滤
  15. db = db.Debug().Model(gorm_model.YounggeeLocalLifeInfo{}).Where("task_status = 4")
  16. // 根据Project条件过滤
  17. conditionType := reflect.TypeOf(condition).Elem()
  18. conditionValue := reflect.ValueOf(condition).Elem()
  19. for i := 0; i < conditionType.NumField(); i++ {
  20. field := conditionType.Field(i)
  21. tag := field.Tag.Get("condition")
  22. value := conditionValue.FieldByName(field.Name)
  23. if (tag == "local_id" || tag == "local_name") && !util.IsBlank(value) {
  24. db = db.Where(fmt.Sprintf("local_id like '%%%v%%' or local_name like '%%%v%%'", value.Interface(), value.Interface()))
  25. } else if tag == "updated_at" && value.Interface() != "0" {
  26. db = db.Where(fmt.Sprintf("updated_at like '%s%%'", value.Interface()))
  27. } else if !util.IsBlank(value) && tag != "updated_at" {
  28. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  29. }
  30. }
  31. // 查询总数
  32. var total int64
  33. var fullLocals []*gorm_model.YounggeeLocalLifeInfo
  34. if err := db.Count(&total).Error; err != nil {
  35. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  36. return nil, 0, err
  37. }
  38. // 查询该页数据
  39. limit := pageSize
  40. offset := pageSize * pageNum // assert pageNum start with 0
  41. err := db.Order("updated_at desc").Limit(int(limit)).Offset(int(offset)).Find(&fullLocals).Error
  42. if err != nil {
  43. logrus.WithContext(ctx).Errorf("[GetFullProjectList] error query mysql total, err:%+v", err)
  44. return nil, 0, err
  45. }
  46. return fullLocals, total, nil
  47. }