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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.esign.*;
import com.kingdee.eas.custom.esign.bizEnum.SyncStatusEnum;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.shr.base.syssetting.BaseItemStateEnum;
import com.kingdee.shr.base.syssetting.api.bean.BatchMessageTipsBody;
import com.kingdee.shr.base.syssetting.api.bean.BatchMessageTipsHeader;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.web.handler.EditHandler;
import com.kingdee.util.StringUtils;
import org.springframework.ui.ModelMap;

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

/**
 * @Description 电子签模板表单Handler
 * @Date 2025/11/17 16:43
 * @Created by 禾源
 */
public class ESignTemplateEditHandler extends EditHandler {

    /**
     * 创建新的分录模型对象
     *
     * @param request  HTTP请求对象
     * @param response HTTP响应对象
     * @param modelMap 模型映射对象
     * @return CoreBaseInfo 返回创建的条目信息对象
     * @throws SHRWebException 当创建过程中发生错误时抛出异常
     */
    protected CoreBaseInfo createNewEntryModel(
            HttpServletRequest request,
            HttpServletResponse response,
            ModelMap modelMap
    ) throws SHRWebException {
        ESignTemplateFileEntryInfo entryInfo =
                (ESignTemplateFileEntryInfo) super.createNewEntryModel(request, response, modelMap);
        if (entryInfo == null) {
            entryInfo = new ESignTemplateFileEntryInfo();
        }
        //设置默认状态为启用
        entryInfo.setState(BaseItemStateEnum.ENABLE);
        entryInfo.setSyncStatus(SyncStatusEnum.UNSYNCED);
        return entryInfo;
    }


    /**
     * 创建新的入口模型通过服务器
     *
     * @return boolean 返回true表示成功创建新的入口模型，false表示创建失败
     */
    @Override
    protected boolean createNewEntryModelByServer() {
        return true;
    }

    /**
     * 同步模板字段
     *
     * @param request
     * @param response
     * @param modelMap
     * @throws SHRWebException
     */
    public void syncTemplateFieldsAction(
            HttpServletRequest request,
            HttpServletResponse response,
            ModelMap modelMap
    ) throws SHRWebException {
        Context ctx = this.getContext();
        String billId = this.getBillId(request);
        if (StringUtils.isEmpty(billId)) {
            throw new ShrWebBizException("单据id不能为空!");
        }
        try {
            ESignTemplateFactory.getLocalInstance(ctx).syncTemplateFields(billId);
        } catch (Exception e) {
            throw new ShrWebBizException(e);
        }


//        int sucess = 0;
//        int failure = 0;
//        BatchMessageTipsHeader batchMessageTipsHeader = new BatchMessageTipsHeader();
//        try {
//            String[] entryIdArr = entryIds.split(",");
//            IESignTemplateFileEntry iESignTemplateFileEntry = ESignTemplateFileEntryFactory.getLocalInstance(ctx);
//            for (int i = 0; i < entryIdArr.length; i++) {
//                BatchMessageTipsBody body = new BatchMessageTipsBody();
//                String entryId = entryIdArr[i];
//                body.setId(entryId);
//                try {
//                    iESignTemplateFileEntry.syncTemplateFields(entryId);
//                    iESignTemplateFileEntry.syncSuccess(entryId, null);
//                    ++sucess;
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    ++failure;
//                    iESignTemplateFileEntry.syncFail(entryId, e.getMessage());
//                    body.setMuitTipsState(Boolean.FALSE);
//                    body.setMuitTipsMessage(e.getMessage());
//                }
//                batchMessageTipsHeader.addResult(body);
//            }
//            batchMessageTipsHeader.setBillId(entryIds);
//            batchMessageTipsHeader.setFailureCount(failure);
//            batchMessageTipsHeader.setSuccessCount(sucess);
//            this.writeSuccessData(batchMessageTipsHeader);
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new ShrWebBizException(e.getMessage(), e);
//        }
    }
}
