package com.persagy.dmp.file.handler;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.persagy.dmp.common.context.AppContext;
import com.persagy.dmp.file.context.OldFileAppContext;
import com.persagy.dmp.file.context.OldFileContext;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OldFileContextHandler implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        OldFileContext context = OldFileAppContext.getContext();
        context.setSystemId(request.getParameter(OldFileContext.SYSTEM_ID));
        context.setSecret(request.getParameter(OldFileContext.SECRET));
        context.setKey(request.getParameter(OldFileContext.KEY));
        context.setWorkOrderId(request.getParameter(OldFileContext.WORK_ORDER_ID));
        context.setProjectId(request.getParameter(OldFileContext.PROJECT_ID));
        context.setOverwrite(BooleanUtil.toBoolean(request.getParameter(OldFileContext.OVERWRITE)));
        context.setUploadId(request.getParameter(OldFileContext.UPLOAD_ID));
        AppContext.getContext().setAccountId(request.getParameter(OldFileContext.SYSTEM_ID));
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) {
        OldFileAppContext.unloadContext();
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        OldFileAppContext.unloadContext();
    }


}
