package com.flossom.system.controller;

import javax.servlet.http.HttpServletResponse;

import com.flossom.common.core.domain.entity.SysRegion;
import com.flossom.common.core.utils.poi.ExcelUtil;
import com.flossom.common.core.web.controller.BaseController;
import com.flossom.common.core.web.domain.AjaxResult;
import com.flossom.common.core.web.page.TableDataInfo;
import com.flossom.common.log.annotation.Log;
import com.flossom.common.log.enums.BusinessType;
import com.flossom.common.security.annotation.RequiresPermissions;
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.flossom.system.service.ISysRegionService;

import java.util.List;

/**
 * 地区列Controller
 *
 * @author flossom
 * @date 2023-12-12
 */
@RestController
@RequestMapping("/region")
public class SysRegionController extends BaseController {

    @Autowired
    private ISysRegionService sysRegionService;

    /**
     * 查询地区列列表
     */
    @RequiresPermissions("system:region:list")
    @GetMapping("/list")
    public TableDataInfo list(SysRegion sysRegion) {
        startPage();
        List<SysRegion> list = sysRegionService.selectSysRegionList(sysRegion);
        return getDataTable(list);
    }

    /**
     * 导出地区列列表
     */
    @RequiresPermissions("system:region:export")
    @Log(title = "地区列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRegion sysRegion) {
        List<SysRegion> list = sysRegionService.selectSysRegionList(sysRegion);
        ExcelUtil<SysRegion> util = new ExcelUtil<SysRegion>(SysRegion.class);
        util.exportExcel(response, list, "地区列数据");
    }

    /**
     * 获取地区列详细信息
     */
    @RequiresPermissions("system:region:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysRegionService.selectSysRegionById(id));
    }

    /**
     * 新增地区列
     */
    @RequiresPermissions("system:region:add")
    @Log(title = "地区列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysRegion sysRegion) {
        return toAjax(sysRegionService.insertSysRegion(sysRegion));
    }

    /**
     * 修改地区列
     */
    @RequiresPermissions("system:region:edit")
    @Log(title = "地区列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysRegion sysRegion) {
        return toAjax(sysRegionService.updateSysRegion(sysRegion));
    }

    /**
     * 删除地区列
     */
    @RequiresPermissions("system:region:remove")
    @Log(title = "地区列", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysRegionService.deleteSysRegionByIds(ids));
    }
}
