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.WaterloggingPoint;
import com.zksy.drainage.mapper.WaterloggingPointMapper;
import com.zksy.drainage.service.WaterloggingPointService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 易积水点管理 ServiceImpl
 */
@Slf4j
@Service
public class WaterloggingPointServiceImpl extends ServiceImpl<WaterloggingPointMapper, WaterloggingPoint>
        implements WaterloggingPointService {

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

    @Override
    @Transactional
    public boolean saveWithCheck(WaterloggingPoint entity) {
        return this.save(entity);
    }

    @Override
    @Transactional
    public boolean updateWithCheck(WaterloggingPoint entity) {
        String id = entity.getId();
        if (this.getById(id) == null) {
            throw new ServiceException("易积水点不存在，无法修改：" + id);
        }
        return this.updateById(entity);
    }

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

    @Override
    @Transactional
    public boolean removeBatchWithCheck(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new ServiceException("批量删除失败：ID列表不能为空");
        }
        List<WaterloggingPoint> existing = this.listByIds(ids);
        if (existing.isEmpty()) {
            throw new ServiceException("批量删除失败：所有ID均不存在");
        }
        List<String> existIds = existing.stream()
                .map(WaterloggingPoint::getId)
                .collect(Collectors.toList());
        return this.removeByIds(existIds);
    }

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