reward_standard.go 7.0 KB
package service

import (
	"time"

	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/rewardStandard/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/rewardStandard/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/rewardStandard/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
)

type RewardStandardService struct{}

func NewProductService(options map[string]interface{}) *RewardStandardService {
	newService := &RewardStandardService{}
	return newService
}

//SaveRewardStandard 保存奖惩标准数据
func (srv RewardStandardService) SaveRewardStandard(operateInfo domain.OperateInfo, param *command.SaveRewardStandardCommand) (*command.SaveRewardStandardCommand, error) {
	if err := param.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()
	}()

	rewardStandardRepo, _ := factory.CreateRewardStandardRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})

	_, dataList, err := rewardStandardRepo.Find(map[string]interface{}{
		"limit":      1,
		"workshopId": param.WorkshopId,
		"lineId":     param.LineId,
		"sectionId":  param.SectionId,
		"targetType": param.TargetType,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if len(dataList) > 0 {
		if dataList[0].Id != param.Id {
			return nil, application.ThrowError(application.BUSINESS_ERROR, "当前指标已存在")
		}
	}
	var rewardStandardData *domain.RewardStandard
	if param.Id > 0 {
		rewardStandardData, err = rewardStandardRepo.FindOne(map[string]interface{}{
			"id": param.Id,
		})
		if err != nil {
			return nil, application.ThrowError(application.BUSINESS_ERROR, "指标数据不存在"+err.Error())
		}
		if rewardStandardData.CompanyId != operateInfo.CompanyId {
			return nil, application.ThrowError(application.BUSINESS_ERROR, "指标数据填写错误")
		}

	} else {
		rewardStandardData = &domain.RewardStandard{
			CreatedAt: time.Now(),
		}
	}

	workShopRepo, _ := factory.CreateWorkshopRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	//获取车间数据
	workShopData, err := workShopRepo.FindOne(map[string]interface{}{
		"workshopId": param.WorkshopId,
	})
	if err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, "车间数据不能存在"+err.Error())
	}
	if workShopData.CompanyId != operateInfo.CompanyId {
		return nil, application.ThrowError(application.ARG_ERROR, "车间数据填写错误")
	}

	productLineData, err := workShopData.FindLine(param.LineId)
	if err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	productSection, err := productLineData.FindSection(param.SectionId)
	if err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	rewardStandardData.UpdatedAt = time.Now()
	rewardStandardData.ProductSection = *productSection
	rewardStandardData.ProductLine = productLineData.SimpleProductLine()
	rewardStandardData.Workshop = workShopData.SimpleWorkshop()
	rewardStandardData.Remark = param.Remark
	err = rewardStandardData.UpdateTarge(
		param.TargetType,
		param.TargeVal1,
		param.TargeVal2,
		param.TargeVal3,
		param.TargeVal4)
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	_, err = rewardStandardRepo.Save(rewardStandardData)
	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())
	}
	return nil, nil
}

//GetRewardStandard 根据id获取奖惩标准数据
func (srv RewardStandardService) GetRewardStandard(param *query.GetRewardStandard) (*dto.RewardStandardInfo, 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()
	}()

	rewardStandardRepo, _ := factory.CreateRewardStandardRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	rewardStandardData, err := rewardStandardRepo.FindOne(map[string]interface{}{
		"id": param.Id,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, "指标数据不存在"+err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}

	result := dto.RewardStandardInfo{
		Id:           rewardStandardData.Id,
		WorkshopId:   rewardStandardData.Workshop.WorkshopId,
		WorkshopName: rewardStandardData.Workshop.WorkshopName,
		LineId:       rewardStandardData.ProductLine.LineId,
		LineName:     rewardStandardData.ProductLine.LineName,
		SectionId:    rewardStandardData.ProductSection.SectionId,
		SectionName:  rewardStandardData.ProductSection.SectionName,
		Remark:       rewardStandardData.Remark,
		TargetType:   rewardStandardData.TargetType,
		TargeVal1:    rewardStandardData.TargeVal1,
		TargeVal2:    rewardStandardData.TargeVal2,
		TargeVal3:    rewardStandardData.TargeVal3,
		TargeVal4:    rewardStandardData.TargeVal4,
	}
	return &result, nil
}

//DeleteRewardStandard 根据id删除奖惩标准
func (srv RewardStandardService) DeleteRewardStandard(param *query.GetRewardStandard) (*domain.RewardStandard, 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()
	}()

	rewardStandardRepo, _ := factory.CreateRewardStandardRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	rewardStandardData, err := rewardStandardRepo.FindOne(map[string]interface{}{
		"id": param.Id,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, "指标数据不存在"+err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return rewardStandardData, nil
}