package weaver.interfaces.htsc.jjhfw;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.gwlc.WrokflowToOAUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Weaver.SongYuchen
 * @Date 2022-03-28
 * @Description 【基金会发文】发文类别为“规章制度”时，办结后自动触发规章制度入库流程
 */
public class triggerRulesStorageAction implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        String requestId = requestInfo.getRequestid();
        String billTable = requestInfo.getRequestManager().getBillTableName();
        String sql = "";

        if (!"".equals(requestId)) {
            String lx = ""; //类型
            sql = "select lx from " + billTable + " where requestid = " + requestId;
            rs.execute(sql);
            if (rs.next()) {
                lx = Util.null2String(rs.getString("lx"));
            }

            List<String> list = new ArrayList<>();
            String fieldId = "";
            sql = "select k.id from workflow_billfield k\n" +
                    " inner join workflow_bill l on l.id = k.billid\n" +
                    " where l.tablename = '" + billTable + "'\n" +
                    " and fieldname = 'lx'\n ";
            rs.execute(sql);
            if (rs.next()) {
                fieldId = Util.null2String(rs.getString("id"));
            }

            sql = " select selectvalue from workflow_selectitem where fieldid = " + fieldId + " and selectname like '%规章制度%'";
            rs.execute(sql);
            while (rs.next()) {
                String selectValue = Util.null2String(rs.getString("selectvalue"));
                list.add(selectValue);
            }

            try {
                if (list.size() > 0 && list != null) {
                    if (list.contains(lx)) {
                        WrokflowToOAUtil workflowToOAUtil = new WrokflowToOAUtil();
                        String message = workflowToOAUtil.createWorkflow2OldOA(Util.getIntValue(requestId));
                        if (!"".equals(message)) {
                            JSONObject messageObject = JSONObject.parseObject(message);
                            String status = Util.null2String(messageObject.get("status"));
                            if ("200".equals(status)) {
                                JSONObject dataObject = messageObject.getJSONObject("data");
                                String res = workflowToOAUtil.insertDetail(dataObject);
                                if (!"1".equals(res)) {
                                    requestInfo.getRequestManager().setMessageid("111");
                                    requestInfo.getRequestManager().setMessagecontent("生成规章制度回写信息异常");
                                    return Action.FAILURE_AND_CONTINUE;
                                }
                            } else {
                                requestInfo.getRequestManager().setMessageid("222");
                                requestInfo.getRequestManager().setMessagecontent("生成规章制度接口异常");
                                return Action.FAILURE_AND_CONTINUE;
                            }
                        }
                    }
                }
            } catch (Exception e) {
                bb.writeLog("triggerRulesStorageAction error: " + e);
                requestInfo.getRequestManager().setMessageid("333");
                requestInfo.getRequestManager().setMessagecontent("生成规章制度异常");
                return Action.FAILURE_AND_CONTINUE;
            }
        }
        return Action.SUCCESS;
    }
}
