package com.ruoyi.web.controller.app;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
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.Hook;
import com.ruoyi.app.service.IHookService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 吊钩Controller
 * 
 * @author coede
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/app/hook")
public class HookController extends BaseController
{
    @Autowired
    private IHookService hookService;

    /**
     * 查询吊钩列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Hook hook)
    {
        startPage();
        List<Hook> list = hookService.selectHookList(hook);
        return getDataTable(list);
    }

    /**
     * 导出吊钩列表
     */
    @Log(title = "吊钩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Hook hook)
    {
        List<Hook> list = hookService.selectHookList(hook);
        ExcelUtil<Hook> util = new ExcelUtil<Hook>(Hook.class);
        util.exportExcel(response, list, "吊钩数据");
    }

    /**
     * 获取吊钩详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hookService.selectHookById(id));
    }

    /**
     * 新增吊钩
     */
    // @PreAuthorize("@ss.hasPermi('app:hook:add')")
    @Log(title = "吊钩", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Hook hook)
    {
        if (!hookService.checkHookNameUnique(hook))
        {
            return error("新增吊钩'" + hook.getHookName() + "'失败，列表中存在重复的吊钩类型");
        }
        hook.setCreateBy(getUsername());
        return toAjax(hookService.insertHook(hook));
    }

    /**
     * 修改吊钩
     */
    // @PreAuthorize("@ss.hasPermi('app:hook:edit')")
    @Log(title = "吊钩", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Hook hook)
    {
        if (!hookService.checkHookNameUnique(hook))
        {
            return error("修改吊钩'" + hook.getHookName() + "'失败，列表中存在重复的吊钩类型");
        }
        hook.setUpdateBy(getUsername());
        return toAjax(hookService.updateHook(hook));
    }

    /**
     * 删除吊钩
     */
    // @PreAuthorize("@ss.hasPermi('app:hook:remove')")
    @Log(title = "吊钩", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hookService.deleteHookByIds(ids));
    }

    /**
     * 吊钩选择
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        Hook hook = new Hook();
        List<Hook> list = hookService.selectHookList(hook);
        return success(list);
    }
}
