package com.yc.ship.module.trade.controller.admin.audituser;

import com.yc.ship.framework.apilog.core.annotation.ApiAccessLog;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.module.trade.controller.admin.audituser.vo.AuditUserPageReqVO;
import com.yc.ship.module.trade.controller.admin.audituser.vo.AuditUserRespVO;
import com.yc.ship.module.trade.controller.admin.audituser.vo.AuditUserSaveReqVO;
import com.yc.ship.module.trade.dal.dataobject.audituser.AuditUserDO;
import com.yc.ship.module.trade.service.audituser.AuditUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.yc.ship.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.yc.ship.framework.common.pojo.CommonResult.success;

/**
 * @author qsl
 */
@Tag(name = "管理后台 - 审核人管理")
@RestController
@RequestMapping("/trade/audit-user")
@Validated
public class AuditUserController {

    @Resource
    private AuditUserService auditUserService;

    @PostMapping("/create")
    @Operation(summary = "创建审核人管理")
    @PreAuthorize("@ss.hasPermission('trade:audit-user:create')")
    public CommonResult<Long> createAuditUser(@Valid @RequestBody AuditUserSaveReqVO createReqVO) {
        return success(auditUserService.createAuditUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新审核人管理")
    @PreAuthorize("@ss.hasPermission('trade:audit-user:update')")
    public CommonResult<Boolean> updateAuditUser(@Valid @RequestBody AuditUserSaveReqVO updateReqVO) {
        auditUserService.updateAuditUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除审核人管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:audit-user:delete')")
    public CommonResult<Boolean> deleteAuditUser(@RequestParam("id") Long id) {
        auditUserService.deleteAuditUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得审核人管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:audit-user:query')")
    public CommonResult<AuditUserRespVO> getAuditUser(@RequestParam("id") Long id) {
        AuditUserDO auditUser = auditUserService.getAuditUser(id);
        return success(BeanUtils.toBean(auditUser, AuditUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得审核人管理分页")
    @PreAuthorize("@ss.hasPermission('trade:audit-user:query')")
    public CommonResult<PageResult<AuditUserRespVO>> getAuditUserPage(@Valid AuditUserPageReqVO pageReqVO) {
        PageResult<AuditUserDO> pageResult = auditUserService.getAuditUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AuditUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出审核人管理 Excel")
    @PreAuthorize("@ss.hasPermission('trade:audit-user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAuditUserExcel(@Valid AuditUserPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AuditUserDO> list = auditUserService.getAuditUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "审核人管理.xls", "数据", AuditUserRespVO.class,
                BeanUtils.toBean(list, AuditUserRespVO.class));
    }

}