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 io.swagger.annotations.ApiParam;
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/detail/{id}")
    public AjaxResult bindDetail(@PathVariable("id") Long id) {
        return bindUserService.bindDetail(id);
    }

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

    @ApiOperation(value = "切换就诊人")
    @GetMapping(value = "/bind/change/{id}")
    public AjaxResult change(@ApiParam(value = "就诊人ID", required = true) @PathVariable("id") Long id) throws ServerException {
        return bindUserService.change(id);
    }

    @ApiOperation(value = "就诊人列表")
    @GetMapping("/bind/list")
    public AjaxResult list() {
        List<BindUser> list = bindUserService.selectList(new BindUser().setUserId(getUser().getId()));
        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();
    }

}

