package weaver.interfaces.workflow.action.javacode;

import com.cloudstore.dev.api.bean.MessageBean;
import com.cloudstore.dev.api.bean.MessageType;
import com.cloudstore.dev.api.util.Util_Message;
import weaver.conn.RecordSet;
import weaver.file.Prop;
import weaver.general.StringUtil;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.general.BaseBean;
import weaver.soa.workflow.request.RequestInfo;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * Online custom action interface
 */
public class Action20231221085819 extends BaseBean implements Action {
    /**
     * After selecting aciton after the process path node, this method will be executed after the node is submitted.
     */
    @Override
    public String execute(RequestInfo request) {
        try {
            String requestid = request.getRequestid();
            String billTableName = request.getRequestManager().getBillTableName();
            String sqr = getsqrbyReqid(requestid, billTableName);
            String bh = getBHbyreqId(requestid, billTableName);
            sendMsgForSuccess(sqr,bh,Util.getIntValue(requestid));
        }catch (Exception e){
            e.printStackTrace();
            writeLog("补助提醒异常"+e.getMessage());
        }

        return Action.SUCCESS;
    }

    private void sendMsgForSuccess(String userid, String oaTrvlBnsExpnsAcctNo, int requestid) {
        MessageType messageType = MessageType.newInstance(1371); // 消息来源（见文档第四点补充 必填）
        Set<String> userIdList = new HashSet<>(); // 接收人id 必填
        String[] useridArr = userid.split(",");
        String proplinkUrl = Prop.getPropValue("subsidy_reminder", "linkUrl");
        String proplinkMobileUrl = Prop.getPropValue("subsidy_reminder", "linkMobileUrl");
        for (String s : useridArr) {
            userIdList.add(s);
        }
        String title = "您有新的差旅补助到账,请您查收"; // 标题
        String context = "差补报销(" + oaTrvlBnsExpnsAcctNo + ")";
        String linkUrl = "/spa/workflow/static4form/index.html#/main/workflow/req?requestid=" + requestid; // PC端链接
        String linkMobileUrl = "/spa/workflow/static4mobileform/index.html#/req?requestid=" + requestid; // 移动端链接
        try {
            MessageBean messageBean = Util_Message.createMessage(messageType, userIdList, title, context, linkUrl, linkMobileUrl);
            messageBean.setCreater(1);// 创建人id
            //message.setBizState("0");// 需要修改消息为已处理等状态时传入,表示消息最初状态为待处理
            //messageBean.setTargetId("121|22"); //消息来源code +“|”+业务id需要修改消息为已处理等状态时传入
            Util_Message.store(messageBean);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public String getsqrbyReqid(String requestid, String tableName) {
        RecordSet recordSet = new RecordSet();
        HashMap<String, String> resultMap = new HashMap<>();
        String sql = "select jbrxm from " + tableName + " where requestid = ?";
        recordSet.executeQuery(sql, requestid);
        recordSet.next();
        return Util.null2String(recordSet.getString("jbrxm"));

    }

    public String getBHbyreqId(String requestid, String tableName) {
        RecordSet recordSet = new RecordSet();
        HashMap<String, String> resultMap = new HashMap<>();
        String sql = "select djbh from " + tableName + " where requestid = ?";
        recordSet.executeQuery(sql, requestid);
        recordSet.next();
        return Util.null2String(recordSet.getString("djbh"));
    }
}
