package ${domain}.action.api.wsys;

import org.springframework.beans.factory.annotation.Autowired;
import ${domain}.frame.auth.LocalData;
import ${domain}.frame.base.ErrorType;
import ${domain}.frame.utils.MD5Util;
import ${domain}.frame.utils.RequestUtil;
import ${domain}.frame.utils.ValidationUtil;
import ${domain}.module.wsys.ent.User;
import ${domain}.module.wsys.mgr.TokensManager;
import ${domain}.module.wsys.mgr.UserManager;
import ${domain}.module.wsys.req.TokensCreateRequest;
import ${domain}.module.wsys.req.UserCreateRequest;
import ${domain}.module.wsys.req.UserDeleteRequest;
import ${domain}.module.wsys.req.UserFindRequest;
import ${domain}.module.wsys.req.UserGetRequest;
import ${domain}.module.wsys.req.UserLoginRequest;
import ${domain}.module.wsys.req.UserUpdateRequest;
import ${domain}.module.wsys.rsp.TokensCreateResponse;
import ${domain}.module.wsys.rsp.UserCreateResponse;
import ${domain}.module.wsys.rsp.UserDeleteResponse;
import ${domain}.module.wsys.rsp.UserFindResponse;
import ${domain}.module.wsys.rsp.UserGetResponse;
import ${domain}.module.wsys.rsp.UserLoginResponse;
import ${domain}.module.wsys.rsp.UserUpdateResponse;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

public class UserApi{

    @Autowired
    private TokensManager tokensManager;
    @Autowired
    private UserManager userManager;

    public UserLoginResponse login(UserLoginRequest request, HttpServletRequest httpServletRequest) {
        UserLoginResponse response = new UserLoginResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        UserFindRequest userFindRequest = new UserFindRequest();
        userFindRequest.setUserName(request.getUsername());
        UserFindResponse userFindResponse = userManager.find(userFindRequest, LocalData.getSysToken());

        if (userFindResponse.hasError()) {
            response.addErrors(userFindResponse.getErrors());
            return response;
        } else if (userFindResponse.getResult().size() == 0) {
            response.addError(ErrorType.BUSINESS_ERROR, "用户名或密码错误!");
            return response;
        } else if (userFindResponse.getResult().size() > 1) {
            response.addError(ErrorType.BUSINESS_ERROR, "用户信息异常!");
            return response;
        }

        User user = userFindResponse.getResult().get(0);

        String generatePwd = MD5Util.generatePwd(request.getPassword());
        if (!generatePwd.equals(user.getUserPwd())) {
            response.addError(ErrorType.BUSINESS_ERROR, "用户名或密码错误!");
            return response;
        }

        Date current = new Date();
        Calendar instance = Calendar.getInstance();
        instance.setTime(current);
        instance.add(Calendar.HOUR_OF_DAY, 1);//默认一个小时内有效

        TokensCreateRequest tokensCreateRequest = new TokensCreateRequest();
        tokensCreateRequest.setToken(UUID.randomUUID().toString());
        tokensCreateRequest.setUserId(user.getId());
        tokensCreateRequest.setUserName(user.getUserName());
        tokensCreateRequest.setLoginTime(current);
        tokensCreateRequest.setValidTime(instance.getTime());
        tokensCreateRequest.setValid(true);
        tokensCreateRequest.setDeptId(user.getDeptId());
        tokensCreateRequest.setDeptCode(user.getDeptCode());
        tokensCreateRequest.setDeptName(user.getDeptName());
        tokensCreateRequest.setTerminalType("WEB");
        tokensCreateRequest.setTerminalIp(RequestUtil.getIp(httpServletRequest));
        tokensCreateRequest.setTerminalInfo(RequestUtil.getUserAgent(httpServletRequest));
        TokensCreateResponse tokensCreateResponse = tokensManager.create(tokensCreateRequest, LocalData.getSysToken());

        if (tokensCreateResponse.hasError()) {
            response.addErrors(tokensCreateResponse.getErrors());
            return response;
        }
        response.setToken(tokensCreateRequest.getToken());
        return response;
    }
}
