package com.ruoyi.set.controller;

import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
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.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.set.domain.SetRegion;
import com.ruoyi.set.service.ISetRegionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 行政区域Controller
 *
 * @author LCL
 * @date 2024-03-12
 */
@RestController
@RequestMapping("/set/setRegion")
public class SetRegionController extends BaseController
{
    @Resource
    private ISetRegionService setRegionService;

    /**
     * 查询行政区域列表
     */
    @PreAuthorize("@ss.hasPermi('set:setRegion:list')")
    @GetMapping("/list")
    public TableDataInfo list(SetRegion setRegion)
    {
        startPage();
        List<SetRegion> list = setRegionService.selectSetRegionList(setRegion);
        return getDataTable(list);
    }

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

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

    /**
     * 新增行政区域
     */
    @PreAuthorize("@ss.hasPermi('set:setRegion:add')")
    @Log(title = "行政区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SetRegion setRegion)
    {
        return toAjax(setRegionService.insertSetRegion(setRegion));
    }

    /**
     * 修改行政区域
     */
    @PreAuthorize("@ss.hasPermi('set:setRegion:edit')")
    @Log(title = "行政区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SetRegion setRegion)
    {
        return toAjax(setRegionService.updateSetRegion(setRegion));
    }

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

    /**
     * 获取省列表
     *
     * @return 行政区域集合
     */
    @GetMapping("/getProvince")
    public AjaxResult getProvince() {
        HashMap<String,Object> map = new HashMap<>();
        List<SetRegion> list = setRegionService.selectRegionProvinceList();
        map.put("data",list);
        return AjaxResult.success(map);
    }

    /**
     * 根据ID获取市或区县列表
     *
     * @param id 行政区域主键ID
     * @return 行政区域集合
     */
    @GetMapping("/getCityOrArea")
    public AjaxResult getCityOrArea(Integer id) {
        HashMap<String,Object> map = new HashMap<>();
        List<SetRegion> list = setRegionService.selectRegionCityOrAreaList(id);
        map.put("data",list);
        return AjaxResult.success(map);
    }
}
