package com.pj.home;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.stp.StpUtil;
import com.pj.current.satoken.StpUserUtil;
import com.pj.project.sys_client_visit.SysClientVisit;
import com.pj.project.sys_client_visit.SysClientVisitMapper;
import com.pj.project.sys_user.SysUserMapper;
import com.pj.project.sys_user_acc.SysUserAccUtil;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * SSO 平台中心模式示例，跳连接进入子系统
 */
@RestController
public class HomeController {
    @Autowired
    SysClientVisitMapper sysClientVisitMapper;
    // 平台化首页
    @RequestMapping("/home")
    public Object index() {
        // 如果未登录，则先去登录
        if(!StpUserUtil.isLogin()) {
            return SaHolder.getResponse().redirect("/sso/auth");
        }
        ModelAndView modelAndView = new ModelAndView("home.html");
        SoMap so = SoMap.getRequestSoMap();
        so.set("userId",StpUserUtil.getLoginId());
        so.set("pageSize",1000);
        List<SysClientVisit> list = sysClientVisitMapper.getByUserId(so.startPage());
        modelAndView.addObject("userInfo",SysUserAccUtil.getUserById(Long.valueOf(StpUserUtil.getLoginId().toString())));
        modelAndView.addObject("dataSource",list);
        return modelAndView;
    }
}
