pg_cooperation_contract_dao.go 2.7 KB
package dao

import (
	"fmt"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
	"time"
)

type CooperationContractDao struct {
	transactionContext *pgTransaction.TransactionContext
}

// GenerateContractNumber 生成共创合约编号
func (dao *CooperationContractDao) GenerateContractNumber() (string, error) {
	tx := dao.transactionContext.PgTx
	var cooperationContractModels []*models.CooperationContract
	query := tx.Model(&cooperationContractModels)
	currentTime := time.Now()
	todayZeroTime := utils.GetZeroTime(currentTime)
	nextDayZeroTime := utils.GetNextDayZeroTime(currentTime)
	query.Where("cooperation_contract.created_at >= ?", todayZeroTime)
	query.Where("cooperation_contract.created_at < ?", nextDayZeroTime)
	if count, err := query.AllWithDeleted().SelectAndCount(); err != nil {
		return "", err
	} else {
		countStr := fmt.Sprintf("%03d", count+1)
		timestamp := currentTime.Unix()
		timeNow := time.Unix(timestamp, 0)
		timeString := timeNow.Format("20060102")
		timeString = timeString[2:len(timeString)]
		contractNumber := "HY" + timeString + "#" + countStr
		return contractNumber, nil
	}
}

// CheckUndertakerTypesUncheckedAvailable TODO 校验合约承接对象是否可以删除
func (dao *CooperationContractDao) CheckUndertakerTypesUncheckedAvailable(queryOptions map[string]interface{}) ([]int32, error) {

	return []int32{}, nil
}

// CheckContractNumberAvailable 检验合约编号唯一性
func (dao *CooperationContractDao) CheckContractNumberAvailable(queryOptions map[string]interface{}) (bool, error) {
	tx := dao.transactionContext.PgTx
	var cooperationContractModels []*models.CooperationContract
	query := tx.Model(&cooperationContractModels)
	if cooperationContractNumber, ok := queryOptions["cooperationContractNumber"]; ok && cooperationContractNumber != "" {
		query = query.Where("cooperation_contract_number = ?", cooperationContractNumber)
	}
	if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
		query = query.Where(`cooperation_contract.company @> '{"companyId":"?"}'`, companyId)
	}
	if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
		query = query.Where(`cooperation_contract.org @> '{"orgId":"?"}'`, orgId)
	}
	ok, err := query.Exists()
	return !ok, err
}

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