pg_task_dao.go 8.3 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, isRemind bool) error {
	tx := dao.transactionContext.PgTx
	_, err := tx.QueryOne(
		pg.Scan(),
		"INSERT INTO bid_infos (task_id, bid_start_time, bid_end_time, is_remind) VALUES (?, ?, ?, ?)",
		taskId, bidStartTime, bidEndTime, isRemind)
	return err
}

func (dao *TaskDao) UpdateBidInfo(taskId int64, bidStartTime time.Time, bidEndTime time.Time, isRemind bool) error {
	tx := dao.transactionContext.PgTx
	_, err := tx.QueryOne(
		pg.Scan(),
		"UPDATE bid_infos SET bid_start_time=?, bid_end_time=?, is_remind=? WHERE task_id=?",
		bidStartTime, bidEndTime, isRemind, taskId)
	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) (map[string]interface{}, error) {
	var released int64
	var underway int64
	var completed int64
	tx := dao.transactionContext.PgTx
	taskModel := new(models.Task)
	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 {
		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 {
		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 {
		completed = int64(count)
	}
	return map[string]interface{}{
		"released":  released + underway + completed,
		"underway":  underway,
		"completed": completed,
	}, nil
}

func (dao *TaskDao) CalculatePersonTask(uid int64) (map[string]interface{}, error) {
	var underwayAsAssignedPerson int64
	var unAcceptanceAsAssignedPerson int64
	var completedAsAssignedPerson int64
	var unConfirmedAsReceiver int64
	var underwayAsReceiver int64
	var unAcceptanceAsReceiver int64
	var completedAsReceiver int64
	var unReleasedAsSponsor int64
	var unClaimedAsSponsor int64
	var unConfirmedAsSponsor int64
	var underwayAsSponsor int64
	var unAcceptanceAsSponsor int64
	var completedAsSponsor int64
	var bidAsParticipator int64
	var completedAsParticipator int64
	tx := dao.transactionContext.PgTx
	taskModel := new(models.Task)
	if count, err := tx.Model(taskModel).
		Where(`task.assigned_person @> '{"uid":?}'`, uid).
		Where("task.task_status = ? ", domain.TASK_STATUS_UNDERWAY).
		Count(); err != nil {
		return nil, err
	} else {
		underwayAsAssignedPerson = int64(count)
	}
	if count, err := tx.Model(taskModel).
		Where(`task.assigned_person @> '{"uid":?}'`, uid).
		Where("task.task_status = ? ", domain.TASK_STATUS_UNACCEPTANCE).
		Count(); err != nil {
		return nil, err
	} else {
		unAcceptanceAsAssignedPerson = int64(count)
	}
	if count, err := tx.Model(taskModel).
		Where(`task.assigned_person @> '{"uid":?}'`, uid).
		Where("task.task_status = ? ", domain.TASK_STATUS_COMPLETED).
		Count(); err != nil {
		return nil, err
	} else {
		completedAsAssignedPerson = int64(count)
	}
	if count, err := tx.Model(taskModel).
		Where("task.receiver_uid = ?", uid).
		Where("task.task_status = ? ", domain.TASK_STATUS_UNCONFIRMED).
		Count(); err != nil {
		return nil, err
	} else {
		unConfirmedAsReceiver = int64(count)
	}
	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 {
		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 {
		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 {
		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 {
		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 {
		unClaimedAsSponsor = int64(count)
	}
	if count, err := tx.Model(taskModel).
		Where(`task.sponsor @> '{"uid":?}'`, uid).
		Where("task.task_status = ? ", domain.TASK_STATUS_UNCONFIRMED).
		Count(); err != nil {
		return nil, err
	} else {
		unConfirmedAsSponsor = 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 {
		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 {
		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 {
		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 {
		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 {
		completedAsParticipator = int64(count)
	}
	return map[string]interface{}{
		"underwayAsAssignedPerson":     underwayAsAssignedPerson,
		"unAcceptanceAsAssignedPerson": unAcceptanceAsAssignedPerson,
		"completedAsAssignedPerson":    completedAsAssignedPerson,
		"unConfirmedAsReceiver":        unConfirmedAsReceiver,
		"underwayAsReceiver":           underwayAsReceiver,
		"unAcceptanceAsReceiver":       unAcceptanceAsReceiver,
		"completedAsReceiver":          completedAsReceiver,
		"unReleasedAsSponsor":          unReleasedAsSponsor,
		"unClaimedAsSponsor":           unClaimedAsSponsor,
		"unConfirmedAsSponsor":         unConfirmedAsSponsor,
		"underwayAsSponsor":            underwayAsSponsor,
		"unAcceptanceAsSponsor":        unAcceptanceAsSponsor,
		"completedAsSponsor":           completedAsSponsor,
		"bidAsParticipator":            bidAsParticipator,
		"completedAsParticipator":      completedAsParticipator,
	}, nil
}

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