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

import com.engine.core.impl.Service;
import com.engine.htsc.HKContract.dao.HKContractDao;
import com.engine.htsc.HKContract.service.HKContractService;
import com.engine.integration.util.StringUtils;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;

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

/**
 * 香港合同
 */
public class HKContractServiceImpl extends Service implements HKContractService {


    /**
     * 校验CEO是否审批
     * @param map
     * @return
     */
    @Override
    public boolean verifyCEOApproval(Map<String, Object> map) {
        PropBean pb = new PropBean();
        String unnumber = Util.null2String(pb.getPropName("hkcontract.xght_gsld"));
        map.put("unnumber", unnumber);
        String requestid = Util.null2String(map.get("requestid"));
        if (StringUtils.isBlank(unnumber) || StringUtils.isBlank(requestid)) {
            return false;
        }
        if (StringUtils.isBlank(unnumber) || StringUtils.isBlank(requestid)) {
            return false;
        }
        //获取审批领导id
        Set<String> jobset = HKContractDao.getAppLeaderIdSet(map);
        if (jobset == null) {
            return false;
        }
        String ceo = Util.null2String(pb.getPropName("hkcontract.hkceojob"));
        if (StringUtils.isBlank(ceo)) {
            return false;
        }
        //获取香港公司的CEO人员
        Set<String> ceoset = HKContractDao.getCeoIdSet(ceo);
        if (ceoset == null || ceoset.size() <= 0) {
            return true;
        }
        Set<String> result = new HashSet<String>(1);
        result.addAll(jobset);
        result.retainAll(ceoset);
        if (result != null && result.size() > 0) {
            return true;
        }
        return false;
    }



    /**
     * 获取已审批领导逗号分隔
     * @param map
     * @return
     */
    @Override
    public  String getApprovedLeaderIdStr(Map<String,Object> map){
        PropBean pb = new PropBean();
        String unnumber=Util.null2String(pb.getPropName("hkcontract.xght_gsld"));
        map.put("unnumber",unnumber);
        String requestid = Util.null2String( map.get("requestid"));
        if(StringUtils.isBlank(unnumber) || StringUtils.isBlank(requestid)){
            return "";
        }
        List<String> approvedLeaderIdList = getApprovedLeaderIdList(map);
        return approvedLeaderIdList.stream().collect(Collectors.joining(","));
    }

    /**
     * 查询已审批领导
     * @param map
     * @return
     */
    public  List<String> getApprovedLeaderIdList(Map<String,Object> map){
        List<String> strlist=new ArrayList<>();
        List<Map<String,Object>> userlist= HKContractDao.getApprovedLeaders(map);
        if(userlist==null || userlist.size()<=0){
            return strlist;
        }
        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")));
            }
        });
        return strlist.stream().distinct().filter(value-> !StringUtils.isBlank(value)).collect(Collectors.toList());
    }
}
