package com.engine.recruit.conn;

import com.engine.recruit.entity.position.PositionPo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;

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

/**
 * 招聘职位信息
 *
 * @author:dxfeng
 * @createTime: 2023/10/26
 * @version: 1.0
 */
public class PositionCommonInfo {

    /**
     * 根据千里聆岗位ID，获取招聘职位ID
     *
     * @param qllGwId
     * @return
     */
    public static String getPositionIdByQll(Long qllGwId) {
        String positionId = "";
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from uf_jcl_zp_zpzw where qllgwid = ?", qllGwId);
        if (rs.next()) {
            positionId = rs.getString("id");
        }
        return positionId;

    }

    /**
     * 获取招聘职位关联的招聘流程
     *
     * @param positionId
     * @return
     */
    public static String getRecruitFlowId(String positionId) {
        String recruitFlowId = "";
        if (StringUtils.isBlank(positionId)) {
            return recruitFlowId;
        }
        RecordSet rs = new RecordSet();
        rs.executeQuery("select zplc from uf_jcl_zp_zpzw where id = ?", positionId);
        if (rs.next()) {
            recruitFlowId = rs.getString("zplc");
        }
        return recruitFlowId;

    }


    /**
     * 根据职位名称，获取职位ID
     *
     * @param positionName 职位名称
     * @param mobile       职位跟进人手机号
     * @return
     */
    public static String getPositionIdByName(String positionName, String mobile) {
        RecordSet rs = new RecordSet();
        // 根据职位名称、查询招聘中的数据
        rs.executeQuery("select id ,zpzwmc ,zpzt ,modedatacreater ,modedatacreatedate ,modedatacreatetime  from uf_jcl_zp_zpzw where zpzt = 0 and zpzwmc = ? ", positionName);
        List<PositionPo> positionPoList = new ArrayList<>();
        while (rs.next()) {
            PositionPo positionPo = PositionPo.builder()
                    .id(rs.getString("id"))
                    .name(rs.getString("zpzwmc"))
                    .status(rs.getString("zpzt"))
                    .creator(rs.getString("modedatacreater"))
                    .createDate(rs.getString("modedatacreatedate"))
                    .createTime(rs.getString("modedatacreatetime"))
                    .build();
            positionPoList.add(positionPo);
        }

        // 未查询到数据，返回空
        if (CollectionUtils.isEmpty(positionPoList)) {
            return "";
        }

        // 只有一条数据，直接返回当前数据,如果有多个，再添加其他条件过滤
        if (positionPoList.size() == 1) {
            return positionPoList.get(0).getId();
        }

        // 根据手机号查询人员ID
        Set<String> userIds = new HashSet<>();
        rs.executeQuery("select id from hrmresource where mobile = ? ", mobile);
        while (rs.next()) {
            userIds.add(rs.getString("id"));
        }

        // 筛选跟进人手机号匹配的职位数据，并按创建时间倒序排序
        List<PositionPo> filterList = positionPoList.stream().filter(item -> userIds.contains(item.getCreator())).sorted(Comparator.comparing(PositionPo::getCreationTime).reversed()).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(filterList)) {
            // 跟进人手机号未获取到，选择最新的一条数据
            return positionPoList.stream().sorted(Comparator.comparing(PositionPo::getCreationTime).reversed()).collect(Collectors.toList()).get(0).getId();
        } else {
            // 再一样，就默认匹配到创建时间最新的职位
            return filterList.get(0).getId();
        }
    }
}
