package cn.com.ty.lift.system.user.service.impl;

import cn.com.ty.lift.common.constants.ApiConstants;
import cn.com.ty.lift.common.model.AreaCode;
import cn.com.ty.lift.system.user.dao.mapper.AreaCodeMapper;
import cn.com.ty.lift.system.user.service.IAreaCodeService;
import cn.com.ty.lift.common.utils.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

/**
 * @author huangyuan
 * @date 2019-12-03
 * @description
 */
@Service
public class AreaCodeService extends ServiceImpl<AreaCodeMapper, AreaCode> implements IAreaCodeService {


    @Override
    public List<AreaCode> areaTree() {
        List<AreaCode> allAreaCodeList = list();
        return TreeUtil.listToTree(allAreaCodeList, ApiConstants.AREA_PARENT_ATTR, ApiConstants.ROOT_AREA_PARENT_ID, "code");
    }

    @Override
    public Map<String, String> getProvinceCodeToName() {
        Map<String, String> provinceCodeToName = new HashMap<>();
        List<AreaCode> provinceArea = list(new QueryWrapper<AreaCode>()
                .eq("parent", ApiConstants.ROOT_AREA_PARENT_ID)
        );
        if (provinceArea != null && provinceArea.size() > 0) {
            provinceCodeToName = provinceArea.stream()
                    .collect(Collectors.toMap(AreaCode::getCode, AreaCode::getName));
        }
        return provinceCodeToName;
    }
}
