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.EngineeringFacility;
import com.zksy.drainage.mapper.EngineeringFacilityMapper;
import com.zksy.drainage.service.EngineeringFacilityService;
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 EngineeringFacilityServiceImpl extends ServiceImpl<EngineeringFacilityMapper, EngineeringFacility>
        implements EngineeringFacilityService {

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

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

    @Override
    @Transactional
    public boolean updateEntity(EngineeringFacility 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<EngineeringFacility> existing = this.listByIds(ids);
        if (existing.isEmpty()) {
            throw new ServiceException("批量删除失败：所有ID均不存在");
        }
        List<String> existIds = existing.stream()
                .map(EngineeringFacility::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) {
        EngineeringFacility entity = this.getById(id);
        if (entity == null) return false;
        entity.setEquipmentId(String.join(",", equipmentIds));
        return this.updateById(entity);
    }
}
