package com.engine.htsc.ReimbursementFlow.Service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.ReimbursementFlow.Dao.UfZbbxandclsplDaoImpl;
import com.engine.htsc.ReimbursementFlow.Service.UfCommonService;
import com.engine.htsc.ReimbursementFlow.Service.UfZbbxandclsplService;

import com.engine.htsc.ReimbursementFlow.model.NewUser;
import com.engine.htsc.ReimbursementFlow.model.ReportUfZbtybxsp;
import com.engine.core.impl.Service;
import com.engine.htsc.ReimbursementFlow.model.UfZbtybxsp;
import com.engine.htsc.ReimbursementFlow.tool.ElementInstance;
import com.engine.htsc.ReimbursementFlow.tool.ModelToVOTools;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.ConstUtil;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.*;
import java.util.stream.IntStream;

public class UfZbbxandclsplServiceImpl extends Service implements UfZbbxandclsplService {

    private final UfZbbxandclsplDaoImpl ufZbbxandclsplDao=new UfZbbxandclsplDaoImpl();
    private final ElementInstance elementInstance=new ElementInstance();
    private UfCommonService ufCommonSerice= ServiceUtil.getService(UfCommonSericeImpl.class);

    @Override
    public JSONObject setFormInnerValue(String reqid,String detailid)  {

        String Value="";
        String tablename= JAVATools.getMaintable(reqid);
        String numbertable=JAVATools.getmainunnumber(reqid);
        JAVATools.SetCmdwritelog("request:"+reqid+" 查询主表的表名:"+tablename+" numbertable："+numbertable);
        List<ReportUfZbtybxsp> listUfReportZbtybxsp=new ArrayList<>();
        String ngr="ngr";
        String bxr="bxr";
        //更具tablename获取对应的明细表
        Map<String,Object> map= ufZbbxandclsplDao.getAllTables(reqid,tablename,detailid);
        if (!JAVATools.ismapisnotnull(map)) {
            return null;
        }
        //开始转换，差旅对应的是出差人，报销是报销人
        //开始调用map转换
        Map<String,Object> tijiaomap= ufZbbxandclsplDao.GetTablesRelation(numbertable);
        if(tijiaomap.get("ngr")!=null&&!"".equals(ConstUtil.newNullString(tijiaomap.get("ngr")))){
            ngr= ConstUtil.newNullString(tijiaomap.get("ngr"));
        }
        if(tijiaomap.get("bxr")!=null&&!"".equals(ConstUtil.newNullString(tijiaomap.get("bxr")))){
            bxr= ConstUtil.newNullString(tijiaomap.get("bxr"));
        }
        String ngrid=Util.null2String(map.get(ngr).toString());
        NewUser ngrUser=new NewUser(ngrid);
        // String sfzcsp=Util.null2String(map.get("sfzcsp").toString());  //是否送总裁审批
        List<Map<String,Object>> list=(List) map.get("listdata");
        if (!JAVATools.islistisnotnull(list)) {
            return null;
        }
        for (int i=0;i<list.size();i++) {
            List<Map<String,Object>> listdetail= (List)list.get(i);
            for (Map<String, Object> detailmap : listdetail) {

                String bxrid = ConstUtil.newNullString(detailmap.get(bxr));     //报销人
                Map<String, NewUser> usermap = new HashMap<>();
                usermap.put(bxr, new NewUser(bxrid));
                usermap.put(ngr, ngrUser);
                Map<String, Object> condition = getCondingtion(map, detailmap, numbertable);
                listUfReportZbtybxsp = ufCommonSerice.getSeqByCondition(map,usermap, numbertable,"", condition, detailmap, bxr, listUfReportZbtybxsp);
            }
        }
        //List<ReportUfZbtybxsp> listreportufzbtybxsp= ModelToVOTools.ConvertModelToVO(listUfZbtybxsp,ngrUser,numbertable);

        Map<String,String> maplist= distinctListUfZbtybxsp(listUfReportZbtybxsp,"",null);
        JSONObject jsonObject=JAVATools.MapToJson(maplist);
        return  jsonObject;
    }



    @Override
    public JSONObject NewGetTravellist(List mapdetaillist,String numbertable,String nodeunnumber)  {
        String Value="";

        List<ReportUfZbtybxsp> listUfReportZbtybxsp=new ArrayList<>();
        String ngr="ngr";
        String bxr="bxr";
        //    Map<String,Object> map=new HashMap<>();
        //更具tablename获取对应的明细表，重载数据从前端获取
        Map<String,Object> map= ufZbbxandclsplDao.GetSelectModel(mapdetaillist,numbertable);
        if (!JAVATools.ismapisnotnull(map)) {
            return null;
        }
        //开始转换，差旅对应的是出差人，报销是报销人
        //开始调用map转换
        Map<String,Object> tijiaomap= ufZbbxandclsplDao.GetTablesRelation(numbertable);
        if(tijiaomap.get("ngr")!=null&&!"".equals(ConstUtil.newNullString(tijiaomap.get("ngr")))){
            ngr= ConstUtil.newNullString(tijiaomap.get("ngr"));
        }
        if(tijiaomap.get("bxr")!=null&&!"".equals(ConstUtil.newNullString(tijiaomap.get("bxr")))){
            bxr= ConstUtil.newNullString(tijiaomap.get("bxr"));
        }
        String ngrid=Util.null2String(map.get(ngr).toString());
        NewUser ngrUser=new NewUser(ngrid);
        // String sfzcsp=Util.null2String(map.get("sfzcsp").toString());  //是否送总裁审批
        List<Map<String,Object>> list=(List) map.get("listdata");
        if (!JAVATools.islistisnotnull(list)) {
            return null;
        }
        for (int i=0;i<list.size();i++) {
            //   List<Map<String,Object>> listdetail= (List)list.get(i);
            Map<String, NewUser> usermap = new HashMap<>();
            Map<String, Object> Dtmap= list.get(i);


            String bxrid = ConstUtil.newNullString(Dtmap.get(bxr));     //报销人
            NewUser bxrUser = new NewUser(bxrid);
            if("zbclbx".equals(numbertable)){
                bxrUser = new NewUser(bxrid,Dtmap.get("ccrszbm").toString());
            }
            usermap.put(bxr, bxrUser);
            usermap.put(ngr, ngrUser);
            Map<String, Object> condition = getCondingtion(map, Dtmap, numbertable);
            listUfReportZbtybxsp = ufCommonSerice.getSeqByCondition(map,usermap, numbertable,nodeunnumber, condition, Dtmap, bxr, listUfReportZbtybxsp);
        }
        Map<String,String> maplist= distinctListUfZbtybxsp(listUfReportZbtybxsp,nodeunnumber, ngrUser);
        JSONObject jsonObject=JAVATools.MapToJson(maplist);
        return  jsonObject;
    }






    private  Map<String, String> distinctListUfZbtybxsp(List<ReportUfZbtybxsp> reportUfZbtybxspList,String nodeunnumber,NewUser ngrUser){
        //测试
        String filterstr=new PropBean().getPropName("zbbxfgldfilter");
        String nounnumber_zbclbx=new PropBean().getPropName("nounnumber_zbclbx");
        String ufzfbtybxspid="";
        //首先排除重复数据为前端拼装数据
        String fgldsp="";
        String fyqr="";
        String zcsp="";
        String ysgly="";
        String fyhjsp="";
        String onedeptadmin="";
        String twodeptadmin="";
        String dwsj="";
        String dwfsj="";
        String cwzj="";
        String sfldsp="";
        String zzfptjr="";

        String fhr = "";
        boolean issfldspflag=true;
        if(reportUfZbtybxspList==null){
            return null;
        }
        for (ReportUfZbtybxsp reportUfZbtybxsp:reportUfZbtybxspList) {
            //测试可以不用考虑
            ufzfbtybxspid=JAVATools.addidinreloadString(ufzfbtybxspid,Util.null2String(reportUfZbtybxsp.getUfZbtybxsp().getId()));
            //分管领导
            fgldsp=JAVATools.addreloadString(fgldsp,reportUfZbtybxsp.getFgldsp());
            //费用确认
            fyqr=JAVATools.addreloadString(fyqr,reportUfZbtybxsp.getFyqr());
            //总裁
            zcsp=JAVATools.addreloadString(zcsp,reportUfZbtybxsp.getZcsp());
            //预算管理员
            ysgly=JAVATools.addreloadString(ysgly,reportUfZbtybxsp.getYsgly());
            //费用会
            fyhjsp=JAVATools.addreloadString(fyhjsp,reportUfZbtybxsp.getFyhjsp());
            onedeptadmin=JAVATools.addreloadString(onedeptadmin,reportUfZbtybxsp.getOnebbmfzr());
            twodeptadmin=JAVATools.addreloadString(twodeptadmin,reportUfZbtybxsp.getTwobbmfzr());
            cwzj=JAVATools.addreloadString(cwzj,reportUfZbtybxsp.getCwzj());
            sfldsp=JAVATools.addidinreloadString(sfldsp,reportUfZbtybxsp.getSfsld());
            //纸质发票提交人
            if ("0".equals(reportUfZbtybxsp.getZzfptjr())){
                zzfptjr="0";
            }
            fhr = JAVATools.addidinreloadString(fhr,reportUfZbtybxsp.getFhr());
            if(!reportUfZbtybxsp.getSfsld().equals("1")){
                issfldspflag=false;
            }
        }
        fyhjsp=elementInstance.getsumfykj(fyhjsp);
        //纸质发票提交与费用会计节点保持一致
        if (!"0".equals(zzfptjr)){
            zzfptjr = fyhjsp;
        }else {
            zzfptjr = "";
        }

        fhr=elementInstance.getsumfykj(fhr);
        if(nounnumber_zbclbx!=""&&nodeunnumber.indexOf(nounnumber_zbclbx)>=0&&issfldspflag==false){
            sfldsp="";
        }

        if(filterstr.indexOf(nodeunnumber)<0){
            onedeptadmin=JAVATools.leavedistinct(onedeptadmin,fgldsp);
            twodeptadmin=JAVATools.leavedistinct(twodeptadmin,fgldsp);
            cwzj=JAVATools.leavedistinct(cwzj,fgldsp);
        }

        //一级部门负责人、二级部门负责人、财务总监节点
        if(ngrUser!=null&&ngrUser.getUserId()!=""){
            onedeptadmin=JAVATools.leavedistinct(onedeptadmin,ngrUser.getUserId());
            twodeptadmin=JAVATools.leavedistinct(twodeptadmin,ngrUser.getUserId());
            cwzj=JAVATools.leavedistinct(cwzj,ngrUser.getUserId());
        }

        fgldsp=JAVATools.leavedistinct(fgldsp,zcsp);
        fyqr=JAVATools.leavedistinct(fyqr,twodeptadmin);
        fyqr=JAVATools.leavedistinct(fyqr,onedeptadmin);
        twodeptadmin=JAVATools.leavedistinct(twodeptadmin,onedeptadmin);

        Map<String,String> mapstr= elementInstance.getfgldreloads(fgldsp);
        dwfsj=  mapstr.get("dwfsj");
        dwsj= mapstr.get("dwsj");
        fgldsp=mapstr.get("fgld");
        Map<String,String> map=new HashMap<>();
        //当总部通用报销，总部差旅报销 流程同时需要党委书记张伟和党委副书记尹立鸿（018500）审批时，仅由党委书记张伟（015150）审批即可；
        if (!"".equals(dwsj)&&!"".equals(dwfsj)){
            String dwsjgh = JAVATools.gethrmidtoworkcode(dwsj);
            String dwfsjgh = JAVATools.gethrmidtoworkcode(dwfsj);
            if (dwsjgh.contains("015150")&&dwfsjgh.contains("018500")){
                dwfsj = "";
            }
        }
        // 财务总监:cwzj
        map.put("fgld",fgldsp); //  分管领导:fgld
        map.put("lcqrr",fyqr); //费用确认: lcqrr
        map.put("zc",zcsp); //   总裁:zc
        map.put("ysgly",ysgly); //预算管理员:ysgly
        map.put("fykj",fyhjsp); // 费用会计:fykj
        map.put("fhr",fhr); // 复核人fhr
        map.put("dwfsj",dwfsj); // 党委副书记:
        map.put("dwsj",dwsj); //  党委书记
        map.put("cwzj",cwzj); //  首席财务官
        map.put("yjbmfzr",onedeptadmin); // 一级部门负责人:
        map.put("ejbmfzr",twodeptadmin); //  二级部门负责人:
        map.put("sfsldxsyc",sfldsp); //  二级部门负责人:
        map.put("zzfptjr",zzfptjr); //  纸质发票提交人:
        map.put("ufzfbtybxspid",ufzfbtybxspid); //  匹配的审批链id:
        System.out.println("----------------end-----------------");
        System.out.println("分管领导:"+fgldsp);
        System.out.println("党委书记:"+dwsj);
        System.out.println("党委副书记:"+dwfsj);
        System.out.println("费用确认:"+fyqr);
        System.out.println("总裁:"+zcsp);
        System.out.println("预算管理员:"+ysgly);
        System.out.println("费用会计:"+fyhjsp);
        System.out.println("复核人:"+fhr);
        System.out.println("一级部门负责人:"+onedeptadmin);
        System.out.println("二级部门负责人:"+twodeptadmin);
        System.out.println("cwzj:"+cwzj);
        System.out.println("------------------reolad------------------");
        System.out.println("分管领导:"+JAVATools.getidlastname(fgldsp));
        System.out.println("党委书记:"+JAVATools.getidlastname(dwsj));
        System.out.println("cfo:"+JAVATools.getidlastname(cwzj));
        System.out.println("党委副书记:"+JAVATools.getidlastname(dwfsj));
        System.out.println("费用确认:"+JAVATools.getidlastname(fyqr));
        System.out.println("总裁:"+JAVATools.getidlastname(zcsp));
        System.out.println("预算管理员:"+JAVATools.getidlastname(ysgly));
        System.out.println("费用会计:"+JAVATools.getidlastname(fyhjsp));
        System.out.println("一级部门负责人:"+JAVATools.getidlastname(onedeptadmin));
        System.out.println("二级部门负责人:"+JAVATools.getidlastname(twodeptadmin));
        System.out.println("纸质发票提交人:"+JAVATools.getidlastname(zzfptjr));
        System.out.println("按钮是否正常显示:"+sfldsp);
        System.out.println("------------------匹配成功的数据------------------");
        System.out.println(ufzfbtybxspid);
        return map;
    }



    //获取条件人员
    private  Map<String,Object> getCondingtion(Map<String,Object> mainmap,
                                               Map<String,Object> detailmap,
                                               String numbertable){
        //获取筛选条件表达式
        List<Map<String,Object>> condingtionlist= ufZbbxandclsplDao.GetTJRelation(numbertable);
        Map<String,Object> map=new HashMap<>();
        for (Map<String,Object> mapvalue:condingtionlist) {
            String value = "";
            String keyvalue = ConstUtil.newNullString(mapvalue.get("zdz"));
            String typevalue = ConstUtil.newNullString(mapvalue.get("sfszb"));
            String qzlx = ConstUtil.newNullString(mapvalue.get("qzlx"));
            if ( "0".equals(typevalue)) {
                value = ConstUtil.newNullString(mainmap.get(keyvalue));
                map.put(keyvalue,  getCondingreloadValue(qzlx,value));
            } else if ("1".equals(typevalue)) {
                //去子表取值
                value = ConstUtil.newNullString(detailmap.get(keyvalue));
                map.put(keyvalue,  getCondingreloadValue(qzlx,value));

            }

        }
        return  map;
    }


    //根据一些表达式对应转换
    public String getCondingreloadValue(String type,String value){
        String reStr="";
        if("0".equals(type)){
            if("0".equals(value)){
                reStr="true";
            }else// if("1".equals(value))
            {
                reStr="false";
            }
        }else if ("1".equals(type)){
            reStr=value;
        }
        return reStr;
    }


    //判断报销人的参数是否对应
    public boolean GetCointionType(String js,NewUser newUser){
        boolean flag=true;
        if("".equals(js)){
            return flag;
        }
        if(js.indexOf("PT")>=0){
            //判断是包含关系还是等于关系
            String RoleCode=  newUser.getRoleCode();
            //如果条件里存在值，但是报销人里没有，则返回错误
            if("".equals(RoleCode)){
                return false;
            }
            if(js.indexOf("PT_IN")>=0){
                //判断是否包含关系
                String jsPonit=js.replace("PT_IN","PT");
                return JAVATools.iscontaionString(RoleCode,jsPonit);
            }else{
                return JAVATools.isequestString(RoleCode,js);
            }
        }if(js.indexOf("DT")>=0){
            //判断是包含关系还是等于关系
            String deptid=  newUser.getDepId();
            //如果条件里存在值，但是报销人里没有，则返回错误
            if("".equals(deptid)){
                return false;
            }
            if(js.indexOf("DT_IN")>=0){
                //判断是否包含关系
                String jsPonit=js.replace("DT_IN","DT");
                String  jsvalue=jsPonit.split("DT_")[1];
                return JAVATools.iscontaionString(deptid,jsvalue);
            }else{
                String  jsvalue=js.split("DT_")[1];
                return JAVATools.isequestString(deptid,jsvalue);
            }
        } else if(js.indexOf("HRM")>=0){
            String WorkCode=  newUser.getWorkCode();
            if("".equals(WorkCode)){ return false; }
            if(js.indexOf("HRM_IN")>=0){
                //判断是否包含关系
                String jsPonit=js.replace("HRM_IN","HRM");
                return JAVATools.iscontaionString(WorkCode,jsPonit);
            }else{
                return JAVATools.isequestString(WorkCode,js);
            }
        }
        return flag;
    }


}
