package com.ruoyi.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.base.domain.BaseCity;
import com.ruoyi.base.service.IBaseCityService;
import com.ruoyi.base.vo.BaseCityVO;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.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 javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 城市Controller
 *
 * @author vinjor-m
 * @date 2025-03-18
 */
@RestController
@RequestMapping("/base/city")
public class BaseCityController extends BaseController {
    @Autowired
    private IBaseCityService baseCityService;

    /**
     * 查询城市列表
     */
    @PreAuthorize("@ss.hasPermi('base:city:list')")
    @GetMapping("/list")
    public AjaxResult list(BaseCity baseCity) {
        List<BaseCity> list = baseCityService.list();
        return success(list);
    }

    /**
     * 查询城市数列表
     *
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 15:11 2025/4/2
     **/
    @GetMapping("/treeCity")
    @Anonymous
    public AjaxResult treeCity() {
        List<BaseCityVO> list = baseCityService.treeCity();
        return success(list);
    }


    /**
     * 导出城市列表
     */
    @PreAuthorize("@ss.hasPermi('base:city:export')")
    @Log(title = "城市", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseCity baseCity) {
        List<BaseCity> list = baseCityService.list();
        ExcelUtil<BaseCity> util = new ExcelUtil<BaseCity>(BaseCity.class);
        util.exportExcel(response, list, "城市数据");
    }


    /**
     * 新增城市
     */
    @PreAuthorize("@ss.hasPermi('base:city:add')")
    @Log(title = "城市", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseCity baseCity) {
        return toAjax(baseCityService.save(baseCity));
    }

    /**
     * 修改城市
     */
    @PreAuthorize("@ss.hasPermi('base:city:edit')")
    @Log(title = "城市", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseCity baseCity) {
        return toAjax(baseCityService.updateById(baseCity));
    }

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


    /**
     * 删除城市
     */
    @PreAuthorize("@ss.hasPermi('base:city:remove')")
    @Log(title = "城市", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        List<Long> list = new ArrayList<>(Arrays.asList(ids));
        return toAjax(baseCityService.removeByIds(list));
    }

    /**
     * 查询子级城市
     *
     * @param parentId 父级id
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author vinjor-M
     * @date 18:09 2025/3/18
     **/
    @GetMapping("/listByPid")
    public AjaxResult listByPid(@RequestParam(value = "parentId", required = false) Long parentId) {
        if (null == parentId) {
            parentId = 0L;
        }
        List<BaseCity> list = baseCityService.list(new LambdaQueryWrapper<BaseCity>().eq(BaseCity::getParentId, parentId).orderByAsc(BaseCity::getAreaCode));
        return success(list);
    }
}
