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

import com.engine.core.impl.Service;
import com.engine.htsc.ReimbursementFlow.Dao.UfZbbxandclsplDaoImpl;
import com.engine.htsc.ReimbursementFlow.Service.UfCommonService;
import com.engine.htsc.ReimbursementFlow.model.NewDepart;
import com.engine.htsc.ReimbursementFlow.model.NewUser;
import com.engine.htsc.ReimbursementFlow.model.ReportUfZbtybxsp;
import com.engine.htsc.ReimbursementFlow.model.UfZbtybxsp;
import com.engine.htsc.ReimbursementFlow.tool.ElementInstance;
import com.engine.htsc.ReimbursementFlow.tool.Logictools;
import com.engine.htsc.ReimbursementFlow.tool.ModelToVOTools;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

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

public class UfCommonSericeImpl extends Service implements UfCommonService {

    private final UfZbbxandclsplDaoImpl ufZbbxandclsplDao=new UfZbbxandclsplDaoImpl();






    @Override
    public List<ReportUfZbtybxsp>    getSeqByCondition(Map<String,Object> mainmap,
                                                    Map<String, NewUser> usermap,
                                                    String numbertable,
                                                    String nodeunnumber,
                                                    Map<String,Object>  conditionMap,  //明细表
                                                    Map<String,Object> detaimap,
                                                    String bxrkey,
                                                    List<ReportUfZbtybxsp> listreportufZbtybxsp) {
        //获取审批查询依据的人
        Map<String ,String > userlevelcodemap=new HashMap<>();
        for (String mapkey:usermap.keySet()){
            userlevelcodemap.put(mapkey,usermap.get(mapkey).getDeptLevelcode());
        }
        NewUser ngruser= usermap.get("ngr");
        NewUser bxruser=usermap.get(bxrkey);

        //首先根据人员报销情况
        List<UfZbtybxsp> UfZbtybxspList= ufZbbxandclsplDao.getUSerToUfzbtybxsp(userlevelcodemap,numbertable);
        if(UfZbtybxspList==null||UfZbtybxspList.size()<=0){
            return listreportufZbtybxsp;
        }

        List<String> sucesslist=new ArrayList();
        List<String> errorlist=new ArrayList();
        List<String> roliseerrorlist=new ArrayList();
        for (UfZbtybxsp ufZbtybxsp : UfZbtybxspList){
            //cxsplry
            String cxsplryvalue=ufZbtybxsp.getCxsplry();
            NewUser cxsplryUser=usermap.get(cxsplryvalue);
            //获取查询人员的userid
            ReportUfZbtybxsp reportUfZbtybxsp=new ReportUfZbtybxsp();
            String tj1= Util.null2String(ufZbtybxsp.getTj1());
            String tj2= Util.null2String(ufZbtybxsp.getTj2());
            String tj3= Util.null2String( ufZbtybxsp.getTj3());
            String cxrjs=  Util.null2String(ufZbtybxsp.getBxrjs());
            String ptjs=Util.null2String( ufZbtybxsp.getPtjs());
            String ptbm=Util.null2String( ufZbtybxsp.getPtbm());



            if(!isrolesyesNO(cxrjs,ptjs,bxruser)){
                //如果报销人身份不正确则退出循环
                roliseerrorlist.add(ufZbtybxsp.getId().toString());
                continue;
            }

            //判断部门
            if(!isptbmlist(ptbm,cxsplryUser)){
                //如果报销人排他部门不正确则退出循环
                roliseerrorlist.add(ufZbtybxsp.getId().toString());
                continue;
            }

            if(!getconitionparams(tj1,tj2,tj3,conditionMap)){
                //如果条件不成列，则推出
                errorlist.add(ufZbtybxsp.getId().toString());
                continue;
            }
            sucesslist.add(ufZbtybxsp.getId().toString());
            Map<String,Object> newusermap=new HashMap<>();
            newusermap.put("ngruser",ngruser);
            newusermap.put("cxspuser",cxsplryUser);
            newusermap.put("bxruser",bxruser);
            reportUfZbtybxsp= ModelToVOTools.ConvertModelToVO(ufZbtybxsp,newusermap,numbertable,nodeunnumber,mainmap,detaimap);
            reportUfZbtybxsp.setCxspr(cxsplryUser);
            listreportufZbtybxsp.add(reportUfZbtybxsp);
        }
        JAVATools.SetCmdwritelog("报销联成功的数据:"+sucesslist.size());
        JAVATools.SetCmdwritelog(" ngruser: "+ngruser.getRoleCode()+" bxr："+bxruser.getRoleCode());
        JAVATools.SetCmdwritelog(" ngruser: "+ngruser.getUserId()+" bxr："+bxruser.getUserId());
        if(sucesslist.size()>=1){
            for (String idvalue:sucesslist){
                JAVATools.SetCmdwritelog(idvalue);
            }
        }

        listreportufZbtybxsp= isdistdeptreportlislt(listreportufZbtybxsp);
        return listreportufZbtybxsp;
    }

    //判断部门和机构
    public boolean isdepartmentyesno(UfZbtybxsp ufZbtybxsp,NewUser newUser){
        boolean flag=false;
        if(ufZbtybxsp.getBm().equals(newUser.getDepId())){
            flag=true;
        } else if(Util.null2String(ufZbtybxsp.getJg()).equals(new NewDepart(newUser.getDepId()).getDepartmentId())){
            flag=true;
        }
        return flag;
    }

    //判断部门和机构
    public List<ReportUfZbtybxsp> isdistdeptreportlislt(List<ReportUfZbtybxsp> reportUfZbtybxspList){
        if(reportUfZbtybxspList==null || reportUfZbtybxspList.size()<=0){
            return  new ArrayList<>();
        }
        List<ReportUfZbtybxsp> bmlist=new ArrayList<>();
        List<ReportUfZbtybxsp> jglist=new ArrayList<>();
        String bmstr="";
        for (ReportUfZbtybxsp reportUf:reportUfZbtybxspList) {
            String dept=reportUf.getCxspr().getDepId();
            String jg=new NewDepart(reportUf.getCxspr().getDepId()).getDepartmentId();
            if("".equals(dept)|| "".equals(jg)){
                return  null;
            }
            String bmid=reportUf.getUfZbtybxsp().getBm();
            if(dept.equals(bmid)) {
                bmstr=bmstr==""?bmid:","+bmid;
                bmlist.add(reportUf);
            }
            if(jg.equals(Util.null2String(reportUf.getUfZbtybxsp().getJg()))) {
                jglist.add(reportUf);
            }
        }

        if(bmlist!=null && bmlist.size()>0){
            //其他情况特殊情况
            return otherlist(bmlist,jglist,bmstr);

        }
        if(jglist!=null && jglist.size()>0){
            return  jglist;
        }
        return new ArrayList<>();
    }

    public List<ReportUfZbtybxsp> otherlist(List<ReportUfZbtybxsp> bmlist,List<ReportUfZbtybxsp> jglist,String bmstr){
        if("".equals(bmstr)){
            return  jglist;
        }
        for (ReportUfZbtybxsp ref:jglist ) {
            //如果部门在里面则不返回
            if (bmstr.indexOf(ref.getCxspr().getDepId())<0 || ref.getUfZbtybxsp().getCxsplry().equals("ngr")) {
                bmlist.add(ref);
            }
        }
        return  bmlist;
    }

    public boolean getconitionparams(String tj1,String tj2,String tj3,Map<String,Object> detailmap){
        boolean flag=false;
        if (Logictools.getconitionparams(tj1,detailmap)) {
            if (Logictools.getconitionparams(tj2,detailmap)) {
                if (Logictools.getconitionparams(tj3,detailmap)) {
                    flag=true;
                }
            }
        }
        return flag;
    }
    //判断报销人的身份是否正常
    public boolean isrolesyesNO(String js,String ptjs,NewUser newUser){
        boolean flag=false;
        //逻辑或的操作
        //  System.out.println("开始判断人员");
        // System.out.println("js:" + js + "ptjs:" + ptjs + "newUser.getUserId():" + newUser.getUserId());
        if(Logictools.valueisdist(js,newUser)){
            if(isptroleslist(ptjs,newUser)){
                flag=true;
            }
        }



        return flag;
    }
    //排他角色的判断
    public boolean isptroleslist(String js,NewUser newUser ){
        boolean flag=true;
        if("".equals(js)){
            return  flag;
        }
        flag= Logictools.valueisdist(js,newUser);
        if(flag==true){
            return  false;
        }else {
            return  true;
        }
    }
    //排他部门的判断
    public boolean isptbmlist(String ptbm,NewUser newUser ){
        boolean flag=true;
        if("".equals(ptbm)){
            return  flag;
        }
        flag= Logictools.valueisdist(ptbm,newUser);
        if(flag==true){
            return  false;
        }else {
            return  true;
        }
    }



}
