package com.fs.city.controller;

import com.fs.city.domain.FsCity;
import com.fs.city.service.IFsCityService;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 城市Controller
 * 
 * @author fs
 * @date 2026-01-14
 */
@RestController
@RequestMapping("/city/city")
public class FsCityController extends BaseController
{
    @Autowired
    private IFsCityService fsCityService;

    /**
     * 查询城市列表
     */
    @PreAuthorize("@ss.hasPermi('city:city:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsCity fsCity)
    {
        startPage();
        List<FsCity> list = fsCityService.selectFsCityList(fsCity);
        return getDataTable(list);
    }

    /**
     * 导出城市列表
     */
    @Log(title = "城市", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsCity fsCity)
    {
        List<FsCity> list = fsCityService.selectFsCityList(fsCity);
        ExcelUtil<FsCity> util = new ExcelUtil<FsCity>(FsCity.class);
        return util.exportExcel(list, "城市数据");
    }

    /**
     * 获取城市详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fsCityService.selectFsCityById(id));
    }

    /**
     * 新增城市
     */
    @PreAuthorize("@ss.hasPermi('city:city:add')")
    @Log(title = "城市", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsCity fsCity)
    {
        return toAjax(fsCityService.insertFsCity(fsCity));
    }

    /**
     * 修改城市
     */
    @Log(title = "城市", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsCity fsCity)
    {
        return toAjax(fsCityService.updateFsCity(fsCity));
    }

    /**
     * 删除城市
     */
    @Log(title = "城市", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fsCityService.deleteFsCityByIds(ids));
    }


    /**
     * 级联：按父级编码查询下一级
     * parentId 为空 -> 默认取全国下的省（parent_id = "0" 或 level=1，按你数据定）
     */
    @GetMapping("/cascaderChildren")
    public AjaxResult cascaderChildren(@RequestParam(required = false) String parentId) {
        List<FsCity> list = fsCityService.selectChildren(parentId);
        // 组装成 el-cascader 需要的结构：value/label/leaf
        List<Map<String, Object>> result = list.stream().map(c -> {
            Map<String, Object> m = new HashMap<>();
            m.put("value", c.getCityId());     // 用 city_id 作为value（强烈推荐）
            m.put("label", c.getCityName());
            // leaf：是否叶子（这里按 level 判断：省/市/区县...你想停在区县就 level>=3 leaf=true）
            boolean leaf = c.getLevel() != null && c.getLevel() >= 3;
            m.put("leaf", leaf);
            return m;
        }).collect(Collectors.toList());

        return AjaxResult.success(result);
    }


    @GetMapping("/path")
    public AjaxResult path(@RequestParam String cityId) {
        return AjaxResult.success(fsCityService.selectPath(cityId));
    }
}

