package com.muyu.openbusiness.controller;

import com.muyu.openbusiness.domain.SysFence;
import com.muyu.openbusiness.service.ISysFenceService;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.security.annotation.RequiresPermissions;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 围栏组Controller
 *
 * @author muyu
 * @date 2024-09-17
 */
@RestController
@RequestMapping("/fence")
public class SysFenceController extends BaseController
{
    @Resource
    private ISysFenceService sysFenceService;

    /**
     * 查询围栏组列表
     */
    @RequiresPermissions("fence:fence:list")
    @GetMapping("/list")
    public Result<TableDataInfo<SysFence>> list(SysFence sysFence)
    {
        startPage();
        List<SysFence> list = sysFenceService.selectSysFenceList(sysFence);
        return getDataTable(list);
    }

    /**
     * 导出围栏组列表
     */
    @RequiresPermissions("fence:fence:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysFence sysFence)
    {
        List<SysFence> list = sysFenceService.selectSysFenceList(sysFence);
        ExcelUtil<SysFence> util = new ExcelUtil<SysFence>(SysFence.class);
        util.exportExcel(response, list, "围栏组数据");
    }

    /**
     * 获取围栏组详细信息
     */
    @RequiresPermissions("fence:fence:query")
    @GetMapping(value = "/{id}")
    public Result<List<SysFence>> getInfo(@PathVariable("id") Long id)
    {
        return success(sysFenceService.selectSysFenceById(id));
    }

    /**
     * 新增围栏组
     */
    @RequiresPermissions("fence:fence:add")
    @PostMapping
    public Result<Integer> add(
            @Validated @RequestBody SysFence sysFence)
    {

        return toAjax(sysFenceService.save(sysFence));
    }

    /**
     * 修改围栏组
     */
    @RequiresPermissions("fence:fence:edit")
    @PutMapping
    public Result<Integer> edit(
            @Validated @RequestBody SysFence sysFence)
    {

        return toAjax(sysFenceService.updateById(sysFence));
    }

    /**
     * 删除围栏组
     */
    @RequiresPermissions("fence:fence:remove")
	@DeleteMapping("/{ids}")
    public Result<Integer> remove(@PathVariable("ids") Long[] ids)
    {
        sysFenceService.removeBatchByIds(Arrays.asList(ids));
        return success();
    }
}
