package com.ruoyi.web.controller.system;

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

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.system.domain.SysRegisterRequest;
import com.ruoyi.system.service.ISysRegisterRequestService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 注册申请Controller
 *
 * @author liangwen
 * @date 2025-08-26
 */
@Api(tags = "2 注册管理")
@ApiSupport(order = 2)
@RestController
@RequestMapping("/system/sysRegisterRequest")
public class SysRegisterRequestController extends BaseController
{
    @Autowired
    private ISysRegisterRequestService sysRegisterRequestService;

    /**
     * 查询注册申请列表
     */
    @ApiOperation(value = "2.1 查询注册申请列表")
    @ApiOperationSupport(order = 1)
    @PreAuthorize("@ss.hasPermi('system:sysRegisterRequest:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysRegisterRequest sysRegisterRequest)
    {
        startPage();
        List<SysRegisterRequest> list = sysRegisterRequestService.selectSysRegisterRequestList(sysRegisterRequest);
        return getDataTable(list);
    }

    /**
     * 导出注册申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysRegisterRequest:export')")
    @Log(title = "注册申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRegisterRequest sysRegisterRequest)
    {
        List<SysRegisterRequest> list = sysRegisterRequestService.selectSysRegisterRequestList(sysRegisterRequest);
        ExcelUtil<SysRegisterRequest> util = new ExcelUtil<SysRegisterRequest>(SysRegisterRequest.class);
        util.exportExcel(response, list, "注册申请数据");
    }

    /**
     * 获取注册申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sysRegisterRequest:query')")
    @GetMapping(value = "/{requestId}")
    public AjaxResult getInfo(@PathVariable("requestId") Long requestId)
    {
        return success(sysRegisterRequestService.selectSysRegisterRequestByRequestId(requestId));
    }

    /**
     * 新增注册申请
     */
    @PreAuthorize("@ss.hasPermi('system:sysRegisterRequest:add')")
    @Log(title = "注册申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysRegisterRequest sysRegisterRequest)
    {
        return toAjax(sysRegisterRequestService.insertSysRegisterRequest(sysRegisterRequest));
    }

    /**
     * 修改注册申请
     */
    @PreAuthorize("@ss.hasPermi('system:sysRegisterRequest:edit')")
    @Log(title = "注册申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysRegisterRequest sysRegisterRequest)
    {
        return toAjax(sysRegisterRequestService.updateSysRegisterRequest(sysRegisterRequest));
    }

    /**
     * 删除注册申请
     */
    @PreAuthorize("@ss.hasPermi('system:sysRegisterRequest:remove')")
    @Log(title = "注册申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{requestIds}")
    public AjaxResult remove(@PathVariable Long[] requestIds)
    {
        return toAjax(sysRegisterRequestService.deleteSysRegisterRequestByRequestIds(requestIds));
    }

    @ApiOperation(value = "2.2 注册申请审核通过")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "requestId", value = "申请id", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class, required = false),
    })
    @PostMapping("/approve/{requestId}")
    public AjaxResult approve(@PathVariable Long requestId, @RequestParam(required = false) String remark) {
        try {
            sysRegisterRequestService.approve(requestId, true, remark);
            return AjaxResult.success("审批通过");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("审批出错");
        }
    }

    @ApiOperation(value = "2.3 注册申请驳回")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "requestId", value = "申请id", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
    })
    @PostMapping("/reject/{requestId}")
    public AjaxResult reject(@PathVariable Long requestId, @RequestParam String remark) {
        try {
            sysRegisterRequestService.approve(requestId, false, remark);
            return AjaxResult.success("已驳回");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("已驳回出错");
        }
    }
}
