package com.engine.htsc.zgsqs.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.zgsqs.dao.ZgsqsDao;
import com.engine.htsc.zgsqs.service.ZgsqsService;
import com.engine.integration.util.StringUtils;
import com.sap.db.jdbc.Hash;
import org.apache.axis2.databinding.types.xsd._short;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;

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

/**
 * @ClassName ZgsqsServiceImpl
 * @Author K1810015
 * @Date 2022/2/15 13:48
 * Version 1.0
 */
public class ZgsqsServiceImpl extends Service implements ZgsqsService {

    @Override
    public  Map<String,String> getLeadRange(Map<String, Object> map) {
       String szbm=Util.null2String(map.get("szbm"));
       String szjg=Util.null2String(map.get("szjg"));
       if(StringUtils.isBlank(szbm) || StringUtils.isBlank(szjg)){
           return null;
       }
       Map<String,String> userids=ZgsqsDao.getLeadRange(map);
        return userids;
    }

    @Override
    public boolean getleadflowstatus(Map<String, Object> map) {
        PropBean pb=new PropBean();
        Set<String> result = new HashSet<String>(1);
        if(map==null ||map.size()<=0){
            return false;
        }
        String xgfgsid=Util.null2String(pb.getPropName("zgsqs.xgfgsid"));
        String nunmber=Util.null2String(pb.getPropName("zgsqs.zgsqssp_gsldsp"));
        String requestid=Util.null2String( map.get("requestid"));
        String szjg=Util.null2String( map.get("szjg"));

        if(StringUtils.isBlank(nunmber) || StringUtils.isBlank(requestid)
          || StringUtils.isBlank(xgfgsid)|| StringUtils.isBlank(szjg)){
            return false;
        }
        if(!xgfgsid.equals(szjg)){
            return true;
        }
        Set<String> jobset=ZgsqsDao.getflowleaderids(map);
        if(jobset==null){
            return false;
        }
        String ceo=Util.null2String(pb.getPropName("zgsqs.xgceojob"));
        if(StringUtils.isBlank(ceo)){
            return false;
        }
        //获取香港公司的CEO人员
        Set<String> ceoset= ZgsqsDao.getjobtitleid(ceo);

        if(ceoset==null || ceoset.size()<=0 ){
            return true;
        }
        result.addAll(jobset);
        result.retainAll(ceoset);
        if(result==null || result.size()<=0){
            return false;
        }
        return true;
    }

    @Override
    public String getOperator(Map<String,Object> map) {

        if(map == null || map.size() <= 0){
            return null;
        }
        List<String> strlist=new ArrayList<>();
        PropBean pb = new PropBean();
        String unnumber = Util.null2String(pb.getPropName("zgsqs.nodeunnumber.zgsqssp_gsldsp"));
        String requestid = Util.null2String( map.get("requestid"));
        if(StringUtils.isBlank(unnumber) || StringUtils.isBlank(requestid)){
            return null;
        }
        map.put("unnumber",unnumber);
        List<Map<String,Object>> userlist = ZgsqsDao.getOperator(map);
        if(userlist==null || userlist.size()<=0){
            return "";
        }
        userlist.forEach((item)->{
            String agentorbyagentid= Util.null2String(item.get("agentorbyagentid")) ;
            if(!"-1".equals(agentorbyagentid)){
                strlist.add(agentorbyagentid);
            }else{
                strlist.add(Util.null2String(item.get("operator")));
            }
        });
        if(strlist==null || strlist.size()<=0){
            return "";
        }
        String userid=strlist.stream().distinct().filter(value-> !StringUtils.isBlank(value)).collect(Collectors.joining(","));
        return userid;
    }


    @Override
    public JSONObject getdeptrange(Map<String, Object> map) {
        String bmid=Util.null2String(map.get("bmid"));
        String jgid=Util.null2String(map.get("jgid"));
        if(StringUtils.isBlank(bmid) || StringUtils.isBlank(jgid)){
            return null;
        }
        Map<String,Object> retmap=ZgsqsDao.getdeptrange(map);
        if(retmap==null || retmap.size()<=0){
            return null;
        }
        JSONObject jsonObject=new JSONObject();
        Arrays.stream(bmid.split(",")).distinct().forEach((value)->{
            String jgidvalue= Util.null2String(retmap.get(value));
            if(jgid.equals(value)){
                jsonObject.put(value,true);
                return ;
            }
            if(StringUtils.isBlank(jgidvalue)){
                jsonObject.put(value,false);
                return ;
            }
            if(jgid.equals(jgidvalue)){
                jsonObject.put(value,true);
                return ;
            }else{
                jsonObject.put(value,false);
                return ;
            }
        });
        return jsonObject;
    }

}
