package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.TblDzbcPathSite;
import org.dromara.system.domain.bo.TblDzbcPathSiteBo;
import org.dromara.system.domain.vo.TblDzbcPathSiteVo;
import org.dromara.system.domain.vo.TblDzbcSiteVo;
import org.dromara.system.mapper.TblDzbcPathSiteMapper;
import org.dromara.system.mapper.TblDzbcSiteMapper;
import org.dromara.system.service.ITblDzbcPathSiteService;
import org.springframework.stereotype.Service;

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

@RequiredArgsConstructor
@Service
public class TblDzbcPathSiteServiceImpl implements ITblDzbcPathSiteService {

    private final TblDzbcPathSiteMapper baseMapper;
    private final TblDzbcSiteMapper siteMapper;

    @Override
    public TblDzbcPathSiteVo queryById(Long id) {
        TblDzbcPathSiteVo vo = baseMapper.selectVoById(id);
        if (vo != null && vo.getSiteId() != null) {
            var site = siteMapper.selectVoById(vo.getSiteId());
            if (site != null) {
                vo.setSiteName(site.getName());
                vo.setSiteAddr(site.getAddr());
            }
        }
        return vo;
    }

    @Override
    public TableDataInfo<TblDzbcPathSiteVo> queryPageList(TblDzbcPathSiteBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblDzbcPathSite> lqw = buildQueryWrapper(bo);
        Page<TblDzbcPathSiteVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        fillSiteInfo(result.getRecords());
        return TableDataInfo.build(result);
    }

    @Override
    public List<TblDzbcPathSiteVo> queryList(TblDzbcPathSiteBo bo) {
        LambdaQueryWrapper<TblDzbcPathSite> lqw = buildQueryWrapper(bo);
        List<TblDzbcPathSiteVo> list = baseMapper.selectVoList(lqw);
        fillSiteInfo(list);
        return list;
    }

    @Override
    public List<TblDzbcPathSiteVo> queryByPathId(Long pathId) {
        LambdaQueryWrapper<TblDzbcPathSite> lqw = Wrappers.lambdaQuery();
        lqw.eq(TblDzbcPathSite::getPathId, pathId);
        lqw.orderByAsc(TblDzbcPathSite::getSiteOrder);
        List<TblDzbcPathSiteVo> list = baseMapper.selectVoList(lqw);
        fillSiteInfo(list);
        return list;
    }

    @Override
    public Boolean insertByBo(TblDzbcPathSiteBo bo) {
        TblDzbcPathSite add = MapstructUtils.convert(bo, TblDzbcPathSite.class);
        validEntityBeforeSave(add);
        return baseMapper.insert(add) > 0;
    }

    @Override
    public Boolean updateByBo(TblDzbcPathSiteBo bo) {
        TblDzbcPathSite update = MapstructUtils.convert(bo, TblDzbcPathSite.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    private LambdaQueryWrapper<TblDzbcPathSite> buildQueryWrapper(TblDzbcPathSiteBo bo) {
        LambdaQueryWrapper<TblDzbcPathSite> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPathId() != null, TblDzbcPathSite::getPathId, bo.getPathId());
        lqw.eq(bo.getSiteId() != null, TblDzbcPathSite::getSiteId, bo.getSiteId());
        lqw.orderByAsc(TblDzbcPathSite::getSiteOrder);
        return lqw;
    }

    private void validEntityBeforeSave(TblDzbcPathSite entity) {
    }

    private void fillSiteInfo(List<TblDzbcPathSiteVo> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        List<Long> siteIds = list.stream().map(TblDzbcPathSiteVo::getSiteId).distinct().collect(Collectors.toList());
        Map<Long, TblDzbcSiteVo> siteMap = siteMapper.selectVoBatchIds(siteIds, TblDzbcSiteVo.class).stream()
            .collect(Collectors.toMap(TblDzbcSiteVo::getId, s -> s));
        list.forEach(vo -> {
            TblDzbcSiteVo site = siteMap.get(vo.getSiteId());
            if (site != null) {
                vo.setSiteName(site.getName());
                vo.setSiteAddr(site.getAddr());
            }
        });
    }
}
