prd_mo_k3cloud.go 5.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 PrdMoK3cloudDao struct {
	transactionContext *pgTransaction.TransactionContext
}

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

//SyncDataPrdMoK3cloud 同步PrdMoK3cloud表数据
func (d *PrdMoK3cloudDao) SyncDataPrdMoK3cloud(data []models.PrdMoK3cloud) error {
	//插入或者更新
	sqlValues := []string{}
	var strTemp []string
	for i := range data {
		strTemp = make([]string, 0, 15)
		strTemp = append(strTemp, `'`+data[i].RowId+`'`)
		strTemp = append(strTemp, `'`+data[i].BillNo+`'`)
		strTemp = append(strTemp, strconv.Itoa(data[i].WorkShopID))
		strTemp = append(strTemp, `'`+data[i].WorkShopName+`'`)
		strTemp = append(strTemp, strconv.Itoa(data[i].MaterialId))
		strTemp = append(strTemp, `'`+data[i].MaterialName+`'`)
		strTemp = append(strTemp, `'`+data[i].MaterialNumber+`'`)
		strTemp = append(strTemp, `'`+data[i].PlanStartDate.Format("2006-01-02 15:04:05.999")+`'`)
		strTemp = append(strTemp, `'`+data[i].PlanFinishDate.Format("2006-01-02 15:04:05.999")+`'`)
		strTemp = append(strTemp, `'`+data[i].FDate.Format("2006-01-02 15:04:05.999")+`'`)
		strTemp = append(strTemp, fmt.Sprintf("%.10f", data[i].Qty)) //15.5000000000
		strTemp = append(strTemp, `'`+data[i].CreateDate.Format("2006-01-02 15:04:05.999")+`'`)
		strTemp = append(strTemp, `'`+data[i].ModifyDate.Format("2006-01-02 15:04:05.999")+`'`)
		strTemp = append(strTemp, strconv.Itoa(int(data[i].DataVersion)))
		strTemp = append(strTemp, strconv.Itoa(data[i].PrdOrgId))
		strTemp = append(strTemp, `'`+data[i].PrdOrgName+`'`)
		strTemp = append(strTemp, `'`+data[i].UnitName+`'`)
		strTemp = append(strTemp, strconv.Itoa(data[i].UnitId))
		strTemp = append(strTemp, `'`+data[i].Specification+`'`)
		strTemp = append(strTemp, `'`+data[i].Description+`'`)
		//关联的计划管理表id ,使用 product_plan 产品表的自增序列表
		strTemp = append(strTemp, "nextval('manufacture.product_plan_product_plan_id_seq'::regclass)")
		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"."prd_mo_k3cloud" (
			"row_id","bill_no","work_shop_id","work_shop_name",
			"material_id","material_name","material_number",
			"plan_start_date","plan_finish_date","f_date",
			"qty","create_date","modify_date","data_version",
			"prd_org_id","prd_org_name","unit_name","unit_id",
			"specification","description","join_product_plan_id")
		VALUES ` +
			strings.Join(valueTemp, ",") +
			` ON conflict ( "row_id" ) DO
		UPDATE
			SET (
				"bill_no","work_shop_id","work_shop_name",
			"material_id","material_name","material_number",
			"plan_start_date","plan_finish_date","f_date",
			"qty","create_date","modify_date","data_version",
			"prd_org_id","prd_org_name","unit_name","unit_id",
			"specification","description") = (
			EXCLUDED."bill_no",EXCLUDED."work_shop_id",
			EXCLUDED."work_shop_name",EXCLUDED."material_id",
			EXCLUDED."material_name",EXCLUDED."material_number",
			EXCLUDED."plan_start_date",EXCLUDED."plan_finish_date",
			EXCLUDED."f_date",EXCLUDED."qty",EXCLUDED."create_date",
			EXCLUDED."modify_date",EXCLUDED."data_version",
			EXCLUDED."prd_org_id",EXCLUDED."prd_org_name",
			EXCLUDED."unit_name",EXCLUDED."unit_id",
			EXCLUDED."specification",EXCLUDED."description"
			)`
		_, err := d.transactionContext.PgTx.Exec(sql)
		if err != nil {
			return err
		}
	}

	return nil
}

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

//SyncDataProductPlan 同步数据到
func (d *PrdMoK3cloudDao) SyncDataProductPlan(version int64) error {
	sql := `
	INSERT INTO "manufacture"."product_plan"(
		"product_plan_id","batch_number","product_date","workshop",
		"plan_product_name","plan_devoted","plan_status","remark",
		"ext","created_at","updated_at"
		 )
		SELECT prd_mo_k3cloud."join_product_plan_id",prd_mo_k3cloud."bill_no",
		prd_mo_k3cloud."plan_start_date",
		json_build_object('workshopId',"workshop"."workshop_id",'workshopName',prd_mo_k3cloud."work_shop_name"),
		prd_mo_k3cloud."material_name", json_build_object('unit',prd_mo_k3cloud."unit_name",'quantity',prd_mo_k3cloud."qty"),
		2,prd_mo_k3cloud."description",
		json_build_object('productPlanExt',json_build_object('productId',prd_mo_k3cloud."join_product_plan_id",'productCode',prd_mo_k3cloud."material_number",'productName',prd_mo_k3cloud."material_name")),
		now(),now()
		FROM "manufacture"."prd_mo_k3cloud"
		LEFT JOIN "material_k3cloud" ON "prd_mo_k3cloud"."material_number"="material_k3cloud"."number"
		LEFT JOIN "workshop" ON "workshop"."workshop_name" = "prd_mo_k3cloud"."work_shop_name"
		WHERE prd_mo_k3cloud."data_version"=?
		ON conflict ("product_plan_id") DO
		UPDATE
			SET (
			"batch_number","product_date","workshop",
		  "plan_product_name","plan_devoted","plan_status","remark",
		  "ext","updated_at"
			)=(
		EXCLUDED."batch_number",
		EXCLUDED."product_date",
		"product_plan"."workshop"||EXCLUDED."workshop",
		EXCLUDED."plan_product_name",
		EXCLUDED."plan_devoted",
		EXCLUDED."plan_status",
		EXCLUDED."remark",
		"product_plan"."ext"||EXCLUDED."ext",
		EXCLUDED."updated_at"
		)
		`
	_, err := d.transactionContext.PgTx.Exec(sql, version)
	return err
}