package com.customization.htsc.autoremind;

import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.workflow.cmd.requestForm.RequestSubmitCmd;
import com.engine.workflow.constant.requestForm.RequestExecuteType;
import com.engine.workflow.entity.requestForm.RequestOperationResultBean;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@CommandDynamicProxy(target = RequestSubmitCmd.class, desc = "流程提交报错提醒系统管理员")
public class RequestSubmitErrorRemindCmd extends AbstractCommandProxy<Map<String, Object>> {
    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> command) {


        BaseBean bb = new BaseBean();
        bb.infoLog("RequestSubmitErrorRemindCmd","RequestSubmitErrorRemindCmd");
        RequestSubmitCmd requestSubmitCmd = (RequestSubmitCmd) command;
        HttpServletRequest request = requestSubmitCmd.getRequest();//从request中可以取到参数，不用以前的方法了
        User user = requestSubmitCmd.getUser();

        String requestid = Util.null2String(request.getParameter("requestid"));
        String workflowid = Util.null2String(request.getParameter("workflowid"));
        String src = Util.null2String(request.getParameter("src"));

        bb.infoLog("RequestSubmitErrorRemindCmd",requestid);


        Map<String, Object> result  = nextExecute(command);;
        String remark = "流程流转出错";
        try{
            if(!"1".equals(workflowid))
            {
                RequestOperationResultBean resultBean = (RequestOperationResultBean) result.get("data");

                Map<String, Object> messagemap = resultBean.getMessageInfo();
//                for (Map.Entry<String, Object> entry : messagemap.entrySet()) {
//                    bb.writeLog("RequestSubmitErrorRemindCmd--messagemap--Key = " + entry.getKey() + ", Value = " + entry.getValue());
//                }
//
//                for (Map.Entry<String, Object> entry : result.entrySet()) {
//                    bb.writeLog("RequestSubmitErrorRemindCmd--result--Key = " + entry.getKey() + ", Value = " + entry.getValue());
//                }
//
//                Map<String, Object> resultinfo = resultBean.getMessageInfo();
//                for (Map.Entry<String, Object> entry : resultinfo.entrySet()) {
//                    bb.writeLog("RequestSubmitErrorRemindCmd--messagemap--Key = " + entry.getKey() + ", Value = " + entry.getValue());
//                }

//                Map<String, Object> submitparams = resultBean.getMessageInfo();
//                for (Map.Entry<String, Object> entry : submitparams.entrySet()) {
//                    bb.writeLog("RequestSubmitErrorRemindCmd--messagemap--Key = " + entry.getKey() + ", Value = " + entry.getValue());
//                }

                RequestExecuteType requestExecuteType = resultBean.getType();
//                bb.writeLog("RequestSubmitErrorRemindCmd-requestExecuteType = " + requestExecuteType.toString());
                //prompttype

                String detail = Util.null2String(messagemap.get("detail"));
                String bottom = Util.null2String(messagemap.get("bottom"));
                if(!"".equals(remark)){
                    remark = "<div>"+detail+"</div><div>"+bottom+"</div>";
                }
                String isSuccess = Util.null2String(messagemap.get("isSuccess"));

                bb.writeLog("RequestSubmitErrorRemindCmd---isSuccess:"+isSuccess);
                bb.writeLog("RequestSubmitErrorRemindCmd---src:"+src);
                bb.writeLog("RequestSubmitErrorRemindCmd---requestExecuteType:"+requestExecuteType.toString());

                if(("false".equals(isSuccess) || ("".equals(isSuccess) && "FAILD".equalsIgnoreCase(requestExecuteType.toString()))) && "submit".equals(src))
                {
                    bb.writeLog("requestSubmitErrorRemindThread---start");
                    RequestSubmitErrorRemindThread requestSubmitErrorRemindThread = new RequestSubmitErrorRemindThread(requestid,user,remark);
                    new Thread(requestSubmitErrorRemindThread).start();
                    bb.writeLog("requestSubmitErrorRemindThread----end");
                }
            }
        }catch (Exception e){
            bb.writeLog("RequestSubmitErrorRemindCmd-e:"+e);
            if(!"1".equals(workflowid))
            {
                bb.writeLog("requestSubmitErrorRemindThread--Exception--start");
                RequestSubmitErrorRemindThread requestSubmitErrorRemindThread = new RequestSubmitErrorRemindThread(requestid,user,remark);
                new Thread(requestSubmitErrorRemindThread).start();
                bb.writeLog("requestSubmitErrorRemindThread--Exception--end");
            }
        }

        return result;
    }







}
