attendance.go 15.8 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/attendance/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/attendance/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/attendance/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
	"time"
)

type AttendanceService struct {
}

// 审核工时
func (attendanceService *AttendanceService) ApproveAttendance(cmd *command.ApproveAttendanceCommand) (interface{}, error) {
	if err := cmd.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var productAttendanceRecordRepository domain.ProductAttendanceRecordRepository
	var attendance *domain.ProductAttendanceRecord
	productAttendanceRecordRepository, attendance, err = factory.FastPgAttendance(transactionContext, cmd.ProductAttendanceId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	var user *domain.User
	userService := domainService.NewUserService()
	user, err = userService.User(cmd.ApproveUserId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	if err = attendance.Approve(user, cmd.WorkTimeAfter, domain.AttendanceApproved); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	if _, err := productAttendanceRecordRepository.Save(attendance); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := domainService.SendWorkshopWorkTimeStaticJob(attendance); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return struct{}{}, nil
}

// 创建
func (attendanceService *AttendanceService) CreateAttendance(operateInfo *domain.OperateInfo, cmd *command.CreateAttendanceCommand) (interface{}, error) {
	if err := cmd.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	var user *domain.User
	userService := domainService.NewUserService()
	user, err = userService.User(cmd.ProductWorkerId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	var workStation *domain.WorkStation
	_, workStation, err = factory.FastPgWorkstation(transactionContext, cmd.WorkshopId, cmd.LineId, cmd.SectionId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	var org *domain.Org
	org, err = userService.Organization(operateInfo.OrgId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	var productGroup *domain.ProductGroup
	_, productGroup, err = factory.FastPgProductGroup(transactionContext, cmd.ProductGroupId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	newAttendance := &domain.ProductAttendanceRecord{
		//ProductAttendanceId: cmd.ProductAttendanceId,
		CompanyId:        operateInfo.CompanyId,
		OrgId:            operateInfo.OrgId,
		AttendanceType:   cmd.AttendanceType,
		ProductWorker:    user,
		WorkStation:      workStation,
		SignIn:           cmd.SignIn,
		SignOut:          cmd.SignOut,
		AttendanceStatus: domain.AttendanceNotApprove,
		WorkTimeBefore:   0,
		WorkTimeAfter:    0,
		CreatedAt:        time.Now(),
		UpdatedAt:        time.Now(),
		Ext: domain.NewExt(org.OrgName).WithAttendanceExt(&domain.ProductAttendanceRecordExt{
			GroupName:      productGroup.GroupName,
			ProductGroupId: productGroup.ProductGroupId,
		}),
	}
	newAttendance.WorkTimeBefore = newAttendance.ComputeWorkTimeBefore()
	var attendanceRepository domain.ProductAttendanceRecordRepository

	attendanceRepository, _, _ = factory.FastPgAttendance(transactionContext, 0)

	if attendance, err := attendanceRepository.Save(newAttendance); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return attendance, nil
	}
}

// 返回
func (attendanceService *AttendanceService) GetAttendance(getAttendanceQuery *query.GetAttendanceQuery) (interface{}, error) {
	if err := getAttendanceQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	//var attendanceRepository domain.ProductAttendanceRecordRepository
	var attendance *domain.ProductAttendanceRecord
	_, attendance, err = factory.FastPgAttendance(transactionContext, getAttendanceQuery.ProductAttendanceId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	result := &dto.AttendanceRecordDto{}
	result.LoadDto(attendance, 0)
	return result, nil
}

// 返回列表
func (attendanceService *AttendanceService) ListAttendance(listAttendanceQuery *query.ListAttendanceQuery) (interface{}, error) {
	if err := listAttendanceQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	//var attendanceRepository attendance.AttendanceRepository
	//if value, err := factory.CreateAttendanceRepository(map[string]interface{}{
	//	"transactionContext": transactionContext,
	//}); err != nil {
	//	return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	//} else {
	//	attendanceRepository = value
	//}
	//if count, attendances, err := attendanceRepository.Find(tool_funs.SimpleStructToMap(listAttendanceQuery)); err != nil {
	//	return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	//} else {
	//	if err := transactionContext.CommitTransaction(); err != nil {
	//		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	//	}
	//	return map[string]interface{}{
	//		"count":       count,
	//		"attendances": attendances,
	//	}, nil
	//}
	return nil, nil
}

// 移除
func (attendanceService *AttendanceService) RemoveAttendance(removeAttendanceCommand *command.RemoveAttendanceCommand) (interface{}, error) {
	if err := removeAttendanceCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var attendanceRepository domain.ProductAttendanceRecordRepository
	var attendance *domain.ProductAttendanceRecord

	attendanceRepository, attendance, err = factory.FastPgAttendance(transactionContext, removeAttendanceCommand.ProductAttendanceId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	if attendance, err := attendanceRepository.Remove(attendance); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return attendance, nil
	}
}

// 更新
func (attendanceService *AttendanceService) UpdateAttendance(updateAttendanceCommand *command.UpdateAttendanceCommand) (interface{}, error) {
	if err := updateAttendanceCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	//var attendanceRepository attendance.AttendanceRepository
	//if value, err := factory.CreateAttendanceRepository(map[string]interface{}{
	//	"transactionContext": transactionContext,
	//}); err != nil {
	//	return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	//} else {
	//	attendanceRepository = value
	//}
	//attendance, err := attendanceRepository.FindOne(map[string]interface{}{"attendanceId": updateAttendanceCommand.AttendanceId})
	//if err != nil {
	//	return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	//}
	//if attendance == nil {
	//	return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateAttendanceCommand.AttendanceId)))
	//}
	//if err := attendance.Update(tool_funs.SimpleStructToMap(updateAttendanceCommand)); err != nil {
	//	return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	//}
	//if attendance, err := attendanceRepository.Save(attendance); err != nil {
	//	return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	//} else {
	//	if err := transactionContext.CommitTransaction(); err != nil {
	//		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	//	}
	//	return attendance, nil
	//}
	return nil, nil
}

// 返回列表
func (attendanceService *AttendanceService) SearchAttendance(operateInfo *domain.OperateInfo, cmd *query.SearchAttendanceQuery) (int64, interface{}, error) {
	if err := cmd.ValidateQuery(); err != nil {
		return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var attendanceRepository domain.ProductAttendanceRecordRepository
	attendanceRepository, _, _ = factory.FastPgAttendance(transactionContext, 0)

	queryOptions := utils.ObjectToMap(cmd)

	count, attendances, err := attendanceRepository.Find(queryOptions)
	if err != nil {
		return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	var result = make([]*dto.AttendanceRecordDto, 0)
	for i := range attendances {
		newItem := &dto.AttendanceRecordDto{}
		result = append(result, newItem.LoadDto(attendances[i], operateInfo.OrgId))
	}
	return count, result, nil
}

// 员工工时统计
func (attendanceService *AttendanceService) SearchEmployeeAttendanceStatics(operateInfo *domain.OperateInfo, cmd *query.SearchEmployeeAttendanceQuery) (int64, interface{}, error) {
	if err := cmd.ValidateQuery(); err != nil {
		return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var attendanceRepository domain.ProductAttendanceRecordRepository
	attendanceRepository, _, _ = factory.FastPgAttendance(transactionContext, 0)

	queryOptions := utils.ObjectToMap(cmd)
	count, attendances, err := attendanceRepository.Find(queryOptions)
	if err != nil {
		return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	var result = make([]*dto.EmployeeAttendanceRecordDto, 0)
	for i := range attendances {
		newItem := &dto.EmployeeAttendanceRecordDto{}
		result = append(result, newItem.LoadDto(attendances[i], operateInfo.OrgId))
	}
	return count, result, nil
}

// 车间工时统计
func (attendanceService *AttendanceService) SearchWorkshopWorkTimeStatics(operateInfo *domain.OperateInfo, cmd *query.SearchEmployeeAttendanceQuery) (int64, interface{}, error) {
	if err := cmd.ValidateQuery(); err != nil {
		return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var attendanceRepository domain.WorkshopWorkTimeRecordRepository
	attendanceRepository, _, _ = factory.FastPgWorkshopWorkTimeRecord(transactionContext, 0)

	queryOptions := utils.ObjectToMap(cmd)
	count, attendances, err := attendanceRepository.Find(queryOptions)
	if err != nil {
		return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	var result = make([]*dto.WorkshopWorkTimeRecordDto, 0)
	for i := range attendances {
		newItem := &dto.WorkshopWorkTimeRecordDto{}
		result = append(result, newItem.LoadDto(attendances[i], operateInfo.OrgId))
	}
	return count, result, nil
}

func NewAttendanceService(options map[string]interface{}) *AttendanceService {
	newAttendanceService := &AttendanceService{}
	return newAttendanceService
}