|
|
package repository
|
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"github.com/go-pg/pg/v10"
|
|
|
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
type TaskRecordRepository struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
func NewTaskRecordRepository(transactionContext *pgTransaction.TransactionContext) *TaskRecordRepository {
|
|
|
return &TaskRecordRepository{transactionContext: transactionContext}
|
|
|
}
|
|
|
|
|
|
func (repo *TaskRecordRepository) TransformToDomain(m *models.TaskRecord) domain.TaskRecord {
|
|
|
return domain.TaskRecord{
|
|
|
Id: m.Id,
|
|
|
CompanyId: m.CompanyId,
|
|
|
StaffAssessId: m.StaffAssessId,
|
|
|
TaskId: m.TaskId,
|
|
|
TaskName: m.TaskName,
|
|
|
TaskAlias: m.TaskAlias,
|
|
|
TaskLeader: m.TaskLeader,
|
|
|
AssistLevel: m.AssistLevel,
|
|
|
AssistContent: m.AssistContent,
|
|
|
TaskStages: m.TaskStages,
|
|
|
TaskStageCheck: m.TaskStageCheck,
|
|
|
CreatedAt: m.CreatedAt.Local(),
|
|
|
UpdatedAt: m.UpdatedAt.Local(),
|
|
|
DeletedAt: m.DeletedAt,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (repo *TaskRecordRepository) TransformToModel(d *domain.TaskRecord) models.TaskRecord {
|
|
|
return models.TaskRecord{
|
|
|
Id: d.Id,
|
|
|
CompanyId: d.CompanyId,
|
|
|
StaffAssessId: d.StaffAssessId,
|
|
|
TaskId: d.TaskId,
|
|
|
TaskName: d.TaskName,
|
|
|
TaskAlias: d.TaskAlias,
|
|
|
TaskLeader: d.TaskLeader,
|
|
|
AssistLevel: d.AssistLevel,
|
|
|
AssistContent: d.AssistContent,
|
|
|
TaskStages: d.TaskStages,
|
|
|
TaskStageCheck: d.TaskStageCheck,
|
|
|
CreatedAt: d.CreatedAt,
|
|
|
UpdatedAt: d.UpdatedAt,
|
|
|
DeletedAt: d.DeletedAt,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (repo *TaskRecordRepository) nextIdentify() (int64, error) {
|
|
|
return utils.NewSnowflakeId()
|
|
|
}
|
|
|
|
|
|
func (repo *TaskRecordRepository) Insert(d *domain.TaskRecord) (*domain.TaskRecord, error) {
|
|
|
var isCreate = d.Id == 0
|
|
|
if isCreate {
|
|
|
id, err := repo.nextIdentify()
|
|
|
if err != nil {
|
|
|
return d, err
|
|
|
}
|
|
|
d.Id = int(id)
|
|
|
d.CreatedAt = time.Now()
|
|
|
d.UpdatedAt = d.CreatedAt
|
|
|
} else {
|
|
|
d.UpdatedAt = time.Now()
|
|
|
}
|
|
|
m := repo.TransformToModel(d)
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
var err error
|
|
|
if isCreate {
|
|
|
_, err = tx.Model(&m).Returning("id").Insert()
|
|
|
} else {
|
|
|
_, err = tx.Model(&m).Returning("id").WherePK().Update() // 更新和删除必须增加条件
|
|
|
}
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
d.Id = m.Id
|
|
|
return d, nil
|
|
|
}
|
|
|
|
|
|
func (repo *TaskRecordRepository) Remove(d *domain.TaskRecord) (*domain.TaskRecord, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
nowTime := time.Now()
|
|
|
m := repo.TransformToModel(d)
|
|
|
m.DeletedAt = &nowTime
|
|
|
if _, err := tx.Model(&m).WherePK().Update(); err != nil {
|
|
|
return d, err
|
|
|
}
|
|
|
return d, nil
|
|
|
}
|
|
|
|
|
|
func (repo *TaskRecordRepository) FindOne(queryOptions map[string]interface{}) (*domain.TaskRecord, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
m := new(models.TaskRecord)
|
|
|
query := tx.Model(m)
|
|
|
query.Where("deleted_at isnull")
|
|
|
if id, ok := queryOptions["id"]; ok {
|
|
|
query.Where("id=?", id)
|
|
|
}
|
|
|
if err := query.First(); err != nil {
|
|
|
if errors.Is(err, pg.ErrNoRows) {
|
|
|
return nil, fmt.Errorf("没有此资源")
|
|
|
} else {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
u := repo.TransformToDomain(m)
|
|
|
return &u, nil
|
|
|
}
|
|
|
|
|
|
func (repo *TaskRecordRepository) Find(queryOptions map[string]interface{}) (int, []*domain.TaskRecord, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
var m []*models.TaskRecord
|
|
|
query := tx.Model(&m).Where("deleted_at isnull")
|
|
|
|
|
|
if v, ok := queryOptions["ids"]; ok {
|
|
|
query.Where("id in(?)", pg.In(v))
|
|
|
}
|
|
|
|
|
|
if v, ok := queryOptions["notId"]; ok {
|
|
|
query.Where("id != ?", v)
|
|
|
}
|
|
|
|
|
|
if v, ok := queryOptions["name"].(string); ok && len(v) > 0 {
|
|
|
query.Where("name LIKE ?", v)
|
|
|
}
|
|
|
|
|
|
if v, ok := queryOptions["companyId"]; ok {
|
|
|
query.Where("company_id = ?", v)
|
|
|
}
|
|
|
|
|
|
//if v, ok := queryOptions["state"]; ok && v.(int) >= 0 {
|
|
|
// query.Where("state = ?", v)
|
|
|
//}
|
|
|
|
|
|
if v, ok := queryOptions["limit"].(int64); ok {
|
|
|
query.Limit(int(v))
|
|
|
}
|
|
|
if v, ok := queryOptions["offset"].(int64); ok {
|
|
|
query.Offset(int(v))
|
|
|
}
|
|
|
|
|
|
//// 按创建时间降序
|
|
|
//query.Order("created_at DESC")
|
|
|
|
|
|
count, err := query.SelectAndCount()
|
|
|
if err != nil {
|
|
|
return 0, nil, err
|
|
|
}
|
|
|
var arrays []*domain.TaskRecord
|
|
|
for _, v := range m {
|
|
|
d := repo.TransformToDomain(v)
|
|
|
arrays = append(arrays, &d)
|
|
|
}
|
|
|
return count, arrays, nil
|
|
|
} |
...
|
...
|
|