package com.ruoyi.hezhi.controller;

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 com.ruoyi.hezhi.domain.TbRegion;
import com.ruoyi.hezhi.service.ITbRegionService;
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.HashMap;
import java.util.List;

/**
 * 行政区域Controller
 *
 * @author ruoyi
 * @date 2024-10-21
 */
@RestController
@RequestMapping("/hezhi/region")
public class TbRegionController extends BaseController
{
    @Autowired
    private ITbRegionService tbRegionService;

    /**
     * 查询行政区域列表
     */
    @PreAuthorize("@ss.hasPermi('hezhi:region:list')")
    @GetMapping("/list")
    public AjaxResult list(TbRegion tbRegion)
    {
        List<TbRegion> list = tbRegionService.selectTbRegionList(tbRegion);
        return success(list);
    }

    /**
     * 查询行政区域列表
     */
    @GetMapping("/treeList")
    public AjaxResult treeList(TbRegion tbRegion)
    {
        List<TbRegion> list = tbRegionService.selectTbRegionTreeList(tbRegion);
        return success(list);
    }

    /**
     * 导出行政区域列表
     */
    @PreAuthorize("@ss.hasPermi('hezhi:region:export')")
    @Log(title = "行政区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbRegion tbRegion)
    {
        List<TbRegion> list = tbRegionService.selectTbRegionList(tbRegion);
        ExcelUtil<TbRegion> util = new ExcelUtil<TbRegion>(TbRegion.class);
        util.exportExcel(response, list, "行政区域数据");
    }

    /**
     * 获取行政区域详细信息
     */
    @PreAuthorize("@ss.hasPermi('hezhi:region:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbRegionService.selectTbRegionById(id));
    }

    /**
     * 新增行政区域
     */
    @PreAuthorize("@ss.hasPermi('hezhi:region:add')")
    @Log(title = "行政区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbRegion tbRegion)
    {
        return toAjax(tbRegionService.insertTbRegion(tbRegion));
    }

    /**
     * 修改行政区域
     */
    @PreAuthorize("@ss.hasPermi('hezhi:region:edit')")
    @Log(title = "行政区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbRegion tbRegion)
    {
        return toAjax(tbRegionService.updateTbRegion(tbRegion));
    }

    /**
     * 删除行政区域
     */
    @PreAuthorize("@ss.hasPermi('hezhi:region:remove')")
    @Log(title = "行政区域", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbRegionService.deleteTbRegionByIds(ids));
    }

    /**
     * 获取省
     * @return
     */
    @GetMapping("/getProvince")
    public AjaxResult getProvince() {
        HashMap<String,Object> map = new HashMap<>();
        List<TbRegion> list = tbRegionService.selectRegionProvinceList();
        map.put("data",list);
        return AjaxResult.success(map);
    }

    /**
     * 根据ID获取市或区（县）
     * @return
     */
    @GetMapping("/getCityOrArea")
    public AjaxResult getCityOrArea(Integer id) {
        HashMap<String,Object> map = new HashMap<>();
        List<TbRegion> list = tbRegionService.selectRegionCityOrAreaList(id);
        map.put("data",list);
        return AjaxResult.success(map);
    }
}
