package com.ruoyi.web.controller.clock;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.clock.domain.bo.AgentBo;
import com.ruoyi.clock.domain.vo.AgentVo;
import com.ruoyi.clock.service.IAgentService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 代理商信息Controller
 *
 * @author pengc
 * @date 2026-03-04
 */
@Validated
@Api(value = "代理商信息控制器", tags = {"代理商信息管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/clock/agent")
public class AgentController extends BaseController {

    private final IAgentService iAgentService;

    /**
     * 分页查询代理商信息列表
     */
    @ApiOperation("查询代理商信息列表")
    @SaCheckPermission("clock:agent:list")
    @GetMapping("/page")
    public TableDataInfo<AgentVo> page(@Validated(QueryGroup.class) AgentBo bo, PageQuery pageQuery) {
        return iAgentService.queryPageList(bo, pageQuery);
    }

    /**
     *查询代理商信息列表
     */
    @ApiOperation("查询代理商信息列表")
    @GetMapping("/list")
    public R<List<AgentVo>> list(@Validated(QueryGroup.class) AgentBo bo) {
        return R.ok(iAgentService.queryList(bo));
    }

    /**
     * 获取代理商信息详细信息
     */
    @ApiOperation("获取代理商信息详细信息")
    @SaCheckPermission("clock:agent:query")
    @GetMapping("/info/{agentId}")
    public R<AgentVo> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("agentId") Long agentId) {
        return R.ok(iAgentService.queryById(agentId));
    }

    /**
     * 新增代理商信息
     */
    @ApiOperation("新增代理商信息")
    @SaCheckPermission("clock:agent:add")
    @Log(title = "代理商信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AgentBo bo) {
        return toAjax(iAgentService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改代理商信息
     */
    @ApiOperation("修改代理商信息")
    @SaCheckPermission("clock:agent:edit")
    @Log(title = "代理商信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AgentBo bo) {
        return toAjax(iAgentService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除代理商信息
     */
    @ApiOperation("删除代理商信息")
    @SaCheckPermission("clock:agent:remove")
    @Log(title = "代理商信息", businessType = BusinessType.DELETE)
    @PostMapping("/del/{agentIds}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] agentIds) {
        return toAjax(iAgentService.deleteWithValidByIds(Arrays.asList(agentIds), true) ? 1 : 0);
    }

    /**
     * 导出代理商信息列表
     */
    @ApiOperation("导出代理商信息列表")
    @SaCheckPermission("clock:agent:export")
    @Log(title = "代理商信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated AgentBo bo, HttpServletResponse response) {
        List<AgentVo> list = iAgentService.queryList(bo);
        ExcelUtil.exportExcel(list, "代理商信息", AgentVo.class, response);
    }

}
