package com.engine.kq.cmd.settings;

import com.api.browser.bean.SearchConditionItem;
import com.api.hrm.bean.HrmFieldBean;
import com.api.hrm.util.HrmFieldSearchConditionComInfo;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

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

/**
 * 应用设置--获取设置表单
 */
public class GetSettingsFormCmd extends AbstractCommonCommand<Map<String, Object>> {

    public GetSettingsFormCmd(Map<String, Object> params, User user) {
        this.user = user;
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        List<Map<String, Object>> groupList = new ArrayList<Map<String, Object>>();
        Map<String, Object> groupItem = new HashMap<String, Object>();
        List<Object> itemList = new ArrayList<Object>();
        HrmFieldSearchConditionComInfo hrmFieldSearchConditionComInfo = new HrmFieldSearchConditionComInfo();
        SearchConditionItem searchConditionItem = null;
        HrmFieldBean hrmFieldBean = null;

        String sql = "select * from kq_settings where isHide is null or isHide<>1 order by showOrder,id";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql);
        while (recordSet.next()) {
            String label = recordSet.getString("label");
            String main_key = Util.null2String(recordSet.getString("main_key"));
            String main_value = Util.null2String(recordSet.getString("main_val"));
            String main_desc_label = Util.null2String(recordSet.getString("main_desc_label"));

            hrmFieldBean = new HrmFieldBean();
            hrmFieldBean.setFieldname(main_key);
            hrmFieldBean.setFieldlabel(label);
            hrmFieldBean.setFieldhtmltype("4");
            hrmFieldBean.setType("1");
            hrmFieldBean.setFieldvalue(main_value);
            hrmFieldBean.setIsFormField(true);
            hrmFieldBean.setViewAttr(2);
            searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
            Map<String, Object> otherParamsMap = new HashMap<String, Object>();
            otherParamsMap.put("display", "switch");
            searchConditionItem.setOtherParams(otherParamsMap);
            if (!"".equals(main_desc_label)) {
                searchConditionItem.setHelpfulTip(SystemEnv.getHtmlLabelNames(main_desc_label, user.getLanguage()));
            }
            itemList.add(searchConditionItem);
        }
        groupItem.put("items", itemList);
        groupList.add(groupItem);
        resultMap.put("condition", groupList);

        return resultMap;
    }
}
