package weaver.interfaces.zjrb.workflow.action;

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

import java.util.UUID;

public class AutoInsuranceEsbAction implements Action{

    @Override
    public String execute(RequestInfo request) {

        RecordSet rs = new RecordSet();

        BaseBean baseBean = new BaseBean();
        String request_type = "01170126";
        String uuid = UUID.randomUUID().toString();
        String sender = "33000100314";
        String server_version = "00000000";
        String user = "33000100314";
        String password = "1234567";

        String requestId = request.getRequestid();
        String tablename = request.getRequestManager().getBillTableName();
        // 1.投保单传业务号，批单不传业务号；2.商业险传被保险人，交强险传投保人。3.传人保分和业务号

        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 insuredcodemain = "";
        String inflag = "";
        if (busno.substring(1,4).equals("DZA")){
            insuredcodemain = rs.getString("applycode");
            inflag = "2";
        }else {
            insuredcodemain = rs.getString("insuredcode");
            inflag = "1";
        }
        String no = "";
        if (busno.substring(0,1).equals("T")) {
            no = busno;
        }else {
            no = "";
        }

        String eventKey = "undwrt";
//        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\":\"\"}]}";
        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;
    }
}
