# FdProcedureCheckErrorTypeServiceImpl.java

    package cloud.wconnect.workperformance.service.fdprocedurecheck.impl;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.exception.ForbiddenException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.commonlib.util.UpdateLogUtil;
    import cloud.wconnect.workperformance.cons.fdprocedurecheck.FdProcedureCheckI18nConstant;
    import cloud.wconnect.workperformance.entity.fdprocedurecheck.FdProcedureCheckErrorType;
    import cloud.wconnect.workperformance.mapper.fdprocedurecheck.FdProcedureCheckErrorTypeMapper;
    import cloud.wconnect.workperformance.pojo.dto.fdprocedurecheck.FdProcedureCheckErrorTypeDTO;
    import cloud.wconnect.workperformance.pojo.query.fdprocedurecheck.FdProcedureCheckErrorTypeQuery;
    import cloud.wconnect.workperformance.service.fdprocedurecheck.IFdProcedureCheckErrorTypeService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.stereotype.Service;

    import jakarta.annotation.Resource;

    import java.text.MessageFormat;
    import java.util.List;
    import java.util.Objects;

    /**
     * <p>
     * FD Procedure Check error type 服务实现类
     * </p>
     *
     * @author zc
     * @since 2023-10-05
     */
    @Service
    @DS("work_performance")
    public class FdProcedureCheckErrorTypeServiceImpl extends ServiceImpl<FdProcedureCheckErrorTypeMapper, FdProcedureCheckErrorType> implements IFdProcedureCheckErrorTypeService {
        @Resource
        private BeanMapper beanMapper;
        @Resource
        private UpdateLogUtil updateLogUtil;
        @Resource
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 获取 Error Type
         *
         * @param propertyId     资产 id
         * @param errorTypeQuery 查询条件
         * @return FdProcedureCheckErrorTypeDTO 集合
         * @author zc
         * @date 2023/10/5
         */
        @Override
        public ListPagedDTO<FdProcedureCheckErrorTypeDTO> list(int propertyId, FdProcedureCheckErrorTypeQuery errorTypeQuery) {
            LambdaQueryWrapper<FdProcedureCheckErrorType> queryWrapper = new QueryWrapper<FdProcedureCheckErrorType>().lambda()
                    .eq(FdProcedureCheckErrorType::getPropertyId, propertyId)
                    .eq(Objects.nonNull(errorTypeQuery.getSupportDesc()),
                            FdProcedureCheckErrorType::getSupportDesc, errorTypeQuery.getSupportDesc())
                    .eq(Objects.nonNull(errorTypeQuery.getRequired()),
                            FdProcedureCheckErrorType::getRequired, errorTypeQuery.getRequired());

            long total;
            List<FdProcedureCheckErrorType> errorTypeList;
            if (Objects.nonNull(errorTypeQuery.getPage()) && Objects.nonNull(errorTypeQuery.getSize())) {
                queryWrapper.like(StringUtils.isNotBlank(errorTypeQuery.getLabel()),
                        FdProcedureCheckErrorType::getLabel, errorTypeQuery.getLabel());
                Page<FdProcedureCheckErrorType> page = page(new Page<>(errorTypeQuery.getPage(),
                        errorTypeQuery.getSize()), queryWrapper.orderByAsc(FdProcedureCheckErrorType::getLabel));

                errorTypeList = page.getRecords();
                total = page.getTotal();
            } else {
                queryWrapper.eq(StringUtils.isNotBlank(errorTypeQuery.getLabel()),
                        FdProcedureCheckErrorType::getLabel, errorTypeQuery.getLabel());
                errorTypeList = list(queryWrapper);
                total = errorTypeList.size();
            }
            if (CollectionUtils.isEmpty(errorTypeList)) {
                return new ListPagedDTO<>();
            }

            return new ListPagedDTO<>(total, beanMapper.mapList(errorTypeList, FdProcedureCheckErrorTypeDTO.class));
        }

        /**
         * 校验 Error Type Label 是否已存在
         *
         * @param propertyId 资产 id
         * @param label      label
         * @param id         Error Type id
         * @author zc
         * @date 2023/10/5
         */
        @Override
        public void checkAlreadyExist(int propertyId, String label, Integer id) {
            long count = count(new QueryWrapper<FdProcedureCheckErrorType>().lambda()
                    .eq(FdProcedureCheckErrorType::getPropertyId, propertyId)
                    .eq(FdProcedureCheckErrorType::getLabel, label)
                    .ne(Objects.nonNull(id), FdProcedureCheckErrorType::getId, id));
            if (count > 0) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_EXISTED,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The error type label already exists");
            }
        }

        /**
         * 新增 Error Type
         *
         * @param propertyId   资产 id
         * @param errorTypeDTO Error Type 内容
         * @param loginUserId  登录用户id
         * @author zc
         * @date 2023/10/5
         */
        @Override
        public void add(int propertyId, FdProcedureCheckErrorTypeDTO errorTypeDTO, int loginUserId) {
            save(beanMapper.map(errorTypeDTO, FdProcedureCheckErrorType.class, item -> {
                item.setPropertyId(propertyId);
                item.setUpdateLog(updateLogUtil.get(loginUserId));
            }));
        }

        /**
         * 校验数据是否存在
         *
         * @param id Error Type id
         * @return FdProcedureCheckErrorType
         * @author zc
         * @date 2023/10/5
         */
        @Override
        public FdProcedureCheckErrorType checkExist(int id) {
            FdProcedureCheckErrorType errorType = getById(id);
            if (Objects.isNull(errorType)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The errorType is not found, id:" + id);
            }
            return errorType;
        }

        /**
         * 校验数据归属
         *
         * @param propertyId 资产 id
         * @param errorType  FdProcedureCheckErrorType
         * @author zc
         * @date 2023/10/5
         */
        @Override
        public void checkBelong(int propertyId, FdProcedureCheckErrorType errorType) {
            if (!errorType.getPropertyId().equals(propertyId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("The errorType does not belong to this property; propertyId:{0}; errorTypeId:{1}",
                                propertyId, errorType.getId()));
            }
        }

        /**
         * 更新数据
         *
         * @param originData   原数据
         * @param errorTypeDTO 更新内容
         * @param loginUserId  登录用户id
         * @author zc
         * @date 2023/10/5
         */
        @Override
        public void update(FdProcedureCheckErrorType originData, FdProcedureCheckErrorTypeDTO errorTypeDTO, int loginUserId) {
            update(new UpdateWrapper<FdProcedureCheckErrorType>().lambda()
                    .eq(FdProcedureCheckErrorType::getId, errorTypeDTO.getId())
                    .set(FdProcedureCheckErrorType::getLabel, errorTypeDTO.getLabel())
                    .set(FdProcedureCheckErrorType::getUpdateLog, updateLogUtil.append(originData.getUpdateLog(), loginUserId)));
        }

        /**
         * 根据属性校验输入内容
         *
         * @param propertyId 资产 id
         * @param id         Error Type id
         * @param content    内容
         * @author zc
         * @date 2023/10/6
         */
        @Override
        public void checkAttribute(int propertyId, Integer id, String content) {
            if (Objects.isNull(id) || id == BaseConstant.ZERO) {
                if (StringUtils.isNotBlank(content)) {
                    throw new BadRequestException(CommonErrorI18nConstant.MUST_EMPTY,
                            i18nMessageUtils.getErrorParamMapNeedTranslate(FdProcedureCheckI18nConstant.ERROR_TYPE_DESCRIPTION),
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "The errorTypeAdditional must be blank.");
                }
                return;
            }
            FdProcedureCheckErrorType errorType = checkExist(id);
            checkBelong(propertyId, errorType);
            if (!errorType.getSupportDesc() && StringUtils.isNotBlank(content)) {
                throw new BadRequestException(CommonErrorI18nConstant.MUST_EMPTY,
                        i18nMessageUtils.getErrorParamMapNeedTranslate(FdProcedureCheckI18nConstant.ERROR_TYPE_DESCRIPTION),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The errorTypeAdditional must be blank.");
            }
            if (Objects.nonNull(errorType.getRequired())
                    && errorType.getRequired() && StringUtils.isBlank(content)) {
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                        i18nMessageUtils.getErrorParamMapNeedTranslate(FdProcedureCheckI18nConstant.ERROR_TYPE_DESCRIPTION),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The errorTypeAdditional cannot be blank.");
            }
        }
    }