package com.jojubanking.boot.module.system.controller.admin.usersxq;

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.annotations.*;

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

import com.jojubanking.boot.framework.common.pojo.PageResult;
import com.jojubanking.boot.framework.common.pojo.CommonResult;
import static com.jojubanking.boot.framework.common.pojo.CommonResult.success;

import com.jojubanking.boot.framework.excel.core.util.ExcelUtils;

import com.jojubanking.boot.framework.operatelog.core.annotations.OperateLog;
import static com.jojubanking.boot.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.jojubanking.boot.module.system.controller.admin.usersxq.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.usersxq.UsersXqDO;
import com.jojubanking.boot.module.system.convert.usersxq.UsersXqConvert;
import com.jojubanking.boot.module.system.service.usersxq.UsersXqService;

@Api(tags = "管理后台 - 用户信息")
@RestController
@RequestMapping("/system/users-xq")
@Validated
public class UsersXqController {

    @Resource
    private UsersXqService usersXqService;

    @PostMapping("/create")
    @ApiOperation("创建用户信息")
    @PreAuthorize("@ss.hasPermission('system:users-xq:create')")
    public CommonResult<Long> createUsersXq(@Valid @RequestBody UsersXqCreateReqVO createReqVO) {
        return success(usersXqService.createUsersXq(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户信息")
    @PreAuthorize("@ss.hasPermission('system:users-xq:update')")
    public CommonResult<Boolean> updateUsersXq(@Valid @RequestBody UsersXqUpdateReqVO updateReqVO) {
        usersXqService.updateUsersXq(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:users-xq:delete')")
    public CommonResult<Boolean> deleteUsersXq(@RequestParam("id") Long id) {
        usersXqService.deleteUsersXq(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:users-xq:query')")
    public CommonResult<UsersXqRespVO> getUsersXq(@RequestParam("id") Long id) {
        UsersXqDO usersXq = usersXqService.getUsersXq(id);
        return success(UsersXqConvert.INSTANCE.convert(usersXq));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:users-xq:query')")
    public CommonResult<List<UsersXqRespVO>> getUsersXqList(@RequestParam("ids") Collection<Long> ids) {
        List<UsersXqDO> list = usersXqService.getUsersXqList(ids);
        return success(UsersXqConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户信息分页")
    @PreAuthorize("@ss.hasPermission('system:users-xq:query')")
    public CommonResult<PageResult<UsersXqRespVO>> getUsersXqPage(@Valid UsersXqPageReqVO pageVO) {
        PageResult<UsersXqDO> pageResult = usersXqService.getUsersXqPage(pageVO);
        return success(UsersXqConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:users-xq:export')")
    @OperateLog(type = EXPORT)
    public void exportUsersXqExcel(@Valid UsersXqExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UsersXqDO> list = usersXqService.getUsersXqList(exportReqVO);
        // 导出 Excel
        List<UsersXqExcelVO> datas = UsersXqConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户信息.xls", "数据", UsersXqExcelVO.class, datas);
    }

}
