package com.qryord.mp.controller;

import com.qryord.common.exception.user.*;
import com.qryord.framework.shiro.service.LoginService;
import com.qryord.framework.web.domain.AjaxResult;
import com.qryord.mp.common.BaseApiController;
import com.qryord.mp.common.RespResultModel;
import com.qryord.mp.model.UserReq;
import com.qryord.mp.model.UserResp;
import com.qryord.mp.service.SysUserServiceI;
import com.qryord.project.system.config.service.IConfigService;
import com.qryord.project.system.role.domain.Role;
import com.qryord.project.system.role.service.IRoleService;
import com.qryord.project.system.user.domain.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api("用户相关使用")
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/api/app")
public class UserApiController extends BaseApiController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private LoginService loginService;

    @ApiOperation(value = "小程序登录，loginName,password必须")
    @PostMapping("/mplogin")
    public RespResultModel mplogin(@RequestBody UserReq userReq) {

        User user = null;
        try
        {
            user = loginService.login(userReq.getLoginName(), userReq.getPassword());
        }
        catch (CaptchaException e)
        {
            throw new AuthenticationException(e.getMessage(), e);
        }
        catch (UserNotExistsException e)
        {
            throw new UnknownAccountException(e.getMessage(), e);
        }
        catch (UserPasswordNotMatchException e)
        {
            throw new IncorrectCredentialsException(e.getMessage(), e);
        }
        catch (UserPasswordRetryLimitExceedException e)
        {
            throw new ExcessiveAttemptsException(e.getMessage(), e);
        }
        catch (UserBlockedException e)
        {
            throw new LockedAccountException(e.getMessage(), e);
        }
        catch (RoleBlockedException e)
        {
            throw new LockedAccountException(e.getMessage(), e);
        }
        catch (Exception e)
        {
            log.info("对用户[" + userReq.getLoginName() + "]进行登录验证..验证未通过{}", e.getMessage());
            throw new AuthenticationException(e.getMessage(), e);
        }

        UserResp userResp = new UserResp();

        userResp.setUserId(user.getUserId());
        userResp.setUserName(user.getUserName());
        userResp.setPhonenumber(user.getPhonenumber());
        userResp.setEmail(user.getEmail());

        if (user.getRoles() != null && user.getRoles().size()>0) {
            Role role = roleService.selectRoleById(user.getRoles().get(0).getRoleId());

            userResp.setRoleKey(role.getRoleKey());
            userResp.setRoleName(role.getRoleName());
        } else {
            log.info("用户[" + userReq.getLoginName() + "]未设置角色。");
            throw new AuthenticationException("用户[" + userReq.getLoginName() + "]未设置角色。");
        }

        return new RespResultModel(userResp);
    }

}
