package com.kingdee.eas.hr.emp.web.handler;

import com.kingdee.bos.Context;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.hr.emp.WordTemplateResumeType;
import com.kingdee.eas.hr.emp.web.handler.wordtemplate.PrintResumeWordInfo;
import com.kingdee.eas.hr.emp.web.handler.wordtemplate.service.ResumeWordTemplateService;
import com.kingdee.eas.hr.emp.web.handler.wordtemplate.service.impl.ResumeWordTemplateServiceImplEx;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;

/**
 * 员工列表 Handler
 * 202605
 * 主要因为导出员工履历时，需要使用 ResumeWordTemplateServiceImplEx。`
 * coyle
 */
public class EmployeeListHandlerEx extends EmployeeListHandler {

    private static final Logger LOGGER = Logger.getLogger(EmployeeListHandlerEx.class);

    /** 履历 Word 服务，固定返回二开实现。 */
    protected ResumeWordTemplateService getResumeWordTemplateService() {
        return ResumeWordTemplateServiceImplEx.instance();
    }

    /**
     * 复制标品逻辑，唯一差异：通过 getResumeWordTemplateService() 使用 ResumeWordTemplateServiceImplEx。
     * 单人导出直接返回 doc 字节；多人导出 zip 包。
     */
    @Override
    protected byte[] generateWordResumeFileBytes(
            Context ctx,
            HttpServletRequest request,
            PrintResumeWordInfo printResumeWordInfo
    ) throws ShrWebBizException {
        String wordTemplateId = printResumeWordInfo.getWordTemplateInfo().getId().toString();
        Map<String, PersonInfo> personIdMap = printResumeWordInfo.getPersonIdMap();
        ResumeWordTemplateService service = getResumeWordTemplateService();

        if (WordTemplateResumeType.PERSON.equals(printResumeWordInfo.getType())
                && personIdMap != null && personIdMap.size() == 1) {
            for (Map.Entry<String, PersonInfo> entry : personIdMap.entrySet()) {
                return service.generateResumeWordDocument(
                        ctx, request, entry.getKey(), wordTemplateId, "001");
            }
            return null;
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ZipOutputStream zipOut = new ZipOutputStream(baos);
        try {
            zipOut.setEncoding("UTF-8");
            if (personIdMap != null) {
                for (Map.Entry<String, PersonInfo> entry : personIdMap.entrySet()) {
                    PersonInfo personInfo = entry.getValue();
                    String fileName = getPersonWordFileName(ctx, personInfo).toString();
                    zipOut.putNextEntry(new ZipEntry(fileName));
                    byte[] bytes = service.generateResumeWordDocument(
                            ctx, request, entry.getKey(), wordTemplateId, "001");
                    zipOut.write(bytes, 0, bytes.length);
                }
            }
            zipOut.closeEntry();
        } catch (IOException e) {
            e.printStackTrace();
            throw new ShrWebBizException(e.getMessage(), e);
        } finally {
            try {
                zipOut.close();
            } catch (IOException e) {
                LOGGER.warn("io close fail!");
            }
        }
        return baos.toByteArray();
    }
}
