package com.starry.admin.common.mybatis.handler;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.starry.admin.modules.weichat.service.WxTokenService;
import com.starry.admin.utils.SecurityUtils;
import com.starry.common.constant.Constants;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * @author admin 字段默认值处理类
 * @since 2021/9/1
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Resource
    private HttpServletRequest request;

    @Resource
    private WxTokenService tokenService;

    @Override
    public void insertFill(MetaObject metaObject) {

        this.setFieldValByName("createdTime", new Date(), metaObject);
        this.setFieldValByName("deleted", false, metaObject);
        this.setFieldValByName("version", 1L, metaObject);
        Object createUser = this.getFieldValByName("createdBy", metaObject);
        if (createUser == null) {
            if (SecurityUtils.isLogin()) {
                this.setFieldValByName("createdBy", getOperatorId(), metaObject);
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updatedTime", getDate(), metaObject);
        Object createUser = this.getFieldValByName("updatedBy", metaObject);
        if (createUser == null) {
            this.setFieldValByName("createdBy", getOperatorId(), metaObject);
        }
    }

    public Date getDate() {

        LocalDateTime localDateTime = LocalDateTime.now();
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        // return Date.from(localDateTime.toInstant(ZoneOffset.ofHours(8)));
    }

    public String getOperatorId() {
        try {
            if (request.getServletPath().startsWith("/wx/")) {
                String tenantKey = request.getHeader("tenantkey");
                if (StrUtil.isBlankIfStr(tenantKey)) {
                    String clerkToken = request.getHeader(Constants.CLERK_USER_LOGIN_TOKEN);
                    String customToken = request.getHeader(Constants.CUSTOM_USER_LOGIN_TOKEN);
                    if (clerkToken != null) {
                        return tokenService.getWxUserIdByToken(clerkToken);
                    }
                    if (customToken != null) {
                        return tokenService.getWxUserIdByToken(customToken);
                    }
                }
                return "";
            } else {
                if (SecurityUtils.isLogin()) {
                    return SecurityUtils.getUserId();
                }
            }
        } catch (Exception ignored) {
//            if (SecurityUtils.isLogin()) {
//                return SecurityUtils.getUserId();
//            }
        }
        return "";
    }
}
