package com.ruoyi.web.controller.app;

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

import com.ruoyi.app.DTO.RegionTreeDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.app.model.Region;
import com.ruoyi.app.service.IRegionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 区域Controller
 * 
 * @author coede
 * @date 2025-03-19
 */
@Api("区域信息管理")
@RestController
@RequestMapping("/app/region")
public class RegionController extends BaseController
{
    @Autowired
    private IRegionService regionService;

    /**
     * 查询区域列表
     */
    @ApiOperation("查询区域列表")
    @GetMapping("/list")
    public TableDataInfo list(Region region)
    {
        startPage();
        List<Region> list = regionService.selectRegionList(region);
        return getDataTable(list);
    }

    /**
     * 导出区域列表
     */
    @ApiOperation("导出区域列表")
    // @PreAuthorize("@ss.hasPermi('app:region:export')")
    @Log(title = "区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Region region)
    {
        List<Region> list = regionService.selectRegionList(region);
        ExcelUtil<Region> util = new ExcelUtil<Region>(Region.class);
        util.exportExcel(response, list, "区域数据");
    }

    /**
     * 获取区域详细信息
     */
    @ApiOperation("获取区域详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(regionService.selectRegionById(id));
    }

    /**
     * 新增区域
     */
    @ApiOperation("新增区域")
    // @PreAuthorize("@ss.hasPermi('app:region:add')")
    @Log(title = "区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated  @RequestBody Region region)
    {
        if (!regionService.checkRegionNameUnique(region))
        {
            return error("新增区域'" + region.getRegionName() + "'失败，列表中存在重复的区域名称");
        }
        region.setCreateBy(getUsername());
        return toAjax(regionService.insertRegion(region));
    }

    /**
     * 修改区域
     */
    @ApiOperation("修改区域")
    // @PreAuthorize("@ss.hasPermi('app:region:edit')")
    @Log(title = "区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Region region)
    {
        if (!regionService.checkRegionNameUnique(region))
        {
            return error("修改区域'" + region.getRegionName() + "'失败，列表中存在重复的区域名称");
        }
        region.setUpdateBy(getUsername());
        return toAjax(regionService.updateRegion(region));
    }

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

    /**
     * 查询区域选择
     */
    @ApiOperation("查询区域选择")
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        Region region = new Region();
        List<Region> list = regionService.selectRegionList(region);
        return success(list);
    }

    /**
     * 查询区域设备树
     */
    @ApiOperation("查询区域设备树")
    @GetMapping("/treeList")
    public AjaxResult treeList(Region region)
    {
        List<RegionTreeDTO> list = regionService.selectRegionTreeList(region);
        return success(list);
    }
}
