package com.ningdatech.carapi.sys.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.carapi.sys.convert.RegionConverter;
import com.ningdatech.carapi.sys.entity.Region;
import com.ningdatech.carapi.sys.entity.dto.RegionDTO;
import com.ningdatech.carapi.sys.mapper.RegionMapper;
import com.ningdatech.carapi.sys.service.IRegionService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LiuXinXin
 * @since 2022-10-08
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements IRegionService {

    @Override
    public List<RegionDTO> queryAll() {
        List<Region> allRegions = this.lambdaQuery().ne(Region::getId, -1).list();
        return CollUtils.convert(allRegions, RegionConverter::toRegionDTO);
    }

    @Override
    public List<RegionDTO> listGeLevel(int regionLevel) {
        List<Region> regionsByLevel = lambdaQuery().ne(Region::getId, -1)
                .le(Region::getLevel, regionLevel).list();
        return CollUtils.convert(regionsByLevel, RegionConverter::toRegionDTO);
    }

    @Override
    public boolean isParentRegion(Long regionId) {
        return baseMapper.exists(Wrappers.lambdaQuery(Region.class)
                .eq(Region::getParentId, regionId)
                .eq(Region::getDeleted, false));
    }

    @Override
    public Long getRegionIdByParentIdWithSameRegionCode(Long parentId, String regionCode) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(Region.class)
                        .eq(Region::getParentId, parentId)
                        .eq(Region::getRegionCode, regionCode)
                        .eq(Region::getDeleted, false))
                .getId();
    }


}
