package com.user.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.SessionObject;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.MD5Util;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.SecMenu;
import com.hfkj.entity.SecRoleMenuRel;
import com.hfkj.entity.SecUser;
import com.hfkj.model.MenuTreeModel;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.SecMenuService;
import com.hfkj.service.SecRoleMenuRelService;
import com.hfkj.service.SecUserService;
import com.hfkj.sysenum.SecUserStatusEnum;
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.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @className: secUser
 * @author: HuRui
 * @date: 2024/3/28
 **/
@Controller
@RequestMapping(value="/secUser")
@Api(value="系统用户")
public class SecUserController {

    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.POST)
    @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);
        }
    }



}
