package com.engine.kq.cmd.reportfielddefine;

import com.cloudstore.dev.api.util.TextUtil;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQReportFieldGroupComInfo;
import weaver.conn.RecordSet;
import weaver.hrm.HrmUserVarify;
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 GetGroupListCmd extends AbstractCommonCommand<Map<String, Object>> {

    public GetGroupListCmd(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>();

        Map<String,Object> groupitem = null;
        List<Object> itemlist = null;
        try {
            if (!HrmUserVarify.checkUserRight("KQReportFieldDefine:Add", user)) {
                retmap.put("status", "-1");
                retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
                return retmap;
            }

            RecordSet rs = new RecordSet();
            itemlist = new ArrayList<Object>();
            groupitem = new HashMap<String, Object>();

            //生成字段的集合
            Map<String,Object> infoMap = new HashMap<String, Object>();
            List<Map<String,Object>> datasList = new ArrayList<Map<String,Object>>();
            List<Map<String,Object>> columnsList = new ArrayList<Map<String,Object>>();

            columnsList = setColumns();
            datasList = setDatas();

            infoMap.put("showTitle", false);
            infoMap.put("draggable", true);
            infoMap.put("columns", columnsList);
            infoMap.put("datas", datasList);

            groupitem.put("title", SystemEnv.getHtmlLabelName(1361,weaver.general.Util.getIntValue(user.getLanguage())));
            groupitem.put("table", infoMap);
            itemlist.add(groupitem);

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

        return retmap;
    }

    private List<Map<String,Object>> setColumns() {
        List<Map<String, Object>> columns = new ArrayList<Map<String,Object>>();
        String td1 = "groupname,30127,1,1";//字段
        String td3 = "isshow,89,4,1";//显示

        String[] fields = new String[]{td1,td3};
        Map<String,Object> col = null;
        List<Object> comLists = new ArrayList<Object>();
        Map<String,Object> comMap = new HashMap<String, Object>();
        for(int i=0;i<fields.length;i++){
            col = new HashMap<String,Object>();
            comMap = new HashMap<String, Object>();
            comLists = new ArrayList<Object>();
            String[] fieldinfo = fields[i].split(",");
            String tmpkey = fieldinfo[0];

            col.put("title", SystemEnv.getHtmlLabelNames(fieldinfo[1],user.getLanguage()));
            col.put("key", tmpkey);
            col.put("dataIndex", tmpkey);
            col.put("colSpan", 1);

            if("groupname".equals(fieldinfo[0])){
                comMap.put("label", "");
                comMap.put("type", "INPUT");
                comMap.put("key", tmpkey);
                comMap.put("viewAttr", "3");
                Map<String, Object> otherParams = new HashMap<String, Object>();
                otherParams.put("isBase64", true);
                otherParams.put("inputType", "multilang");
                comMap.put("otherParams",otherParams);
                comLists.add(comMap);
                col.put("width", "60%");
                col.put("com", comLists);
            }else if("isshow".equals(fieldinfo[0])){
                comMap.put("label", "");
                comMap.put("type", "checkbox");
                comMap.put("key", tmpkey);
                comMap.put("viewAttr", "2");
                comLists.add(comMap);

                String[] checkVauleTypes = new String[]{"1","0"};
                col.put("showCheckAll", true);
                col.put("checkVauleType", checkVauleTypes);
                col.put("width", "40%");
                col.put("com", comLists);
            }
            columns.add(col);
        }
        return columns;
    }

    private List<Map<String, Object>> setDatas() {
        List<Map<String,Object>> datasList = new ArrayList<Map<String,Object>>();
        Map<String,Object> data = new HashMap<String, Object>();

        KQReportFieldGroupComInfo kqReportFieldGroupComInfo = new KQReportFieldGroupComInfo();
        while(kqReportFieldGroupComInfo.next()){
            String id = kqReportFieldGroupComInfo.getId();
            String groupname = kqReportFieldGroupComInfo.getGroupname();
            String isshow = kqReportFieldGroupComInfo.getIsshow();

            String td0 = "id";
            String td1 = "groupname";
            String td2 = "isshow";

            data = new HashMap<String, Object>();
            data.put(td0, id);
            data.put(td1, TextUtil.toBase64ForMultilang(groupname));
            data.put(td2, isshow);

            datasList.add(data);
        }

        return datasList;
    }

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

}
