package com.fastbee.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.repository.query.Param;
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.fastbee.common.annotation.Log;
import com.fastbee.common.core.controller.BaseController;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.common.enums.BusinessType;
import com.fastbee.system.domain.SysDistrict;
import com.fastbee.system.service.ISysDistrictService;
import com.fastbee.common.utils.poi.ExcelUtil;
import com.fastbee.common.core.page.TableDataInfo;

/**
 * 行政区划Controller
 *
 * @author kerwincui
 * &#064;date  2024-10-18
 */
@RestController
@RequestMapping("/system/district")
@Api(tags = "行政区划")
public class SysDistrictController extends BaseController
{
    @Autowired
    private ISysDistrictService sysDistrictService;

    /**
     * 查询行政区划树状列表
     */
//    @PreAuthorize("@ss.hasPermi('system:district:list')")
    @GetMapping("/tree")
    @ApiOperation("查询行政区划树状列表")
    public AjaxResult tree( Integer level){

        return success(sysDistrictService.selectSysDistrictTree(level));
    }

    /**
     * 查询指定地区行政区划列表
     */
    @GetMapping("/tree-by-code")
    @ApiOperation("查询指定地区行政区划列表")
    public AjaxResult listByLevel(Long areaCode){
        return success(sysDistrictService.selectSysDistrictTreeByCode(areaCode));
    }

    /**
 * 查询行政区划列表
 */
@PreAuthorize("@ss.hasPermi('system:district:list')")
@GetMapping("/list")
@ApiOperation("查询行政区划列表")
    public TableDataInfo list(SysDistrict sysDistrict)
    {
        startPage();
        List<SysDistrict> list = sysDistrictService.selectSysDistrictList(sysDistrict);
        return getDataTable(list);
    }

    /**
     * 导出行政区划列表
     */
    @ApiOperation("导出行政区划列表")
    @PreAuthorize("@ss.hasPermi('system:district:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDistrict sysDistrict)
    {
        List<SysDistrict> list = sysDistrictService.selectSysDistrictList(sysDistrict);
        ExcelUtil<SysDistrict> util = new ExcelUtil<SysDistrict>(SysDistrict.class);
        util.exportExcel(response, list, "行政区划数据");
    }

    /**
     * 根据id获取行政区划详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:district:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取行政区划详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysDistrictService.selectSysDistrictById(id));
    }
    /**
     * 根据地区代码获取行政区划详细信息
     */

    @GetMapping(value = "/by-code/{areaCode}")
    @ApiOperation("获取行政区划详细信息")
    public AjaxResult getInfoByCode(@PathVariable("areaCode") Long areaCode)
    {
        return success(sysDistrictService.selectSysDistrictByCode(areaCode));
    }

    /**
     * 新增行政区划
     */
    @PreAuthorize("@ss.hasPermi('system:district:add')")
    @PostMapping
    @ApiOperation("新增行政区划")
    public AjaxResult add(@RequestBody SysDistrict sysDistrict)
    {
        return toAjax(sysDistrictService.insertSysDistrict(sysDistrict));
    }

    /**
     * 修改行政区划
     */
    @PreAuthorize("@ss.hasPermi('system:district:edit')")
    @PutMapping
    @ApiOperation("修改行政区划")
    public AjaxResult edit(@RequestBody SysDistrict sysDistrict)
    {
        return toAjax(sysDistrictService.updateSysDistrict(sysDistrict));
    }

    /**
     * 删除行政区划
     */
    @PreAuthorize("@ss.hasPermi('system:district:remove')")
    @DeleteMapping("/{ids}")
    @ApiOperation("删除行政区划")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysDistrictService.deleteSysDistrictByIds(ids));
    }
}
