package weaver.interfaces.htsc.zbtybx;

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

import java.util.*;
import java.util.stream.Collectors;

/**
 * 移动报销插入建模表单
 */

public class WorkflowtoModeAction extends BaseBean implements Action {

    private static RecordSet fydrs = new RecordSet();

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSetTrans rst = new RecordSetTrans();
        String requestid = requestInfo.getRequestid();
        writeLog("----------------------WorkflowtoModeAction----------------------------");
        try {
            Map<String, Object> map = getWorkflowData(requestid);
            //流程编号
            String lcbh = Util.null2String(map.get("lcbh"));
            //流程名称
            String lcmc = Util.null2String(map.get("lcmc"));
            //流程类型
            String lclx = Util.null2String(map.get("lclx"));
            //查询主表信息
            Map<Object, Object> mainInfo = getMainInfo(requestInfo);
            //拟稿时间
            String ngsj = Util.null2String(mainInfo.get("ngsj"));
            //拟稿人工号
            String ngrgh = Util.null2String(mainInfo.get("ngrgh"));
            //流程类型为空，返回
            if ("".equals(lclx)) {
                return Action.FAILURE_AND_CONTINUE;
            }
            //关闭自动提交
            rst.setAutoCommit(false);
            //总部通用报销和分支机构通用报销转建模
            if ("zbtybx".equals(lclx) || "fzjgtybx".equals(lclx)) {
                //查询明细表信息
                List<Object> dtList = getDetailInfo(requestInfo, 8);
                for (Object detilinfo : dtList) {
                    Map<Object, Object> row = (Map) detilinfo;
                    //预算科目id
                    String yskmid = Util.null2String(row.get("yskmid"));   //要去头
                    //费用承担部门
                    String fycdbm = Util.null2String(row.get("fysjcdbm"));
                    //费用承担部门负责人
                    String fycdbmfzr = Util.null2String(row.get("fycdbmfzr"));
                    //报销人
                    String bxr = Util.null2String(row.get("bxr"));
                    //报销人部门
                    String bxrbm = Util.null2String(row.get("bxrbm"));
                    //费用发生日期
                    String fyfsrq = Util.null2String(row.get("fyfsrq"));
                    //不含税金额
                    String bhsje = Util.null2String(row.get("bhsje"));
                    //税率
                    String sl = Util.null2String(row.get("slz"));
                    //税金
                    String sj = Util.null2String(row.get("sj"));
                    //价税合计金额
                    String jshjje = Util.null2String(row.get("jshjje"));
                    //事由
                    String sy = Util.null2String(row.get("sy"));
                    //超预算
                    String cys = Util.null2String(row.get("cys"));

                    //去头获取yskmbm
                    String[] arr1 = yskmid.split("_");
                    String yskmbm ="";
                    if (arr1.length>1){
                        yskmbm=arr1[1];
                    }

                    //是否快速报销标识位
                    String sfksbx = "";
                    if("zbtybx".equals(lclx)){
                         sfksbx = Util.null2String(mainInfo.get("bsw"));
                    }

                    Map<String, Object> yskmmap = findYskmData(yskmbm);
                    //预算科目名称
                    String yskmmc = Util.null2String(yskmmap.get("yskmmc"));
                    //上级预算科目名称
                    String sjbmyskmmc = Util.null2String(yskmmap.get("sjbmyskmmc"));
                    //上级预算科目编码
                    String sjbmyskmbm = Util.null2String(yskmmap.get("sjbmyskmbm"));
                    //上上级预算科目编码
                    String ssjbmyskmbm = Util.null2String(yskmmap.get("ssjbmyskmbm"));
                    //上上级预算科目名称
                    String ssjbmyskmmc = Util.null2String(yskmmap.get("ssjbmyskmmc"));

                    //报销人部门负责人
                    String bxrbmfzr = "";
                    if(!"".equals(bxrbm)){
                        String bxbmfzrsql = "select DEPTDIRECTORS from hrmdepartmentdefined where deptid = ?";
                        writeLog("执行的sql------------------------" + bxbmfzrsql + "参数" + bxrbm);
                        rs.executeQuery(bxbmfzrsql, bxrbm);
                        if (rs.next()) {
                            bxrbmfzr = weaver.general.Util.null2String(rs.getString("DEPTDIRECTORS"));
                        }
                    }

                    //报销人工号
                    String bxrgh = "";
                    if (!"".equals(bxr)){
                        String bxrghsql = "select workcode from hrmresource where id = ?";
                        writeLog("执行的sql" + bxrghsql + "参数 ===================" + bxr);
                        rs.executeQuery(bxrghsql, bxr);
                        if (rs.next()) {
                            bxrgh = weaver.general.Util.null2String(rs.getString("workcode"));
                        }
                    }

                    String insertsql = "insert into uf_ydbxxx " +
                            "(requid,lcbh,lcmc,lclx,sfksbx,ngsj,yskmbm,yskmmc,sjyskmbm,sjyskmmc,ssjyskmbm,ssjyskmmc,fycdbm,fycdbmfzr,bxr,bxrbm,bxrbmfzr,fyfsrq,bhsje,sl,sj,jshjje,sy,cys,bxrgh,ngrgh)" +
                            "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                    writeLog("执行的插入sql" + insertsql + "参数" + "sjbmyskmbm" + sjbmyskmbm + "sjbmyskmmc" + sjbmyskmmc + "ssjbmyskmbm" + ssjbmyskmbm + "ssjbmyskmmc" + ssjbmyskmmc);
                    rst.executeUpdate(insertsql, requestid, lcbh, lcmc, lclx, sfksbx, ngsj, yskmbm, yskmmc, sjbmyskmbm, sjbmyskmmc, ssjbmyskmbm, ssjbmyskmmc, fycdbm, fycdbmfzr, bxr, bxrbm, bxrbmfzr, fyfsrq, bhsje, sl, sj, jshjje, sy, cys, bxrgh, ngrgh);
                }
            } else {
                //费用承担部门负责人  默认为空
                String fycdbmfzr = "";
                //费用发生日期    默认为空
                String fyfsrq = "";
                //税率 默认为空
                String sl = "";
                //事由  默认为空
                String sy = "";
                //是否快速报销  除总部通用报销外默认为空
                String sfksbx ="";

                //获取明细表12数据
                List<Map<String, Object>> dtList12 = toListMap(getDetailInfo(requestInfo, 12));
                //获取明细表9数据
                List<Map<String, Object>> dtList9 = toListMap(getDetailInfo(requestInfo, 9));
                //匹配明细表9，12数据
                List<Map<String, Object>> resultList2 = dtList12.stream().map(m -> {
                    m.put("fycdbm", "");
                    m.put("bhsje", "");
                    m.put("sj", "");
                    m.put("jshjje", "");
                    m.put("yjyskmsfcys", "");
                    m.put("qzzsbhsje", "");
                    m.put("zssj", "");
                    m.put("lkysbhsje", "");
                    m.put("lkyssj", "");
                    m.put("qt", "");
                    dtList9.stream().filter(m2 -> Objects.equals(m.get("ccr"), m2.get("ccr"))).forEach(s -> {
                        m.put("fycdbm", s.get("fycdbm"));
                        m.put("bhsje", s.get("bhsje"));
                        m.put("sj", s.get("sj"));
                        m.put("jshjje", s.get("jshjje"));
                        m.put("yjyskmsfcys", s.get("yjyskmsfcys"));
                        m.put("qzzsbhsje", s.get("qzzsbhsje"));
                        m.put("zssj", s.get("zssj"));
                        m.put("lkysbhsje", s.get("lkysbhsje"));
                        m.put("lkyssj", s.get("lkyssj"));
                        m.put("qt", s.get("qt"));
                    });
                    return m;
                }).collect(Collectors.toList());
                resultList2.stream().forEach(s -> {
                    //费用承担部门
                    String fycdbm = Util.null2String(s.get("fycdbm"));
                    //出差人
                    String bxr = Util.null2String(s.get("ccr"));
                    //报销人部门
                    String bxrbm = Util.null2String(s.get("ccrszbm"));
                    //报销人部门负责人对应出差人部门负责人
                    String bxrbmfzr = Util.null2String(s.get("ccrbmfzr"));
                    //不含税金额
                    String bhsje = Util.null2String(s.get("bhsje"));
                    //税金
                    String sj = Util.null2String(s.get("sj"));
                    //价税合计金额
                    String jshjje = Util.null2String(s.get("jshjje"));
                    //超预算
                    String cys = Util.null2String(s.get("yjyskmsfcys"));
                    //住宿不含税金额
                    String qzzsbhsje = Util.null2String(s.get("qzzsbhsje"));
                    //住宿税金
                    String zssj = Util.null2String(s.get("zssj"));
                    //旅客运输不含税
                    String lkysbhsje = Util.null2String(s.get("lkysbhsje"));
                    //旅客运输税金
                    String lkyssj = Util.null2String(s.get("lkyssj"));
                    //其他
                    String qt = Util.null2String(s.get("qt"));

                    //报销人工号

                    String bxrgh = "";
                    if(!"".equals(bxr)){
                        String bxrghsql = "select workcode from hrmresource where id = ?";
                        rs.executeQuery(bxrghsql, bxr);
                        writeLog("执行的sql" + bxrghsql + "参数 ===================" + bxr);
                        if (rs.next()) {
                            bxrgh = Util.null2String(rs.getString("workcode"));
                        }
                    }

                    //住宿费用
                    Double zsfy = Util.getDoubleValue(qzzsbhsje) + Util.getDoubleValue(zssj);
                    //交通费用
                    Double jtfy = Util.getDoubleValue(lkysbhsje) + Util.getDoubleValue(lkyssj);
                    //其他费用
                    Double qtfy = Util.getDoubleValue(qt);

                    String insertsql = "insert into uf_ydbxxx " +
                            "(requid,lcbh,lcmc,lclx,sfksbx,ngsj,yskmbm,yskmmc,sjyskmbm,sjyskmmc,ssjyskmbm,ssjyskmmc,fycdbm,fycdbmfzr,bxr,bxrbm,bxrbmfzr,fyfsrq,bhsje,sl,sj,jshjje,sy,cys,bxrgh,ngrgh)" +
                            "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

                    try {
                        if (zsfy > 0) {
                            String yskmbm = "8601050303";
                            Map<String, Object> yskmmap = findYskmData(yskmbm);
                            //预算科目名称
                            String yskmmc = Util.null2String(yskmmap.get("yskmmc"));
                            //上级预算科目名称
                            String sjbmyskmmc = Util.null2String(yskmmap.get("sjbmyskmmc"));
                            //上级预算科目编码
                            String sjbmyskmbm = Util.null2String(yskmmap.get("sjbmyskmbm"));
                            //上上级预算科目编码
                            String ssjbmyskmbm = Util.null2String(yskmmap.get("ssjbmyskmbm"));
                            //上上级预算科目名称
                            String ssjbmyskmmc = Util.null2String(yskmmap.get("ssjbmyskmmc"));
                            rst.executeUpdate(insertsql, requestid, lcbh, lcmc, lclx, sfksbx, ngsj, yskmbm, yskmmc, sjbmyskmbm, sjbmyskmmc, ssjbmyskmbm, ssjbmyskmmc, fycdbm, fycdbmfzr, bxr, bxrbm, bxrbmfzr, fyfsrq, bhsje, sl, sj, jshjje, sy, cys, bxrgh, ngrgh);
                            writeLog("执行的插入sql" + insertsql + "参数" + "sjbmyskmbm" + sjbmyskmbm + "sjbmyskmmc" + sjbmyskmmc + "ssjbmyskmbm" + ssjbmyskmbm + "ssjbmyskmmc" + ssjbmyskmmc);
                        }
                        if (jtfy > 0) {
                            String yskmbm = "8601050301";
                            Map<String, Object> yskmmap = findYskmData(yskmbm);
                            //预算科目名称
                            String yskmmc = Util.null2String(yskmmap.get("yskmmc"));
                            //上级预算科目名称
                            String sjbmyskmmc = Util.null2String(yskmmap.get("sjbmyskmmc"));
                            //上级预算科目编码
                            String sjbmyskmbm = Util.null2String(yskmmap.get("sjbmyskmbm"));
                            //上上级预算科目编码
                            String ssjbmyskmbm = Util.null2String(yskmmap.get("ssjbmyskmbm"));
                            //上上级预算科目名称
                            String ssjbmyskmmc = Util.null2String(yskmmap.get("ssjbmyskmmc"));
                            rst.executeUpdate(insertsql, requestid, lcbh, lcmc, lclx, sfksbx, ngsj, yskmbm, yskmmc, sjbmyskmbm, sjbmyskmmc, ssjbmyskmbm, ssjbmyskmmc, fycdbm, fycdbmfzr, bxr, bxrbm, bxrbmfzr, fyfsrq, bhsje, sl, sj, jshjje, sy, cys, bxrgh, ngrgh);
                            writeLog("执行的插入sql" + insertsql + "参数" + "sjbmyskmbm" + sjbmyskmbm + "sjbmyskmmc" + sjbmyskmmc + "ssjbmyskmbm" + ssjbmyskmbm + "ssjbmyskmmc" + ssjbmyskmmc);
                        }

                        if (qtfy > 0) {
                            String yskmbm = "8601050302";
                            Map<String, Object> yskmmap = findYskmData(yskmbm);
                            //预算科目名称
                            String yskmmc = Util.null2String(yskmmap.get("yskmmc"));
                            //上级预算科目名称
                            String sjbmyskmmc = Util.null2String(yskmmap.get("sjbmyskmmc"));
                            //上级预算科目编码
                            String sjbmyskmbm = Util.null2String(yskmmap.get("sjbmyskmbm"));
                            //上上级预算科目编码
                            String ssjbmyskmbm = Util.null2String(yskmmap.get("ssjbmyskmbm"));
                            //上上级预算科目名称
                            String ssjbmyskmmc = Util.null2String(yskmmap.get("ssjbmyskmmc"));
                            writeLog("执行的插入sql" + insertsql + "参数" + "sjbmyskmbm" + sjbmyskmbm + "sjbmyskmmc" + sjbmyskmmc + "ssjbmyskmbm" + ssjbmyskmbm + "ssjbmyskmmc" + ssjbmyskmmc);
                            rst.executeUpdate(insertsql, requestid, lcbh, lcmc, lclx, sfksbx, ngsj, yskmbm, yskmmc, sjbmyskmbm, sjbmyskmmc, ssjbmyskmbm, ssjbmyskmmc, fycdbm, fycdbmfzr, bxr, bxrbm, bxrbmfzr, fyfsrq, bhsje, sl, sj, jshjje, sy, cys, bxrgh, ngrgh);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
                rst.commit();
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Action.FAILURE_AND_CONTINUE;
        }
    }

    /**
     * 查询预算科目
     *
     * @param yskmbm
     * @return
     */
    public static Map<String, Object> findYskmData(String yskmbm) {
        Map<String, Object> map = new HashMap<>();
        String querysql = "";
        String yskmmc = "";
        String sjyskm = "";
        fydrs.writeLog("执行的sql---------------" + querysql + "参数" + yskmbm);
        querysql = "select yskmmc,sjyskm,yskmbm from uf_kmpz where yskmbm = ?";
        fydrs.executeQuery(querysql, yskmbm);
        if (fydrs.next()) {
            yskmmc = Util.null2String(fydrs.getString("yskmmc"));
            sjyskm = Util.null2String(fydrs.getString("sjyskm"));
            map.put("yskmmc", yskmmc);
        }
        if ("".equals(sjyskm)) {
            return map;
        }
        //上级预算科目名称
        String sjbmyskmmc = "";
        //上级预算科目编码
        String sjbmyskmbm = "";
        String ssjyskm = "";
        fydrs.writeLog("执行的sql" + querysql + "参数" + sjyskm);
        fydrs.executeQuery(querysql, sjyskm);
        if (fydrs.next()) {
            sjbmyskmbm = Util.null2String(fydrs.getString("yskmbm"));
            sjbmyskmmc = Util.null2String(fydrs.getString("yskmmc"));
            ssjyskm = Util.null2String(fydrs.getString("sjyskm"));
            map.put("sjbmyskmbm", sjbmyskmbm);
            map.put("sjbmyskmmc", sjbmyskmmc);
        }
        if ("".equals(ssjyskm)) {
            return map;
        }
        //上上级预算科目名称
        String ssjbmyskmmc = "";
        //上上级预算科目编码
        String ssjbmyskmbm = "";
        fydrs.writeLog("执行的sql" + querysql + "参数" + ssjyskm);
        fydrs.executeQuery(querysql, ssjyskm);
        if (fydrs.next()) {
            ssjbmyskmbm = Util.null2String(fydrs.getString("yskmbm"));
            ssjbmyskmmc = Util.null2String(fydrs.getString("yskmmc"));
            map.put("ssjbmyskmbm", ssjbmyskmbm);
            map.put("ssjbmyskmmc", ssjbmyskmmc);
        }
        fydrs.writeLog("执行的sql---------------" + querysql + "结果" + map.toString());
        return map;
    }

    /**
     * 获取流程数据
     *
     * @param requestid
     * @return
     */
    public static Map<String, Object> getWorkflowData(String requestid) {
        Map<String, Object> map = new HashMap<>();
        //流程编号
        String lcbh = "";
        //流程名称
        String lcmc = "";
        //流程类型
        String lclx = "";
        if(!"".equals(requestid)){
            String sql = "select b.unnumber as lclx, a.REQUESTMARK ,a.REQUESTNAME from workflow_requestbase a join workflow_base b " +
                    "on a.WORKFLOWID = b.id where a.REQUESTID = ?";
            fydrs.writeLog("执行的sql =====================" + sql + "参数" + requestid);
            fydrs.executeQuery(sql, requestid);
            if (fydrs.next()) {
                //流程编号
                lcbh = Util.null2String(fydrs.getString("REQUESTMARK"));
                //流程名称
                lcmc = Util.null2String(fydrs.getString("REQUESTNAME"));
                //流程类型
                lclx = Util.null2String(fydrs.getString("lclx"));
                map.put("lcbh", lcbh);
                map.put("lcmc", lcmc);
                map.put("lclx", lclx);
            }
            fydrs.writeLog("执行的sql =====================" + sql + "结果" + map.toString());
        }
        return map;
    }

    /**
     * List<Object>转List<Map<String,Object>>
     *
     * @param objects
     * @return
     */
    public static List<Map<String, Object>> toListMap(List<Object> objects) {
        List<Map<String, Object>> result = new ArrayList<>();
        for (Object o : objects) {
            Map<String, Object> row = (Map) o;
            result.add(row);
        }
        return result;
    }


    /**
     * 查询主表信息
     *
     * @param requestInfo
     * @return
     */
    public static Map<Object, Object> getMainInfo(RequestInfo requestInfo) {
        Map<Object, Object> 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<Object> getDetailInfo(RequestInfo requestInfo, int num) {
        num = num - 1;
        DetailTable detailTable = requestInfo.getDetailTableInfo().getDetailTable(num);

        Row[] rows = detailTable.getRow();
        List<Object> sublist = new ArrayList<>();
        for (int i = 0; i < rows.length; i++) {
            Row row = rows[i];
            Map<Object, Object> 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(), weaver.general.Util.null2String(cell.getValue()));
            }
        }
        return sublist;
    }

}
