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

import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.CanNotDeleteException;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.*;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.custom.esign.*;
import com.kingdee.eas.custom.esign.bizEnum.SyncStatusEnum;
import com.kingdee.shr.base.syssetting.BaseItemStateEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.kingdee.bos.*;
import com.kingdee.eas.common.EASBizException;

import java.lang.String;
import java.sql.Time;

/**
 * 电子签文件模板
 */
public class ESignTemplateFileEntryControllerBean extends AbstractESignTemplateFileEntryControllerBean {
    private static Logger logger = Logger.getLogger(ESignTemplateFileEntryControllerBean.class);

    /**
     * 同步成功
     *
     * @param ctx
     * @param entryId
     * @param syncResult
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected void _syncSuccess(Context ctx, String entryId, String syncResult) throws BOSException, EASBizException {
        super._syncSuccess(ctx, entryId, syncResult);
        if (StringUtils.isBlank(entryId)) {
            throw new EASBizException(EASBizException.CHECKBLANK, new Object[]{"单据id不能为空!"});
        }
        if (StringUtils.isBlank(syncResult)) {
            syncResult = "同步成功";
        }
        ESignTemplateFileEntryInfo eSignTemplateFileEntryInfo = getESignTemplateFileEntryInfo(ctx, new ObjectUuidPK(entryId));
        eSignTemplateFileEntryInfo.setSyncStatus(SyncStatusEnum.SYNCED);
        eSignTemplateFileEntryInfo.setSyncResult(syncResult);
        eSignTemplateFileEntryInfo.setSyncTime(new Time(System.currentTimeMillis()));
        SelectorItemCollection sic = new SelectorItemCollection();
        sic.add("syncStatus");
        sic.add("syncResult");
        sic.add("syncTime");
        updatePartial(ctx, eSignTemplateFileEntryInfo, sic);
    }

    /**
     * 同步失败
     *
     * @param ctx
     * @param entryId
     * @param syncResult
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected void _syncFail(Context ctx, String entryId, String syncResult) throws BOSException, EASBizException {
        super._syncFail(ctx, entryId, syncResult);
        if (StringUtils.isBlank(entryId)) {
            throw new EASBizException(EASBizException.CHECKBLANK, new Object[]{"单据id不能为空!"});
        }
        if (StringUtils.isBlank(syncResult)) {
            syncResult = "同步失败";
        }
        ESignTemplateFileEntryInfo eSignTemplateFileEntryInfo = getESignTemplateFileEntryInfo(ctx, new ObjectUuidPK(entryId));
        eSignTemplateFileEntryInfo.setSyncStatus(SyncStatusEnum.SYNC_FAILED);
        eSignTemplateFileEntryInfo.setSyncResult(syncResult);
        eSignTemplateFileEntryInfo.setSyncTime(new Time(System.currentTimeMillis()));
        SelectorItemCollection sic = new SelectorItemCollection();
        sic.add("syncStatus");
        sic.add("syncResult");
        sic.add("syncTime");
        updatePartial(ctx, eSignTemplateFileEntryInfo, sic);
    }

    /**
     * 同步模板字段
     *
     * @param ctx
     * @param entryId
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected void _syncTemplateFields(Context ctx, String entryId)
            throws BOSException, EASBizException {
        super._syncTemplateFields(ctx, entryId);
    }

    /**
     * 删除电子签名模板数据
     *
     * @param ctx 系统上下文对象
     * @param pk  要删除数据的主键
     * @throws BOSException    业务操作异常
     * @throws EASBizException EAS业务异常
     */
    @Override
    protected void _delete(Context ctx, IObjectPK pk) throws BOSException, EASBizException {
        // 获取电子签名模板文件信息并检查状态
        ESignTemplateFileEntryInfo info = getESignTemplateFileEntryInfo(ctx, pk);
        BaseItemStateEnum state = info.getState();
        if (BaseItemStateEnum.ENABLE.equals(state)) {
            throw new CanNotDeleteException("启用的数据，不允许删除！");
        }
        super._delete(ctx, pk);
    }

    /**
     * @param ctx
     * @param fileName
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected String _findByFileName(Context ctx, String fileName) throws BOSException, EASBizException {
        String componentId = "default";
        super._findByFileName(ctx, fileName);
        if (StringUtils.isBlank(fileName)) {
            throw new BOSException("文件名不能为空");
        }
        FilterInfo filterInfo = new FilterInfo();
        FilterItemCollection filterItems = filterInfo.getFilterItems();
        filterItems.add(new FilterItemInfo("name", "%" + fileName + "%", CompareType.LIKE));
        filterItems.add(new FilterItemInfo("state", BaseItemStateEnum.ENABLE_VALUE));
        SelectorItemCollection sic = new SelectorItemCollection();
        sic.add("id");
        sic.add("attachmentCategory.componentId");
        EntityViewInfo viewInfo = EntityViewInfo.getInstance(filterInfo, sic, null);
        ESignTemplateFileEntryCollection eSignTemplateFileEntryCol = getESignTemplateFileEntryCollection(ctx, viewInfo);
        if (!eSignTemplateFileEntryCol.isEmpty()) {
            ESignTemplateFileEntryInfo eSignTemplateFileEntryInfo = eSignTemplateFileEntryCol.get(0);
            logger.error("匹配到模板分录：" + eSignTemplateFileEntryInfo.getId());
            //附件类别
            AttachmentStoragePathConfigInfo attachmentCategory = eSignTemplateFileEntryInfo.getAttachmentCategory();
            if (attachmentCategory != null) {
                componentId = attachmentCategory.getComponentId();
            }
        } else {
            logger.error("未匹配到启用状态的模板分录：" + fileName);
        }
        return componentId;

    }
}