pg_project_module_version_dao.go 1.5 KB
package dao

import (
	"fmt"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/transaction"
)

type ProjectModuleDao struct {
	transactionContext *transaction.TransactionContext
}

func (dao *ProjectModuleDao) DuplicateVersion(srcProjectId, srcVersionId int64, curVersionId int64) error {
	tx := dao.transactionContext.PgTx
	_, err := tx.Exec(`insert into project_module_files(project_module_id,project_module_version_id,file_type,file_name,file_key,code_block,parent_id,sort,remark,create_time,update_time,path)
select ?,?,file_type,file_name,file_key,code_block,parent_id,sort,remark,now(),now(),path from project_module_files where project_module_id=? and project_module_version_id=?`,
		srcProjectId, curVersionId, srcProjectId, srcVersionId)
	return err
}

func (dao *ProjectModuleDao) FixParentIdByPath(srcProjectId, curVersionId int64) error {
	tx := dao.transactionContext.PgTx
	_, err := tx.Exec(`update project_module_files  A  set parent_id =coalesce((select id from project_module_files B where B.project_module_id=? and B.project_module_version_id=? and B.path=substr(A.path,0,"position"(A.path,A.file_name)-1)  limit 1),0) where project_module_id=? and project_module_version_id=?`,
		srcProjectId, curVersionId, srcProjectId, curVersionId)
	return err
}

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