package com.weaver.seconddev.njwebservice;

import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.seconddev.njwebservice.entity.WLPS_SY_Response;
import com.weaver.seconddev.njwebservice.entity.WLPS_SY_Return;
import com.weaver.seconddev.njwebservice.util.SecondUtil;
import com.weaver.seconddev.njwebservice.util.WfUtil;
import com.weaver.verupgrade.conn.CONN_TYPE;
import com.weaver.verupgrade.conn.RecordSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author liang.cheng
 * @Date 2025/1/20 13:54
 * @Description:
 * @Version 1.0
 */

@Service("WLPS_SY_GetRes")
public class WLPS_SY_GetResService {

    private final static Logger log = LoggerFactory.getLogger(WLPS_SY_GetResService.class);

    public void getRes(WLPS_SY_Response res) {
        log.error("接收MesWLPS_SY接口返回值开始 : res = " + res.toString());
        String isTat = res.getISTAT();
        String systemId = res.getSYSID();
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        String poolname = CONN_TYPE.workflow.getType();
        try{
            if("S".equals(isTat)){
                WLPS_SY_Return[] ret = res.getRETURN();

                for(int i=0;i<ret.length;i++){
                    String workflowid = ret[i].getWorkflowid();

                    WfUtil wfUtil = new WfUtil();
                    Map<String, String> zhWfMap = wfUtil.getZhWfMapNoUser("t6kj9c07jr", workflowid);
//
                    workflowid = zhWfMap.get("workflowid");
                    log.error("转换后的流程ID:"+workflowid);

                    String tablename = SecondUtil.getTableName(workflowid);

                    String requestid = ret[i].getRequestId();
                    String state = ret[i].getIstat();
                    String message  = ret[i].getMessage();
                    if(!"".equals(workflowid)&&!"".equals(requestid)&&workflowid.equals(SecondUtil.getWorkflowIdByReq(requestid))){
                        String dataid = SecondUtil.getidByrequestid(requestid);
                        String sql = "update e10_core_business.dbo."+tablename+" set state='"+state+"',message='"+message+"' where id="+dataid;
                        log.error( "sql" + sql);
                        boolean b1 = rs.executeSql(sql, poolname);
                        log.error(systemId+"--"+requestid+"更新成功" + b1);
                    }else{
                        log.error("MesWLPS_SY返回数据不符合要求"+res);
                    }
                }
            }
            log.error("接收MesWLPS_SY接口返回值结束");
        }catch(Exception e){
            log.error("接收MesWLPS_SY接口返回值出错："+e.toString());
        }
    }

}
