package com.chinaweal.aiccs.aiccs.market.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinaweal.aiccs.aiccs.dict.entity.CascadeTreeNode;
import com.chinaweal.aiccs.aiccs.market.entity.AreaSlice;
import com.chinaweal.aiccs.aiccs.market.mapper.AreaSliceMapper;
import com.chinaweal.aiccs.aiccs.market.service.AreaSliceService;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 片区设置记录 服务实现类
 * </p>
 *
 * @author <a href="https://blog.lroyia.top">lroyia</a>
 * @since 2021-04-20
 */
@Service
public class AreaSliceServiceImpl extends BaseServiceImpl<AreaSliceMapper, AreaSlice> implements AreaSliceService {

    @Override
    public List<CascadeTreeNode> getListByOrg(String org) {
        return baseMapper.getListByOrg(org);
    }

    @Override
    public List<CascadeTreeNode> selectByParentId(String pid) {
        return baseMapper.selectByParentId(pid);
    }

    @Override
    public  List<String> getIdsByParentId(String pid){
        List<String> list = new ArrayList<>();
        this.getChildIds(pid,list);
        return list;
    }

    /**
     * 递归获取片区子id
     * @param pid
     * @param list
     */
    private void getChildIds(String pid, List<String> list){
        List<String> ids = baseMapper.selectIdsByParentId(pid);
        if(CollectionUtils.isNotEmpty(ids)){
            ids.stream().forEach(id -> {
                list.add(id);
                getChildIds(id, list);
            });
        }
    }
    @Override
    public IPage<AreaSlice> getPageWithOrg(IPage<AreaSlice> page, Map<String, Object> params) {
        return baseMapper.selectPageWithOrg(page, params);
    }
}
