pg_cooperation_contract_dao.go 7.0 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 校验合约承接对象是否可以删除
func (dao *CooperationContractDao) CheckUndertakerTypesUncheckedAvailable(queryOptions map[string]interface{}) (bool, error) {
	tx := dao.transactionContext.PgTx
	var cooperationContractUndertakerModels []*models.CooperationContractUndertaker
	query := tx.Model(&cooperationContractUndertakerModels)
	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)
	}
	if user, ok := queryOptions["user"]; ok && user.(bool) != false {
		query = query.Where("user_type = ?", 1)
	}
	if partner, ok := queryOptions["partner"]; ok && partner.(bool) != false {
		query = query.Where("user_type = ?", 2)
	}
	ok, err := query.Exists()
	return !ok, err
}

// 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
}

// CheckContractNumberExist 检验合约编号是否存在
func (dao *CooperationContractDao) CheckContractNumberExist(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
}

// SearchCooperationContractByUndertaker 查询共创合约并根据承接人返回
func (dao *CooperationContractDao) SearchCooperationContractByUndertaker(queryOptions map[string]interface{}) (int, []*models.CooperationContractByUndertaker, error) {
	tx := dao.transactionContext.PgTx
	var cooperationContractByUndertakerModels []*models.CooperationContractByUndertaker
	var cooperationUndertakerModels []*models.CooperationContractUndertaker
	query := tx.Model(&cooperationUndertakerModels)
	query.ColumnExpr("cooperation_contract_undertaker.cooperation_contract_undertaker_id")
	query.ColumnExpr("cooperation_contract_undertaker.user_info->>'userCode' AS undertaker_code")
	query.ColumnExpr("cooperation_contract_undertaker.user_info->>'userName' AS undertaker_name")
	query.ColumnExpr("cooperation_contract_undertaker.user_info->>'userPhone' AS undertaker_phone")
	query.ColumnExpr("cooperation_contract_undertaker.contract_attachment")
	query.ColumnExpr("A.cooperation_contract_sponsor")
	query.ColumnExpr("A.cooperation_contract_number")
	query.ColumnExpr("A.cooperation_contract_name")
	query.ColumnExpr("A.cooperation_contract_description")
	query.ColumnExpr("A.department")
	query.ColumnExpr("A.org")
	query.ColumnExpr("A.status AS contract_status")
	query.ColumnExpr("B.cooperation_mode_name")
	if cooperationContractName, ok := queryOptions["cooperationContractName"]; ok && cooperationContractName != "" {
		query = query.Where("A.cooperation_contract_name ilike ?", fmt.Sprintf("%%%s%%", cooperationContractName))
	}
	if sponsorName, ok := queryOptions["sponsorName"]; ok && sponsorName != "" {
		query = query.Where(`A.cooperation_contract_sponsor->>'userName')::text LIKE ?`, fmt.Sprintf("%%%s%%", sponsorName))
	}
	query.Join("JOIN cooperation_contracts AS A ON A.cooperation_contract_number = cooperation_contract_undertaker.cooperation_contract_number")
	query.Join("JOIN cooperation_modes AS B ON B.cooperation_mode_number = A.cooperation_mode_number")
	query = query.Order("cooperation_contract_undertaker_id DESC")
	if offset, ok := queryOptions["offset"]; ok && offset.(int64) != 0 {
		offset := offset.(int64)
		if offset > -1 {
			query = query.Offset(int(offset))
		}
	} else {
		query = query.Offset(0)
	}
	if limit, ok := queryOptions["limit"]; ok && limit.(int64) != 0 {
		limit := limit.(int64)
		if limit > -1 {
			query = query.Limit(int(limit))
		}
	} else {
		query = query.Limit(20)
	}
	if count, err := query.SelectAndCount(&cooperationContractByUndertakerModels); err != nil {
		return 0, nil, err
	} else {
		return count, cooperationContractByUndertakerModels, nil
	}
}

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