package com.hzya.frame.sysnew.login.impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.sysnew.login.ILoginService;
import com.hzya.frame.sysnew.organ.dao.ISysOrganDao;
import com.hzya.frame.sysnew.organ.entity.SysOrganEntity;
import com.hzya.frame.sysnew.popedomInterface.entity.SysPopedomInterfaceEntity;
import com.hzya.frame.sysnew.popedomInterface.service.impl.InterfaceCache;
import com.hzya.frame.sysnew.sysInterface.entity.SysInterfaceEntity;
import com.hzya.frame.sysnew.user.dao.ISysUserDao;
import com.hzya.frame.sysnew.user.entity.SysUserEntity;
import com.hzya.frame.sysnew.userCompany.dao.ISysUserCompanyDao;
import com.hzya.frame.sysnew.userCompany.entity.SysUserCompanyEntity;
import com.hzya.frame.sysnew.userRoles.entity.SysUserRolesEntity;
import com.hzya.frame.util.AESUtil;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * @Description 登录
 * @Author xiangerlin
 * @Date 2022/12/20 16:09
 **/
@Service(value = "loginService")
public class LoginServiceImpl implements ILoginService {

    @Resource
    private ISysUserDao sysUserDao;
    @Resource
    private ISysOrganDao sysOrganDao;

    @Resource
    private InterfaceCache interfaceCache;
    /**
     * 登录
     *
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @Override
    public JsonResultEntity doLogin(JSONObject jsonObject) throws Exception {
        SysUserEntity entity = getData("jsonStr", jsonObject, SysUserEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("请输入用户名或密码");
        }
        if (entity.getPassword() == null || "".equals(entity.getPassword())) {
            return BaseResult.getFailureMessageEntity("请输入用户名或密码");
        }
        if (entity.getLoginCode() == null || "".equals(entity.getLoginCode())) {
            return BaseResult.getFailureMessageEntity("请输入用户名或密码");
        }
        entity.setPassword(AESUtil.encrypt(entity.getLoginCode() + "-" + entity.getPassword()));
        SysUserEntity sysUserEntity = sysUserDao.queryOne(entity);
        if (sysUserEntity == null || sysUserEntity.getId() == null || "".equals(sysUserEntity.getId())) {
            return BaseResult.getFailureMessageEntity("用户名或密码错误");
        }
        if (sysUserEntity.getState() == null || !"0".equals(sysUserEntity.getState())) {
            return BaseResult.getFailureMessageEntity("当前用户已停用，请先启用");
        }
        //校验当前登陆人是否有权限
        boolean flag = false;
        SysInterfaceEntity sysInterfaceEntity = (SysInterfaceEntity) interfaceCache.get("6","beanNameloginServiceinterfacNamedoLogin");
        if(sysInterfaceEntity == null || sysInterfaceEntity.getId() == null){
            return BaseResult.getFailureMessageEntity("用户无访问权限，请联系管理员");
        }
        //查询用户权限
        if(!flag){
            SysPopedomInterfaceEntity userPopedomInterfaceEntity = (SysPopedomInterfaceEntity) interfaceCache.get("4","userId"+sysUserEntity.getId()+"interfaceId"+sysInterfaceEntity.getId());
            if(userPopedomInterfaceEntity != null && userPopedomInterfaceEntity.getId() != null ){
                flag = true;
            }
        }
        //查询用户角色的权限
        if(!flag){
            List<SysUserRolesEntity> userRoleMap = (List<SysUserRolesEntity>) interfaceCache.get("3",null);
            if(userRoleMap != null && userRoleMap.size() > 0){
                for (SysUserRolesEntity sysUserRolesEntity : userRoleMap) {
                    if(sysUserRolesEntity.getUserId().equals(sysUserEntity.getId())){
                        SysPopedomInterfaceEntity sysPopedomInterfaceEntity = (SysPopedomInterfaceEntity) interfaceCache.get("5","roleId"+sysUserRolesEntity.getRoleId()+"interfaceId"+sysInterfaceEntity.getId());
                        if(sysPopedomInterfaceEntity != null && sysPopedomInterfaceEntity.getId() != null ){
                            flag = true;
                            break;
                        }
                    }
                }
            }
        }
        if(!flag){
            return BaseResult.getFailureMessageEntity("用户无访问权限，请联系管理员");
        }
        //登录
        StpUtil.login(sysUserEntity.getId());
        //获取token
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        String token = tokenInfo.getTokenValue();
        //获取公司
        SysOrganEntity sysOrganEntity = new SysOrganEntity();
        sysOrganEntity.setUserId(sysUserEntity.getId());
        List<SysOrganEntity> sysOrganEntities = sysOrganDao.queryUserCompany(sysOrganEntity);
        //返回值
        JSONObject res = new JSONObject();
        res.put("token", token);
        res.put("userInfo", sysUserEntity);
        res.put("company", sysOrganEntities);
        return BaseResult.getSuccessMessageEntity("登录成功", res);
    }

    protected <T> T getData(String key, JSONObject jsonObject, Class<T> clz) {
        if (checkStr(jsonObject.getString(key))) {
            return jsonObject.getJSONObject(key).toJavaObject(clz);
        }
        return null;
    }

    /**
     * @param str
     * @return void
     * @Author lvleigang
     * @Description 校验字符串
     * @Date 11:41 上午 2022/12/7
     **/
    protected Boolean checkStr(String str) {
        Boolean flag = true;
        if (str == null || "".equals(str)) {
            flag = false;
        }
        return flag;
    }
}
