package com.yunfeiyun.agmp.common.web.system.controller;

import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.web.system.domain.SysZone;
import com.yunfeiyun.agmp.common.web.system.service.ISysZoneService;
import org.springframework.security.access.prepost.PreAuthorize;
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.RestController;
import com.yunfeiyun.agmp.common.core.controller.BaseController;

/**
 * 地区Controller
 * 
 * @author 杨晓辉
 * @date 2023-08-03
 */
@RestController
@RequestMapping("/sys/zone")
public class SysZoneController extends BaseController
{
    @Autowired
    private ISysZoneService sysZoneService;

    /**
     * 查询地区列表
     */
    @PreAuthorize("@ss.hasPermi('sys:zone:list')")
    @GetMapping("/list")
    public AjaxResult list(SysZone sysZone)
    {
        List<SysZone> sysZones = sysZoneService.selectSysZoneListProvincezh(sysZone);
        List<SysZone> sysZones1 = sysZoneService.selectSysZoneListCapitalzh(sysZone);
        List<SysZone> list = sysZoneService.selectSysZoneList(sysZone);

        JSONArray jsonArray = new JSONArray();
        for (SysZone zone : sysZones) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name", zone.getZoneProvincezh());

            JSONArray jsonArray1 = new JSONArray();
            for (SysZone sysZone1 : sysZones1) {

                JSONArray jsonArray2 = new JSONArray();
                for (SysZone sysZone2 : list) {
                    if (sysZone1.getZoneCapitalzh().equals(sysZone2.getZoneCapitalzh())) {
                        JSONObject jsonObject2 = new JSONObject();
                        jsonObject2.put("name", sysZone2.getZoneCityzh());
                        jsonArray2.add(jsonObject2);
                    }

                }
                if (zone.getZoneProvincezh().equals(sysZone1.getZoneProvincezh())) {
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.put("name", sysZone1.getZoneCapitalzh());
                    jsonObject1.put("children", jsonArray2);
                    jsonArray1.add(jsonObject1);
                }
            }

            jsonObject.put("children", jsonArray1);
            jsonArray.add(jsonObject);
        }

        return AjaxResult.success(jsonArray);
    }


}
