package com.ankaibei.workFlow.webadmin.api;

import com.ankaibei.workFlow.webadmin.upms.model.SysRegion;
import com.ankaibei.workFlow.webadmin.upms.service.SysRegionService;
import com.ankaibei.workFlow.webadmin.utils.GaoDeWebUtil;
import com.ankaibei.workflow.api.AreaApi;
import com.ankaibei.workflow.vo.AreaInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.jsonwebtoken.lang.Collections;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/foreign/admin/area")
public class AreaApiService implements AreaApi {

    @Autowired
    private SysRegionService sysRegionService;

    @Override
    @RequestMapping("/getInfoLikeKeywordsAndSubdistrict")
    public List<AreaInfo> getInfoLikeKeywordsAndSubdistrict(String keywords, Integer subdistrict) {
        List<SysRegion> list = sysRegionService.list();
        if (!Collections.isEmpty(list)) {
            List<AreaInfo> collect = list.stream().filter(o->o.getAdcode().equals(keywords) || o.getName().contains(keywords)).map(o -> {
                AreaInfo areaInfo = new AreaInfo();
                BeanUtils.copyProperties(o, areaInfo);
                return areaInfo;
            }).collect(Collectors.toList());
            for (AreaInfo areaInfo : collect) {
                List<AreaInfo> areaInfos = buildData(Arrays.asList(areaInfo), list);
                // 构建树
                areaInfo.setDistricts(buildTree(areaInfos, list));
            }
            return collect;
        }
        return new ArrayList<>();
//        return GaoDeWebUtil.getInfoLikeKeywordsAndSubdistrict(keywords, subdistrict);
    }

    /**
     * 查询省份信息
     */
    @Override
    @RequestMapping("/getProvinces")
    public List<AreaInfo> getProvinces() {


        List<SysRegion> list = sysRegionService.list();
        if (!Collections.isEmpty(list)) {
            List<AreaInfo> collect = list.stream().filter(o->o.getName().equals("中华人民共和国")).map(o -> {
                AreaInfo areaInfo = new AreaInfo();
                BeanUtils.copyProperties(o, areaInfo);
                return areaInfo;
            }).collect(Collectors.toList());
            List<AreaInfo> areaInfos = buildData(collect, list);
            // 构建树
            return buildTree(areaInfos, list);
        }
        return new ArrayList<>();
//        return GaoDeWebUtil.getProvinces();
    }


    // 根据父编码获取子集合
    @Override
    @RequestMapping("/getCity")
    public List<AreaInfo> getCity(String keywords) {
        if (StringUtils.isBlank(keywords)) {
            return new ArrayList<>();
        }
        List<SysRegion> list = sysRegionService.list();
        if (!Collections.isEmpty(list)) {
            List<AreaInfo> collect = list.stream().filter(fo -> {
                if (fo.getAdcode().equals(keywords) || fo.getName().contains(keywords)) {
                    return true;
                } else {
                    return false;
                }
            }).filter(o -> o.getLevel().equals("province"))
                    .map(o -> {
                        AreaInfo areaInfo = new AreaInfo();
                        BeanUtils.copyProperties(o, areaInfo);
                        return areaInfo;
                    }).collect(Collectors.toList());
            List<AreaInfo> areaInfos = buildData(collect, list);
//            return areaInfos;
            // 构建树
            return buildTree(areaInfos, list);
        }
        return new ArrayList<>();
//        return GaoDeWebUtil.getCity(keywords);
    }

    /**
     * 构建树
     *
     * @param areaInfos 满足条件的子区域集合
     * @param list      全部行政区域
     * @return
     */
    private List<AreaInfo> buildTree(List<AreaInfo> areaInfos, List<SysRegion> list) {
        if (areaInfos.size() > 0) {
            for (AreaInfo areaInfo : areaInfos) {
                List<AreaInfo> collect1 = list.stream()
                        .filter(o -> o.getParentAdcode().equals(areaInfo.getAdcode()))
                        .map(o -> {
                            AreaInfo ai = new AreaInfo();
                            BeanUtils.copyProperties(o, ai);
                            return ai;
                        })
                        .collect(Collectors.toList());
                areaInfo.setDistricts(collect1);
                if (!ObjectUtils.isEmpty(collect1) && collect1.size() > 0) {
                     buildTree(collect1, list);
                }
            }
        }
        return areaInfos;
    }

    /**
     * 构建数据
     *
     * @param collect 满足条件的父集合
     * @param list    全部行政区域
     * @return
     */
    private List<AreaInfo> buildData(List<AreaInfo> collect, List<SysRegion> list) {
        List<AreaInfo> rlist = new ArrayList<>();
        for (AreaInfo areaInfo : collect) {
            List<AreaInfo> collect1 = list.stream().filter(o -> o.getParentAdcode().equals(areaInfo.getAdcode()))
                    .map(o -> {
                        AreaInfo ai = new AreaInfo();
                        BeanUtils.copyProperties(o, ai);
                        return ai;
                    })
                    .collect(Collectors.toList());
            rlist.addAll(collect1);
        }
        return rlist;
    }

}
