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.TblDzbcSitePrice;
import org.dromara.system.domain.bo.TblDzbcSitePriceBo;
import org.dromara.system.domain.vo.TblDzbcSitePriceVo;
import org.dromara.system.domain.vo.TblDzbcSiteVo;
import org.dromara.system.mapper.TblDzbcSitePriceMapper;
import org.dromara.system.mapper.TblDzbcSiteMapper;
import org.dromara.system.service.ITblDzbcSitePriceService;
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 TblDzbcSitePriceServiceImpl implements ITblDzbcSitePriceService {

    private final TblDzbcSitePriceMapper baseMapper;
    private final TblDzbcSiteMapper siteMapper;

    @Override
    public TblDzbcSitePriceVo queryById(Long id) {
        TblDzbcSitePriceVo vo = baseMapper.selectVoById(id);
        if (vo != null) {
            fillSiteInfo(vo);
        }
        return vo;
    }

    @Override
    public TableDataInfo<TblDzbcSitePriceVo> queryPageList(TblDzbcSitePriceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblDzbcSitePrice> lqw = buildQueryWrapper(bo);
        Page<TblDzbcSitePriceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(this::fillSiteInfo);
        return TableDataInfo.build(result);
    }

    @Override
    public List<TblDzbcSitePriceVo> queryList(TblDzbcSitePriceBo bo) {
        LambdaQueryWrapper<TblDzbcSitePrice> lqw = buildQueryWrapper(bo);
        List<TblDzbcSitePriceVo> list = baseMapper.selectVoList(lqw);
        list.forEach(this::fillSiteInfo);
        return list;
    }

    @Override
    public List<TblDzbcSitePriceVo> queryByPathId(Long pathId) {
        LambdaQueryWrapper<TblDzbcSitePrice> lqw = Wrappers.lambdaQuery();
        lqw.eq(TblDzbcSitePrice::getPathId, pathId);
        List<TblDzbcSitePriceVo> list = baseMapper.selectVoList(lqw);
        list.forEach(this::fillSiteInfo);
        return list;
    }

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

    @Override
    public Boolean updateByBo(TblDzbcSitePriceBo bo) {
        TblDzbcSitePrice update = MapstructUtils.convert(bo, TblDzbcSitePrice.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<TblDzbcSitePrice> buildQueryWrapper(TblDzbcSitePriceBo bo) {
        LambdaQueryWrapper<TblDzbcSitePrice> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPathId() != null, TblDzbcSitePrice::getPathId, bo.getPathId());
        lqw.eq(bo.getFromSiteId() != null, TblDzbcSitePrice::getFromSiteId, bo.getFromSiteId());
        lqw.eq(bo.getToSiteId() != null, TblDzbcSitePrice::getToSiteId, bo.getToSiteId());
        return lqw;
    }

    private void validEntityBeforeSave(TblDzbcSitePrice entity) {
    }

    private void fillSiteInfo(TblDzbcSitePriceVo vo) {
        if (vo.getFromSiteId() != null) {
            TblDzbcSiteVo fromSite = siteMapper.selectVoById(vo.getFromSiteId());
            if (fromSite != null) {
                vo.setFromSiteName(fromSite.getName());
            }
        }
        if (vo.getToSiteId() != null) {
            TblDzbcSiteVo toSite = siteMapper.selectVoById(vo.getToSiteId());
            if (toSite != null) {
                vo.setToSiteName(toSite.getName());
            }
        }
    }
}
