# FdProcedureCheckProblemDescServiceImpl.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.FdProcedureCheckProblemDesc;
    import cloud.wconnect.workperformance.mapper.fdprocedurecheck.FdProcedureCheckProblemDescMapper;
    import cloud.wconnect.workperformance.pojo.dto.fdprocedurecheck.FdProcedureCheckProblemDescriptionDTO;
    import cloud.wconnect.workperformance.pojo.query.fdprocedurecheck.FdProcedureCheckProblemDescriptionQuery;
    import cloud.wconnect.workperformance.service.fdprocedurecheck.IFdProcedureCheckProblemDescService;
    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 Problem Description 服务实现类
     * </p>
     *
     * @author zc
     * @since 2023-10-05
     */
    @Service
    @DS("work_performance")
    public class FdProcedureCheckProblemDescServiceImpl extends ServiceImpl<FdProcedureCheckProblemDescMapper, FdProcedureCheckProblemDesc> implements IFdProcedureCheckProblemDescService {
        @Resource
        private BeanMapper beanMapper;
        @Resource
        private UpdateLogUtil updateLogUtil;
        @Resource
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 获取 Problem Description
         *
         * @param propertyId              资产 id
         * @param problemDescriptionQuery 查询条件
         * @return FdProcedureCheckProblemDescriptionDTO 集合
         * @author zc
         * @date 2023/10/6
         */
        @Override
        public ListPagedDTO<FdProcedureCheckProblemDescriptionDTO> list(int propertyId, FdProcedureCheckProblemDescriptionQuery problemDescriptionQuery) {
            LambdaQueryWrapper<FdProcedureCheckProblemDesc> queryWrapper = new QueryWrapper<FdProcedureCheckProblemDesc>().lambda()
                    .eq(FdProcedureCheckProblemDesc::getPropertyId, propertyId)
                    .eq(Objects.nonNull(problemDescriptionQuery.getSupportDesc()),
                            FdProcedureCheckProblemDesc::getSupportDesc, problemDescriptionQuery.getSupportDesc())
                    .eq(Objects.nonNull(problemDescriptionQuery.getRequired()),
                            FdProcedureCheckProblemDesc::getRequired, problemDescriptionQuery.getRequired());

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

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

            return new ListPagedDTO<>(total, beanMapper.mapList(problemDescList, FdProcedureCheckProblemDescriptionDTO.class));
        }

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

        /**
         * 新增 Problem Description
         *
         * @param propertyId     资产 id
         * @param problemDescDTO Problem Description 内容
         * @param loginUserId    登录用户id
         * @author zc
         * @date 2023/10/6
         */
        @Override
        public void add(int propertyId, FdProcedureCheckProblemDescriptionDTO problemDescDTO, int loginUserId) {
            save(beanMapper.map(problemDescDTO, FdProcedureCheckProblemDesc.class, item -> {
                item.setPropertyId(propertyId);
                item.setUpdateLog(updateLogUtil.get(loginUserId));
            }));
        }

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

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

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

        /**
         * 根据属性校验输入内容
         *
         * @param propertyId 资产 id
         * @param id         problem Desc 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.PROBLEM_DESCRIPTION),
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "The problemDescAdditional must be blank.");
                }
                return;
            }
            FdProcedureCheckProblemDesc problemDesc = checkExist(id);
            checkBelong(propertyId, problemDesc);
            if (!problemDesc.getSupportDesc() && StringUtils.isNotBlank(content)) {
                throw new BadRequestException(CommonErrorI18nConstant.MUST_EMPTY,
                        i18nMessageUtils.getErrorParamMapNeedTranslate(FdProcedureCheckI18nConstant.PROBLEM_DESCRIPTION),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The problemDescAdditional must be blank.");
            }
            if (Objects.nonNull(problemDesc.getSupportDesc())
                    && problemDesc.getSupportDesc() && problemDesc.getRequired() && StringUtils.isBlank(content)) {
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                        i18nMessageUtils.getErrorParamMapNeedTranslate(FdProcedureCheckI18nConstant.PROBLEM_DESCRIPTION),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The problemDescAdditional cannot be blank.");
            }
        }
    }