package com.muyu.fence.controller;

import java.util.Arrays;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
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.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.fence.domain.TbFence;
import com.muyu.fence.service.ITbFenceService;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.security.utils.SecurityUtils;
import org.springframework.validation.annotation.Validated;
import com.muyu.common.core.web.page.TableDataInfo;

/**
 * 电子围栏Controller
 *
 * @author muyu
 * @date 2024-09-18
 */
@RestController
@RequestMapping("/fence")
public class TbFenceController extends BaseController
{
    @Resource
    private ITbFenceService tbFenceService;

    /**
     * 查询电子围栏列表
     */
    @RequiresPermissions("fence:fence:list")
    @GetMapping("/list")
    public Result<TableDataInfo<TbFence>> list(TbFence tbFence)
    {
        startPage();
        List<TbFence> list = tbFenceService.selectTbFenceList(tbFence);
        return getDataTable(list);
    }

    /**
     * 导出电子围栏列表
     */
    @RequiresPermissions("fence:fence:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbFence tbFence)
    {
        List<TbFence> list = tbFenceService.selectTbFenceList(tbFence);
        ExcelUtil<TbFence> util = new ExcelUtil<TbFence>(TbFence.class);
        util.exportExcel(response, list, "电子围栏数据");
    }

    /**
     * 获取电子围栏详细信息
     */
    @RequiresPermissions("fence:fence:query")
    @GetMapping(value = "/{fenceId}")
    public Result<List<TbFence>> getInfo(@PathVariable("fenceId") Long fenceId)
    {
        return success(tbFenceService.selectTbFenceByFenceId(fenceId));
    }

    /**
     * 新增电子围栏
     */
    @RequiresPermissions("fence:fence:add")
    @PostMapping
    public Result<Integer> add(
            @Validated @RequestBody TbFence tbFence)
    {
        if (tbFenceService.checkIdUnique(tbFence)) {
            return error("新增 电子围栏 '" + tbFence + "'失败，电子围栏已存在");
        }
        tbFence.setCreateBy(SecurityUtils.getUsername());
        return toAjax(tbFenceService.save(tbFence));
    }

    /**
     * 修改电子围栏
     */
    @RequiresPermissions("fence:fence:edit")
    @PutMapping
    public Result<Integer> edit(
            @Validated @RequestBody TbFence tbFence)
    {
        if (!tbFenceService.checkIdUnique(tbFence)) {
            return error("修改 电子围栏 '" + tbFence + "'失败，电子围栏不存在");
        }
        tbFence.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(tbFenceService.updateById(tbFence));
    }

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