pg_reward_summary_statics_service.go 2.8 KB
package domainService

import (
	"fmt"
	"time"

	"github.com/linmadan/egglib-go/core/application"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/repository"
)

//功过奖惩明细汇总

type PGRewardSummaryStaticService struct {
	transactionContext *pgTransaction.TransactionContext
}

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

// 根据
func (c *PGRewardSummaryStaticService) CreateRewardSummaryByProductTrouble(param *domain.ProductTrouble) error {
	rewardSummaryRepo, _ := repository.NewRewardSummaryRepository(c.transactionContext)
	//查询是否已经有汇总数据
	condtion := map[string]interface{}{
		"orgId":         param.OrgId,
		"companyId":     param.CompanyId,
		"lineId":        param.WorkStation.LineId,
		"sectionId":     param.WorkStation.SectionId,
		"workshopId":    param.WorkStation.WorkshopId,
		"workerId":      param.ProductWorker.UserId,
		"recordDateStr": param.RecordDate.Format("200-01-02"),
	}

	_, summaryList, err := rewardSummaryRepo.Find(condtion)
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	var summaryData *domain.RewardSummary
	nowTime := time.Now()
	if len(summaryList) > 0 {
		summaryData = summaryList[0]
	} else {
		summaryData = &domain.RewardSummary{
			Id:              0,
			CompanyId:       param.CompanyId,
			OrgId:           param.OrgId,
			WorkStation:     param.WorkStation,
			Worker:          param.ProductWorker,
			RecordDate:      param.RecordDate,
			RecordDateStr:   param.RecordDate.Format("2006-01-02"),
			UpToStandard:    0.0,
			Yield:           0.0,
			AccidentNum1:    0,
			AccidentAmount1: 0.0,
			AccidentNum2:    0,
			AccidentAmount2: 0.0,
			AccidentNum3:    0,
			AccidentNum4:    0,
			SummaryResult:   0.0,
			CreatedAt:       nowTime,
			UpdatedAt:       nowTime,
		}
	}
	// 将审核过的事故放入汇总数据计算
	summaryData.SummaryAccident(param)

	//获取奖惩标准 按
	rewardStandardRepo, _ := repository.NewRewardStandardRepository(c.transactionContext)

	rewardStandardRepo.Find(map[string]interface{}{
		"companyId": param.CompanyId,
		"orgId":     param.OrgId,
	})

	summaryData.UpdatedAt = nowTime
	_, err = rewardSummaryRepo.Save(summaryData)
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	return nil
}