golang gorm rows.next或者批量插入CreateInBatches 缺失数据的问题?

各位大佬,有对 grom 熟悉的吗,请教个问题,为什么rows.next 的时候,fetch游标有时候会缺失输入,比如原表有 20W,但是扫描结果只有6W,但也不是每次都这样,只是偶尔。
因为我是 rows.next后,调用批量插入参数CreateInBatches 进行插入到新表的,新表数据只有6W,但是原表有20W,偶发的,目前还没有定位出来 是rows fetch的问题,还是批量插入的问题。

func (b *MchtSettTask) GenData(ctx context.Context, ctl entity.TblSysBatchCtl) error {
//导入
    rows, err := b.Infra.TblEasyMerPayAccInfoRepo.FindRowsByHostDate(ctx, ctl.HostDate, ctl.BatchId, ctl.LishouProjectId)

    if err != nil {
        logger.Errorc(ctx, "信息表查询数据失败[%s][%s]", err, ctl.BatchId)
        return err
    }
    defer rows.Close()
    b.Logger.Infoc(ctx, "开始遍历处理[%s][%s]", ctl.BatDesc, ctl.BatchId)

    etys := make([]*entity.TblTempMchtAcctInf, 0, consts.INSERT_BATCH)

    for {
        order, err := b.Infra.TblEasyMerPayAccInfoRepo.ScanRows2Entity(ctx, rows)
        if err != nil {
            logger.Errorc(ctx, "ScanRows2Entity: 遍历失败[%s][%s]", err, ctl.BatchId)
            return err
        }
        //完成遍历
        if order == nil {
            break
        }

    tempMchtAcctInfo := &entity.TblTempMchtAcctInf{
            HostDate:        ctl.HostDate,
            BatId:           ctl.BatID,
            BatchId:         ctl.BatchId,
            MchtCd:          order.MerCode,
            LishouProjectId: ctl.LishouProjectId,
            EasyGangwayComm: "test12121212", // '',
            EasyProjectId:   "test12121212",   // '',
            EasyProjectBank: "test12121212", //'',
            MchtName:        "test12121212",
            AcctType:        order.AccType, // '',
            AcctStatus:      "",            //'',
            CertifTp:        ConvCertif(order.LegalType),
            CertifID:       order.LegalCode, // '',
            CertifPhone:    order.AccPhone,  // '',
            CipCertifID:    sql.NullString{GetLegalEncrypt(order.LegalEncrypt), true},
            CipCertifPhone: sql.NullString{GetLegalEncrypt(order.AccphoneEncrypt), true},

            AcctNm:        order.AccName,  // '',
            SettAcctNo:    order.Account,  // '',
            IssCode:       order.BankCode, //iss_code VARCHAR(32) COLLATE utf8mb4_bin NOT NULL COMMENT '发卡行号',
            IssName:       order.BankName, //iss_name VARCHAR(32) COLLATE utf8mb4_bin NOT NULL COMMENT '发卡行名称',
            CipAcctNm:     sql.NullString{order.AccnameEncrypt, true},
            CipSettAcctNo: sql.NullString{order.AccEncrypt, true},
        }
        etys = append(etys, tempMchtAcctInfo)
        if len(etys) < consts.INSERT_BATCH {
            continue
        }
        //保存
        if err := b.Infra.TblTempMchtAcctInfRepo.BatchInsert(ctx, etys); err != nil {
            logger.Errorc(ctx, "Create 数据失败[%s][%s]", err, ctl.BatchId)
            return err
        }

        etys = make([]*entity.TblTempMchtAcctInf, 0, consts.INSERT_BATCH)
    }
    if err := b.Infra.TblTempMchtAcctInfRepo.BatchInsert(ctx, etys); err != nil {
        logger.Errorc(ctx, "Create 数据失败[%s][%s]", err, ctl.BatchId)
        return err
    }
}


func (dtt *DbTblTempMchtAcctInfRepo) BatchInsert(ctx context.Context, etys []*entity.TblTempMchtAcctInf) error {
    return dtt.tblTempMchtAcctInfDao.GetDb(ctx).CreateInBatches(etys, consts.BATCH_INSERT_NUM).Error
}


func (dte *DbTblEasyMerPayAccInfoRepo) FindRowsByHostDate(ctx context.Context, host_date, batch_id, bank_id string) (*sql.Rows, error) {
    var err error

    rows, err := dte.tblEasyMerPayAccInfoDao.GetSlaveDb(ctx).Where("host_date = ? and batch_id=? and lishou_project_id=?", host_date, batch_id, bank_id).Rows()
    if err != nil {
        dte.logger.Errorc(ctx, "FindRowsByHostDate: 遍历[%s]数据失败[%s]", host_date, err)
        return nil, err
    }

    return rows, nil
}
阅读 1.4k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题