# AdjTrxCodeConfigController.java

    package cloud.wconnect.frontoffice.controller.adjustment;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.component.SessionContext;
    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.JsonUtils;
    import cloud.wconnect.frontoffice.cons.adjustment.AdjColumnEnum;
    import cloud.wconnect.frontoffice.cons.adjustment.TrxCodeTypeEnum;
    import cloud.wconnect.frontoffice.entity.adjustment.AdjTrxCodeConfig;
    import cloud.wconnect.frontoffice.pojo.dto.adjustment.AdjTrxCodeConfigDTO;
    import cloud.wconnect.frontoffice.pojo.dto.adjustment.ScopeMatchDTO;
    import cloud.wconnect.frontoffice.pojo.query.adjustment.TrxCodeQuery;
    import cloud.wconnect.frontoffice.service.adjustment.IAdjTrxCodeConfigService;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.google.common.collect.Lists;
    import jakarta.validation.Valid;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.validation.annotation.Validated;
    import org.springframework.web.bind.annotation.*;

    import java.util.*;

    /**
     * <p>
     * Adjustment Requests trxCode 忽略配置表 前端控制器
     * </p>
     *
     * @author lichen
     * @since 2023-04-04
     */
    @RestController
    @RequestMapping("/adjustment/trx-code-config")
    public class AdjTrxCodeConfigController {

        @Autowired
        private IAdjTrxCodeConfigService adjTrxCodeConfigService;
        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private ObjectMapper objectMapper;
        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 查询trxCodeConfig
         *
         * @param property     资产标识
         * @param trxCodeQuery 查询对象
         * @return cloud.wconnect.commonlib.pojo.dto.ListPagedDTO<cloud.wconnect.frontoffice.pojo.dto.adjustment.AdjTrxCodeConfigDTO>
         * @author lichen
         * @date 2025/8/22
         */
        @ApiId(274)
        @GetMapping
        public ListPagedDTO<AdjTrxCodeConfigDTO> page(String property, @Valid TrxCodeQuery trxCodeQuery) {
            int propertyId = sessionContext.getPropertyId(property);

            /*
             * Content非空时，matchType不能为null
             */
            String content = trxCodeQuery.getContent();
            if (StringUtils.isNotBlank(content)) {
                Integer matchType = trxCodeQuery.getMatchType();
                if (Objects.isNull(matchType)) {
                    throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                            i18nMessageUtils.getErrorParamMapNeedTranslate(AdjColumnEnum.MATCH_TYPE.getI18nCode()),
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "matchType cannot be null");
                }
            }

            return adjTrxCodeConfigService.pageList(propertyId, trxCodeQuery);
        }

        /**
         * 新增忽略的trxCodeConfig
         *
         * @param property         资产标识
         * @param trxCodeConfigDTO 忽略的trxCodeConfig
         * @return java.util.Map
         * @author lichen
         * @date 2025/8/22
         */
        @ApiId(275)
        @PostMapping
        public Map save(String property, @Validated @RequestBody AdjTrxCodeConfigDTO trxCodeConfigDTO) {
            int propertyId = sessionContext.getPropertyId(property);

            if (Lists.newArrayList(TrxCodeTypeEnum.RANGE_MATCH.getType(), TrxCodeTypeEnum.RANGE_MATCH_WITH_PREFIX.getType())
                    .contains(trxCodeConfigDTO.getMatchType())) {
                ScopeMatchDTO scopeMatchDTO = trxCodeConfigDTO.getScopeMatch();
                //校验scopeMatchDTO不能为null
                if (Objects.isNull(scopeMatchDTO)) {
                    throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                            i18nMessageUtils.getErrorParamMapNotNeedTranslate("scopeMatch"),
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "scope match cannot be null");
                }
                if (Objects.isNull(scopeMatchDTO.getStart())) {
                    throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                            i18nMessageUtils.getErrorParamMapNotNeedTranslate("startValue"),
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "startValue cannot be null");
                }
                if (Objects.isNull(scopeMatchDTO.getEnd())) {
                    throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                            i18nMessageUtils.getErrorParamMapNotNeedTranslate("endValue"),
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "endValue cannot be null");
                }
                //校验start < end
                if (scopeMatchDTO.getStart() > scopeMatchDTO.getEnd()) {
                    throw new BadRequestException(CommonErrorI18nConstant.CANNOT_GREATER_THAN,
                            i18nMessageUtils.getErrorParamMapNotNeedTranslate("startValue", "endValue"),
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "the start value cannot be greater than the end value");
                }
                //校验前缀不能为null
                if (TrxCodeTypeEnum.RANGE_MATCH_WITH_PREFIX.getType() == trxCodeConfigDTO.getMatchType()) {
                    if (StringUtils.isBlank(scopeMatchDTO.getPrefix())) {
                        throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                                i18nMessageUtils.getErrorParamMapNotNeedTranslate("prefix"),
                                BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                                "prefix cannot be blank");
                    }
                }

                trxCodeConfigDTO.setTrxCode(JsonUtils.convertObjToString(objectMapper, scopeMatchDTO));
            } else {
                if (StringUtils.isBlank(trxCodeConfigDTO.getTrxCode())) {
                    throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                            i18nMessageUtils.getErrorParamMapNeedTranslate(AdjColumnEnum.TRANSACTION_CODE.getI18nCode()),
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "trxCode cannot be blank");
                }
            }

            // 校验是否存在
            adjTrxCodeConfigService.checkExist(propertyId, trxCodeConfigDTO.getMatchType(), trxCodeConfigDTO.getTrxCode());

            AdjTrxCodeConfig entity = beanMapper.map(trxCodeConfigDTO, AdjTrxCodeConfig.class);
            entity.setPropertyId(propertyId);
            adjTrxCodeConfigService.save(entity);
            return Map.of("id", entity.getId());
        }

        /**
         * 删除trxCodeConfig配置
         *
         * @param property 资产标识
         * @param id       主键id
         * @return java.util.Map
         * @author lichen
         * @date 2025/8/22
         */
        @ApiId(276)
        @DeleteMapping("/{id:\\d+}")
        public Map delete(String property, @PathVariable long id) {
            int propertyId = sessionContext.getPropertyId(property);
            checkExistAndBelongProperty(id, propertyId);
            adjTrxCodeConfigService.removeById(id);
            return Collections.emptyMap();
        }

        /**
         * 校验存在和归属
         *
         * @param id         主键id
         * @param propertyId 资产id
         * @author lichen
         * @date 2025/8/18
         */
        private void checkExistAndBelongProperty(Long id, Integer propertyId) {
            AdjTrxCodeConfig byId = adjTrxCodeConfigService.getById(id);
            if (Objects.isNull(byId)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "id=" + id + " ,Adjustment Requests TrxCode Config not found");
            }
            if (byId.getPropertyId().compareTo(propertyId) != 0) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        "data has no access");
            }
        }

    }