package com.kingdee.eas.custom.synctask.handler;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.base.permission.UserInfo;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.shr.ats.web.handler.AtsTripBillEditHandler;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.web.dynamic.util.DynamicUtil;
import com.kingdee.shr.base.syssetting.web.dynamic.util.MD5;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
import com.kingdee.shr.base.syssetting.web.util.UserUtil;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.text.MessageFormat;
import java.util.*;

/**
 * @author qingwu
 * @date 2024/11/13
 * @apiNote
 * 员工自助我要出差
 */
public class AtsTripBillEditHandlerEx extends AtsTripBillEditHandler {
    Logger logger = Logger.getLogger(AtsTripBillEditHandlerEx.class);


    /**
     * @param request
     * @param response
     * @param modelMap
     * @throws BOSException
     * @throws ShrWebBizException
     */
    public void evectionTemplateAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ShrWebBizException {
        logger.error("evectionTemplateAction---");
        try {
            Context ctx = SHRContext.getInstance().getContext();//上下文

            HttpSession session = request.getSession();
            String tempFilePath = UserUtil.getUserTempDirAbsolutePath(session); //存放
            File tempFile = new File(tempFilePath);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }
            String path = System.getProperty("EAS_HOME") + "/server//lib/addon/customer/template/evectionTemplate.xlsx";
            FileInputStream fileInputStream = new FileInputStream(path);
            String zipName =   "outA.xlsx";
            String zipNameHash = MD5.md5Hash(zipName);
            String realFileName = getEncryRealFileName(ctx,zipNameHash);
            String fileStr = zipNameHash;
            File outFile = new File(tempFilePath + "/"+realFileName);
            FileOutputStream fileOutputStream = new FileOutputStream(outFile);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = fileInputStream.read(buffer)) > 0) {
                fileOutputStream.write(buffer, 0, length);
            }
            Map<String, String> map = new HashMap<>();
            Map<String, String> param = new HashMap<String, String>();
            param.put("method", "tmp");
            param.put("file", zipNameHash);
            //param.put("filename", fileStr+".xlsx");
            param.put("filename","出差行程表.xlsx");
            //String link = DynamicUtil.assembleUrl("/shr/downloadfile.do", param).replaceAll("\\+", "%20");
            String link = DynamicUtil.assembleUrl("/shr/downloadfile.do", param);
            map.put("msgType", "1");
            map.put("link", link);
            this.writeSuccessData(map);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ShrWebBizException(e.getMessage());
        }
    }
    public static String getEncryRealFileName(Context ctx, String realFileName) {
        UserInfo currentUserInfo = ContextUtil.getCurrentUserInfo(ctx);
        return MessageFormat.format("{0}_{1}", realFileName, MD5.md5Hash(currentUserInfo.getId().toString()));
    }
}
