package com.njyunzhi.pet_identity.common;

import cn.dev33.satoken.stp.StpUtil;
import com.njyunzhi.pet_identity.entity.SysUser;
import com.njyunzhi.pet_identity.entity.TaPerson;
import com.njyunzhi.pet_identity.service.ISysUserService;
import com.njyunzhi.pet_identity.service.ITaPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BaseController {

    @Autowired
    public ISysUserService iSysUserService;

    @Autowired
    public ITaPersonService iTaPersonService;

    public SysUser currentUser() throws Exception {
        String loginId = StpUtil.getLoginIdAsString();
        if (StringUtils.isEmpty(loginId)) {
            throw new Exception("请先登录");
        }

        SysUser user = iSysUserService.getById(loginId);
        if (user == null || user.getStatus() == Constants.STATUS_DELETE) {
            throw new Exception("人员不存在");
        }

        return user;
    }

    public TaPerson currentPerson() throws Exception {
        String loginId = StpUtil.getLoginIdAsString();
        if (StringUtils.isEmpty(loginId)) {
            throw new Exception("请先登录");
        }

        TaPerson user = iTaPersonService.getById(loginId);
        if (user == null || user.getStatus() == Constants.STATUS_DELETE) {
            throw new Exception("人员不存在");
        }

        return user;
    }

    public boolean checkRole(String srcRole, String targRole) {
        return Constants.USER_ADMIN.equals(srcRole) || Constants.USER_MANAGER.equals(srcRole) || srcRole.equals(targRole);
    }
}
