package io.renren.modules.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.business.dao.BusinessDictDao;
import io.renren.modules.business.entity.BusinessDictEntity;
import io.renren.modules.business.service.BusinessDictService;
import org.springframework.stereotype.Service;

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


@Service("businessDictService")
public class BusinessDictServiceImpl extends ServiceImpl<BusinessDictDao, BusinessDictEntity> implements BusinessDictService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BusinessDictEntity> page = this.page(
                new Query<BusinessDictEntity>().getPage(params),
                new QueryWrapper<BusinessDictEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<BusinessDictEntity> getByParentId(Integer parentId) {
        QueryWrapper<BusinessDictEntity> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("parent_id", parentId);
        return this.list(dictQueryWrapper);
    }

    @Override
    public List<BusinessDictEntity> listWithTree() {
        QueryWrapper<BusinessDictEntity> qw = new QueryWrapper<>();
        qw.orderByAsc("order_num");
        qw.orderByAsc("dict_id");
        List<BusinessDictEntity> menuList = this.list(qw);
        List<BusinessDictEntity> menuLevelList = menuList.stream().filter(dictEntity ->
            dictEntity.getParentId() == 0
        ).map(dictEntity -> {
            dictEntity.setChildren(getChildren(dictEntity, menuList));
            return dictEntity;
        }).collect(Collectors.toList());

        return menuLevelList;
    }

    @Override
    public List<BusinessDictEntity> getOneList() {
        return baseMapper.getOneList();
    }

    /**
     * 获取位置 职位列表
     * @param
     * @return
     */
    @Override
    public List<BusinessDictEntity> queryWzAndZw() {
        return baseMapper.queryWzAndZw();
    }

    private List<BusinessDictEntity> getChildren(BusinessDictEntity menu, List<BusinessDictEntity> menuList) {
        return menuList.stream().filter(dictEntity ->
                menu.getDictId() == dictEntity.getParentId()
        ).collect(Collectors.toList());
    }

    @Override
    public List<BusinessDictEntity> queryAll() {
        return baseMapper.queryAll();
    }

    @Override
    public Integer findCnByName(Map<String, Object> params){
        return baseMapper.findCnByName(params);
    }

}