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

import com.kingdee.bos.metadata.MetaDataPK;
import com.kingdee.bos.service.ServiceContext;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.notify.*;
import com.kingdee.eas.custom.notify.bziEnum.ReturnStateEnum;
import com.kingdee.eas.custom.notify.bziEnum.todoType;
import org.apache.log4j.Logger;

import java.lang.String;
import java.util.HashMap;
import java.util.Map;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.custom.notify.notifyTodo.*;
import com.kingdee.util.StringUtils;

import java.io.*;
import java.lang.Exception;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

public class NotifyTodoControllerBean extends AbstractNotifyTodoControllerBean {
    private static Logger logger = Logger.getLogger(NotifyTodoControllerBean.class);
    private static Map maps = new HashMap();

    static {
        loadConfig();
    }

    /**
     * 加载配置
     */
    public static void loadConfig() {
        Properties props = new Properties();
        try {
            String propertiesUrl = System.getProperty("EAS_HOME") + "/server/properties/notifyTodo/propertie.properties";
            logger.debug("propertiesUrl:" + propertiesUrl);
            InputStream InputStream = new BufferedInputStream(new FileInputStream(new File(propertiesUrl)));
            props.load(InputStream);
        } catch (IOException var4) {
            var4.printStackTrace();
            logger.error("待办集成获取配置文件报错: " + var4.getMessage());
        }
        Enumeration en = props.propertyNames();
        while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
            String value = props.getProperty(key);
            maps.put(key, value);
            logger.debug(key + " : " + value);
        }
    }

    /**
     * 获取配置属性
     *
     * @param key
     * @return
     */
    public static String getConfigValue(String key) {
        return (String) maps.get(key);
    }


    /**
     * 删除待办
     *
     * @param ctx
     * @throws BOSException
     */
    @Override
    public void deleteTodo(Context ctx, RemoveContextInfo content)
            throws BOSException, EASBizException {
        try {
            ServiceContext svcCtx = createServiceContext(new MetaDataPK("f40441e1-a114-4e6a-b7fc-811712f783a6"), new Object[]{ctx, content});
            invokeServiceBefore(svcCtx);
            if(!svcCtx.invokeBreak()) {
                String portAddress = getConfigValue("portAddress");
                if (StringUtils.isEmpty(portAddress)) {
                    throw new BOSException("删除待办接口地址为空，请检查配置文件（/server/properties/notifyTodo/propertie.properties）中是否维护portAddress元素");
                }
                NotifyTodoAppResult result = null;
                try {
                    ISysNotifyTodoWebServiceServiceLocator iSysNotifyTodoWebServiceServiceLocator = new ISysNotifyTodoWebServiceServiceLocator();
                    URL url = new URL(portAddress);
                    ISysNotifyTodoWebService iSysNotifyTodoWebServicePort = iSysNotifyTodoWebServiceServiceLocator.getISysNotifyTodoWebServicePort(url);
                    NotifyTodoRemoveContext removeContext = new NotifyTodoRemoveContext((RemoveContextInfo) content);
                    //删除待办
                    result = iSysNotifyTodoWebServicePort.deleteTodo(removeContext);
                    RemoveContextFactory.getLocalInstance(ctx).save(content);
                    NotifyTodoInfo info = new NotifyTodoInfo();
                    info.setTodoType(todoType.deleteTodo);
                    info.setOperationType("删除待办");
                    info.setSetdelTodoDone(content);
                    ReturnStateEnum anEnum = ReturnStateEnum.getEnum(String.valueOf(result.getReturnState()));
                    info.setReturnState(anEnum);
                    info.setMessage(result.getMessage());
                    save(ctx, info);
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error(e.getMessage());
                    throw new BOSException(e);
                }
                if (result.getReturnState() != 2) {
                    String message = result.getMessage();
                    logger.error("发送OA待办集成状态 " + result.getReturnState() + " : " + message);
                    throw new BOSException(message);
                }
            }
            invokeServiceAfter(svcCtx);
        }finally {
            super.cleanUpServiceState();
        }
    }

    /**
     * 更新待办
     *
     * @param ctx
     * @throws BOSException
     */
    @Override
    public void updateTodo(Context ctx, UpdateContentInfo content) throws BOSException {
        try {
            ServiceContext svcCtx = createServiceContext(new MetaDataPK("e8cd4c97-f9e5-4b06-aaa9-80dd4b909c49"), new Object[]{ctx, content});
            invokeServiceBefore(svcCtx);
            if(!svcCtx.invokeBreak()) {
                String portAddress = getConfigValue("portAddress");
                if (StringUtils.isEmpty(portAddress)) {
                    throw new BOSException("更新待办接口地址为空，请检查配置文件（/server/properties/notifyTodo/propertie.properties）中是否维护portAddress元素");
                }
                NotifyTodoAppResult result = null;
                try {
                    ISysNotifyTodoWebServiceServiceLocator iSysNotifyTodoWebServiceServiceLocator = new ISysNotifyTodoWebServiceServiceLocator();
                    URL url = new URL(portAddress);
                    ISysNotifyTodoWebService iSysNotifyTodoWebServicePort = iSysNotifyTodoWebServiceServiceLocator.getISysNotifyTodoWebServicePort(url);
                    NotifyTodoUpdateContext context = new NotifyTodoUpdateContext(content);
                    //更新待办
                    result = iSysNotifyTodoWebServicePort.updateTodo(context);
                    UpdateContentFactory.getLocalInstance(ctx).save(content);
                    NotifyTodoInfo info = new NotifyTodoInfo();
                    info.setTodoType(todoType.updateTodo);
                    info.setOperationType(content.getSubject());
                    info.setUpdateTodo(content);
                    ReturnStateEnum anEnum = ReturnStateEnum.getEnum(String.valueOf(result.getReturnState()));
                    info.setReturnState(anEnum);
                    info.setMessage(result.getMessage());
                    save(ctx, info);
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error(e.getMessage());
                    throw new BOSException(e);
                }
                if (result.getReturnState() != 2) {
                    String message = result.getMessage();
                    logger.error("发送OA待办集成状态 " + result.getReturnState() + " : " + message);
                    throw new BOSException(message);
                }
            }
            invokeServiceAfter(svcCtx);
        }finally {
            super.cleanUpServiceState();
        }
    }

    /**
     * 置为已办
     *
     * @param ctx
     * @throws BOSException
     */
    @Override
    public void setTodoDone(Context ctx, RemoveContextInfo content) throws BOSException {
        try {
            ServiceContext svcCtx = createServiceContext(new MetaDataPK("49566ce2-32ff-4521-bbb6-1e8d133f9e65"), new Object[]{ctx, content});
            invokeServiceBefore(svcCtx);
            if(!svcCtx.invokeBreak()) {
                String portAddress = getConfigValue("portAddress");
                if (StringUtils.isEmpty(portAddress)) {
                    throw new BOSException("置为已办接口地址为空，请检查配置文件（/server/properties/notifyTodo/propertie.properties）中是否维护portAddress元素");
                }

                NotifyTodoAppResult result = null;
                try {
                    ISysNotifyTodoWebServiceServiceLocator iSysNotifyTodoWebServiceServiceLocator = new ISysNotifyTodoWebServiceServiceLocator();
                    URL url = new URL(portAddress);
                    ISysNotifyTodoWebService iSysNotifyTodoWebServicePort = iSysNotifyTodoWebServiceServiceLocator.getISysNotifyTodoWebServicePort(url);
                    NotifyTodoRemoveContext context = new NotifyTodoRemoveContext(content);
                    //置为已办
                    result = iSysNotifyTodoWebServicePort.setTodoDone(context);
                    //生成对应的 请求参数体 和  OA待办集成
                    RemoveContextFactory.getLocalInstance(ctx).save(content);
                    NotifyTodoInfo info = new NotifyTodoInfo();
                    info.setTodoType(todoType.setTodoDone);
                    info.setOperationType("置为已办");
                    info.setSetdelTodoDone(content);
                    ReturnStateEnum anEnum = ReturnStateEnum.getEnum(String.valueOf(result.getReturnState()));
                    info.setReturnState(anEnum);
                    info.setMessage(result.getMessage());
                    save(ctx, info);
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error(e.getMessage());
                    throw new BOSException(e);
                }
                if (result.getReturnState() != 2) {
                    String message = result.getMessage();
                    logger.error("发送OA待办集成状态 " + result.getReturnState() + " : " + message);
                    throw new BOSException(message);
                }
            }
            invokeServiceAfter(svcCtx);
        }finally {
            super.cleanUpServiceState();
        }
    }


    /**
     * 发送待办
     * http://eiptest.gemdale.com:8080/sys/webservice/sysNotifyTodoWebService
     * @param ctx
     * @throws BOSException
     */
    @Override
    public void sendTodo(Context ctx, SendContextInfo content) throws BOSException {
        try {
            ServiceContext svcCtx = createServiceContext(new MetaDataPK("00e74178-7123-4657-9af7-ae161bcfbe60"), new Object[]{ctx, content});
            invokeServiceBefore(svcCtx);
            if(!svcCtx.invokeBreak()) {
                String portAddress = getConfigValue("portAddress");
                if (StringUtils.isEmpty(portAddress)) {
                    throw new BOSException("发送待办接口地址为空，请检查配置文件（/server/properties/notifyTodo/propertie.properties）中是否维护portAddress元素");
                }
                NotifyTodoAppResult result = null;
                try {
                    ISysNotifyTodoWebServiceServiceLocator iSysNotifyTodoWebServiceServiceLocator = new ISysNotifyTodoWebServiceServiceLocator();
                    URL url = new URL(portAddress);
                    ISysNotifyTodoWebService iSysNotifyTodoWebServicePort = iSysNotifyTodoWebServiceServiceLocator.getISysNotifyTodoWebServicePort(url);
                    NotifyTodoSendContext context = new NotifyTodoSendContext(content);
                    //发送待办
                    result = iSysNotifyTodoWebServicePort.sendTodo(context);
                    //生成对应的 请求参数体 和  OA待办集成
                    SendContextFactory.getLocalInstance(ctx).save(content);
                    NotifyTodoInfo info = new NotifyTodoInfo();
                    info.setTodoType(todoType.sendTodo);
                    info.setOperationType(content.getSubject());
                    info.setSendTodo(content);
                    ReturnStateEnum anEnum = ReturnStateEnum.getEnum(String.valueOf(result.getReturnState()));
                    info.setReturnState(anEnum);
                    info.setMessage(result.getMessage());
                    save(ctx, info);
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error(e.getMessage());
                    throw new BOSException(e);
                }
                if (result.getReturnState() != 2) {
                    String message = result.getMessage();
                    logger.error("发送OA待办集成状态 " + result.getReturnState() + " : " + message);
                    throw new BOSException(message);
                }
            }
            invokeServiceAfter(svcCtx);
        } finally {
            super.cleanUpServiceState();
        }
    }
}