package weaver.interfaces.htsc.gwlc;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

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

/**
 * Created by K1810007 on 2020/10/21.
 */

public class sendPostRulRegulAction implements Action {
    @Override
    public String execute(RequestInfo requestInfo)
    {
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        PropBean pb = new PropBean();
        String requestid = requestInfo.getRequestid();
        String billtable = requestInfo.getRequestManager().getBillTableName();
        if(!"".equals(requestid))
        {
            String lx = "";
            String 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>();
            String fieldid = pb.getPropName("lx_fieldid");
            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 wrokflowToOAUtil = new WrokflowToOAUtil();
                        String message = wrokflowToOAUtil.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 = wrokflowToOAUtil.insertDetail(dataObject);
                                if(!"1".equals(res)){
                                    requestInfo.getRequestManager().setMessageid("1111");
                                    requestInfo.getRequestManager().setMessagecontent("生成规章制度回写信息异常");
                                    return Action.FAILURE_AND_CONTINUE;
                                }
                            }else{
                                requestInfo.getRequestManager().setMessageid("1111");
                                requestInfo.getRequestManager().setMessagecontent("生成规章制度接口异常");
                                return Action.FAILURE_AND_CONTINUE;
                            }
                        }
                    }
                }
            }catch (Exception e){
                bb.writeLog("sendPostRulRegulAction-e:"+e);
                requestInfo.getRequestManager().setMessageid("2222");
                requestInfo.getRequestManager().setMessagecontent("生成规章制度异常");
                return Action.FAILURE_AND_CONTINUE;
            }
        }
        return Action.SUCCESS;
    }

    public static void main(String[] args) {
        GCONST.setRootPath("WEB-INF/");
        GCONST.setServerName("ecology");

        sendPostRulRegulAction sendPostRulRegulAction = new sendPostRulRegulAction();

        System.out.println("1111111");
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setRequestid("1530531");
        //requestInfo.getRequestManager().setBilltablename("formtable_main_48");

        sendPostRulRegulAction.execute(requestInfo);

    }

}
