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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaweal.aiccs.aiccs.dict.entity.CascadeTreeNode;
import com.chinaweal.aiccs.aiccs.dict.entity.CmsDictCode;
import com.chinaweal.aiccs.aiccs.dict.mapper.CmsDictCodeMapper;
import com.chinaweal.aiccs.aiccs.dict.service.CmsDictCodeService;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典代码 服务实现类
 * </p>
 *
 * @author Lee
 * @since 2021-11-17
 */
@Service
public class CmsDictCodeServiceImpl extends BaseServiceImpl<CmsDictCodeMapper, CmsDictCode> implements CmsDictCodeService {

    @Override
    public IPage<CmsDictCode> getCodeListPage(Page<CmsDictCode> page, Map<String, Object> customParamMap) {
        return baseMapper.selectCodeListPage(page, customParamMap);
    }

    @Override
    public List<CascadeTreeNode> getList(int level, String type) {
        return baseMapper.getNodeList(level, type, null);
    }

    @Override
    public List<CascadeTreeNode> getChildList(String parentid) {

        return baseMapper.getNodeList(-99, null, parentid);
    }

    @Override
    public CmsDictCode getList(String type, String code) {
        LambdaQueryWrapper<CmsDictCode> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CmsDictCode::getType, type).eq(CmsDictCode::getCode, code);
        return getOne(wrapper, false);
    }

    @Override
    public Map<String, CmsDictCode> getAllCodeMap(String type) {
        List<CmsDictCode> list = lambdaQuery().eq(CmsDictCode::getType, type).list();
        Map<String, CmsDictCode> result = new HashMap<>(list.size());
        for (CmsDictCode each : list) {
            result.put(each.getConstant(), each);
        }
        return result;
    }
}
