package cn.iocoder.yudao.module.portal.controller.app.portaluser;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.portal.controller.admin.portaluser.vo.PortalUserPageReqVO;
import cn.iocoder.yudao.module.portal.controller.admin.portaluser.vo.PortalUserRespVO;
import cn.iocoder.yudao.module.portal.controller.admin.portaluser.vo.PortalUserSaveReqVO;
import cn.iocoder.yudao.module.portal.dal.dataobject.portaluser.PortalUserDO;
import cn.iocoder.yudao.module.portal.service.user.PortalUserService;
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.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 cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 用户")
@RestController
@RequestMapping("/portal/user")
@Validated
public class AppPortalUserController {

    @Resource
    private PortalUserService userService;

    @PostMapping("/create")
    @Operation(summary = "创建用户")
    public CommonResult<Long> createUser(@Valid @RequestBody PortalUserSaveReqVO createReqVO) {
        return success(userService.createUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody PortalUserSaveReqVO updateReqVO) {
        userService.updateUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteUser(@RequestParam("id") Long id) {
        userService.deleteUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<PortalUserRespVO> getUser(@RequestParam("id") Long id) {
        PortalUserDO user = userService.getUser(id);
        return success(BeanUtils.toBean(user, PortalUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户分页")
    public CommonResult<PageResult<PortalUserRespVO>> getUserPage(@Valid PortalUserPageReqVO pageReqVO) {
        PageResult<PortalUserDO> pageResult = userService.getUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PortalUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserExcel(@Valid PortalUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PortalUserDO> list = userService.getUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户.xls", "数据", PortalUserRespVO.class,
                        BeanUtils.toBean(list, PortalUserRespVO.class));
    }

}