package weaver.interfaces.htsc.linkMsg;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.Smstools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class SendLinkMsgAction implements Action {
    private final Log log = LogFactory.getLog(SendLinkMsgAction.class);

    private String content;
    private String nextNode;

    private String getContent() {
        return content;
    }

    private void setContent(String content) {
        this.content = content;
    }

    private String getNextNode(){
        return nextNode;
    }
    private void setNextNode(String nextNode){
        this.nextNode = nextNode;
    }

    @Override
    public String execute(RequestInfo requestInfo) {
        try {
            Map map = mainData(requestInfo);
            String nextOperatorid = Util.null2String(map.get(nextNode));
            PropBean propBean = new PropBean();
            String specialNodeNotice = propBean.getPropName("SpecialNodeNotice");
            int specialNodeNoticeId = Util.getIntValue(specialNodeNotice);
            String requestid = requestInfo.getRequestid();
            String sql = "select distinct t.creater,t.requestname,t.requestmark,b.workflowname from workflow_requestbase t " +
                    "left join workflow_base b on t.workflowid = b.id " +
                    "where t.requestid=" + requestid;
            String sql2 = "select t.workcode,t.lastname from hrmresource t where t.id in " + "('" + nextOperatorid.replaceAll(",","','") + "')";
            RecordSet rs = new RecordSet();
            rs.executeQuery(sql);
            if (rs.next()) {
                StringBuilder currentOperatorNames = new StringBuilder();
                String creater = Util.null2String(rs.getString("creater"));
                String requestname = Util.null2String(rs.getString("requestname"));
                String requestmark = Util.null2String(rs.getString("requestmark"));
                String workflowname = Util.null2String(rs.getString("workflowname"));
                Set<String> cjrsetuser = new HashSet<>();
                cjrsetuser.add(creater);
                rs.executeQuery(sql2);
                while(rs.next()){
                    currentOperatorNames.append(Util.null2String(rs.getString("lastname"))).append("(").append(Util.null2String(rs.getString("workcode"))).append(");");
                }
                String contentStr = "\n流程类型：" + workflowname + "\n" +
                        "流程编号：" + requestmark + "\n" +
                        "流程标题：" + requestname + "\n" +
                        "当前会计：" + currentOperatorNames;
                Smstools smstools = new Smstools();
                boolean sendResult = smstools.sendMessage(specialNodeNoticeId, cjrsetuser, "发票提交提醒", content + contentStr, "", "");
                if(sendResult){
                    return SUCCESS;
                }
                log.error("SendLinkMsgAction fail, response :" + sendResult);
                return SUCCESS;
            }
            log.error("SendLinkMsgAction executeQuery empty");
            return SUCCESS;
        } catch (Exception e) {
            log.error("SendLinkMsgAction fail :" + e);
            return SUCCESS;
        }
    }


    /**
     * 主表数据
     */
    private static Map mainData(RequestInfo requestInfo) {
        Map mainTableDataMap = new HashMap();
        Property[] property = requestInfo.getMainTableInfo().getProperty();
        for (int i = 0; i < property.length; i++) {
            mainTableDataMap.put(property[i].getName().toLowerCase(), Util.null2String(property[i].getValue()));
        }
        return mainTableDataMap;
    }
}
