package com.zd.common.security.utils;

import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.utils.SpringUtils;
import com.zd.common.core.web.domain.BaseBean;
import com.zd.common.core.web.domain.BaseEntity;
import com.zd.common.security.service.TokenService;
import com.zd.system.api.domain.SysUser;
import com.zd.system.api.model.LoginUser;

/**
 * @Author: zhoupan
 * @Date: 2021/09/17/9:33
 * @Description:
 */
public class SaveUtil {

    private SaveUtil() {
        throw new IllegalStateException("SaveUtil class");
    }

    public static void setCommonAttr(BaseEntity o) {
        //设置创建时间
        o.setCreateTime(DateUtils.getNowDate());
        //-2 为内部用户 不再处理
        if (Long.valueOf("-2").equals(o.getUserId())) {
            return;
        }

        //设置创建人
        o.setCreateBy(SecurityUtils.getUsername());
        o.setUserId(SecurityUtils.getUserId());
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser();
        if (null == loginUser) {
            return;
        }
        SysUser sysUser = loginUser.getSysUser();
        if (sysUser != null && sysUser.getDept() != null) {
            o.setDeptId(sysUser.getDept().getDeptId());
            o.setDeptName(sysUser.getDept().getDeptName());
        }
    }

    public static void setCommonAttr(BaseBean o) {
        //设置创建时间
        o.setCreateTime(DateUtils.getNowDate());
        //-2 为内部用户 不再处理
        if (Long.valueOf("-2").equals(o.getUserId())) {
            return;
        }

        //设置创建人
        o.setCreateBy(SecurityUtils.getUsername());
        o.setUserId(SecurityUtils.getUserId());
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser();
        if (null == loginUser) {
            return;
        }
        SysUser sysUser = loginUser.getSysUser();
        if (sysUser != null && sysUser.getDept() != null) {
            o.setDeptId(sysUser.getDept().getDeptId());
        }
    }
}
