package com.nantian.cloud.jljc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.jljc.entity.DictRegion;
import com.nantian.cloud.jljc.mapper.DictRegionMapper;
import com.nantian.cloud.jljc.service.DictRegionService;
import com.nantian.cloud.jljc.vo.Query;
import com.nantian.cloud.jljc.vo.TreeVo;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;

import static com.nantian.cloud.jljc.vo.util.TreeVoUtil.findTree;


/**
 *
 * @date 2020-08-13
 */
@Service("dictRegionService")
public class DictRegionServiceImpl extends ServiceImpl<DictRegionMapper, DictRegion> implements DictRegionService {

    @Override
    public DictRegion getNameByCode(DictRegion dictRegion) {
        return baseMapper.getNameByCode(dictRegion);
    }

    @Override
    public List<TreeVo> getDictRegionTreee() {
        List<TreeVo> dictRegionTreee = baseMapper.getDictRegionTreee();
        dictRegionTreee  = findTree(dictRegionTreee);
        return dictRegionTreee;
    }

    @Override
    public List<DictRegion> getCityRegion() {
        return baseMapper.getCityRegion();
    }

    @Override
    public  List<DictRegion> getDictRegion(){
        return  baseMapper.getDictRegion();
    }

    @Override
    public List<TreeVo> getDictRegionTreeeScope(String regionCode, String version) {
        ScUser u = SecurityUtils.getUser();
        String deptId = StringUtils.isEmpty(regionCode) ? u.getDeptId().toString() : regionCode;
        Query query = new Query();
        if (deptId.contains("0000")) { //省
            query.setArea(StrUtil.subWithLength(deptId, 0, 2));
        } else if (deptId.contains("00")) {  //州市
            query.setArea(StrUtil.subWithLength(deptId, 0, 4));
        }else{ //区县
            query.setArea(deptId);
        }
        query.setType(version); //version为0，只查询行政区
        List<TreeVo> dictRegionTreeeScope = baseMapper.getDictRegionTreeeScope(query);
        if(dictRegionTreeeScope.size()>1){
            return findTree(dictRegionTreeeScope);
        }else{
            return dictRegionTreeeScope;
        }
    }

    @Override
    public List<TreeVo> getDictRegionTreeDataScope(String regionCode, String version) {
        ScUser u = SecurityUtils.getUser();
        String deptId = StringUtils.isEmpty(regionCode) ? u.getDeptId().toString() : regionCode;
        Query query = new Query();
        query.setArea(deptId.replaceAll("0000","00").replaceAll("00",""));
        query.setType(version); //version为0，只查询行政区
        List<TreeVo> dictRegionTreeeScope = baseMapper.getDictRegionTreeDataScope(query);
        if(dictRegionTreeeScope.size()>1){
            return findTree(dictRegionTreeeScope);
        }else{
            return dictRegionTreeeScope;
        }
    }

    @Override
    public List<String> getAreaTmp(String area) {
        return baseMapper.getAreaTmp(area);
    }

}

