package com.ruoyi.web.controller.app;

import com.ruoyi.app.model.HookRegister;
import com.ruoyi.app.model.request.ReqHookRegister;
import com.ruoyi.app.service.IHookRegisterService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 吊钩注册Controller
 *
 * @author coede
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/app/hookRegister")
public class HookRegisterController extends BaseController
{
    @Autowired
    private IHookRegisterService hookRegisterService;

    /**
     * 查询吊钩注册列表
     */
    // @PreAuthorize("@ss.hasPermi('app:hookRegister:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReqHookRegister req)
    {
        startPage();
        List<HookRegister> list = hookRegisterService.selectHookRegisterList(req);
        return getDataTable(list);
    }

    /**
     * 导出吊钩注册列表
     */
    // @PreAuthorize("@ss.hasPermi('app:hookRegister:export')")
    @Log(title = "吊钩注册", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReqHookRegister req)
    {
        List<HookRegister> list = hookRegisterService.selectHookRegisterList(req);
        ExcelUtil<HookRegister> util = new ExcelUtil<HookRegister>(HookRegister.class);
        util.exportExcel(response, list, "吊钩注册数据");
    }

    /**
     * 获取吊钩注册详细信息
     */
    // @PreAuthorize("@ss.hasPermi('app:hookRegister:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hookRegisterService.selectHookRegisterById(id));
    }

    /**
     * 新增吊钩注册
     */
    // @PreAuthorize("@ss.hasPermi('app:hookRegister:add')")
    @Log(title = "吊钩注册", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody HookRegister hookRegister)
    {
        if (!hookRegisterService.checkHookNoUnique(hookRegister))
        {
            return error("新增吊钩注册'" + hookRegister.getHookNo() + "'失败，列表中存在重复的吊钩编号");
        }
        if (!hookRegisterService.checkEpcNoUnique(hookRegister))
        {
            return error("新增吊钩注册'" + hookRegister.getEpcNo() + "'失败，列表中存在重复的芯片编号");
        }
        hookRegister.setCreateBy(getUsername());
        return toAjax(hookRegisterService.insertHookRegister(hookRegister));
    }

    /**
     * 修改吊钩注册
     */
    // @PreAuthorize("@ss.hasPermi('app:hookRegister:edit')")
    @Log(title = "吊钩注册", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody HookRegister hookRegister)
    {
        if (!hookRegisterService.checkHookNoUnique(hookRegister))
        {
            return error("修改吊钩注册'" + hookRegister.getHookNo() + "'失败，列表中存在重复的吊钩编号");
        }
        if (!hookRegisterService.checkEpcNoUnique(hookRegister))
        {
            return error("修改吊钩注册'" + hookRegister.getEpcNo() + "'失败，列表中存在重复的芯片编号");
        }
        hookRegister.setUpdateBy(getUsername());
        return toAjax(hookRegisterService.updateHookRegister(hookRegister));
    }

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

    /**
     * 获取选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<HookRegister> list = hookRegisterService.selectHookRegisterList(new ReqHookRegister());
        return success(list);
    }
}
