package copydata import ( consts "cid/consts/public" dto "cid/model/dto/copydata" entity "cid/model/entity/copydata" "context" "errors" "gitea.com/red-future/common/db/gfdb" "github.com/gogf/gf/v2/util/gconv" ) var PopulationReport = new(populationReportDao) type populationReportDao struct{} // Insert 插入人群报表数据 func (d *populationReportDao) Insert(ctx context.Context, req *dto.PopulationReportItem) (id int64, err error) { var entityData *entity.PopulationReport if err = gconv.Struct(req, &entityData); err != nil { return } r, err := gfdb.DB(ctx).Model(ctx, consts.PopulationReportTable).Data(&entityData).Insert() if err != nil { return } return r.LastInsertId() } // BatchInsert 批量插入人群报表数据 func (d *populationReportDao) BatchInsert(ctx context.Context, reqs []*dto.PopulationReportItem) (successCount int64, failCount int64, failedIndexes []int64, err error) { if len(reqs) == 0 { return 0, 0, nil, errors.New("批量插入数据不能为空") } // 分批处理,每批 100 条 batchSize := 100 successCount = 0 failCount = 0 failedIndexes = make([]int64, 0) for i := 0; i < len(reqs); i += batchSize { end := i + batchSize if end > len(reqs) { end = len(reqs) } batch := reqs[i:end] entityList := make([]*entity.PopulationReport, len(batch)) for j, req := range batch { var entityData entity.PopulationReport if err = gconv.Struct(req, &entityData); err != nil { failCount++ failedIndexes = append(failedIndexes, int64(i+j)) continue } entityList[j] = &entityData } if len(entityList) == 0 { continue } // 执行批量插入 _, err = gfdb.DB(ctx).Model(ctx, consts.PopulationReportTable).Data(entityList).Insert() if err != nil { // 批量插入失败,尝试逐条插入 for k := range batch { _, singleErr := d.Insert(ctx, batch[k]) if singleErr != nil { failCount++ failedIndexes = append(failedIndexes, int64(i+k)) } else { successCount++ } } } else { successCount += int64(len(entityList)) } } return successCount, failCount, failedIndexes, nil } // List 查询人群报表数据列表 func (d *populationReportDao) List(ctx context.Context, req *dto.ListPopulationReportReq) ([]*entity.PopulationReport, int, error) { model := gfdb.DB(ctx).Model(ctx, consts.PopulationReportTable).Model // 构建查询条件 if req.ReportDateStr != "" { model = model.Where("report_date_str", req.ReportDateStr) } if req.PhotoId != "" { model = model.Where("photo_id", req.PhotoId) } if req.Keyword != "" { model = model.Where("photo_name LIKE ?", "%"+req.Keyword+"%") } // 设置排序 model = model.OrderDesc("created_at") // 分页查询并获取总数 total, err := model.Count() if err != nil { return nil, 0, err } var list []*entity.PopulationReport if req.Page != nil { err = model.Page(int(req.Page.PageNum), int(req.Page.PageSize)).Scan(&list) } else { err = model.Scan(&list) } if err != nil { return nil, 0, err } return list, total, nil }