package com.zksy.WaterSupply.WaterPipeInfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.WaterSupply.WaterPipeInfo.domain.WaterPipeInfo;
import com.zksy.WaterSupply.WaterPipeInfo.mapper.WaterPipeInfoMapper;
import com.zksy.WaterSupply.WaterPipeInfo.service.IWaterPipeInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

@Service
public class WaterPipeInfoServiceImpl extends ServiceImpl<WaterPipeInfoMapper, WaterPipeInfo>
        implements IWaterPipeInfoService {

    @Override
    public IPage<WaterPipeInfo> queryPage(Page<WaterPipeInfo> page,
                                          String pipeCode,
                                          String pipeName,
                                          String pipeMaterial,
                                          String status,
                                          LocalDateTime startTime,
                                          LocalDateTime endTime) {
        List<WaterPipeInfo> list = baseMapper.selectPipeList(pipeCode, pipeName, pipeMaterial, status, startTime, endTime);
        page.setRecords(list);
        page.setTotal(list.size());
        return page;
    }

    @Override
    public List<Map<String, Object>> statPipeByStatus() {
        return baseMapper.statPipeByStatus();
    }

    @Override
    public List<Map<String, Object>> statPipeByMaterial() {
        return baseMapper.statPipeByMaterial();
    }

    @Override
    public WaterPipeInfo getLatestPipe() {
        return baseMapper.getLatestPipe();
    }

    @Override
    public Map<String, Object> getPipeOverallStatistics() {
        return baseMapper.getPipeOverallStatistics();
    }

    @Override
    public IPage<WaterPipeInfo> queryDeletedPage(Page<WaterPipeInfo> page,
                                                 String pipeCode,
                                                 String pipeName,
                                                 String pipeMaterial,
                                                 LocalDateTime startTime,
                                                 LocalDateTime endTime) {
        List<WaterPipeInfo> list = baseMapper.selectDeletedList(pipeCode, pipeName, pipeMaterial, startTime, endTime);
        page.setRecords(list);
        page.setTotal(list.size());
        return page;
    }

    @Override
    public boolean restoreByIds(List<Long> ids, String updateBy) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        // 直接传递List，不需要再转字符串
        return baseMapper.restoreByIds(ids, updateBy) > 0;
    }

    @Override
    public boolean deleteByIdsPhysically(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        return baseMapper.deleteByIdsPhysically(ids) > 0;
    }
}