package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.*;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.*;
import com.hfkj.service.sec.SecUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/secUser")
@Api(value = "登录")
public class LoginController {

    Logger log = LoggerFactory.getLogger(SecUserController.class);
    @Resource
    private SecUserService secUserService;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value="/login",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "登录")
    public ResponseData login(@RequestBody JSONObject body) {
        try {
            if (body == null
                    || StringUtils.isBlank(body.getString("loginName"))
                    || StringUtils.isBlank(body.getString("password"))
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            return ResponseMsgUtil.success(secUserService.login(body.getString("loginName"), body.getString("password")));

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/queryUser",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询账户")
    public ResponseData queryUser() {
        try {

            return ResponseMsgUtil.success(userCenter.getSessionModel(SecUserSessionObject.class));

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/loginOut",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "退出登录")
    public ResponseData loginOut(HttpServletRequest request) {
        try {
            try {
                SecUserSessionObject session = userCenter.getSessionModel(SecUserSessionObject.class);
                if (session != null) {
                    userCenter.remove(request);
                }
            } catch (Exception e) {}
            return ResponseMsgUtil.success("退出成功");

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }
}
