reward_standard.go 9.8 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 NewRewardStandardService(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
	rewardStandardData.CompanyId = operateInfo.CompanyId
	rewardStandardData.OrgId = operateInfo.OrgId
	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())
	}
	param.Id = rewardStandardData.Id
	return param, nil
}

//GetRewardStandard 根据id获取奖惩标准数据
func (srv RewardStandardService) GetRewardStandard(operateInfo *domain.OperateInfo, 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())
	}
	if operateInfo.CompanyId != rewardStandardData.CompanyId {
		return nil, application.ThrowError(application.BUSINESS_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(operateInfo *domain.OperateInfo, 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 operateInfo.CompanyId != rewardStandardData.CompanyId {
		return nil, application.ThrowError(application.BUSINESS_ERROR, "指标数据不存在")
	}
	_, err = rewardStandardRepo.Remove(rewardStandardData)
	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
}

func (srv RewardStandardService) ListRewardStandard(param *query.ListRewardStandard) (int64, []dto.RewardStandardList, 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()
	}()

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

	condition := map[string]interface{}{
		"limit":     20,
		"offset":    0,
		"companyId": param.CompanyId,
	}
	if param.PageSize > 0 {
		condition["limit"] = param.PageSize
	}
	offset := param.PageSize * (param.PageNumber - 1)
	if offset > 0 {
		condition["offset"] = offset
	}
	if len(param.WorkshopName) > 0 {
		condition["workshopName"] = param.WorkshopName
	}
	if len(param.SectionName) > 0 {
		condition["sectionName"] = param.SectionName
	}
	if len(param.LineName) > 0 {
		condition["lineName"] = param.LineName
	}
	cnt, rewardStandardList, err := rewardStandardRepo.Find(condition)
	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 listResult []dto.RewardStandardList
	var temp dto.RewardStandardList
	for _, v := range rewardStandardList {
		temp = dto.RewardStandardList{
			Id:             v.Id,
			WorkshopId:     v.Workshop.WorkshopId,
			WorkshopName:   v.Workshop.WorkshopName,
			LineId:         v.ProductLine.LineId,
			LineName:       v.ProductLine.LineName,
			SectionId:      v.ProductSection.SectionId,
			SectionName:    v.ProductSection.SectionName,
			Remark:         v.Remark,
			TargetType:     v.TargetType,
			TargetTypeName: v.TargetTypeName(),
			TargeReward:    v.ShowTargeReward(),
			TargeFault:     v.ShowTargeFault(),
		}
		listResult = append(listResult, temp)
	}
	return cnt, listResult, nil
}