package weaver.interfaces.htsc.yfkqrlc.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.soa.workflow.request.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ActionsUtil {

    //    获取主表数据
    public static Map getMainInfo(RequestInfo requestInfo) {
        Map map = new HashMap();
        Property[] property = requestInfo.getMainTableInfo().getProperty();
        for (int i = 0; i < property.length; i++) {
            map.put(property[i].getName().toLowerCase(), Util.null2String(property[i].getValue()));
        }
        return map;
    }


    //    获取明细表数据
    public static List getDetailInfo(RequestInfo requestInfo, int num) {
        DetailTable detailTable = requestInfo.getDetailTableInfo().getDetailTable(num);
        //dtltable数组中的行数据集合
        Row[] rows = detailTable.getRow();
        List sublist = new ArrayList();
        for (int i = 0; i < rows.length; i++) {
            Row row = rows[i];
            Map onerow = new HashMap();
            sublist.add(onerow);
            Cell[] cells = row.getCell();
            for (int j = 0; j < cells.length; j++) {
                Cell cell = cells[j];
                onerow.put(cell.getName(), Util.null2String(cell.getValue()));
            }
        }
        return sublist;
    }


    /**
     * @param fieldName   获取的字段名
     * @param tableNum    明细表Num
     * @param requestInfo 参数
     * @param interval    间隔符
     * @Describe 通过RequestInfo获取明细表指定字段
     */
    public static String getDataStringByRequestInfo(String fieldName, int tableNum, RequestInfo requestInfo, String interval) {
        String returnResult = "";
        List dtData = getDetailInfo(requestInfo, tableNum - 1);
        if (dtData.size() != 0) {
            List<String> htbhList = new ArrayList<>(dtData.size());
            for (Object dtOne : dtData) {
                Map oneRow = (Map) dtOne;
                String aFieldVal = Util.null2String(oneRow.get(fieldName));
                // 去重去空处理
                if ((!"".equals(aFieldVal)) && (!" ".equals(aFieldVal)) && (!htbhList.contains(aFieldVal))) {
                    htbhList.add(aFieldVal);
                }
            }
            htbhList.remove(null);
            returnResult = StringUtils.join(htbhList, interval);
        }
        return returnResult;
    }

    /**
     * @param requestId    付款流程的requestid
     * @return Map(String, Object)
     * @Describe 根据付款流程编号，查询已经触发的预付款流程，并获取预付款流程的流程编号、开票方、已核销金额参数
     */
    public static Map<String, Object> yfkParamsMap(String requestId) {
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String sql = "";
        String yfkTableName = new PropBean().getPropName("yfkTableName");
        Map<String, Object> resultMap = new HashMap<>();
        // 符合要求的预付款流程编号
        List<String> rightRequestIds = new ArrayList<>();
        // 获取已触发的预付款流程的流程编号(已办结并且不是终止)
        sql = "select a.requestid,currentnodetype,isstopped from " + yfkTableName + " a JOIN workflow_requestbase b " +
                "on a.GLYFKLC = b.requestid where GLYFKLC = '" + requestId + "'";
        bb.writeLog("即将执行的sql是：" + sql);
        rs.executeQuery(sql);
        while (rs.next()) {
            String currentnodetype = Util.null2String(rs.getString("currentnodetype"));
            String isstopped = Util.null2String(rs.getString("isstopped"));
            if ((!"1".equals(isstopped)) && ("3".equals(currentnodetype))) {
                // 已办结并且不是终止
                rightRequestIds.add(Util.null2String(rs.getString("requestid")));
            }
        }
        bb.writeLog("==============获取到归档并且不是终止的预付款流程编号有："+ JSONObject.toJSONString(rightRequestIds));
        String yfkReqIds = StringUtils.join(rightRequestIds,",");
        List<String> kpf = new ArrayList<>();
            // 1、获取流程编号
            sql = "select REPLACE(wm_concat_old(lcbh), ',', '；') as lcbh from " + yfkTableName + " where requestid in ("+yfkReqIds+")";
            bb.writeLog("==========获取流程编号的sql是："+sql);
            rs.executeQuery(sql);
            if (rs.next()) {
                resultMap.put("lcbh",Util.null2String(rs.getString("lcbh")).replaceAll("&nbsp;", " ").replaceAll("<br>", ""));
            }
            // 2、获取开票方
            // 2.1、获取增值税的销方名称
            sql = "select REPLACE(wm_concat_old(xfmc), ',', '；') as xfmcs from "+yfkTableName+"_dt5 " +
                    "where mainid in (select id from "+yfkTableName+" where requestid in ("+yfkReqIds+"))";
            bb.writeLog("获取明细表5销方名称的sql是："+sql);
            rs.executeQuery(sql);
            if (rs.next()) {
                String xfmcs = Util.null2String(rs.getString("xfmcs"));
                if (!"".equals(xfmcs)){
                    kpf.add(xfmcs);
                }
            }
            // 2.2、获取其他票的开票方
            sql = "select REPLACE(wm_concat_old(kpf), ',', '；') as kpfs from "+yfkTableName+"_dt6 " +
                    "where mainid in (select id from "+yfkTableName+" where requestid in ("+yfkReqIds+"))";
            bb.writeLog("获取明细表6开票方的sql是："+sql);
            rs.executeQuery(sql);
            if (rs.next()) {
                String kpfs = Util.null2String(rs.getString("kpfs"));
                if (!"".equals(kpfs)){
                    kpf.add(kpfs);
                }
            }
            resultMap.put("kpf",StringUtils.join(kpf,"；").replaceAll("&nbsp;", " ").replaceAll("<br>", ""));
            // 3、已核销金额
            sql = "select SUM(jshjze) as yhxje from "+yfkTableName+"_dt7 " +
                    "where mainid in (select id from "+yfkTableName+" where requestid in ("+yfkReqIds+"))";
            rs.executeQuery(sql);
            if (rs.next()){
                resultMap.put("yhxje",Util.null2String(rs.getString("yhxje")));
            }
        return resultMap;
    }

    public static int getDataIdByUUID(String uuid, String uf_tableName) {
        RecordSet rs = new RecordSet();
        int dataid = -1;
        String sql = "select id from " + uf_tableName + " where modeuuid = ?";
        if (rs.executeQuery(sql, uuid) && rs.next()) {
            dataid = Util.getIntValue(rs.getString(1));
        }
        return dataid;
    }

}