prd_mo_k3cloud.go 6.9 KB
package dao

import (
	"fmt"
	"strconv"
	"strings"
	"time"

	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, companyId, orgId int, orgName string) error {
	sql := fmt.Sprintf(`
	INSERT INTO "manufacture"."product_plan"(
		"company_id","org_id",
		"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',COALESCE("workshop"."workshop_id",0),'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('orgName','%v','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 "manufacture"."material_k3cloud" ON "prd_mo_k3cloud"."material_id"="material_k3cloud"."material_id"
		INNER JOIN "manufacture"."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"
		)
		`, orgName)
	_, err := d.transactionContext.PgTx.Exec(sql, companyId, orgId, version)
	return err
}

// 增量获取数据
func (d *PrdMoK3cloudDao) GetLatestData(fromTime time.Time) ([]*models.PrdMoK3cloud, error) {
	m := new(models.PrdMoK3cloud)
	result := make([]*models.PrdMoK3cloud, 0)
	query := d.transactionContext.PgTx.Model(m)
	query.Where("modify_date>=?", fromTime)
	err := query.Select(&result)
	if err != nil {
		return nil, err
	}
	return result, nil
}

// GetLatestMaterialData 增量获取物料数据
func (d *PrdMoK3cloudDao) GetLatestMaterialData(fromTime time.Time, offset, limit int) ([]*models.MaterialK3cloud, int, error) {
	m := new(models.MaterialK3cloud)
	result := make([]*models.MaterialK3cloud, 0)
	query := d.transactionContext.PgTx.Model(m)
	if !fromTime.IsZero() {
		query.Where("modify_date>=?", fromTime)
	}
	err := query.Select(&result)
	if err != nil {
		return nil, 0, err
	}
	return result, len(result), nil
}