|
|
package repository
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"github.com/go-pg/pg/v10"
|
|
|
"time"
|
|
|
|
|
|
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
|
|
|
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
"github.com/linmadan/egglib-go/utils/snowflake"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/transform"
|
|
|
)
|
|
|
|
|
|
type WorkshopWorkTimeRecordRepository struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
func (repository *WorkshopWorkTimeRecordRepository) nextIdentify() (int64, error) {
|
|
|
IdWorker, err := snowflake.NewIdWorker(1)
|
|
|
if err != nil {
|
|
|
return 0, err
|
|
|
}
|
|
|
id, err := IdWorker.NextId()
|
|
|
return id, err
|
|
|
}
|
|
|
func (repository *WorkshopWorkTimeRecordRepository) Save(workshopWorkTimeRecord *domain.WorkshopWorkTimeRecord) (*domain.WorkshopWorkTimeRecord, error) {
|
|
|
sqlBuildFields := []string{
|
|
|
"workshop_work_time_record_id",
|
|
|
"work_station",
|
|
|
"created_at",
|
|
|
"company_id",
|
|
|
"org_id",
|
|
|
"workshop_work_time_record_info",
|
|
|
"ext",
|
|
|
"record_date",
|
|
|
}
|
|
|
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "workshop_work_time_record_id"))
|
|
|
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "workshop_work_time_record_id"))
|
|
|
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
|
|
|
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "workshop_work_time_record_id")
|
|
|
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
if workshopWorkTimeRecord.Identify() == nil {
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(
|
|
|
&workshopWorkTimeRecord.WorkshopWorkTimeRecordId,
|
|
|
&workshopWorkTimeRecord.WorkStation,
|
|
|
&workshopWorkTimeRecord.CreatedAt,
|
|
|
&workshopWorkTimeRecord.CompanyId,
|
|
|
&workshopWorkTimeRecord.OrgId,
|
|
|
&workshopWorkTimeRecord.WorkshopWorkTimeRecordInfo,
|
|
|
&workshopWorkTimeRecord.Ext,
|
|
|
&workshopWorkTimeRecord.RecordDate,
|
|
|
),
|
|
|
fmt.Sprintf("INSERT INTO manufacture.workshop_work_time_record (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
|
|
|
//workshopWorkTimeRecord.WorkshopWorkTimeRecordId,
|
|
|
workshopWorkTimeRecord.WorkStation,
|
|
|
workshopWorkTimeRecord.CreatedAt,
|
|
|
workshopWorkTimeRecord.CompanyId,
|
|
|
workshopWorkTimeRecord.OrgId,
|
|
|
workshopWorkTimeRecord.WorkshopWorkTimeRecordInfo,
|
|
|
workshopWorkTimeRecord.Ext,
|
|
|
workshopWorkTimeRecord.RecordDate,
|
|
|
); err != nil {
|
|
|
return workshopWorkTimeRecord, err
|
|
|
}
|
|
|
} else {
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(
|
|
|
&workshopWorkTimeRecord.WorkshopWorkTimeRecordId,
|
|
|
&workshopWorkTimeRecord.WorkStation,
|
|
|
&workshopWorkTimeRecord.CreatedAt,
|
|
|
&workshopWorkTimeRecord.CompanyId,
|
|
|
&workshopWorkTimeRecord.OrgId,
|
|
|
&workshopWorkTimeRecord.WorkshopWorkTimeRecordInfo,
|
|
|
&workshopWorkTimeRecord.Ext,
|
|
|
&workshopWorkTimeRecord.RecordDate,
|
|
|
),
|
|
|
fmt.Sprintf("UPDATE manufacture.workshop_work_time_record SET %s WHERE workshop_work_time_record_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
|
|
|
//workshopWorkTimeRecord.WorkshopWorkTimeRecordId,
|
|
|
workshopWorkTimeRecord.WorkStation,
|
|
|
workshopWorkTimeRecord.CreatedAt,
|
|
|
workshopWorkTimeRecord.CompanyId,
|
|
|
workshopWorkTimeRecord.OrgId,
|
|
|
workshopWorkTimeRecord.WorkshopWorkTimeRecordInfo,
|
|
|
workshopWorkTimeRecord.Ext,
|
|
|
workshopWorkTimeRecord.RecordDate,
|
|
|
workshopWorkTimeRecord.Identify(),
|
|
|
); err != nil {
|
|
|
return workshopWorkTimeRecord, err
|
|
|
}
|
|
|
}
|
|
|
return workshopWorkTimeRecord, nil
|
|
|
}
|
|
|
func (repository *WorkshopWorkTimeRecordRepository) Remove(workshopWorkTimeRecord *domain.WorkshopWorkTimeRecord) (*domain.WorkshopWorkTimeRecord, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
workshopWorkTimeRecordModel := new(models.WorkshopWorkTimeRecord)
|
|
|
workshopWorkTimeRecordModel.WorkshopWorkTimeRecordId = workshopWorkTimeRecord.Identify().(int)
|
|
|
if _, err := tx.Model(workshopWorkTimeRecordModel).WherePK().Delete(); err != nil {
|
|
|
return workshopWorkTimeRecord, err
|
|
|
}
|
|
|
return workshopWorkTimeRecord, nil
|
|
|
}
|
|
|
func (repository *WorkshopWorkTimeRecordRepository) FindOne(queryOptions map[string]interface{}) (*domain.WorkshopWorkTimeRecord, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
workshopWorkTimeRecordModel := new(models.WorkshopWorkTimeRecord)
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(workshopWorkTimeRecordModel), queryOptions)
|
|
|
query.SetWhereByQueryOption("workshop_work_time_record.workshop_work_time_record_id = ?", "workshopWorkTimeRecordId")
|
|
|
if err := query.First(); err != nil {
|
|
|
if err.Error() == "pg: no rows in result set" {
|
|
|
return nil, domain.ErrorNotFound
|
|
|
} else {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
if workshopWorkTimeRecordModel.WorkshopWorkTimeRecordId == 0 {
|
|
|
return nil, nil
|
|
|
} else {
|
|
|
return transform.TransformToWorkshopWorkTimeRecordDomainModelFromPgModels(workshopWorkTimeRecordModel)
|
|
|
}
|
|
|
}
|
|
|
func (repository *WorkshopWorkTimeRecordRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.WorkshopWorkTimeRecord, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
var workshopWorkTimeRecordModels []*models.WorkshopWorkTimeRecord
|
|
|
workshopWorkTimeRecords := make([]*domain.WorkshopWorkTimeRecord, 0)
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(&workshopWorkTimeRecordModels), queryOptions)
|
|
|
|
|
|
query.SetWhereByQueryOption("company_id = ?", "companyId")
|
|
|
query.SetWhereByQueryOption("org_id = ?", "orgId")
|
|
|
if v, ok := queryOptions["inOrgIds"]; ok && len(v.([]int)) > 0 {
|
|
|
query.Where(`org_id in (?)`, pg.In(v))
|
|
|
}
|
|
|
if v, ok := queryOptions["workshopName"]; ok && len(v.(string)) > 0 {
|
|
|
query.Where(fmt.Sprintf(`work_station->>'workshopName' like '%%%v%%'`, v))
|
|
|
}
|
|
|
if v, ok := queryOptions["lineName"]; ok && len(v.(string)) > 0 {
|
|
|
query.Where(fmt.Sprintf(`work_station->>'lineName' like '%%%v%%'`, v))
|
|
|
}
|
|
|
//if v, ok := queryOptions["sectionName"]; ok && len(v.(string)) > 0 {
|
|
|
// query.Where(fmt.Sprintf(`work_station->>'sectionName' like '%%%v%%'`, v))
|
|
|
//}
|
|
|
if v, ok := queryOptions["signBeginTime"]; ok && !((v.(time.Time)).IsZero()) {
|
|
|
query.Where("record_date>=?", v.(time.Time))
|
|
|
}
|
|
|
if v, ok := queryOptions["signEndTime"]; ok && !((v.(time.Time)).IsZero()) {
|
|
|
query.Where("record_date<?", v.(time.Time))
|
|
|
}
|
|
|
|
|
|
query.SetOffsetAndLimit(domain.MaxQueryRow)
|
|
|
query.SetOrderDirect("record_date", "DESC")
|
|
|
query.SetOrderDirect("workshop_work_time_record_id", "DESC")
|
|
|
if count, err := query.SelectAndCount(); err != nil {
|
|
|
return 0, workshopWorkTimeRecords, err
|
|
|
} else {
|
|
|
for _, workshopWorkTimeRecordModel := range workshopWorkTimeRecordModels {
|
|
|
if workshopWorkTimeRecord, err := transform.TransformToWorkshopWorkTimeRecordDomainModelFromPgModels(workshopWorkTimeRecordModel); err != nil {
|
|
|
return 0, workshopWorkTimeRecords, err
|
|
|
} else {
|
|
|
workshopWorkTimeRecords = append(workshopWorkTimeRecords, workshopWorkTimeRecord)
|
|
|
}
|
|
|
}
|
|
|
return int64(count), workshopWorkTimeRecords, nil
|
|
|
}
|
|
|
}
|
|
|
func NewWorkshopWorkTimeRecordRepository(transactionContext *pgTransaction.TransactionContext) (*WorkshopWorkTimeRecordRepository, error) {
|
|
|
if transactionContext == nil {
|
|
|
return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
} else {
|
|
|
return &WorkshopWorkTimeRecordRepository{
|
|
|
transactionContext: transactionContext,
|
|
|
}, nil
|
|
|
}
|
|
|
} |
...
|
...
|
|