summary_evaluation_dao.go 1.8 KB
package dao

import (
	"github.com/go-pg/pg/v10"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
)

type SummaryEvaluationDao struct {
	transactionContext *pgTransaction.TransactionContext
}

func NewSummaryEvaluationDao(options map[string]interface{}) *SummaryEvaluationDao {
	var transactionContext *pgTransaction.TransactionContext
	if value, ok := options["transactionContext"]; ok {
		transactionContext = value.(*pgTransaction.TransactionContext)
	}
	return &SummaryEvaluationDao{
		transactionContext: transactionContext,
	}
}

type PersonalCycle struct {
	ExecutorId string `pg:"executor_id" ` //
	CycleId    string `pg:"cycle_id" `    //
	CycleName  string `pg:"cycle_name"`   //
}

// GetPersonalCycleList 获取执行人拥有的周期列表
func (d *SummaryEvaluationDao) GetPersonalCycleList(executorId int, offset int, limit int) ([]PersonalCycle, error) {
	sqlStr := `select  
	distinct 
	summary_evaluation.cycle_id ,
	summary_evaluation.cycle_name ,
	summary_evaluation.executor ->>'userId' as executor_id
	from  summary_evaluation 
	where summary_evaluation.executor ->>'userId'='?'
	offset ? limit ?`

	tx := d.transactionContext.PgTx
	condition := []interface{}{
		executorId, offset, limit,
	}
	result := []PersonalCycle{}
	_, err := tx.Query(&result, sqlStr, condition...)
	return result, err
}

// CountPersonalCycleList 统计执行人拥有的周期列表
func (d *SummaryEvaluationDao) CountPersonalCycleList(executorId int) (int, error) {
	sqlStr := `select count(
		distinct summary_evaluation.cycle_id
	  ) as cnt
	from  summary_evaluation 
	where summary_evaluation.executor ->>'userId'='?' `

	tx := d.transactionContext.PgTx
	condition := []interface{}{
		executorId,
	}
	var cnt int
	_, err := tx.QueryOne(pg.Scan(&cnt), sqlStr, condition...)
	return cnt, err
}