# AdjPmsConfigController.java

    package cloud.wconnect.frontoffice.controller.adjustment;

    import cloud.wconnect.commonlib.annotation.ApiId;
    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.frontoffice.cons.adjustment.AdjTypeEnum;
    import cloud.wconnect.frontoffice.entity.adjustment.AdjPmsAccount;
    import cloud.wconnect.frontoffice.entity.adjustment.AdjPmsConfig;
    import cloud.wconnect.frontoffice.pojo.dto.adjustment.AdjPmsConfigDTO;
    import cloud.wconnect.frontoffice.pojo.query.adjustment.PmsAccountQuery;
    import cloud.wconnect.frontoffice.pojo.query.adjustment.PmsConfigQuery;
    import cloud.wconnect.frontoffice.service.adjustment.IAdjPmsAccountService;
    import cloud.wconnect.frontoffice.service.adjustment.IAdjPmsConfigService;
    import com.google.common.collect.ImmutableMap;
    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.text.MessageFormat;
    import java.util.*;

    /**
     * <p>
     * Adjustment Requests Opera 配置表 前端控制器
     * </p>
     *
     * @author lichen
     * @since 2023-03-31
     */
    @RestController
    @RequestMapping("/adjustment/pms-config")
    public class AdjPmsConfigController {

        @Autowired
        private IAdjPmsConfigService adjPmsConfigService;
        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private IAdjPmsAccountService pmsAccountService;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 查询pmsAccount配置
         *
         * @param property 资产标识
         * @param query    查询对象
         * @return cloud.wconnect.commonlib.pojo.dto.ListPagedDTO<cloud.wconnect.frontoffice.pojo.dto.adjustment.AdjPmsConfigDTO>
         * @author lichen
         * @date 2025/12/3
         */
        @ApiId(232)
        @GetMapping
        public ListPagedDTO<AdjPmsConfigDTO> page(String property, @Valid PmsConfigQuery query) {
            int propertyId = sessionContext.getPropertyId(property);
            Integer tenantId = sessionContext.getTenantSymbolBO().getId();
            return adjPmsConfigService.pageList(tenantId, propertyId, query);
        }

        /**
         * 保存pmsAccount配置
         *
         * @param property        资产标识
         * @param adjPmsConfigDTO 保存的pms配置对象
         * @return java.util.Map
         * @author lichen
         * @date 2025/8/18
         */
        @ApiId(233)
        @PostMapping
        public Map<String, Long> save(String property, @Validated @RequestBody AdjPmsConfigDTO adjPmsConfigDTO) {
            int propertyId = sessionContext.getPropertyId(property);

            //校验pms账户是否已经存在
            adjPmsConfigService.checkExist(propertyId, adjPmsConfigDTO.getPmsAccount());

            if (AdjPmsConfig.DATA_TYPE_DRAFT == adjPmsConfigDTO.getDataType()) {
                ArrayList<Integer> adjTypeList = Lists.newArrayList(AdjTypeEnum.SALES.getAdjType(),
                        AdjTypeEnum.FRONT_DESK.getAdjType(),
                        AdjTypeEnum.ACCOUNTING.getAdjType());
                if (!adjTypeList.contains(adjPmsConfigDTO.getRequestType())) {
                    throw new BadRequestException(CommonErrorI18nConstant.MUST_IN_LIST,
                            i18nMessageUtils.getErrorParamMapNotNeedTranslate("type", StringUtils.join(adjTypeList, ";")),
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "the requestType must be in [1:Sales 2:Front Desk 3:Accounting]");
                }
                if(adjPmsConfigDTO.getUserId() == 0){
                    throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                            i18nMessageUtils.getErrorParamMapNotNeedTranslate("userId"),
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "The userId cannot be null");
                }
            }else{
                if(adjPmsConfigDTO.getUserId() != 0){
                    throw new BadRequestException(CommonErrorI18nConstant.MUST_EMPTY,
                            i18nMessageUtils.getErrorParamMapNotNeedTranslate("userId"),
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "The userId must be null");
                }
                if(adjPmsConfigDTO.getRequestType() != 0){
                    throw new BadRequestException(CommonErrorI18nConstant.MUST_EMPTY,
                            i18nMessageUtils.getErrorParamMapNotNeedTranslate("type"),
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "The requestType must be null");
                }
            }

            //保存
            long id = adjPmsConfigService.save(propertyId, adjPmsConfigDTO);
            return ImmutableMap.of("id", id);
        }

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

        /**
         * 查询pmsAccount数据
         *
         * @param property        资产标识
         * @param pmsAccountQuery 查询对象
         * @return cloud.wconnect.commonlib.pojo.dto.ListPagedDTO<cloud.wconnect.frontoffice.entity.adjustment.AdjPmsAccount>
         * @author lichen
         * @date 2023/12/2
         */
        @ApiId(939)
        @GetMapping("/pms-account")
        public ListPagedDTO<AdjPmsAccount> listPmsAccount(String property, PmsAccountQuery pmsAccountQuery) {
            int propertyId = sessionContext.getPropertyId(property);

            return pmsAccountService.pageList(propertyId, pmsAccountQuery);
        }

        /**
         * 校验数据是否存在 且 归属于当前资产
         *
         * @param id         主键id
         * @param propertyId 资产id
         * @author lichen
         * @date 2025/8/18
         */
        private void checkExistAndBelongProperty(Long id, Integer propertyId) {
            AdjPmsConfig byId = adjPmsConfigService.getById(id);
            if (Objects.isNull(byId)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "id=" + id + " ,Adjustment Requests Opera Config not found");
            }
            if (byId.getPropertyId().compareTo(propertyId) != 0) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("Adjustment pms config not belong current property, pmsConfigId:{0}, pmsConfigBelongProperty:{1}, currentPropertyId:{2}",
                                id, byId.getPropertyId(), propertyId));
            }
        }

    }