package weaver.interfaces.htsc.zbtyfk;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;


public class UpMainAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String back = "";
        try{
            String requestid = requestInfo.getRequestid();
            String tableName = requestInfo.getRequestManager().getBillTableName();
            String lessTenSql = "update "+tableName+" set ysgly='24735',ejbmfzrlcsp='22850' where requestid='"+requestid+"'";
            String moreTenSql = "update "+tableName+" set ysgly='24735',ejbmfzrlcsp='22850',sxcwglcsp='12016',fgldlcsp='22850' where requestid='"+requestid+"'";
            writeLog("lessTenSql:"+lessTenSql);
            writeLog("moreTenSql:"+moreTenSql);
            writeLog("流程主表名称是："+tableName+",requestid:"+requestid);
            RecordSet rs = new RecordSet();
            String fycdbm8 = Util.null2String(getDtData(tableName,requestid,"fysjcdbm","8"));
            String fycdbm9 = Util.null2String(getDtData(tableName,requestid,"fysjcdbm","9"));
            writeLog("fycdbm8:"+fycdbm8+",fycdbm9:"+fycdbm9);
            //总部字段更新
            String sql = "select szbm from "+tableName+" where  requestid='"+requestid+"'";
            rs.execute(sql);
            if (rs.next()){
//                System.out.println("24".equals(rs.getString("szbm")));
                if ("24".equals(rs.getString("szbm"))){
                    rs.executeUpdate("update "+tableName+" set fyhjshlcsp='23285' where requestid='"+requestid+"'");
                }
            }

            if ("1091".equals(fycdbm8) || "1091".equals(fycdbm9)){//营业部
                if ("1091".equals(fycdbm8)){
                    writeLog("inner 8");
                    String jshjje8 = Util.null2String(getDtData(tableName,requestid,"jshjje","8"));
                    writeLog("明细表8中付款金额："+Util.null2String(jshjje8)); //获取明细表8中的付款金额
                    float jshjje = Util.getFloatValue(Util.null2String(jshjje8));
                    if (jshjje <= 100000.0){
                        writeLog("8---<=10万");//<=10万
                        if (rs.executeUpdate(lessTenSql)){
                            back = SUCCESS;
                        }else {
                            back = FAILURE_AND_CONTINUE;
                            requestInfo.getRequestManager().setMessageid("104");
                            requestInfo.getRequestManager().setMessagecontent("found error:update data when dt8.jshjje less then 10万");
                        }
                    }else {
                        writeLog("8--->10万");//>10万
                        if (rs.executeUpdate(moreTenSql)){
                            back = SUCCESS;
                        }else {
                            back = FAILURE_AND_CONTINUE;
                            requestInfo.getRequestManager().setMessageid("104");
                            requestInfo.getRequestManager().setMessagecontent("found error:update data when dt8.jshjje more then 10万");
                        }
                    }
                }else {
                    writeLog("inner 9");
                    String jshjje9 = Util.null2String(getDtData(tableName,requestid,"rmbje","9"));
                    writeLog("明细表9中的付款金额："+Util.null2String(jshjje9));
                    float rmbje = Util.getFloatValue(Util.null2String(jshjje9));
                    if (rmbje <= 100000.0){
                        writeLog("9---<=10万");//<=10万
                        if (rs.executeUpdate(lessTenSql)){
                            back = SUCCESS;
                        }else {
                            back = FAILURE_AND_CONTINUE;
                            requestInfo.getRequestManager().setMessageid("104");
                            requestInfo.getRequestManager().setMessagecontent("found error:update data when dt9.rmbje less then 10万");
                        }
                    }else {
                        writeLog("9--->10万");//>10万
                        if (rs.executeUpdate(moreTenSql)){
                            back = SUCCESS;
                        }else {
                            back = FAILURE_AND_CONTINUE;
                            requestInfo.getRequestManager().setMessageid("104");
                            requestInfo.getRequestManager().setMessagecontent("found error:update data when dt9.rmbje more then 10万");
                        }
                    }
                }
            }else {
                back = SUCCESS;
            }
        }catch (Exception e){
            e.getMessage();
            back = FAILURE_AND_CONTINUE;
            requestInfo.getRequestManager().setMessageid("104");
            requestInfo.getRequestManager().setMessagecontent("found error when Execution logic");
        }
        writeLog("此时的back为："+back);
//        System.out.println(back);
        return back;
    }

    /**
     * 获取明细表数据
     */
    private static String getDtData(String mainTableName,String requestid,String dtFieldName,String dtTableNum){
        String mainid = "";
        String dtFieldValue = "";
        RecordSet rsFun = new RecordSet();
        String sql = "select id from "+mainTableName+" where requestid = '"+requestid+"'";
        new BaseBean().writeLog(sql);
        rsFun.execute(sql);
        if (rsFun.next()){
            mainid = Util.null2String(rsFun.getString("id"));
        }
        if (!"".equals(mainid)){
            sql = "select "+dtFieldName+" from "+mainTableName+"_dt"+dtTableNum+" where mainid='"+mainid+"'";
            new BaseBean().writeLog(sql);
            rsFun.execute(sql);
            if (rsFun.next()){
                dtFieldValue = rsFun.getString(dtFieldName);
            }
        }
        return dtFieldValue;
    }
}
