package com.engine.kq.cmd.group;

import com.api.hrm.bean.TreeNode;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQGroupComInfo;
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 GetGroupTreeCmd extends AbstractCommonCommand<Map<String, Object>> {

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

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String, Object> retmap = new HashMap<String, Object>();
		RecordSet rs = new RecordSet();
		String sql = "";
		TreeNode root = new TreeNode();
		try {
			//查询左侧树
			String keyword = Util.null2String(params.get("keyword")).trim().toLowerCase();
			root.setId("0");
			root.setName(SystemEnv.getHtmlLabelName(390221,user.getLanguage()));
			root.setType("0");
			root.setIcon("icon-coms-LargeArea");
			List<TreeNode> groups = new ArrayList<TreeNode>();
			KQGroupComInfo kqGroupComInfo = new KQGroupComInfo();
			while (kqGroupComInfo.next()){
				if(keyword.length()>0 && Util.null2String(kqGroupComInfo.getGroupname()).indexOf(keyword)==-1)continue;
				if(Util.null2String(kqGroupComInfo.getIsdelete()).equals("1"))continue;
//				if(Util.null2String(kqGroupComInfo.getKqtype()).equals("2"))continue;
				TreeNode group = new TreeNode();
				group.setId(kqGroupComInfo.getId());
				group.setName(kqGroupComInfo.getGroupname());
				group.setPid("0");
				group.setType("1");
				group.setCanClick(true);
				group.setIcon("icon-coms-LargeArea");
				groups.add(group);
			}
			root.setIsParent(groups.size()>0);
			root.setSubs(groups);

			Map<String, Object> treeObj = new HashMap<String, Object>();
			treeObj.put("rootCompany", root);

			retmap.put("datas", treeObj);
			retmap.put("status", "1");
		}catch (Exception e){
			writeLog(e);
			retmap.put("status", "-1");
			retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
		}
		return retmap;
	}

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