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 com.wbi.util.Util;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.general.BaseBean;
import weaver.soa.workflow.request.RequestInfo;

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

/**
 * Online custom action interface
 */
public class Action20231113045722 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 tablename = request.getRequestManager().getBillTableName();


            RecordSet rs = new RecordSet();
            rs.execute("select * from " + tablename + " where requestid =  " + requestId);
            rs.next();
            // if (1 == 1 ){
            //     throw new Exception("11111"+requestId);
            // }
            String mainid = rs.getString("id");
            String fsr = rs.getString("fsr");
            //标题
            String title = rs.getString("fsnr");
            //内容
            String context = rs.getString("txxx");
            //PC端链接 纯文本就传空字符串
            String linkUrl = rs.getString("pclj");
            //移动端链接 纯文本就传空字符串
            String linkMobileUrl = rs.getString("applj");



            //codeid  codeid
            int codeid = Util.getIntValue(rs.getString("codeid"));
            if (codeid <= 0) {
                request.getRequestManager().setMessageid("90001");
                request.getRequestManager().setMessagecontent("codeid异常");
                return Action.FAILURE_AND_CONTINUE;
            }


            //消息来源（见文档第四点补充）
            MessageType messageType = null;
            try {
                messageType = MessageType.newInstance(codeid);
            }catch (Exception e){
                throw new Exception("MessageType.newInstance推送消息异常:"+codeid);
            }



            //接收人id
            Set<String> userIdList = new TreeSet<>();
            try {
                userIdList = convertToHashSet(fsr);
            }catch (Exception e){
                throw new Exception("convertToHashSet推送消息异常:"+fsr);
            }

            // if (1 == 1 ) {
            //     throw new Exception("userIdList:" + userIdList);
            // }
            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  需要修改消息状态时传入，这个字段是自定义的，和修改消息状态的时候传入相同的值，可做更新。
                boolean store = Util_Message.store(messageBean);
            } catch (IOException e) {
                e.printStackTrace();
                request.getRequestManager().setMessageid("90002");
                request.getRequestManager().setMessagecontent("异常1111111"+e.getMessage()+ e);
                return Action.FAILURE_AND_CONTINUE;
                // throw new Exception("推送消息异常");

            }

        }catch (Exception e){
            request.getRequestManager().setMessageid("90002");
            request.getRequestManager().setMessagecontent("异常"+e.getMessage()+ e);
            return Action.FAILURE_AND_CONTINUE;
        }


        return Action.SUCCESS;
    }


    public static Set<String> convertToHashSet(String str) {
        Set<String> resultSet = new HashSet<>();

        if (str != null && !str.isEmpty()) {
            String[] items = str.split(",");

            for (String item : items) {
                if (item != null && !item.trim().isEmpty()) {
                    resultSet.add(item.trim());
                }
            }
        }

        return resultSet;
    }
}