# CompRoomNotifyRuleServiceImpl.java

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

    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.commonlib.util.UpdateLogUtil;
    import cloud.wconnect.frontoffice.cons.ApproveStatusEnum;
    import cloud.wconnect.frontoffice.cons.comproom.CompRoomNotifyConfigTypeEnum;
    import cloud.wconnect.frontoffice.entity.comproom.CompRoomNotifyRule;
    import cloud.wconnect.frontoffice.mapper.comproom.CompRoomNotifyRuleMapper;
    import cloud.wconnect.frontoffice.pojo.dto.comproom.CompRoomNotifyRuleDTO;
    import cloud.wconnect.frontoffice.service.comproom.ICompRoomNotifyRuleService;
    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 com.google.common.collect.Lists;
    import com.google.common.collect.Maps;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.util.*;
    import java.util.function.Function;
    import java.util.stream.Collectors;

    /**
     * <p>
     * 免费房通知规则表 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2024-06-10
     */
    @Service
    @DS("logbook")
    public class CompRoomNotifyRuleServiceImpl extends ServiceImpl<CompRoomNotifyRuleMapper, CompRoomNotifyRule> implements ICompRoomNotifyRuleService {

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

        /**
         * 根据资产ID和类型查询通知规则
         *
         * @param propertyId 资产ID
         * @param type       类型 1:免费房通知
         * @return cloud.wconnect.frontoffice.entity.comproom.CompRoomNotifyRule
         * @author lichen
         * @date 2024/6/11
         */
        @Override
        public CompRoomNotifyRule getByPropertyIdAndType(Integer propertyId, int type) {
            return getOne(new LambdaQueryWrapper<CompRoomNotifyRule>().eq(CompRoomNotifyRule::getPropertyId, propertyId)
                    .eq(CompRoomNotifyRule::getType, type));
        }

        /**
         * 根据资产ID和类型查询通知规则，当为空时返回默认规则（非审批拒绝）
         *
         * @param propertyId 资产ID
         * @param type       类型 1:免费房通知
         * @return cloud.wconnect.frontoffice.entity.comproom.CompRoomNotifyRule
         * @author lichen
         * @date 2024/6/11
         */
        @Override
        public CompRoomNotifyRule getByPropertyIdAndTypeOrDefault(Integer propertyId, int type) {
            CompRoomNotifyRule compRoomNotifyRule = getByPropertyIdAndType(propertyId, type);

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

            compRoomNotifyRule = new CompRoomNotifyRule();
            compRoomNotifyRule.setPropertyId(propertyId);
            compRoomNotifyRule.setType(CompRoomNotifyConfigTypeEnum.COMP_ROOM_CHECK_IN.getType());
            compRoomNotifyRule.setRule(JsonUtils.convertObjListToString(objectMapper, Lists.newArrayList(ApproveStatusEnum.PENDING.getStatus(),
                    ApproveStatusEnum.APPROVEING.getStatus(), ApproveStatusEnum.APPROVED.getStatus())));
            return compRoomNotifyRule;
        }

        /**
         * 根据资产ID和类型查询通知规则，当为空时返回默认规则（非审批拒绝）
         *
         * @param propertyIds 资产ID集合
         * @param type        类型 1:免费房通知
         * @return java.util.Map<java.lang.Integer, java.util.List < java.lang.Integer>>
         * @author lichen
         * @date 2024/6/11
         */
        @Override
        public Map<Integer, List<Integer>> listByPropertyIdAndTypeOrDefault(List<Integer> propertyIds, int type) {
            if(CollectionUtils.isEmpty(propertyIds)) return Collections.emptyMap();
            Map<Integer, CompRoomNotifyRule> propertyId2NotifyRule = list(new LambdaQueryWrapper<CompRoomNotifyRule>()
                    .in(CompRoomNotifyRule::getPropertyId, propertyIds)
                    .eq(CompRoomNotifyRule::getType, type))
                    .stream().collect(Collectors.toMap(CompRoomNotifyRule::getPropertyId, Function.identity()));

            Map<Integer, List<Integer>> resultMap = Maps.newLinkedHashMapWithExpectedSize(propertyIds.size());
            for (Integer propertyId : propertyIds) {
                List<Integer> approveStatusList = new ArrayList<>();

                //资产配置的通知规则
                CompRoomNotifyRule compRoomNotifyRule = propertyId2NotifyRule.get(propertyId);
                if(Objects.nonNull(compRoomNotifyRule)){
                    approveStatusList = JsonUtils.convertStringToList(objectMapper, compRoomNotifyRule.getRule(), Integer.class);
                }else{
                    //为空时，默认非审批拒绝
                    approveStatusList = Lists.newArrayList(ApproveStatusEnum.PENDING.getStatus(),
                            ApproveStatusEnum.APPROVEING.getStatus(), ApproveStatusEnum.APPROVED.getStatus());
                }

                resultMap.put(propertyId, approveStatusList);
            }
            return resultMap;
        }

        /**
         * 保存通知规则
         *
         * @param propertyId            资产ID
         * @param compRoomNotifyRuleDTO 通知规则DTO
         * @param operatorId            操作人ID
         * @author lichen
         * @date 2025/12/29
         */
        @Override
        public void save(int propertyId, CompRoomNotifyRuleDTO compRoomNotifyRuleDTO, int operatorId) {

            //查询免费房的通知规则
            CompRoomNotifyRule compRoomNotifyRule = getByPropertyIdAndType(propertyId, CompRoomNotifyConfigTypeEnum.COMP_ROOM_CHECK_IN.getType());
            if (Objects.isNull(compRoomNotifyRule)) {
                CompRoomNotifyRule insertEntity = new CompRoomNotifyRule();
                insertEntity.setPropertyId(propertyId);
                insertEntity.setType(CompRoomNotifyConfigTypeEnum.COMP_ROOM_CHECK_IN.getType());
                insertEntity.setRule(JsonUtils.convertObjListToString(objectMapper, compRoomNotifyRuleDTO.getRules()));
                insertEntity.setUpdateLog(updateLogUtil.get(operatorId));
                save(insertEntity);
            } else {
                String oriUpdateLog = compRoomNotifyRule.getUpdateLog();

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