# BgNotifyRuleController.java

    package cloud.wconnect.frontoffice.controller.badguest;

    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.util.I18nMessageUtils;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.frontoffice.cons.badguest.BlacklistApproveStatusEnum;
    import cloud.wconnect.frontoffice.cons.badguest.BlacklistFieldI18nConstant;
    import cloud.wconnect.frontoffice.cons.badguest.BlacklistNotifyConfigTypeEnum;
    import cloud.wconnect.frontoffice.entity.badguest.BgNotifyRule;
    import cloud.wconnect.frontoffice.pojo.dto.badguest.BgNotifyRuleDTO;
    import cloud.wconnect.frontoffice.service.badguest.IBgNotifyRuleService;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.google.common.collect.Lists;
    import com.google.common.collect.Sets;
    import jakarta.validation.Valid;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import java.util.Collections;
    import java.util.Map;
    import java.util.Objects;

    /**
     * <p>
     * 黑名单通知规则表 前端控制器
     * </p>
     *
     * @author lichen
     * @since 2024-06-04
     */
    @RestController
    @RequestMapping("/guest-blacklist/notify-rule")
    public class BgNotifyRuleController {

        @Autowired
        private IBgNotifyRuleService notifyRuleService;
        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private ObjectMapper objectMapper;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 查询通知规则
         *
         * @param property 资产标识
         * @return cloud.wconnect.frontoffice.entity.badguest.BgNotifyRule
         * @author lichen
         * @date 2024/6/5
         */
        @ApiId(1187)
        @GetMapping
        public BgNotifyRule get(String property) {
            int propertyId = sessionContext.getPropertyId(property);

            //查询通知规则
            BgNotifyRule bgNotifyRule = notifyRuleService.getByPropertyIdAndType(propertyId, BlacklistNotifyConfigTypeEnum.BLACK_LIST_CHECKED_IN.getType());

            if (Objects.nonNull(bgNotifyRule)) return bgNotifyRule;

            //默认规则为非审批拒绝
            bgNotifyRule = new BgNotifyRule();
            bgNotifyRule.setPropertyId(propertyId);
            bgNotifyRule.setRule(JsonUtils.convertObjListToString(objectMapper,
                    Lists.newArrayList(BlacklistApproveStatusEnum.PENDING.getStatus(),
                            BlacklistApproveStatusEnum.APPROVEING.getStatus(),
                            BlacklistApproveStatusEnum.APPROVED.getStatus())));
            return bgNotifyRule;
        }

        /**
         * 保存通知规则
         *
         * @param property      资产
         * @param notifyRuleDTO 通知规则DTO
         * @return java.util.Map
         * @author lichen
         * @date 2024/6/5
         */
        @ApiId(1188)
        @PostMapping
        public Map save(String property, @RequestBody @Valid BgNotifyRuleDTO notifyRuleDTO) {
            int propertyId = sessionContext.getPropertyId(property);
            int loginUserId = sessionContext.getLoginUserId();

            //校验通知规则不能重复
            if (notifyRuleDTO.getRules().size() != Sets.newHashSet(notifyRuleDTO.getRules()).size()) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_CANNOT_REPEAT,
                        i18nMessageUtils.getErrorParamMapNeedTranslate(BlacklistFieldI18nConstant.RULE),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The rules cannot repeat");
            }

            // 保存通知规则
            notifyRuleService.save(propertyId, notifyRuleDTO, sessionContext.getLoginUserId());

            return Collections.emptyMap();
        }

    }