package com.engine.organization.util.browser;

import com.api.browser.bean.SearchConditionItem;
import com.api.browser.util.BrowserInitUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.definedfield.HrmFieldManager;

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

/**
 * @author:dxfeng
 * @createTime: 2022/09/06
 * @version: 1.0
 */
public class OrganizationBrowserUtil {
    /**
     * 浏览按钮赋值
     *
     * @param user
     * @param tmpFieldValue
     * @param browserType
     * @param searchConditionItem
     */
    public static void assignBrowser(User user, String tmpFieldValue, int browserType, SearchConditionItem searchConditionItem) {
        HrmFieldManager hrmFieldManager = new HrmFieldManager();
        String fieldShowName = null;
        try {
            fieldShowName = hrmFieldManager.getFieldvalue(user, null, 0, 3, browserType, tmpFieldValue, 0);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        String[] fieldvalues = Util.splitString(tmpFieldValue, ",");
        String[] fieldshownames = Util.splitString(fieldShowName, ",");
        List<Map<String, Object>> replaceDatas = new ArrayList<>();
        for (int i = 0; fieldvalues != null && i < fieldvalues.length; i++) {
            if (fieldvalues.length != fieldshownames.length) {
                break;
            }
            if (Util.null2String(fieldshownames[i]).length() == 0) {
                continue;
            }
            Map<String, Object> replaceData = new HashMap<>();
            replaceData.put("id", fieldvalues[i]);
            replaceData.put("name", fieldshownames[i]);
            replaceDatas.add(replaceData);
        }
        BrowserInitUtil.setConditionItemDefaultValue(searchConditionItem, replaceDatas, 2);

    }
}
