pg_task_dao.go 5.6 KB
package dao

import (
	"fmt"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg/models"
	"time"

	"github.com/go-pg/pg"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
)

type TaskDao struct {
	transactionContext *pgTransaction.TransactionContext
}

func (dao *TaskDao) AddRobInfo(taskId int64, receiver *domain.EmployeeInfo) error {
	tx := dao.transactionContext.PgTx
	_, err := tx.QueryOne(
		pg.Scan(),
		"INSERT INTO rob_infos (task_id, receiver, receive_time) VALUES (?, ?, ?)",
		taskId, receiver, time.Now())
	return err
}

func (dao *TaskDao) AddBidInfo(taskId int64, bidStartTime time.Time, bidEndTime time.Time) error {
	tx := dao.transactionContext.PgTx
	_, err := tx.QueryOne(
		pg.Scan(),
		"INSERT INTO bid_infos (task_id, bid_start_time, bid_end_time) VALUES (?, ?, ?)",
		taskId, bidStartTime, bidEndTime)
	return err
}

func (dao *TaskDao) AddBidderInfo(taskId int64, bidder *domain.EmployeeInfo) error {
	tx := dao.transactionContext.PgTx
	_, err := tx.QueryOne(
		pg.Scan(),
		"INSERT INTO bidder_infos (task_id, bidder, bid_time) VALUES (?, ?, ?)",
		taskId, bidder, time.Now())
	return err
}

func (dao *TaskDao) SetSuccessfulBidder(taskId int64, successfulBidder *domain.EmployeeInfo, winBidTime time.Time) error {
	tx := dao.transactionContext.PgTx
	_, err := tx.QueryOne(
		pg.Scan(),
		"UPDATE bid_infos SET successful_bidder=?, win_bid_time=? WHERE task_id=?",
		successfulBidder, winBidTime, taskId)
	return err
}

func (dao *TaskDao) CalculateSystemTask(companyId int64) (*domain.SystemTaskStatistics, error) {
	tx := dao.transactionContext.PgTx
	taskModel := new(models.Task)
	systemTaskStatistics := &domain.SystemTaskStatistics{}
	if count, err := tx.Model(taskModel).
		Where("task.company_id = ?", companyId).
		Where("task.task_status = ? ", domain.TASK_STATUS_UNCLAIMED).
		Count(); err != nil {
		return nil, err
	} else {
		systemTaskStatistics.Released = int64(count)
	}
	if count, err := tx.Model(taskModel).
		Where("task.company_id = ?", companyId).
		Where("task.task_status = ? ", domain.TASK_STATUS_UNDERWAY).
		Count(); err != nil {
		return nil, err
	} else {
		systemTaskStatistics.Underway = int64(count)
	}
	if count, err := tx.Model(taskModel).
		Where("task.company_id = ?", companyId).
		Where("task.task_status = ? ", domain.TASK_STATUS_COMPLETED).
		Count(); err != nil {
		return nil, err
	} else {
		systemTaskStatistics.Completed = int64(count)
	}
	return systemTaskStatistics, nil
}

func (dao *TaskDao) CalculatePersonTask(uid int64) (*domain.PersonTaskStatistics, error) {
	tx := dao.transactionContext.PgTx
	taskModel := new(models.Task)
	personTaskStatistics := &domain.PersonTaskStatistics{}
	if count, err := tx.Model(taskModel).
		Where("task.receiver_uid = ?", uid).
		Where("task.task_status = ? ", domain.TASK_STATUS_UNDERWAY).
		Count(); err != nil {
		return nil, err
	} else {
		personTaskStatistics.UnderwayAsReceiver = int64(count)
	}
	if count, err := tx.Model(taskModel).
		Where("task.receiver_uid = ?", uid).
		Where("task.task_status = ? ", domain.TASK_STATUS_UNACCEPTANCE).
		Count(); err != nil {
		return nil, err
	} else {
		personTaskStatistics.UnAcceptanceAsReceiver = int64(count)
	}
	if count, err := tx.Model(taskModel).
		Where("task.receiver_uid = ?", uid).
		Where("task.task_status = ? ", domain.TASK_STATUS_COMPLETED).
		Count(); err != nil {
		return nil, err
	} else {
		personTaskStatistics.CompletedAsReceiver = int64(count)
	}
	if count, err := tx.Model(taskModel).
		Where(`task.sponsor @> '{"uid":?}'`, uid).
		Where("task.task_status = ? ", domain.TASK_STATUS_UNRELEASED).
		Count(); err != nil {
		return nil, err
	} else {
		personTaskStatistics.UnReleasedAsSponsor = int64(count)
	}
	if count, err := tx.Model(taskModel).
		Where(`task.sponsor @> '{"uid":?}'`, uid).
		Where("task.task_status = ? ", domain.TASK_STATUS_UNCLAIMED).
		Count(); err != nil {
		return nil, err
	} else {
		personTaskStatistics.UnClaimedAsSponsor = int64(count)
	}
	if count, err := tx.Model(taskModel).
		Where(`task.sponsor @> '{"uid":?}'`, uid).
		Where("task.task_status = ? ", domain.TASK_STATUS_UNDERWAY).
		Count(); err != nil {
		return nil, err
	} else {
		personTaskStatistics.UnderwayAsSponsor = int64(count)
	}
	if count, err := tx.Model(taskModel).
		Where(`task.sponsor @> '{"uid":?}'`, uid).
		Where("task.task_status = ? ", domain.TASK_STATUS_UNACCEPTANCE).
		Count(); err != nil {
		return nil, err
	} else {
		personTaskStatistics.UnAcceptanceAsSponsor = int64(count)
	}
	if count, err := tx.Model(taskModel).
		Where(`task.sponsor @> '{"uid":?}'`, uid).
		Where("task.task_status = ? ", domain.TASK_STATUS_COMPLETED).
		Count(); err != nil {
		return nil, err
	} else {
		personTaskStatistics.CompletedAsSponsor = int64(count)
	}
	if count, err := tx.Model(taskModel).Join("JOIN bidder_infos AS bidder_info ON bidder_info.task_id = task.id").
		Where(`bidder_info.Bidder @> '{"uid":?}'`, uid).
		Where("task.task_status = ? ", domain.TASK_STATUS_UNCLAIMED).
		Count(); err != nil {
		return nil, err
	} else {
		personTaskStatistics.BidAsParticipator = int64(count)
	}
	if count, err := tx.Model(taskModel).
		Where(`task.participators @> '[{"uid":?}]'`, uid).
		Where("task.task_status = ? ", domain.TASK_STATUS_COMPLETED).
		Count(); err != nil {
		return nil, err
	} else {
		personTaskStatistics.CompletedAsParticipator = int64(count)
	}
	return personTaskStatistics, nil
}

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