package com.ruoyi.web.work.api;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.work.api.config.BaseController;
import com.ruoyi.web.work.domain.BindUser;
import com.ruoyi.web.work.domain.dto.BindDto;
import com.ruoyi.web.work.domain.dto.LoginDto;
import com.ruoyi.web.work.domain.dto.UserEditDto;
import com.ruoyi.web.work.service.IBindUserService;
import com.ruoyi.web.work.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.rmi.ServerException;
import java.util.List;

@RestController
@RequestMapping("/app/user")
@Api(tags = "用户中心")
public class Api_UserController extends BaseController {

    @Autowired
    IUserService userService;

    @Autowired
    IBindUserService bindUserService;

    @ApiOperation(value = "微信登录")
    @PostMapping("/login")
    public AjaxResult login(@Validated @RequestBody LoginDto dto) throws ServerException {
        return userService.login(dto);
    }

    @PostMapping("/bind")
    public AjaxResult bind(@Validated @RequestBody BindDto dto) throws ServerException {
        return bindUserService.bind(dto);
    }

    @GetMapping(value = "/bind/remove/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) throws ServerException {
        return bindUserService.remove(id);
    }

    @GetMapping(value = "/bind/change/{id}")
    public AjaxResult change(@PathVariable("id") Long id) throws ServerException {
        return bindUserService.change(id);
    }

    @GetMapping("/bind/list")
    public AjaxResult list(BindUser bindUser) {
        bindUser.setUserId(getUser().getId());
        List<BindUser> list = bindUserService.selectList(bindUser);
        return AjaxResult.success(list);
    }

    @GetMapping("/info")
    public AjaxResult info() {
        return userService.info();
    }

    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody UserEditDto dto) {
        return userService.edit(dto);
    }

    @GetMapping("/exit")
    public AjaxResult exit() {
        return userService.exit();
    }

}

