package weaver.interfaces.zjrb.workflow.action;

import com.weaver.esb.client.EsbClient;
import com.weaver.esb.spi.EsbService;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.general.BaseBean;
import weaver.soa.workflow.request.RequestInfo;
import weaver.conn.RecordSet;

import java.util.UUID;

/**
 * Online custom action interface
 */
public class ActionUndwrtSc extends BaseBean implements Action{
    /**
     * After selecting aciton after the process path node, this method will be executed after the node is submitted.
     */
    @Override
    public String execute(RequestInfo request) {
        BaseBean baseBean = new BaseBean();
        String request_type = "01170126";
        String uuid = UUID.randomUUID().toString();
        String sender = "33000100314";
        String server_version = "00000001";
        String user = "33000100314";
        String password = "db42eeb3-4bb3-4344-ab36-78c57020d74b";

        String requestId = request.getRequestid();
        String tablename = request.getRequestManager().getBillTableName();
// 1.投保单传业务号，批单不传业务号；2.商业险传被保险人，交强险传投保人。3.传人保分和业务号
        RecordSet rs = new RecordSet();
        rs.execute("select * from "+tablename+" where requestid =  "+requestId);
        rs.next();
		
        
        String licensenomain = rs.getString("licenseno");
        String tpjzrqmain = rs.getString("tpjzrq");
        String rulenomain = rs.getString("ruleno");
		int piccsocremain = Util.getIntValue(rs.getString("piccscore"));
		String busno = rs.getString("tdhpdh");
		String vinno = rs.getString("vinno");
		String insuredname;
		String insuredcodemain;
		String inflag;
		if (busno.substring(1,4).equals("DZA")){
			insuredcodemain = rs.getString("applycode");
			insuredname = rs.getString("applyname");
			inflag = "2";
		}else {
			insuredcodemain = rs.getString("insuredcode");
			insuredname = rs.getString("insuredname");
			inflag = "1";
		}
		String no = "";
		if (busno.substring(0,1).equals("T")) {
			no = busno;
		}
		String riskcode = busno.substring(1,4);
        String eventKey = "Undwrt_Tp_Sc";
//        String params_d = "{\"appkey\":\"64caed2d-ab47-4116-b1be-6caec02a2fa1\",\"timestamp\":\"\",\"params\":{\"request_type\":\"\",\"uuid\":\"12345678998765432123456\",\"sender\":\"\",\"server_version\":\"\",\"user\":\"\",\"password\":\"\",\"flowintime\":\"\",\"requestbody\":[{\"applicCode\":\""+insuredcodemain+"\",\"driverInsurance\":\"\",\"finishDate\":\""+tpjzrqmain+"\",\"flag\":\"\",\"insuredFlag\":\"\",\"licenseNo\":\""+licensenomain+"\",\"passengerInsurance\":\"\", \"ruleno\":\""+rulenomain+"\",\"theftAndRescue\":\"\",\"thirdParty\":\"\",\"valid\":\"\",\"minNetPremium\":\"\",\"vehicleDamage\":\"\",\"isDoor\":\"\"}]}}";

        String params_d = "{\"request_type\":\""+request_type+"\",\"uuid\":\""+uuid+"\",\"sender\":\""+sender+"\",\"server_version\":\""+server_version+"\",\"user\":\""+user+"\",\"password\":\""+password+"\",\"flowintime\":\"\",\"requestbody\":[{\"applicCode\":\""+insuredcodemain+"\",\"driverInsurance\":0,\"finishDate\":\""+tpjzrqmain+"\",\"flag\":\"1\",\"insuredFlag\":\""+inflag+"\",\"licenseNo\":\""+licensenomain+"\",\"passengerInsurance\":0,\"ruleno\":\""+rulenomain+"\",\"theftAndRescue\":0,\"thirdParty\":0,\"valid\":\"1\",\"minNetPremium\":0,\"vehicleDamage\":0,\"isDoor\":\"0\",\"businessNo\":\""+no+"\",\"piccScoreAmount\":"+piccsocremain+",\"handlerUser\":\"\",\"riskCode\":\""+riskcode+"\",\"insuredName\":\""+insuredname+"\",\"frameNo\":\""+vinno+"\"}]}";
        baseBean.writeLog("params_d:"+params_d);
        //获取 ESB 服务
        EsbService service = EsbClient.getService();
        String result = service.execute(eventKey, params_d);
        baseBean.writeLog("result:"+result);

        return Action.SUCCESS;
    }
}
