package com.jojubanking.boot.module.system.controller.app.userinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

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.app.userinfo.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.userinfo.UserInfoDO;
import com.jojubanking.boot.module.system.convert.userinfo.UserInfoConvert;
import com.jojubanking.boot.module.system.service.userinfo.UserInfoService;

@Api(tags = "用户 APP - 用户信息")
@RestController
@RequestMapping("/member/user-info")
@Validated
public class AppUserInfoController {

    @Resource
    private UserInfoService userInfoService;

    @PostMapping("/test")
    @ApiOperation("测试")
    public CommonResult<Boolean> test() {
        return success(true);
    }

    @PostMapping("/create")
    @ApiOperation("创建用户信息")

    public CommonResult<Long> createUserInfo(@Valid @RequestBody AppUserInfoCreateReqVO createReqVO) {
        return success(userInfoService.createUserInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户信息")

    public CommonResult<Boolean> updateUserInfo(@Valid @RequestBody AppUserInfoUpdateReqVO updateReqVO) {
        userInfoService.updateUserInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)

    public CommonResult<Boolean> deleteUserInfo(@RequestParam("id") Long id) {
        userInfoService.deleteUserInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)

    public CommonResult<AppUserInfoRespVO> getUserInfo(@RequestParam("id") Long id) {
        UserInfoDO userInfo = userInfoService.getUserInfo(id);
        return success(UserInfoConvert.INSTANCE.convert(userInfo));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)

    public CommonResult<List<AppUserInfoRespVO>> getUserInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<UserInfoDO> list = userInfoService.getUserInfoList(ids);
        return success(UserInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户信息分页")

    public CommonResult<PageResult<AppUserInfoRespVO>> getUserInfoPage(@Valid AppUserInfoPageReqVO pageVO) {
        PageResult<UserInfoDO> pageResult = userInfoService.getUserInfoPage(pageVO);
        return success(UserInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户信息 Excel")

    @OperateLog(type = EXPORT)
    public void exportUserInfoExcel(@Valid AppUserInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserInfoDO> list = userInfoService.getUserInfoList(exportReqVO);
        // 导出 Excel
        List<AppUserInfoExcelVO> datas = UserInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户信息.xls", "数据", AppUserInfoExcelVO.class, datas);
    }

}
