SettingBizService.java 3.8 KB
package cn.fw.valhalla.service.bus.setting;

import cn.fw.valhalla.domain.dto.SettingDTO;
import cn.fw.valhalla.domain.enums.FollowTypeEnum;
import cn.fw.valhalla.domain.enums.SettingTypeEnum;
import cn.fw.valhalla.domain.vo.setting.SettingVO;
import cn.fw.valhalla.service.bus.setting.strategy.SettingStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

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

import static cn.fw.common.businessvalidator.Validator.BV;

/**
 * @author : kurisu
 * @className : SettingBizService
 * @description : 设置处理服务
 * @date: 2020-08-14 09:17
 */
@Slf4j
@Service
public class SettingBizService {
    /**
     * 设置处理器map
     */
    private final Map<FollowTypeEnum, SettingStrategy> processorMap;

    @Autowired
    public SettingBizService(final List<SettingStrategy> settingStrategyList) {
        this.processorMap = settingStrategyList.stream().collect(Collectors.toMap(SettingStrategy::getFollowType, v -> v));
    }

    /**
     * 根据跟进类型查询设置
     *
     * @param type
     * @param groupId
     * @return
     */
    public List<SettingVO> querySettingByCat(Integer type, Long groupId) {
        FollowTypeEnum followTypeEnum = FollowTypeEnum.ofValue(type);
        Assert.notNull(followTypeEnum, "跟进类型不正确");

        SettingStrategy strategy = processorMap.get(followTypeEnum);
        Assert.notNull(strategy, "strategy cannot be null");
        return Optional.ofNullable(strategy.getList(groupId)).orElse(new ArrayList<>());
    }

    /**
     * 保存设置
     *
     * @param groupId
     * @param list
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean saveSetting(List<SettingDTO> list, Long groupId) {
        Assert.isTrue(!CollectionUtils.isEmpty(list), "设置项不能为空");
        final Integer category = list.get(0).getCategory();
        FollowTypeEnum followTypeEnum = FollowTypeEnum.ofValue(category);
        boolean allMatch = list.stream().allMatch(r -> Objects.equals(category, r.getCategory()));
        boolean expression = Objects.nonNull(followTypeEnum) && Boolean.TRUE.equals(allMatch);
        for (SettingDTO dto : list) {
            if (SettingTypeEnum.NOTICE_CYCLE.getValue().equals(dto.getType())) {
                BV.isTrue(dto.getDetailValue() > 5, () -> "消息推送间隔必须大于5天");
            }
            if (SettingTypeEnum.NOTICE_TIMES.getValue().equals(dto.getType())) {
                BV.isTrue(dto.getDetailValue() <= 2, () -> "消息提醒次数不能大于两次");
            }
        }
        Assert.isTrue(expression, "跟进类型不正确");

        SettingStrategy strategy = processorMap.get(followTypeEnum);
        Assert.notNull(strategy, "strategy cannot be null");
        return strategy.save(list, groupId);
    }

    /**
     * 根据设置类型查询设置信息
     *
     * @param followTypeEnum
     * @param settingTypeEnum
     * @param groupId
     * @return
     */
    public Optional<SettingVO> querySettingByType(FollowTypeEnum followTypeEnum, SettingTypeEnum settingTypeEnum, Long groupId) {
        if (Objects.isNull(followTypeEnum)) {
            log.error("跟进类型不正确");
            return Optional.empty();
        }
        if (Objects.isNull(settingTypeEnum)) {
            log.error("设置类型不正确");
            return Optional.empty();
        }

        SettingStrategy strategy = processorMap.get(followTypeEnum);
        Assert.notNull(strategy, "strategy cannot be null");
        return Optional.ofNullable(strategy.getBySettingType(settingTypeEnum, groupId));
    }
}