package com.engine.htsc.zbtybx.dao;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;

import java.util.HashMap;
import java.util.Map;

public class YdbxDataImportCMD extends AbstractCommonCommand<Map<String, Object>> {
    private static RecordSet fydrs = new RecordSet();
    @Override
    public BizLogContext getLogContext() {
        return null;
    }
    public YdbxDataImportCMD(User user, Map<String,Object> params){
        this.user = user;
        this.params = params;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> back = new HashMap<>();
        String lc = Util.null2String(params.get("lclx"));
        String tablename = getTableName(lc);
        if ("".equals(tablename)){
            back.put("result","表名为空");
            return back;
        }
        if ("zbtybx".equals(lc)){
            tybxInsertData(tablename,lc);
            back.put("result","总部通用报销同步完成");
        }
        if ("fzjgtybx".equals(lc)){
            tybxInsertData(tablename,lc);
            back.put("result","分支机构通用报销同步完成");
        }
        if ("fzjgclbx".equals(lc)){
            clbxInsertData(tablename);
            back.put("result","分支机构差旅报销同步完成");
        }
        if ("zbclbx".equals(lc)){
            clbxInsertData(tablename);
            back.put("result","总部差旅报销同步完成");
        }
        return back;
    }


    /**
     * 差旅报销历史数据导入
     *
     * @param tablename
     * @return
     */
    public static String clbxInsertData(String tablename) {
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        bb.writeLog("--------------------clbxInsertData start-------------------------");
        String clbxsql = "select dt12.ccr,dt12.ccrszbm,dt12.ccrbmfzr, o.ngrgh, o.requestid, o.REQUESTMARK,o.REQUESTNAME, o.unnumber,o.ngsj,dt9.fycdbm,dt9.bhsje,dt9.sj,dt9.jshjje,dt9.yjyskmsfcys,dt9.qzzsbhsje,dt9.zssj,dt9.lkysbhsje,dt9.lkyssj,dt9.qt ,dt9.ccr from "+tablename+"_DT12 dt12 " +
                "join (select m.ngrgh, m.requestid, b.unnumber, m.id,m.ngsj,wr.REQUESTMARK,wr.REQUESTNAME from "+tablename+" m " +
                "join workflow_requestbase wr on wr.REQUESTID = m.REQUESTID join workflow_base b on b.id=wr.workflowid where wr.currentnodetype = 3) o on dt12.mainid = o.id " +
                "join "+tablename+"_DT9 dt9 on dt9.ccr = dt12.ccr and o.id = dt9.mainid ";

        RecordSetTrans rst = new RecordSetTrans();

        rs.executeQuery(clbxsql);
        rst.setAutoCommit(false);
        while (rs.next()) {
            //出差人    对应报销人
            String bxr = Util.null2String(rs.getString("ccr"));
            //出差人所在部门   对应报销人所在部门
            String bxrbm = Util.null2String(rs.getString("ccrszbm"));
            //出差人所在部门负责人 对应报销人所在部门负责人
            String bxrbmfzr = Util.null2String(rs.getString("ccrbmfzr"));
            //拟稿人工号
            String ngrgh = Util.null2String(rs.getString("ngrgh"));
            //requestid
            String requid = Util.null2String(rs.getString("requestid"));
            //流程编号
            String lcbh = Util.null2String(rs.getString("REQUESTMARK"));
            //流程名称
            String lcmc = Util.null2String(rs.getString("REQUESTNAME"));
            //流程类型
            String lclx = Util.null2String(rs.getString("unnumber"));
            //拟稿时间
            String ngsj = Util.null2String(rs.getString("ngsj"));
            //费用承担部门
            String fycdbm = Util.null2String(rs.getString("fysjcdbm"));
            //不含税金额
            String bhsje = Util.null2String(rs.getString("bhsje"));
            //税金
            String sj = Util.null2String(rs.getString("sj"));
            //价税合计金额
            String jshjje = Util.null2String(rs.getString("jshjje"));
            //超预算
            String cys = Util.null2String(rs.getString("yjyskmsfcys"));
            //住宿不含税金额
            String qzzsbhsje = Util.null2String(rs.getString("qzzsbhsje"));
            //住宿税金
            String zssj = Util.null2String(rs.getString("zssj"));
            //旅客运输不含税
            String lkysbhsje = Util.null2String(rs.getString("lkysbhsje"));
            //旅客运输税金
            String lkyssj = Util.null2String(rs.getString("lkyssj"));
            //其他
            String qt = Util.null2String(rs.getString("qt"));

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

            //费用承担部门负责人  默认为空
            String fycdbmfzr = "";
            //费用发生日期    默认为空
            String fyfsrq = "";
            //税率 默认为空
            String sl = "";
            //事由  默认为空
            String sy = "";
            //是否快速报销 默认为空
            String sfksbx = "";

            //住宿费用
            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, requid, 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);
                    bb.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, requid, 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);
                    bb.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"));
                    bb.writeLog("执行的插入sql" + insertsql + "参数" + "sjbmyskmbm" + sjbmyskmbm + "sjbmyskmmc" + sjbmyskmmc + "ssjbmyskmbm" + ssjbmyskmbm + "ssjbmyskmmc" + ssjbmyskmmc);
                    rst.executeUpdate(insertsql, requid, 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();
                bb.writeLog("------------------错误原因："+e.getMessage()+"---------------requestid:"+requid);
                return Action.FAILURE_AND_CONTINUE;
            }
        }
        rst.commit();
        return Action.SUCCESS;
    }

    /**
     * 通用报销历史数据导入
     *
     * @param tablename
     * @return
     */
    public static String tybxInsertData(String tablename,String lc) {
        RecordSet rs = new RecordSet();
        RecordSetTrans rst = new RecordSetTrans();
        BaseBean bb = new BaseBean();
        String tybxsql = "";
        if("zbtybx".equals(lc)){
            tybxsql = "select o.ngrgh, o.requestid, o.REQUESTMARK,o.REQUESTNAME, o.unnumber,o.bsw,o.ngsj, d.yskmid,d.fysjcdbm,d.fycdbmfzr,d.bxr,d.bxrbm,d.fyfsrq,d.bhsje,d.slz,d.sj,d.jshjje,d.sy,d.cys  " +
                    "from " + tablename + "_dt8 d join " +
                    "(select m.ngrgh, m.requestid, b.unnumber, m.id, m.bsw,m.ngsj,wr.REQUESTMARK,wr.REQUESTNAME from " + tablename + " m join workflow_requestbase wr on wr.REQUESTID = m.REQUESTID " +
                    "join workflow_base b on b.id=wr.workflowid where wr.currentnodetype = 3) o on d.mainid = o.id";
        }else {
            tybxsql = "select o.ngrgh, o.requestid, o.REQUESTMARK,o.REQUESTNAME, o.unnumber,o.ngsj, d.yskmid,d.fysjcdbm,d.fycdbmfzr,d.bxr,d.bxrbm,d.fyfsrq,d.bhsje,d.slz,d.sj,d.jshjje,d.sy,d.cys  " +
                    "from " + tablename + "_dt8 d join " +
                    "(select m.ngrgh, m.requestid, b.unnumber, m.id,m.ngsj,wr.REQUESTMARK,wr.REQUESTNAME from " + tablename + " m join workflow_requestbase wr on wr.REQUESTID = m.REQUESTID " +
                    "join workflow_base b on b.id=wr.workflowid where wr.currentnodetype = 3) o on d.mainid = o.id";
        }

        rs.executeQuery(tybxsql);
        rst.setAutoCommit(false);
        while (rs.next()) {
            //拟稿人工号
            String ngrgh = Util.null2String(rs.getString("ngrgh"));
            //requestid
            String requid = Util.null2String(rs.getString("requestid"));
            //流程编号
            String lcbh = Util.null2String(rs.getString("REQUESTMARK"));
            //流程名称
            String lcmc = Util.null2String(rs.getString("REQUESTNAME"));
            //流程类型
            String lclx = Util.null2String(rs.getString("unnumber"));

            //是否快速报销标识位
            String sfksbx = "";
            if("zbtybx".equals(lclx)){
                sfksbx = Util.null2String(rs.getString("bsw"));
            }
            //拟稿时间
            String ngsj = Util.null2String(rs.getString("ngsj"));
            //费用承担部门
            String fycdbm = Util.null2String(rs.getString("fysjcdbm"));
            //费用承担部门负责人
            String fycdbmfzr = Util.null2String(rs.getString("fycdbmfzr"));
            //报销人
            String bxr = Util.null2String(rs.getString("bxr"));
            //报销人部门
            String bxrbm = Util.null2String(rs.getString("bxrbm"));
            //费用发生日期
            String fyfsrq = Util.null2String(rs.getString("fyfsrq"));
            //不含税金额
            String bhsje = Util.null2String(rs.getString("bhsje"));
            //税率
            String sl = Util.null2String(rs.getString("slz"));
            //税金
            String sj = Util.null2String(rs.getString("sj"));
            //价税合计金额
            String jshjje = Util.null2String(rs.getString("jshjje"));
            //事由
            String sy = Util.null2String(rs.getString("sy"));
            //超预算
            String cys = Util.null2String(rs.getString("cys"));
            //报销人部门负责人
            String bxrbmfzr = "";
            if (!"".equals(bxrbm)) {
                String bxbmfzrsql = "select DEPTDIRECTORS from hrmdepartmentdefined where deptid = ?";
                bb.writeLog("执行的sql------------------------" + bxbmfzrsql + "参数" + bxrbm);
                fydrs.executeQuery(bxbmfzrsql, bxrbm);
                if (fydrs.next()) {
                    bxrbmfzr = Util.null2String(fydrs.getString("DEPTDIRECTORS"));
                }
            }
            //预算科目
            String yskmid = Util.null2String(rs.getString("yskmid"));
            //去头获取yskmbm
            String[] arr1 = yskmid.split("_");
            String yskmbm = "";
            if (arr1.length > 1) {
                yskmbm = arr1[1];
            }
            //预算科目名称
            String yskmmc = "";
            //上级部门预算科目名称
            String sjbmyskmmc = "";
            //上级部门预算科目编码
            String sjbmyskmbm = "";
            //上上级部门预算科目编码
            String ssjbmyskmbm = "";
            //上上级部门预算科目名称
            String ssjbmyskmmc = "";
            if (!"".equals(yskmbm)) {
                Map<String, Object> yskmmap = findYskmData(yskmbm);
                yskmmc = Util.null2String(yskmmap.get("yskmmc"));
                sjbmyskmmc = Util.null2String(yskmmap.get("sjbmyskmmc"));
                sjbmyskmbm = Util.null2String(yskmmap.get("sjbmyskmbm"));
                ssjbmyskmbm = Util.null2String(yskmmap.get("ssjbmyskmbm"));
                ssjbmyskmmc = Util.null2String(yskmmap.get("ssjbmyskmmc"));
            }
            //报销人工号
            String bxrgh = "";
            if (!"".equals(bxr)) {
                String bxrghsql = "select workcode from hrmresource where id = ?";
                bb.writeLog("执行的sql" + bxrghsql + "参数 ===================" + bxr);
                fydrs.executeQuery(bxrghsql, bxr);
                if (fydrs.next()) {
                    bxrgh = Util.null2String(fydrs.getString("workcode"));
                }
            }
            try {
                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 (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                bb.writeLog("执行的插入sql" + insertsql + "参数" + "sjbmyskmbm" + sjbmyskmbm + "sjbmyskmmc" + sjbmyskmmc + "ssjbmyskmbm" + ssjbmyskmbm + "ssjbmyskmmc" + ssjbmyskmmc);
                rst.executeUpdate(insertsql, requid, 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();
                bb.writeLog("-------------错误原因："+e.getMessage()+"------------------requestid:"+requid);
                return Action.FAILURE_AND_CONTINUE;
            }
        }
        rst.commit();
        return Action.SUCCESS;
    }

    /**
     * 查询预算科目数据
     *
     * @param yskmbm
     * @return
     */

    public static Map<String, Object> findYskmData(String yskmbm) {
        Map<String, Object> map = new HashMap<>();
        String querysql = "";
        String yskmmc = "";
        String sjyskm = "";
        if (!"".equals(yskmbm)) {
            querysql = "select yskmmc,sjyskm,yskmbm from uf_kmpz where yskmbm = ?";
            fydrs.writeLog("执行的sql---------------" + querysql + "参数" + 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;
    }

    public static String getTableName(String lclx){
        String sql = "select t2.tablename from workflow_base t1,workflow_bill t2 where t1.formid = t2.id and  t1.UNNUMBER = ? and ISVALID = '1'";
        fydrs.executeQuery(sql,lclx);
        if(fydrs.next()){
            return Util.null2String(fydrs.getString("tablename"));
        }else {
            return "";
        }
    }
}
