package ${basePackage}.action.api.system;

import org.springframework.beans.factory.annotation.Autowired;
import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.base.ErrorType;
import ${basePackage}.frame.utils.MD5Util;
import ${basePackage}.frame.utils.RequestUtil;
import ${basePackage}.frame.utils.ValidationUtil;
import ${basePackage}.module.system.ent.User;
import ${basePackage}.module.system.mgr.TokensManager;
import ${basePackage}.module.system.mgr.UserManager;
import ${basePackage}.module.system.req.TokensCreateRequest;
import ${basePackage}.module.system.req.UserCreateRequest;
import ${basePackage}.module.system.req.UserDeleteRequest;
import ${basePackage}.module.system.req.UserFindRequest;
import ${basePackage}.module.system.req.UserGetRequest;
import ${basePackage}.module.system.req.UserLoginRequest;
import ${basePackage}.module.system.req.UserUpdateRequest;
import ${basePackage}.module.system.rsp.TokensCreateResponse;
import ${basePackage}.module.system.rsp.UserCreateResponse;
import ${basePackage}.module.system.rsp.UserDeleteResponse;
import ${basePackage}.module.system.rsp.UserFindResponse;
import ${basePackage}.module.system.rsp.UserGetResponse;
import ${basePackage}.module.system.rsp.UserLoginResponse;
import ${basePackage}.module.system.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 UserCreateResponse create(UserCreateRequest request) {
        return userManager.create(request, LocalData.getToken());
    }

    public UserDeleteResponse delete(UserDeleteRequest request) {
        return userManager.delete(request, LocalData.getToken());
    }

    public UserUpdateResponse update(UserUpdateRequest request) {
        return userManager.update(request, LocalData.getToken());
    }

    public UserFindResponse find(UserFindRequest request) {
        return userManager.find(request, LocalData.getToken());
    }

    public UserGetResponse get(UserGetRequest request) {
        return userManager.get(request, LocalData.getToken());
    }

    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;
    }
}
