package cn.iocoder.hake.module.agent.controller.admin.agent;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.hake.framework.common.pojo.PageParam;
import cn.iocoder.hake.framework.common.pojo.PageResult;
import cn.iocoder.hake.framework.common.pojo.CommonResult;
import cn.iocoder.hake.framework.common.util.object.BeanUtils;
import static cn.iocoder.hake.framework.common.pojo.CommonResult.success;

import cn.iocoder.hake.framework.excel.core.util.ExcelUtils;

import cn.iocoder.hake.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.hake.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.hake.module.agent.controller.admin.agent.vo.*;
import cn.iocoder.hake.module.agent.dal.dataobject.agent.AgentDO;
import cn.iocoder.hake.module.agent.service.agent.AgentService;

@Tag(name = "管理后台 - 代理商管理")
@RestController
@RequestMapping("/agent/agent")
@Validated
public class AgentController {

    @Resource
    private AgentService agentService;

    @PostMapping("/create")
    @Operation(summary = "创建代理商管理")
    @PreAuthorize("@ss.hasPermission('agent:info:create')")
    public CommonResult<Long> createAgent(@Valid @RequestBody AgentSaveReqVO createReqVO) {
        return success(agentService.createAgent(createReqVO));
    }

    @PostMapping("/updateStatus")
    @Operation(summary = "代理商审核")
    @PreAuthorize("@ss.hasPermission('agent:info:audit')")
    public CommonResult<Long> updateAgentStatus(@Valid @RequestBody AuditRespVO auditRespVO) {
        return success(agentService.updateAgentStatus(auditRespVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新代理商管理")
    @PreAuthorize("@ss.hasPermission('agent:info:update')")
    public CommonResult<Boolean> updateAgent(@Valid @RequestBody AgentSaveReqVO updateReqVO) {
        agentService.updateAgent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除代理商管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('agent:info:delete')")
    public CommonResult<Boolean> deleteAgent(@RequestParam("id") Long id) {
        agentService.deleteAgent(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除代理商管理")
                @PreAuthorize("@ss.hasPermission('agent:info:delete')")
    public CommonResult<Boolean> deleteAgentList(@RequestParam("ids") List<Long> ids) {
        agentService.deleteAgentListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得代理商管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('agent:info:query')")
    public CommonResult<AgentRespVO> getAgent(@RequestParam("id") Long id) {
        AgentDO agent = agentService.getAgent(id);
        return success(BeanUtils.toBean(agent, AgentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得代理商管理分页")
    @PreAuthorize("@ss.hasPermission('agent:info:query')")
    public CommonResult<PageResult<AgentRespVO>> getAgentPage(@Valid AgentPageReqVO pageReqVO) {
        PageResult<AgentDO> pageResult = agentService.getAgentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AgentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出代理商管理 Excel")
    @PreAuthorize("@ss.hasPermission('agent:info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAgentExcel(@Valid AgentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AgentDO> list = agentService.getAgentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "代理商管理.xls", "数据", AgentRespVO.class,
                        BeanUtils.toBean(list, AgentRespVO.class));
    }

}