package weaver.interfaces.htsc.payment;

import com.engine.common.util.ServiceUtil;
import com.engine.htsc.payment.service.impl.PaymentServiceImpl;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.util.ArrayList;
import java.util.List;

public class QuickApplyAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        String rid = requestInfo.getRequestid();
        RequestManager rm = requestInfo.getRequestManager();
        String tableName = rm.getBillTableName();
        writeLog(String.format("快速报销,requestid:%s; tableName=%s", rid, tableName));
        String bswNew = "";//标识位
        RecordSet rs = new RecordSet();
        rs.execute("SELECT bsw FROM "+tableName+" WHERE REQUESTID = "+rid);
        if (rs.next()){
            bswNew = Util.null2String(rs.getString("bsw"));
        }
        try {
            //快速报销
            if(bswNew.equals("ksbx")) {
                Integer numDt8 = 0;
                RecordSet sqlFindDt8 = new RecordSet();
                sqlFindDt8.execute("SELECT id,mainid,bxr,fysjcdbm,jshjje,bhsje,yskm,yskmid,sy FROM " + tableName + "_Dt8 " +
                        "WHERE MAINID = (SELECT id FROM " + tableName + " WHERE REQUESTID = " + rid + ")");
                while (sqlFindDt8.next()) {
                    numDt8++;
                    Integer mainid = Util.getIntValue(sqlFindDt8.getString("mainid"));
                    Integer bxr = Util.getIntValue(sqlFindDt8.getString("bxr"));
                    Integer fysjcdbm = Util.getIntValue(sqlFindDt8.getString("fysjcdbm"));
                    Integer jshjje = Util.getIntValue(sqlFindDt8.getString("jshjje"));
                    Integer bhsje = Util.getIntValue(sqlFindDt8.getString("bhsje"));
                    String yskm = Util.null2String(sqlFindDt8.getString("yskm"));
                    String yskmid = Util.null2String(sqlFindDt8.getString("yskmid"));
                    String sy = Util.null2String(sqlFindDt8.getString("sy"));
                    String browserValue = ServiceUtil.getService(PaymentServiceImpl.class).getAccountCode(yskm);
                    String hjkmid = "";
                    String hjkm = "";
                    if (!browserValue.equals("")) {
                        String[] valueList = browserValue.split("@");
                        hjkmid = valueList[0];
                        hjkm = valueList[0].split("_")[1];
                    }
                    String yskmidNew =yskmid.substring(yskmid.indexOf("_")+1,yskmid.length()) ;
                    String yskmmc = "";
                    RecordSet findYsName = new RecordSet();
                    findYsName.execute("SELECT yskmmc FROM uf_kmpz WHERE YSKMBM ='"+yskmidNew+"'");
                    if (findYsName.next()){
                        yskmmc = Util.null2String(findYsName.getString("yskmmc"));
                    }
                    System.out.println(mainid+"mainid,"+bxr+"bxr,"+fysjcdbm+"fysjcdbm,"+jshjje+"jshjje,"+bhsje+"bhsje,"+yskm+"yskm,"+yskmmc+"yskmmc,"+sy+"sy,"+hjkmid+"hjkmid,"+hjkm+"hjkm");
                    RecordSet insertDt12 = new RecordSet();
                    insertDt12.execute("INSERT INTO " + tableName + "_DT12 (mainid,fysjcdr,fysjcdbm,yskm,yskmmc,je,sy,hjkmid,hjkm) " +
                            "values(" + mainid + "," + bxr + "," + fysjcdbm + ",'" + yskm + "','" + yskmmc + "'," + bhsje + ",'" + sy + "','" + hjkmid + "','" + hjkm + "')");
                }
                if (numDt8 == 0) {
                    RecordSet sqlFindDt9 = new RecordSet();
                    sqlFindDt9.execute("SELECT id,mainid,fysjcdbm,bxr,wbje,yskm,yskmid,bz,hl,rmbje,sy FROM " + tableName + "_Dt9 " +
                            "WHERE MAINID = (SELECT id FROM " + tableName + " WHERE REQUESTID = " + rid + ")");
                    while (sqlFindDt9.next()) {
                        Integer mainid = Util.getIntValue(sqlFindDt9.getString("mainid"));
                        Integer fysjcdbm = Util.getIntValue(sqlFindDt9.getString("fysjcdbm"));
                        Integer bxr = Util.getIntValue(sqlFindDt9.getString("bxr"));
                        Integer wbje = Util.getIntValue(sqlFindDt9.getString("wbje"));
                        String yskm = Util.null2String(sqlFindDt9.getString("yskm"));
                        String yskmid = Util.null2String(sqlFindDt9.getString("yskmid"));
                        Integer bz = Util.getIntValue(sqlFindDt9.getString("bz"));
                        Integer hl = Util.getIntValue(sqlFindDt9.getString("hl"));
                        Integer rmbje = Util.getIntValue(sqlFindDt9.getString("rmbje"));
                        String sy = Util.null2String(sqlFindDt9.getString("sy"));
                        String browserValue = ServiceUtil.getService(PaymentServiceImpl.class).getAccountCode(yskm);
                        String hjkmid = "";
                        String hjkm = "";
                        if (!browserValue.equals("")) {
                            String[] valueList = browserValue.split("@");
                            hjkmid = valueList[0];
                            hjkm = valueList[0].split("_")[1];
                        }
                        String yskmidNew =yskmid.substring(yskmid.indexOf("_")+1,yskmid.length()) ;
                        String yskmmc = "";
                        RecordSet findYsName = new RecordSet();
                        findYsName.execute("SELECT yskmmc FROM uf_kmpz WHERE YSKMBM ='"+yskmidNew+"'");
                        if (findYsName.next()){
                            yskmmc = Util.null2String(findYsName.getString("yskmmc"));
                        }
                        System.out.println(mainid+"mainid,"+bxr+"bxr,"+fysjcdbm+"fysjcdbm,"+yskm+"yskm,"+yskmid+"yskmid,"+yskmmc+"yskmmc,"+wbje+"wbje,"+bz+"bz,"+hl+"hl,"+rmbje+"rmbje,"+sy+"sy,"+hjkmid+"hjkmid,"+hjkm+"hjkm,");
                        RecordSet insertDt12 = new RecordSet();
                        insertDt12.execute("INSERT INTO " + tableName + "_DT12 (mainid,fysjcdr,fysjcdbm,yskm,yskmmc,je,sjbz,hl,zhrmb,sy,hjkmid,hjkm) " +
                                "values(" + mainid + "," + bxr + "," + fysjcdbm + ",'" + yskm + "','" + yskmmc + "'," + wbje + "," + bz + "," + hl + "," + rmbje + ",'" + sy + "','" + hjkmid + "','" + hjkm + "')");
                    }
                }
                //预留调用数字接口



            }
            return SUCCESS;
        } catch (Exception e) {
            rm.setMessagecontent("快速报销信息计算错误,原因:" + e.getMessage());
            rm.setMessageid("500");
            return FAILURE_AND_CONTINUE;

        }

    }

}
