pg_reward_summary_statics_service.go 7.9 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,
		}
	}

	var targetType int
	switch param.Types {
	case domain.TroubleType1:
		targetType = domain.TargetType3
	case domain.TroubleType2:
		targetType = domain.TargetType4
	case domain.TroubleType3:
		targetType = domain.TargetType5
	case domain.TroubleType4:
		targetType = domain.TargetType5
	}
	//获取奖惩标准 按 车间 线别 工段 类型
	rewardStandardRepo, _ := repository.NewRewardStandardRepository(c.transactionContext)
	_, rewardStandardList, err := rewardStandardRepo.Find(map[string]interface{}{
		"companyId":  param.CompanyId,
		"orgId":      param.OrgId,
		"workshopId": param.WorkStation.WorkshopId,
		"lineId":     param.WorkStation.LineId,
		"sectionId":  param.WorkStation.SectionId,
		"targetType": targetType,
		"limit":      1,
	})
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if len(rewardStandardList) > 0 {
		summaryData.SummaryAccident(param, rewardStandardList[0])
	} else {
		summaryData.SummaryAccident(param, nil)
	}

	//获取奖惩规则
	rewardRuleRepo, _ := repository.NewRewardRuleRepository(c.transactionContext)
	_, rewardRuleList, err := rewardRuleRepo.Find(map[string]interface{}{
		"companyId": param.CompanyId,
		"orgId":     param.OrgId,
	})
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取奖惩规则"+err.Error())
	}
	if len(rewardRuleList) >= 0 {
		summaryData.ApplySummaryResult(rewardRuleList[0])
	}
	summaryData.UpdatedAt = nowTime
	_, err = rewardSummaryRepo.Save(summaryData)
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	return nil
}

// CreateRewardSummaryByEmployeeProductRecord 通过员工生产记录 汇总奖惩结果
func (c *PGRewardSummaryStaticService) CreateRewardSummaryByEmployeeProductRecord(param *domain.EmployeeProductRecord) 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.CreatedAt.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.CreatedAt,
			RecordDateStr:   param.CreatedAt.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,
		}
		if param.WorkStation != nil {
			summaryData.WorkStation = *param.WorkStation
		}
		if param.ProductWorker != nil {
			summaryData.Worker = *param.ProductWorker
		}
	}
	//获取奖惩标准 按 车间 线别 工段 类型 ;合格率类型
	rewardStandardRepo, _ := repository.NewRewardStandardRepository(c.transactionContext)
	_, rewardStandardList, err := rewardStandardRepo.Find(map[string]interface{}{
		"companyId":  param.CompanyId,
		"orgId":      param.OrgId,
		"workshopId": param.WorkStation.WorkshopId,
		"lineId":     param.WorkStation.LineId,
		"sectionId":  param.WorkStation.SectionId,
		"targetType": domain.TargetType2,
		"limit":      1,
	})
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if len(rewardStandardList) > 0 {
		//汇总合格率
		summaryData.SummaryUpToStandard(param.ProductRecordInfo.QualificationRate, rewardStandardList[0])
	} else {
		summaryData.SummaryUpToStandard(param.ProductRecordInfo.QualificationRate, nil)
	}

	//获取奖惩标准 按 车间 线别 工段 类型 ;产能类型
	_, rewardStandardList, err = rewardStandardRepo.Find(map[string]interface{}{
		"companyId":  param.CompanyId,
		"orgId":      param.OrgId,
		"workshopId": param.WorkStation.WorkshopId,
		"lineId":     param.WorkStation.LineId,
		"sectionId":  param.WorkStation.SectionId,
		"targetType": domain.TargetType1,
		"limit":      1,
	})
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if len(rewardStandardList) > 0 {
		//汇总合格率
		summaryData.SummaryYield(param.ProductRecordInfo.OutputWeight, rewardStandardList[0])
	} else {
		summaryData.SummaryYield(param.ProductRecordInfo.OutputWeight, nil)
	}
	//根据规则算 奖惩金额
	//获取奖惩规则
	rewardRuleRepo, _ := repository.NewRewardRuleRepository(c.transactionContext)
	_, rewardRuleList, err := rewardRuleRepo.Find(map[string]interface{}{
		"companyId": param.CompanyId,
		"orgId":     param.OrgId,
	})
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取奖惩规则"+err.Error())
	}
	if len(rewardRuleList) >= 0 {
		summaryData.ApplySummaryResult(rewardRuleList[0])
	}
	summaryData.UpdatedAt = nowTime
	_, err = rewardSummaryRepo.Save(summaryData)
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	return nil
}