RAgentEventServiceImpl.java 1.67 KB
package vion.service.impl.monitor;

import com.github.yulichang.base.MPJBaseServiceImpl;
import org.dromara.hutool.core.lang.Opt;
import org.springframework.stereotype.Service;
import vion.mapper.monitor.RAgentEventMapper;
import vion.model.monitor.RAgentEvent;
import vion.service.monitor.IRAgentEventService;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author vion
 * @date 2024/10/31
 */
@Service
public class RAgentEventServiceImpl extends MPJBaseServiceImpl<RAgentEventMapper, RAgentEvent> implements IRAgentEventService {

    @Override
    public String bind(String agentUid, List<RAgentEvent> dtoList) {
        dtoList.forEach(ae -> ae.setAgentUid(agentUid));
        var agentEventList = this.lambdaQuery().eq(RAgentEvent::getAgentUid, agentUid).list();
        var mallUid2AEMap = agentEventList.stream().collect(Collectors.toMap(RAgentEvent::getMallUid, Function.identity()));

        List<RAgentEvent> updList = new ArrayList<>();
        List<RAgentEvent> insList = new ArrayList<>();
        dtoList.forEach(dto -> {
            if (mallUid2AEMap.containsKey(dto.getMallUid())) {
                var ae = mallUid2AEMap.get(dto.getMallUid());
                dto.setId(ae.getId());
                updList.add(dto);
            } else {
                insList.add(dto);
            }
        });
        Opt.ofEmptyAble(updList).ifPresent(this::updateBatchById);
        Opt.ofEmptyAble(insList).ifPresent(this::saveBatch);
        return "绑定成功";
    }

    @Override
    public String unbind(List<Long> idList) {
        return this.removeBatchByIds(idList) ? "解绑成功" : "解绑失败";
    }
}