package com.wgb.system.service.controller;

import com.data.model.restful.response.InternalResponse;
import com.wgb.system.service.domain.model.User;
import com.wgb.system.service.domain.request.LoginReqest;
import com.wgb.system.service.domain.request.SysLogin;
import com.wgb.system.service.domain.request.UserReuqest;
import com.wgb.system.service.service.AccessService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/access")
public class AccessController {

    private final AccessService accessService;
    public AccessController(AccessService accessService) {
        this.accessService = accessService;
    }

    @RequestMapping(value = "/sysLogin",method = RequestMethod.POST)
    @ResponseBody
    public InternalResponse sysLogin(@Valid @RequestBody SysLogin reqest)throws Exception{
        accessService.getSysLogin(reqest.getUsername(),reqest.getPassword());
        return InternalResponse.success();
    }

    @ApiOperation(value = "/登录", response = User.class)
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public InternalResponse login(@Valid @RequestBody LoginReqest reqest)throws Exception{
        accessService.getLogin(reqest);
        return InternalResponse.success();
    }


    @ApiOperation(value = "/注册", response = User.class)
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @ResponseBody
    public InternalResponse register(@RequestBody UserReuqest reuqest)throws Exception{
        log.info("Start to execute /access/register with, param={}", reuqest);
        User resultUser= accessService.register(reuqest);
        log.info("Return object for /businessCategory/saveOrUpdate is result={}", resultUser);
        return InternalResponse.success().withBody(resultUser);
    }

}
