package com.weaver.seconddev.interfaces.easoa;

import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.seconddev.interfaces.prop.ConfigProp;
import org.apache.axis2.kernel.http.HTTPConstants;
import org.apache.axis2.transport.http.impl.httpclient4.HttpTransportPropertiesImpl;
import java.util.List;
import java.util.Map;
import com.weaver.verupgrade.conn.CONN_TYPE;
import java.lang.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.weaver.verupgrade.conn.RecordSet;
import com.weaver.verupgrade.general.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ChangeWhiteListState  {

	@Autowired
	private ConfigProp configProp; //注入

	public static final org.slf4j.Logger logger = LoggerFactory.getLogger(ChangeWhiteListState.class);

	public void changeState(List<Map<String, String>> list) {
		logger.error(String.valueOf("新增黑名单时更改白名单状态开始 list = " + list.toString()));
		RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
		try {
			for (int i = 0; i < list.size(); i++) {
				SI_WhitelistSync_OUTServiceStub.SyncWhiteListService input = new SI_WhitelistSync_OUTServiceStub.SyncWhiteListService();
				SI_WhitelistSync_OUTServiceStub.SyncWhiteListInfo infos = new SI_WhitelistSync_OUTServiceStub.SyncWhiteListInfo();
				String mc = list.get(i).get("mc");
				String dm = list.get(i).get("dm");
				String sql = "";
				if (!"".equals(mc) && mc != null) {
					sql = "select * from e10_core_business.dbo.formtable_main_1716 where (mc='" + mc + "' or shxydm='" + dm + "') and bmdzt='0'";
					logger.error(String.valueOf("sql1 = " + sql ));
					String poolname = CONN_TYPE.workflow.getType();
					// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
					boolean b1 = rs.executeSql(sql, poolname);
					logger.error(String.valueOf("b1 = " + b1 ));
				}
				if (rs != null) {
					while (rs.next()) {
						SI_WhitelistSync_OUTServiceStub.SieWhiteListInfo info = new SI_WhitelistSync_OUTServiceStub.SieWhiteListInfo();
						String id = Util.null2String(rs.getInt("id") + "");
						String flowCode = Util.null2String(rs.getString("lcbh"));
						String name = Util.null2String(rs.getString("mc"));
						String type = Util.null2String(rs.getInt("bmdlx") + "");
						// String status = Util.null2String(rs.getInt("bmdzt")+"");
						String status = "1";
						String bankAccount = Util.null2String(rs.getString("yhzh"));
						String bankName = Util.null2String(rs.getString("yhmc"));
						String identityNumber = Util.null2String(rs.getString("shxydm"));
						String flowId = Util.null2String(rs.getInt("requestId") + "");
						String rq = Util.null2String(rs.getString("tbsj"));
						String sj = Util.null2String(rs.getString("sj"));
						String yxjzrq = Util.null2String(rs.getString("yxjzrq"));
						String reason = Util.null2String(rs.getString("sqbmdyy"));
						String code = "";
						if (!"".equals(flowCode)) {
							code = flowCode.trim().substring(flowCode.length() - 11, flowCode.length());
						}
						String createTime = rq + " " + sj + ":00";
						String endTime = "";
						if (!"".equals(yxjzrq)) {
							endTime = yxjzrq + " 00:00:00";
						}
						info.setWhiteId(id);
						info.setCode(code);
						info.setName(name);
						info.setType(type);
						info.setStatus(status);
						info.setBankAccount(bankAccount);
						info.setBankName(bankName);
						info.setIdentityNumber(identityNumber);
						info.setFlowId(flowId);
						info.setFlowCode(flowCode);
						info.setCreateTime(createTime);
						info.setEndTime(endTime);
						info.setReason(reason);
						infos.addWhiteListInfos(info);
					}
					String sql_update = "update e10_core_business.dbo.formtable_main_1716 set bmdzt='1' where mc='" + mc + "'";
					logger.error(String.valueOf("sql_update = " + sql_update ));
					boolean b2 = rs.executeSql(sql_update);
					logger.error(String.valueOf("b2 = " + b2 ));
					infos.setITFID("");
					infos.setSENDID("");
					infos.setSYSID("");
					input.setSyncWhiteListInfo(infos);
					String[] returnArr = syncState(input);
					logger.error("新增黑名单时更改白名单状态--(" + mc + dm + "):state--" + returnArr[0] + ",message--" + returnArr[2]);
				}
			}
			logger.error("新增黑名单时更改白名单状态完成");
		} catch (Exception e) {
			logger.error("新增黑名单时更改白名单状态异常：" + e.toString());
		}
	}

	public String[] syncState(SI_WhitelistSync_OUTServiceStub.SyncWhiteListService input) throws Exception {
		String[] result = new String[5];
//		String uri = "http://sappod.hengtong.com:50000/XISOAPAdapter/MessageServlet?senderParty=&senderService=BS_OA&receiverParty=&receiverService=&interface=SI_WhitelistSync_OUT&interfaceNamespace=http://hengtong.com/OA/WhitelistSync";
		String uri = configProp.getWhiteList();
		SI_WhitelistSync_OUTServiceStub stub = new SI_WhitelistSync_OUTServiceStub(uri);
		stub._getServiceClient().getOptions().setProperty("__CHUNKED__", Boolean.FALSE);
		stub._getServiceClient().getOptions().setTimeOutInMilliSeconds(300000);
		stub._getServiceClient().getOptions().setProperty(HTTPConstants.SO_TIMEOUT,300000);
		stub._getServiceClient().getOptions().setProperty(HTTPConstants.CONNECTION_TIMEOUT,300000);
		HttpTransportPropertiesImpl.Authenticator auth = new HttpTransportPropertiesImpl.Authenticator();
//		auth.setUsername("pouser");
//		auth.setPassword("654321");
		String user = configProp.getHtsysjcmt_po_user();
		String password = configProp.getHtsysjcmt_po_password();
		logger.error("------- user : " + user);
		logger.error("------- password : " + password);
		auth.setUsername(user);
		auth.setPassword(password);

		stub._getServiceClient().getOptions().setProperty(org.apache.axis2.kernel.http.HTTPConstants.AUTHENTICATE,auth);
		SI_WhitelistSync_OUTServiceStub.SyncWhiteListServiceResponse response = stub.sI_WhitelistSync_OUT(input);
		SI_WhitelistSync_OUTServiceStub.ResultInfo resultInfo = response.get_return();
		result[0] = resultInfo.getISTAT();
		result[1] = resultInfo.getITFID();
		result[2] = resultInfo.getMESSAGE();
		result[3] = resultInfo.getSENDID();
		result[4] = resultInfo.getSYSID();
		logger.error("ChangeWhiteListState调用接口完成 ：state--" + result[0] + ",message--" + result[2]);
		return result;
	}
}
