pg_task_dao.go 12.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) ListNearThePlannedCompletionTimeTask() ([]*models.Task, error) {
	tx := dao.transactionContext.PgTx
	var taskModels []*models.Task
	currentTime := time.Now()
	currentDay := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 0, 0, 0, 0, time.Now().Location())
	after24Hour, _ := time.ParseDuration("24h")
	after48Hour, _ := time.ParseDuration("48h")
	query := tx.Model(&taskModels).Relation("RobInfo").Relation("BidInfo").
		Where("task.planned_completion_time >= ?", currentDay.Add(after24Hour)).
		Where("task.planned_completion_time < ?", currentDay.Add(after48Hour))
	if err := query.Select(); err != nil {
		return make([]*models.Task, 0), err
	} else {
		return taskModels, nil
	}
}

// 更新已过期的竞标任务的状态
func (dao *TaskDao) UpdateExpiredPlannedCompletionTimeBidTask() error {
	currentTime := time.Now()
	currentDay := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 23, 59, 59, 0, time.Now().Location())
	tx := dao.transactionContext.PgTx
	_, err := tx.Query(
		pg.Scan(),
		"UPDATE tasks SET task_status = ? WHERE planned_completion_time > ? AND task_type = ? AND task_status = ?",
		domain.TASK_STATUS_EXPIRED, currentDay, domain.TASK_TYPE_BID, domain.TASK_STATUS_UNCLAIMED)
	return err
}

// 返回接近竞标截止时间的竞标任务
func (dao *TaskDao) ListNearBidEndTimeTask() ([]*models.Task, error) {
	tx := dao.transactionContext.PgTx
	var taskModels []*models.Task
	currentTime := time.Now()
	currentDay := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 0, 0, 0, 0, time.Now().Location())
	after24Hour, _ := time.ParseDuration("24h")
	after48Hour, _ := time.ParseDuration("48h")
	query := tx.Model(&taskModels).Relation("RobInfo").Relation("BidInfo").
		Where("task.task_type = ?", domain.TASK_TYPE_BID).
		Where("bid_info.bid_end_time >= ?", currentDay.Add(after24Hour)).
		Where("bid_info.bid_end_time < ?", currentDay.Add(after48Hour))
	if err := query.Select(); err != nil {
		return make([]*models.Task, 0), err
	} else {
		return taskModels, nil
	}
}

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) DeleteRobInfo(taskId int64) error {
	tx := dao.transactionContext.PgTx
	_, err := tx.QueryOne(
		pg.Scan(),
		"DELETE FROM rob_infos WHERE task_id=?",
		taskId)
	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) CancelSuccessfulBidder(taskId int64) error {
	tx := dao.transactionContext.PgTx
	_, err := tx.QueryOne(
		pg.Scan(),
		"UPDATE bid_infos SET successful_bidder=?, win_bid_time=? WHERE task_id=?",
		nil, time.Time{}, taskId)
	return err
}

// 系统任务统计
func (dao *TaskDao) CalculateSystemTask(companyId int64) (map[string]interface{}, error) {
	var released int64  // 待领取的任务
	var underway int64  // 进行中的任务
	var completed int64   // 已完成任务
	var reward 	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)
	}
	if count, err := tx.Model(taskModel).
		Where("task.company_id = ?", companyId).
		Where("task.task_status <> ? ", domain.TASK_STATUS_CLOSED).
		Where("task.task_status <> ?", domain.TASK_STATUS_UNRELEASED).
		Where("task.task_status = ?", domain.TASK_STATUS_UNCLAIMED).
		Where("task.is_reward_take = ?", true).
		Count(); err != nil {
			return nil, err
	} else {
		reward = int64(count)
	}
	return map[string]interface{}{
		"released":  released + underway + completed,
		"underway":  underway,
		"completed": completed,
		"reward": reward,
	}, 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
	var expiredAsSponsor 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)
	}
	// 已过期任务统计(竞标时间过期的竞标任务,状态为待领取)
	//currentTime := time.Now()
	//currentDay := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 0, 0, 0, 0, time.Now().Location())
	//if count, err := tx.Model(taskModel).Relation("RobInfo").Relation("BidInfo").
	//	Where(`task.sponsor @> '{"uid":?}'`, uid).
	//	Where("task.task_status = ? ", domain.TASK_STATUS_UNCLAIMED).
	//	Where(`task.task_type = ?`, domain.TASK_TYPE_BID).
	//	Where("bid_info.bid_end_time >= ?", currentDay).
	//	Where("bid_info.bid_end_time < ?", currentDay).
	//	Count(); err != nil {
	//		return nil, err
	//} else {
	//	expiredAsSponsor = int64(count)
	//}
	if count, err := tx.Model(taskModel).
		Where(`task.sponsor @> '{"uid":?}'`, uid).
		Where("task.task_status = ? ", domain.TASK_STATUS_EXPIRED).
		Count(); err != nil {
			return nil, err
	} else {
		expiredAsSponsor = 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,
		"expiredAsSponsor":				expiredAsSponsor,
	}, nil
}

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