material_k3cloud_dao.go 5.7 KB
package dao

import (
	"fmt"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
	"strconv"
	"strings"

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

type MaterialK3cloudDao struct {
	transactionContext *pgTransaction.TransactionContext
}

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

//SyncDataMaterialK3cloud 同步MaterialK3cloud表数据
func (d *MaterialK3cloudDao) SyncDataMaterialK3cloud(data []models.MaterialK3cloud) error {
	sqlValues := []string{}
	var strTemp []string
	for i := range data {
		strTemp = make([]string, 0, 18)
		strTemp = append(strTemp, strconv.Itoa(data[i].MaterialId))
		strTemp = append(strTemp, `'`+data[i].Name+`'`)
		strTemp = append(strTemp, `'`+data[i].Number+`'`)
		strTemp = append(strTemp, `'`+data[i].Specification+`'`)
		strTemp = append(strTemp, `'`+data[i].ForbidStatus+`'`)
		strTemp = append(strTemp, strconv.Itoa(data[i].ErpClsId))
		strTemp = append(strTemp, strconv.Itoa(data[i].BaseUnitId))
		strTemp = append(strTemp, `'`+data[i].BaseUnitName+`'`)
		if data[i].CreateDate.IsZero() {
			strTemp = append(strTemp, `NULL`)
		} else {
			strTemp = append(strTemp, `'`+data[i].CreateDate.Format("2006-01-02 15:04:05.999")+`'`)
		}
		if data[i].ModifyDate.IsZero() {
			strTemp = append(strTemp, `NULL`)
		} else {
			strTemp = append(strTemp, `'`+data[i].ModifyDate.Format("2006-01-02 15:04:05.999")+`'`)
		}
		if data[i].ForbidDate.IsZero() {
			strTemp = append(strTemp, `NULL`)
		} else {
			strTemp = append(strTemp, `'`+data[i].ForbidDate.Format("2006-01-02 15:04:05.999")+`'`)
		}
		if data[i].ApproveDate.IsZero() {
			strTemp = append(strTemp, `NULL`)
		} else {
			strTemp = append(strTemp, `'`+data[i].ApproveDate.Format("2006-01-02 15:04:05.999")+`'`)
		}
		strTemp = append(strTemp, strconv.Itoa(data[i].MaterialGroup))
		strTemp = append(strTemp, `'`+data[i].MaterialGroupNumber+`'`)
		strTemp = append(strTemp, `'`+data[i].MaterialGroupName+`'`)
		strTemp = append(strTemp, strconv.Itoa(data[i].RefStatus))
		//关联的产品表id ,使用 product 产品表的自增序列表
		strTemp = append(strTemp, "nextval('manufacture.product_product_id_seq'::regclass)")
		strTemp = append(strTemp, strconv.Itoa(int(data[i].DataVersion)))
		strTemp = append(strTemp, strconv.Itoa(data[i].UseOrgId))
		strTemp = append(strTemp, `'`+data[i].UseOrgName+`'`)
		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_k3cloud" (
			"material_id","name","number","specification","forbid_status",
			"erp_cls_id","base_unit_id","base_unit_name","create_date",
			"modify_date","forbid_date","approve_date","material_group",
			"material_group_number","material_group_name","ref_status",
			"join_product_id","data_version","use_org_id","use_org_name" )
		VALUES  ` + strings.Join(valueTemp, ",") +
			` ON conflict ( material_id ) DO
		UPDATE
			SET (
				"name","number","specification","forbid_status","erp_cls_id",
				"base_unit_id","base_unit_name","create_date","modify_date",
				"forbid_date","approve_date","material_group","material_group_number",
				"material_group_name","ref_status","data_version","use_org_id","use_org_name" ) = (
				EXCLUDED."name",EXCLUDED."number",EXCLUDED."specification",
				EXCLUDED."forbid_status",EXCLUDED."erp_cls_id",EXCLUDED."base_unit_id",
				EXCLUDED."base_unit_name",EXCLUDED."create_date",EXCLUDED."modify_date",
				EXCLUDED."forbid_date",EXCLUDED."approve_date",EXCLUDED."material_group",
				EXCLUDED."material_group_number",EXCLUDED."material_group_name",
				EXCLUDED."ref_status",EXCLUDED."data_version",EXCLUDED."use_org_id",EXCLUDED."use_org_name")`
		_, err := d.transactionContext.PgTx.Exec(sql)
		if err != nil {
			return err
		}
	}

	return nil
}

//SyncDataProudct 同步MaterialK3cloud表数据到Proudct表
func (d *MaterialK3cloudDao) SyncDataProudct(version int64, orgName string) error {
	// -- 插入或者更新
	sql := fmt.Sprintf(`INSERT INTO "manufacture"."product"(
		"company_id", "org_id", "product_id", "product_code", "product_name",
		"product_category", "product_spec", "created_at", "updated_at","ext"
		 )
		SELECT %v,%v,"join_product_id","number","name","material_group_name",
		json_build_object('unit',specification),now(),now(),json_build_object('orgName','%v')
		FROM  "manufacture"."material_k3cloud" WHERE "data_version"=? AND "material_group_number" LIKE '05%%'
		ON conflict ( product_id ) DO
		UPDATE
			SET (
			"company_id", "org_id", "product_id", "product_code", "product_name",
		"product_category", "product_spec",  "updated_at")=(
		EXCLUDED."company_id", EXCLUDED."org_id",EXCLUDED."product_id",
		EXCLUDED."product_code",EXCLUDED."product_name",EXCLUDED."product_category",
		EXCLUDED."product_spec", EXCLUDED."updated_at") `, constant.MANUFACTURE_DEFAULT_COMPANYID, constant.MANUFACTURE_DEFAULT_ORGID, orgName)
	_, err := d.transactionContext.PgTx.Exec(sql, version)
	return err
}

func (d *MaterialK3cloudDao) GetLastVersion() (int64, error) {
	var materialData []models.MaterialK3cloud
	err := d.transactionContext.PgTx.Model(&materialData).
		Order("data_version DESC").
		Limit(1).
		Select()
	if err != nil {
		return 0, err
	}
	if len(materialData) == 0 {
		return 0, nil
	}
	return materialData[0].DataVersion, nil
}