material_group_k3cloud_dao.go 1.9 KB
package dao

import (
	"fmt"
	"strconv"
	"strings"

	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
)

type MaterialGroupK3cloudDao struct {
	transactionContext *pgTransaction.TransactionContext
}

func NewMaterialGroupK3cloudDao(transactionContext *pgTransaction.TransactionContext) (*MaterialGroupK3cloudDao, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &MaterialGroupK3cloudDao{
			transactionContext: transactionContext,
		}, nil
	}
}

//SyncDataMaterialGroupK3cloud 同步MaterialGroupK3cloud表 物料分组数据
func (d *MaterialGroupK3cloudDao) SyncDataMaterialGroupK3cloud(data []models.MaterialGroupK3cloud) error {
	sqlValues := []string{}
	var strTemp []string
	for i := range data {
		strTemp = make([]string, 0, 5)
		strTemp = append(strTemp, strconv.Itoa(data[i].Id))
		strTemp = append(strTemp, strconv.Itoa(data[i].ParentId))
		strTemp = append(strTemp, `'`+data[i].Number+`'`)
		strTemp = append(strTemp, `'`+data[i].Name+`'`)
		strTemp = append(strTemp, strconv.Itoa(int(data[i].DataVersion)))
		sqlValues = append(sqlValues, "("+strings.Join(strTemp, ",")+")")
	}
	var valueTemp []string
	for i := 0; i < len(sqlValues); i += 100 {
		if i <= len(sqlValues)-100 {
			valueTemp = sqlValues[i : i+100]
		} else {
			valueTemp = sqlValues[i:]
		}
		sql := `INSERT INTO "manufacture"."material_group_k3cloud" (
			"id","parent_id","number","name","data_version")
		VALUES ` + strings.Join(valueTemp, ",") + `
			 ON conflict ( "id" ) DO
		UPDATE
			SET (
				"parent_id","number","name","data_version") = (
				EXCLUDED."parent_id",EXCLUDED."number", 
				EXCLUDED."name",EXCLUDED."data_version" )`
		_, err := d.transactionContext.PgTx.Exec(sql)
		if err != nil {
			return err
		}
	}

	return nil
}