# BgNotifyRuleServiceImpl.java

    package cloud.wconnect.frontoffice.service.badguest.impl;

    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.commonlib.util.UpdateLogUtil;
    import cloud.wconnect.frontoffice.cons.badguest.BlacklistNotifyConfigTypeEnum;
    import cloud.wconnect.frontoffice.entity.badguest.BgNotifyRule;
    import cloud.wconnect.frontoffice.mapper.badguest.BgNotifyRuleMapper;
    import cloud.wconnect.frontoffice.pojo.dto.badguest.BgNotifyRuleDTO;
    import cloud.wconnect.frontoffice.service.badguest.IBgNotifyRuleService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.util.Objects;

    /**
     * <p>
     * 黑名单通知规则表 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2024-06-04
     */
    @Service
    @DS("logbook")
    public class BgNotifyRuleServiceImpl extends ServiceImpl<BgNotifyRuleMapper, BgNotifyRule> implements IBgNotifyRuleService {

        @Autowired
        private UpdateLogUtil updateLogUtil;
        @Autowired
        private ObjectMapper objectMapper;

        /**
         * 查询通知规则
         *
         * @param propertyId 资产ID
         * @param type       通知类型
         * @return cloud.wconnect.frontoffice.entity.badguest.BgNotifyRule
         * @author lichen
         * @date 2024/6/5
         */
        @Override
        public BgNotifyRule getByPropertyIdAndType(Integer propertyId, int type) {
            return getOne(new LambdaQueryWrapper<BgNotifyRule>()
                    .eq(BgNotifyRule::getPropertyId, propertyId)
                    .eq(BgNotifyRule::getType, BlacklistNotifyConfigTypeEnum.BLACK_LIST_CHECKED_IN.getType()));
        }

        /**
         * 保存通知规则
         *
         * @param propertyId    资产ID
         * @param notifyRuleDTO 通知规则DTO
         * @param operatorId    操作人ID
         * @author lichen
         * @date 2025/12/29
         */
        @Override
        public void save(int propertyId, BgNotifyRuleDTO notifyRuleDTO, int operatorId) {
            //查询通知规则
            BgNotifyRule bgNotifyRule = getByPropertyIdAndType(propertyId, BlacklistNotifyConfigTypeEnum.BLACK_LIST_CHECKED_IN.getType());

            if (Objects.isNull(bgNotifyRule)) {
                BgNotifyRule insertEntity = new BgNotifyRule();
                insertEntity.setPropertyId(propertyId);
                insertEntity.setRule(JsonUtils.convertObjListToString(objectMapper, notifyRuleDTO.getRules()));
                insertEntity.setType(BlacklistNotifyConfigTypeEnum.BLACK_LIST_CHECKED_IN.getType());
                insertEntity.setUpdateLog(updateLogUtil.get(operatorId));
                save(insertEntity);
            } else {
                String oriUpdateLog = bgNotifyRule.getUpdateLog();

                bgNotifyRule.setRule(JsonUtils.convertObjListToString(objectMapper, notifyRuleDTO.getRules()));
                bgNotifyRule.setUpdateLog(updateLogUtil.append(oriUpdateLog, operatorId));
                updateById(bgNotifyRule);
            }
        }
    }