package weaver.interfaces.htsc.payment.common;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.engine.htsc.meeting.util.Common;
import com.engine.htsc.payment.log.PaymentLogger;
import com.engine.htsc.payment.log.PaymentLoggerFactory;
import com.engine.htsc.payment.util.AssertUtil;
import com.engine.htsc.payment.util.CommonAPI;
import com.engine.htsc.payment.util.PayUtil;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
import weaver.interfaces.htsc.cus.HTUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.util.*;
import java.util.stream.Collectors;

public class LangChaoToMode implements Action {

    private static final PaymentLogger logger = PaymentLoggerFactory.getLogger(LangChaoToMode.class);
    private final HTUtil htUtil = new HTUtil();


    @Override
    public String execute(RequestInfo requestInfo) {

        try {
                try {
                    String requestid = requestInfo.getRequestid();
                    RequestManager rm = requestInfo.getRequestManager();
                    String tableName = rm.getBillTableName();
                    int workflowid = Integer.parseInt(requestInfo.getWorkflowid());
                    String sql = "select t1.workflowid,t3.lcbh as requestmark ,t2.workcode from workflow_requestbase t1\n" +
                            "left join hrmresource t2 " +
                            "on t1.creater = t2.id " +
                            "left join " + tableName + " t3 " +
                            "on t1.requestid = t3.requestid " +
                            "where t1.requestid =?";
                    Map<String, String> paramsProtoType = CommonAPI.querySingleRow(sql, requestid);
                    String lcbh = paramsProtoType.get("requestmark");
                    String time = DateUtil.now();
                    logger.info("开始插入uf_SyncWavApproForm建模表"+"requestid:"+requestid+"lcbh:"+lcbh+"time:"+time);
                    RecordSet toMode = new RecordSet();
                    String sqlInsertAFFIX = "INSERT INTO uf_SyncWavApproForm(requestid,lcbh,bjsj,tblx,sfytb,tablename,workflowid) " +
                            "values("+requestid+",'"+lcbh+"','"+time+"','PROCESS_AFFIX',1,'"+tableName+"',"+workflowid+") ";
                    toMode.executeUpdate(sqlInsertAFFIX);
                    toMode.next();
                    String sqlInsertPDF = "INSERT INTO uf_SyncWavApproForm(requestid,lcbh,bjsj,tblx,sfytb,tablename,workflowid) " +
                            "values("+requestid+",'"+lcbh+"','"+time+"','PROCESS_PDF',1,'"+tableName+"',"+workflowid+") ";
                    toMode.executeUpdate(sqlInsertPDF);
                    toMode.next();

                } catch (Exception e) {
                    logger.error(String.format("网报流程办结后写入uf_SyncWavApproForm出错 requestid: %s", requestInfo.getRequestid()),e);
                }
            return SUCCESS;
        } catch (Exception e) {
            logger.error(e);
            return SUCCESS;
        }
    }


}
