package com.zcloud.modules.kangzai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcloud.common.utils.PageUtils;
import com.zcloud.modules.kangzai.dao.BusRiverDao;
import com.zcloud.modules.kangzai.dao.BusRiverRiskVillagesDao;
import com.zcloud.modules.kangzai.dao.BusRiverSectionsDao;
import com.zcloud.modules.kangzai.entity.BusReservoirBasicEntity;
import com.zcloud.modules.kangzai.entity.BusRiverEntity;
import com.zcloud.modules.kangzai.entity.BusRiverRiskVillagesEntity;
import com.zcloud.modules.kangzai.entity.BusRiverSectionsEntity;
import com.zcloud.modules.kangzai.service.BusReservoirBasicService;
import com.zcloud.modules.kangzai.service.BusRiverService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

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

/**
 * @Author fangjiakai
 * @Desc 河流基础信息表(BusRiver)表服务实现类
 * @Date 2025-06-09 09:44:24
 */
@Service("busRiverService")
public class BusRiverServiceImpl extends ServiceImpl<BusRiverDao, BusRiverEntity> implements BusRiverService {

    @Autowired
    private BusRiverRiskVillagesDao busRiverRiskVillagesDao;
    @Autowired
    private BusRiverSectionsDao busRiverSectionsDao;
    @Override
    public BusRiverEntity queryByFlexibleParams(String paramName, Object paramValue) {
        QueryWrapper<BusRiverEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(paramName, paramValue); // 根据传入的参数名和参数值动态构建查询条件

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<BusRiverEntity> queryListByFlexibleParams(String paramName, Object paramValue) {
        QueryWrapper<BusRiverEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(paramName, paramValue); // 根据传入的参数名和参数值动态构建查询条件

        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public PageUtils listPage(Map<String, Object> params) {
        Page<Map<String, Object>> page = new Page<>(Integer.parseInt(params.get("curPage").toString()), Integer.parseInt(params.get("limit").toString()));
        params.put("isDelete", 0);
        IPage<BusRiverEntity> ipage = baseMapper.listPage(page, params);
        List<BusRiverEntity> list = ipage.getRecords();
        if (list.size() > 0) {
            List<String> riverIds  = list.stream().map(BusRiverEntity::getRiverId).collect(Collectors.toList());
            LambdaQueryWrapper<BusRiverRiskVillagesEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select(BusRiverRiskVillagesEntity::getRiverRiskVillagesId);
            queryWrapper.select(BusRiverRiskVillagesEntity::getRiverId);
            queryWrapper.in(BusRiverRiskVillagesEntity::getRiverId, riverIds);
            List<BusRiverRiskVillagesEntity> riskVillagesList = busRiverRiskVillagesDao.selectList(queryWrapper);

            LambdaQueryWrapper<BusRiverSectionsEntity> squeryWrapper = new LambdaQueryWrapper<>();
            squeryWrapper.select(BusRiverSectionsEntity::getRiverSectionsId);
            squeryWrapper.select(BusRiverSectionsEntity::getRiverId);
            squeryWrapper.in(BusRiverSectionsEntity::getRiverId, riverIds);
            List<BusRiverSectionsEntity> sectionsList = busRiverSectionsDao.selectList(squeryWrapper);

            list.stream().forEach(dto -> {
                dto.setCountyCount(sectionsList.stream().filter(dto1 -> dto1.getRiverId().equals(dto.getRiverId())).count());
                dto.setRiskVillageCount(riskVillagesList.stream().filter(dto1 -> dto1.getRiverId().equals(dto.getRiverId())).count());
            });

            ipage.setRecords(list);
        }
        return new PageUtils(ipage);
    }


    @Override
    public void delete(String riverId, String userId) {
        UpdateWrapper<BusRiverEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("river_id", riverId);

        BusRiverEntity busRiver = new BusRiverEntity();
        busRiver.setOperator(userId);
        busRiver.setIsDelete(1);

        this.update(busRiver, updateWrapper);
    }
}
