package com.zksy.drainage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.common.exception.ServiceException;
import com.zksy.drainage.domain.PipeNetworkInfo;
import com.zksy.drainage.mapper.PipeNetworkInfoMapper;
import com.zksy.drainage.service.PipeNetworkInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 排水管网基础数据 ServiceImpl
 */
@Slf4j
@Service
public class PipeNetworkInfoServiceImpl extends ServiceImpl<PipeNetworkInfoMapper, PipeNetworkInfo>
        implements PipeNetworkInfoService {

    @Override
    public Page<PipeNetworkInfo> findByPage(long pageNum, long pageSize,
                                            String name, String area, String networkType) {
        Page<PipeNetworkInfo> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PipeNetworkInfo> wrapper = new LambdaQueryWrapper<>();
        if (name != null && !name.isEmpty()) {
            wrapper.like(PipeNetworkInfo::getName, name);
        }
        if (area != null && !area.isEmpty()) {
            wrapper.like(PipeNetworkInfo::getArea, area);
        }
        if (networkType != null && !networkType.isEmpty()) {
            wrapper.eq(PipeNetworkInfo::getNetworkType, networkType);
        }
        wrapper.orderByDesc(PipeNetworkInfo::getCreateTime);
        return this.page(page, wrapper);
    }

    @Override
    @Transactional
    public boolean saveEntity(PipeNetworkInfo entity) {
        entity.setCreateTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());
        return this.save(entity);
    }

    @Override
    @Transactional
    public boolean updateEntity(PipeNetworkInfo entity) {
        String id = entity.getId();
        if (this.getById(id) == null) {
            throw new ServiceException("排水管网不存在，无法修改：" + id);
        }
        entity.setUpdateTime(LocalDateTime.now());
        return this.updateById(entity);
    }

    @Override
    @Transactional
    public boolean removeEntity(String id) {
        if (this.getById(id) == null) {
            throw new ServiceException("排水管网不存在：" + id);
        }
        return this.baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional
    public boolean removeBatchEntity(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new ServiceException("批量删除失败：ID列表不能为空");
        }
        List<PipeNetworkInfo> existing = this.listByIds(ids);
        if (existing.isEmpty()) {
            throw new ServiceException("批量删除失败：所有ID均不存在");
        }
        List<String> existIds = existing.stream()
                .map(PipeNetworkInfo::getId)
                .collect(Collectors.toList());
        List<String> invalidIds = ids.stream()
                .filter(id -> !existIds.contains(id))
                .collect(Collectors.toList());
        if (!invalidIds.isEmpty()) {
            log.warn("批量删除中存在无效排水管网ID：{}", invalidIds);
        }
        return this.removeByIds(existIds);
    }

    @Override
    public boolean bindEquipment(String id, List<String> equipmentIds) {
        PipeNetworkInfo entity = this.getById(id);
        if (entity == null) return false;
        entity.setEquipmentId(String.join(",", equipmentIds));
        return this.updateById(entity);
    }
}
