package com.bjtdba.system.controller;


import com.bjtdba.entity.system.Regions;
import com.bjtdba.system.service.RegionsService;
import com.bjtdba.system.util.redis.RedisUtil;
import com.bjtdba.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system_service/city")
public class CityController {

    @Autowired
    public RegionsService regionsService;

    @Autowired
    public RedisUtil redisUtil;

    /**
     * 4级地址接口
     * @return
     */
    @GetMapping("/new_lst")
    public Object new_lst(){

//        Lis
        List<Regions> nodes=new ArrayList<Regions>();

//        if(redisUtil.hasKey("regionList_nodes")){
//            nodes= (List<Regions>) redisUtil.get("regionList_nodes");
//        }else {
//            List<Regions> regionList = regionsService.queryList(null);
//
//            for (Regions regions : regionList) {
//                Regions node = toTreeNode(regions);
//                Regions parent = Regions.findNode(nodes, node.getPid());
//                if (parent != null)
//                    parent.getChildren().add(node);
//                else
//                    nodes.add(node);
//            }
//
//            redisUtil.set("regionList_nodes", nodes);
//        }
        if(redisUtil.hasKey("regionList_nodes")){
            nodes= (List<Regions>) redisUtil.get("regionList_nodes");
        }else {
            Regions param=new Regions();
            param.setLeve(new Integer[]{1,2});

            List<Regions> regionList = regionsService.queryList(param);//1,2数据


            for (Regions regions : regionList) {
                Regions node = toTreeNode(regions);
                Regions parent = Regions.findNode(nodes, node.getPid());
    //            if(regions.getLeve()==2 && regions.getId()>1000){
    //                break;
    //            }
                if (parent != null)
                    parent.getChildren().add(node);
                else
                    nodes.add(node);
            }

            Regions param34=new Regions();
            param34.setLeve(new Integer[]{3,4});
            List<Regions> regionList34 = regionsService.queryList(param34);//1,2数据
            for (Regions regions : regionList34) {
                Regions node = toTreeNode(regions);
                Regions parent = Regions.findNode(nodes, node.getPid());
    //            if(regions.getLeve()==2 && regions.getId()>1000){
    //                break;
    //            }
                if (parent != null)
                    parent.getChildren().add(node);
                else
                    nodes.add(node);
            }
             redisUtil.set("regionList_nodes", nodes);
        }


//        JSONObject object=new JSONObject(nodes)
        return  ResponseUtil.ok(nodes);
    }


    @GetMapping("/new_city_lst")
    public Object new_city_lst(@RequestParam Map<String,String> pmap){
        if(!pmap.containsKey("pid")){
            return ResponseUtil.fail("参数错误");
        }
        Regions queryRegions=new Regions();
        queryRegions.setPid(Integer.parseInt(pmap.get("pid")));
        List<Regions> regionsList=regionsService.queryList(queryRegions);

        return ResponseUtil.ok(regionsList);

    }

    private Regions toTreeNode(Regions regions) {
        Regions node = new Regions();
        node.setId(regions.getId());
        node.setPid(regions.getPid());
        node.setName(regions.getName());
        return node;
    }
}
