package cn.iocoder.yudao.module.srm.service.evaluation;
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
import cn.iocoder.yudao.module.srm.controller.admin.evaluation.vo.SrmSupplierEvaluationPageReqVO;
|
import cn.iocoder.yudao.module.srm.controller.admin.evaluation.vo.SrmSupplierEvaluationSaveReqVO;
|
import cn.iocoder.yudao.module.srm.dal.dataobject.SrmSupplierEvaluationDO;
|
import cn.iocoder.yudao.module.srm.dal.mysql.SrmSupplierEvaluationMapper;
|
import cn.iocoder.yudao.module.srm.enums.ErrorCodeConstants;
|
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
import jakarta.annotation.Resource;
|
import org.springframework.stereotype.Service;
|
import org.springframework.validation.annotation.Validated;
|
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.time.LocalDateTime;
|
|
/**
|
* SRM 供应商绩效评价 Service 实现类
|
*/
|
@Service
|
@Validated
|
public class SrmSupplierEvaluationServiceImpl implements SrmSupplierEvaluationService {
|
|
@Resource
|
private SrmSupplierEvaluationMapper evaluationMapper;
|
|
@Override
|
public Long createEvaluation(SrmSupplierEvaluationSaveReqVO createReqVO) {
|
// 校验是否已存在相同周期的评价
|
SrmSupplierEvaluationDO existing = evaluationMapper.selectBySupplierAndPeriod(createReqVO.getSupplierId(), createReqVO.getEvaluationPeriod());
|
if (existing != null) {
|
throw ServiceExceptionUtil.exception(ErrorCodeConstants.EVALUATION_PERIOD_EXISTS);
|
}
|
SrmSupplierEvaluationDO evaluation = BeanUtils.toBean(createReqVO, SrmSupplierEvaluationDO.class);
|
calculateCompositeScore(evaluation);
|
evaluation.setEvaluationTime(LocalDateTime.now());
|
evaluationMapper.insert(evaluation);
|
return evaluation.getId();
|
}
|
|
@Override
|
public void updateEvaluation(SrmSupplierEvaluationSaveReqVO updateReqVO) {
|
validateEvaluationExists(updateReqVO.getId());
|
SrmSupplierEvaluationDO updateObj = BeanUtils.toBean(updateReqVO, SrmSupplierEvaluationDO.class);
|
calculateCompositeScore(updateObj);
|
evaluationMapper.updateById(updateObj);
|
}
|
|
@Override
|
public void deleteEvaluation(Long id) {
|
validateEvaluationExists(id);
|
evaluationMapper.deleteById(id);
|
}
|
|
@Override
|
public SrmSupplierEvaluationDO getEvaluation(Long id) {
|
return evaluationMapper.selectById(id);
|
}
|
|
@Override
|
public PageResult<SrmSupplierEvaluationDO> getEvaluationPage(SrmSupplierEvaluationPageReqVO pageReqVO) {
|
return evaluationMapper.selectPage(pageReqVO);
|
}
|
|
@Override
|
public void calculateCompositeScore(SrmSupplierEvaluationDO evaluation) {
|
BigDecimal quality = nvl(evaluation.getQualityScore());
|
BigDecimal delivery = nvl(evaluation.getDeliveryScore());
|
BigDecimal price = nvl(evaluation.getPriceScore());
|
BigDecimal service = nvl(evaluation.getServiceScore());
|
|
BigDecimal composite = quality.multiply(new BigDecimal("0.4"))
|
.add(delivery.multiply(new BigDecimal("0.3")))
|
.add(price.multiply(new BigDecimal("0.2")))
|
.add(service.multiply(new BigDecimal("0.1")))
|
.setScale(2, RoundingMode.HALF_UP);
|
|
evaluation.setCompositeScore(composite);
|
|
if (composite.compareTo(new BigDecimal("90")) >= 0) {
|
evaluation.setEvaluationLevel("A");
|
} else if (composite.compareTo(new BigDecimal("80")) >= 0) {
|
evaluation.setEvaluationLevel("B");
|
} else if (composite.compareTo(new BigDecimal("70")) >= 0) {
|
evaluation.setEvaluationLevel("C");
|
} else {
|
evaluation.setEvaluationLevel("D");
|
}
|
}
|
|
private void validateEvaluationExists(Long id) {
|
if (evaluationMapper.selectById(id) == null) {
|
throw ServiceExceptionUtil.exception(ErrorCodeConstants.EVALUATION_NOT_EXISTS);
|
}
|
}
|
|
private static BigDecimal nvl(BigDecimal value) {
|
return value != null ? value : BigDecimal.ZERO;
|
}
|
|
}
|