package com.fufan.mtsaas.modules.sysmgr.hrsys.controller;

import com.alibaba.fastjson.JSONObject;
import com.fufan.mtsaas.modules.common.bo.request.*;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.ResStatus;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.modules.sysmgr.hrsys.bo.request.LoginRequest;
import com.fufan.mtsaas.modules.sysmgr.hrsys.service.HRLoginUserSvc;
import com.fufan.mtsaas.modules.common.bo.request.DataRequest;
import com.fufan.mtsaas.modules.common.bo.request.ResultPack;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.ResStatus;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.modules.sysmgr.hrsys.bo.request.LoginRequest;
import com.fufan.mtsaas.modules.sysmgr.hrsys.bo.request.LogoutRequest;
import com.fufan.mtsaas.modules.sysmgr.hrsys.service.HRLoginUserSvc;
import com.fufan.mtsaas.util.JSonUtil;
import com.fufan.mtsaas.util.ParamUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

/**
 * 登录登出控制类
 *
 * @author Liby
 * @create 2019-09-15
 * */
@Slf4j
@Api(tags = "MTSaas：登录管理", description = "对象接口")
@RestController
@RequestMapping("/SYS/HRAILOGIN")
public class HRLoginCtrl {

    @Autowired
    private HRLoginUserSvc loginUserSvc;

    /**
     * 登录
     * */
    @RequestMapping(value = "/LOGIN", method = RequestMethod.POST)
    public Result doLogin(@RequestBody DataRequestT<LoginRequest> dataRequest) {
        ResultPack resultPack = new ResultPack();
        Result result = new Result();
        RequestPojo requestPojo = ParamUtil.doChange();
        LoginRequest map = dataRequest.getData().getParam();

        try{
            result = loginUserSvc.loginCheckByUser(map);
        }catch (Exception e){
            log.error("数据出错" + map);
            log.error("数据出错：",e);
            result.setResStatus("E0000");
            result.setResMsg("请求参数不合法！" + e.getMessage());
        }
        return resultPack.doResultPack(result, requestPojo.getIntfId(), requestPojo.getAppKey());
    }

    /**
     * 登出
     * */
    @RequestMapping(value = "/LOGOUT", method = RequestMethod.POST)
    public Result loginOut(@ApiIgnore HttpSession session) {
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();
        Result result = new Result();
        loginUserSvc.loginOut(session,requestPojo);
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        return resultPack.doResultPack(result,requestPojo.getIntfId(),requestPojo.getAppKey());
    }

    /**
     * 微信上登录
     * */
//    @RequestMapping(value = "/doWechatLogin", method = RequestMethod.POST)
    public Result doWechatLogin(@RequestBody String map, @ApiIgnore HttpSession session) {
        ResultPack resultPack = new ResultPack();
        Result result = new Result();
        RequestPojo requestPojo = ParamUtil.doChange();

        try{
            result = loginUserSvc.doWechatLogin(map, requestPojo);
        }catch (Exception e){
            log.error("数据出错" + map);
            log.error("数据出错：",e);
            result.setResStatus("E0000");
            result.setResMsg("请求参数不合法！" + e.getMessage());
        }
        return resultPack.doResultPack(result, requestPojo.getIntfId(), requestPojo.getAppKey());
    }

}
