package cn.service.util;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;
import cn.constant.CommonConst;
import cn.orm.sqltoy.model.DataAuthFilterConfig;
import cn.orm.sqltoy.model.IgnoreCaseSet;
import cn.orm.sqltoy.model.IgnoreKeyCaseMap;
import cn.orm.sqltoy.plugins.IUnifyFieldsHandler;
import cn.pojo.vo.LoginUser;
import cn.util.core.IsKit;
import cn.util.core.TypeKit;
import cn.util.core.ServletKit;
import cn.util.time.TimeKit;

import javax.servlet.http.HttpServletRequest;

@Component
public class UnifyFieldsUtil implements IUnifyFieldsHandler {
    @Override
    public Map<String, Object> createUnifyFields() {
        Map<String, Object> fieldMap = new HashMap<>();
        //创建时间
        fieldMap.put("createTime", TimeKit.getNow());
        //创建者
        fieldMap.put("createBy", getUserName());
        return fieldMap;
    }

    @Override
    public Map<String, Object> updateUnifyFields() {
        Map<String, Object> fieldMap = new HashMap<>();
        //更新时间
        fieldMap.put("updateTime", TimeKit.getNow());
        //更新者
        fieldMap.put("updateBy", getUserName());
        return fieldMap;
    }

    @Override
    public IgnoreCaseSet forceUpdateFields() {
        IgnoreCaseSet forceUpdateFields = new IgnoreCaseSet();
        //强制赋值
        forceUpdateFields.add("updateTime");
        return forceUpdateFields;
    }

    @Override
    public IgnoreKeyCaseMap<String, DataAuthFilterConfig> dataAuthFilters() {
        return null;
    }

    /***以下方法都是获取当前登录用户的一些信息**/
    //执行SQL语句时
    public static void setCreateField(Map<String, Object> mapRecord) {
        mapRecord.put("createTime", TimeKit.getNow());
        mapRecord.put("createBy", getUserName());
    }

    public static void setUpdateField(Map<String, Object> mapRecord) {
        mapRecord.put("updateTime", TimeKit.getNow());
        mapRecord.put("updateBy", getUserName());
    }

    //登录账号
    public static String getUserName() {
        if (IsKit.isNotNull(getUserMap())) {
            return TypeKit.objToStr(getUserMap().get("userName"));
        }
        return null;
    }

    public static Long getUserId() {
        if (IsKit.isNotNull(getUserMap())) {
            return TypeKit.objToLong(getUserMap().get("userId"));
        }
        return -1l;
    }

    public static String getUserId_s() {
        if (IsKit.isNotNull(getUserMap())) {
            return TypeKit.objToStr(getUserMap().get("userId"));
        }
        return "";
    }

    public static LoginUser getUserEntity() {
        HttpServletRequest req = ServletKit.getRequest();
        if (IsKit.isNull(req)) {
            return null;
        }
        Object obj = req.getAttribute(CommonConst.CURRENT_USER);
        //已登陆
        if (IsKit.isNotNullOrEmpty(obj)) {
            return (LoginUser) obj;
        }
        return null;
    }

    public static Map<String, Object> getUserMap() {
        HttpServletRequest req = ServletKit.getRequest();
        if (IsKit.isNull(req)) {
            return null;
        }
        Object obj = req.getAttribute(CommonConst.CURRENT_USER);
        //已登陆
        if (IsKit.isNotNullOrEmpty(obj)) {
            LoginUser loginUser = (LoginUser) obj;
            return loginUser.getUserMap();
        } else {
            return null;
        }
    }
}
