package com.engine.recruit.entity.position;

import com.alibaba.fastjson.JSON;
import com.weaver.rpa.sdk.clients.application.resume.entity.ResumeJobDynamicField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import weaver.general.BaseBean;

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

/**
 * 千里聆职位动态字段，转换为多选下拉框数据格式
 *
 * @author:dxfeng
 * @createTime: 2024/05/22
 * @version: 1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PositionSelectItem {
    private String key;
    private String showname;
    private boolean selected;

    public static Set<PositionSelectItem> convertDynamicFieldItem(List<ResumeJobDynamicField.Item> dynamicFieldItemList) {
        Set<PositionSelectItem> list = new LinkedHashSet<>();
        if (CollectionUtils.isNotEmpty(dynamicFieldItemList)) {
            for (ResumeJobDynamicField.Item item : dynamicFieldItemList) {
                PositionSelectItem positionSelectItem = new PositionSelectItem();
                positionSelectItem.setShowname(item.getLabel());
                try {
                    double parseDouble = Double.parseDouble(item.getValue());
                    positionSelectItem.setKey(String.valueOf((int) parseDouble));
                } catch (Exception e) {
                    positionSelectItem.setKey(item.getValue());
                }
                list.add(positionSelectItem);
            }
        } else {
            new BaseBean().writeLog("动态字段为空" + JSON.toJSONString(dynamicFieldItemList));
        }
        return list;
    }

    public static ResumeJobDynamicField.Item getItemByValue(List<ResumeJobDynamicField.Item> dynamicFieldItemList, String value) {
        return dynamicFieldItemList.stream().filter(item -> (String.valueOf((int) Double.parseDouble(item.getValue())).equals(value))).findFirst().orElse(null);
    }

    public static List<ResumeJobDynamicField.Item> getItemsByValues(List<ResumeJobDynamicField.Item> dynamicFieldItemList, String value) {
        String[] split = value.split(",");
        List<String> valueList = Arrays.asList(split);
        return dynamicFieldItemList.stream().filter(item -> valueList.contains(item.getLabel())).distinct().collect(Collectors.toList());
    }
}
