package cn.hanghui.outapi.platform.common.controller;

import cn.hanghui.outapi.platform.common.annotation.DecryptBySm2;
import cn.hanghui.outapi.platform.common.annotation.DecryptParam;
import cn.hanghui.outapi.platform.common.entity.dto.LoginDto;
import cn.hanghui.outapi.platform.common.entity.dto.PcLoginCodeDto;
import cn.hanghui.outapi.platform.common.entity.dto.PcLoginDto;
import cn.hanghui.outapi.platform.common.entity.rest.RestResult;
import cn.hanghui.outapi.platform.common.entity.rest.Result;
import cn.hanghui.outapi.platform.common.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Api(tags = "登录")
@RestController
@AllArgsConstructor(onConstructor_ = @Autowired)
@Validated
public class LoginController {

    private final LoginService loginService;

    /**
     * 登录
     *
     * @param loginDto LoginDto
     * @return token
     */
    @DecryptBySm2()
    @ApiOperation("登录获取接口凭证")
    @PostMapping("login")
    public Result<String> login(@DecryptParam LoginDto loginDto) {
        return Result.ok(loginService.login(loginDto));
    }

    /**
     * PC登录
     *
     * @param loginDto LoginDto
     * @return token
     */
    @ApiOperation("PC登录获取接口凭证")
    @PostMapping("/pc/login")
    public RestResult<String> pcLogin(@RequestBody PcLoginDto loginDto) {
        LoginDto dto = new LoginDto();
        dto.setAccountName(loginDto.getUsername());
        dto.setPassword(loginDto.getPassword());
        dto.setCode(loginDto.getCode());
        dto.setPhone(loginDto.getPhone());
        return RestResult.ok(loginService.login(dto));
    }
    @ApiOperation("PC登录获取验证码")
    @PostMapping("/pc/getCode")
    public RestResult<Boolean> getCode(@RequestBody @Valid PcLoginCodeDto dto) {
        return RestResult.ok(loginService.getCode(dto));
    }
}
