package com.engine.kq.cmd.reportfielddefine;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
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 GetFieldOrderDefineCmd extends AbstractCommonCommand<Map<String, Object>> {

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

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            RecordSet recordSet = new RecordSet();
            List<Map<String, Object>> srcdatasList = new ArrayList<Map<String, Object>>();
            List<Map<String, Object>> destdatasList = new ArrayList<Map<String, Object>>();

            String dataIndex = "";
            String labelId = "";
            String sort = "";
            String ascOrDesc = "";

            String sql = "select * from kq_report_order where userid=? order by sort DESC, orders ASC";
            recordSet.executeQuery(sql, user.getUID());
            if (recordSet.getCounts() <= 0) {
                sql = "select * from kq_report_order where userId=0 order by sort DESC, orders ASC";
                recordSet.executeQuery(sql);
            }
            while (recordSet.next()) {
                dataIndex = recordSet.getString("dataIndex");
                labelId = recordSet.getString("labelId");
                sort = recordSet.getString("sort");
                ascOrDesc = recordSet.getString("ascOrDesc");

                Map<String, Object> dataMap = new HashMap<String, Object>();
                if (sort.equals("1")) {
                    dataMap.put("dataIndex", dataIndex);
                    dataMap.put("labelId", labelId);
                    dataMap.put("title", SystemEnv.getHtmlLabelNames(labelId, user.getLanguage()));
                    dataMap.put("ascOrDesc", ascOrDesc);
                    dataMap.put("sort", sort);

                    destdatasList.add(dataMap);
                } else {
                    dataMap.put("dataIndex", dataIndex);
                    dataMap.put("labelId", labelId);
                    dataMap.put("title", SystemEnv.getHtmlLabelNames(labelId, user.getLanguage()));
                    dataMap.put("ascOrDesc", ascOrDesc);
                    dataMap.put("sort", sort);

                    srcdatasList.add(dataMap);
                }
            }

            if (destdatasList.size() == 0 && srcdatasList.size() == 0) {
                String[] defaultCol = new String[]{"hrmresource.dspOrder,390525,1,ASC", "dept.deptShowOrder,25036,0,ASC", "subcom.subcomShowOrder,506847,0,ASC", "dept.deptName,15390,0,ASC", "subcom.subcomName,1878,0,ASC", "hrmresource.lastName,81812,0,ASC","organization,376,0,ASC"};
                for (int i = 0; i < defaultCol.length; i++) {
                    String colInfo = defaultCol[i];

                    String[] colInfoArr = colInfo.split(",");

                    dataIndex = colInfoArr[0];
                    labelId = colInfoArr[1];
                    sort = colInfoArr[2];
                    ascOrDesc = colInfoArr[3];

                    Map<String, Object> dataMap = new HashMap<String, Object>();
                    if (sort.equals("1")) {
                        dataMap.put("dataIndex", dataIndex);
                        dataMap.put("labelId", labelId);
                        dataMap.put("title", SystemEnv.getHtmlLabelNames(labelId, user.getLanguage()));
                        dataMap.put("ascOrDesc", ascOrDesc);
                        dataMap.put("sort", sort);

                        destdatasList.add(dataMap);
                    } else {
                        dataMap.put("dataIndex", dataIndex);
                        dataMap.put("labelId", labelId);
                        dataMap.put("title", SystemEnv.getHtmlLabelNames(labelId, user.getLanguage()));
                        dataMap.put("ascOrDesc", ascOrDesc);
                        dataMap.put("sort", sort);

                        srcdatasList.add(dataMap);
                    }
                }
            }

            resultMap.put("status", "1");
            resultMap.put("totalPage", "1");
            resultMap.put("currentPage", "1");
            resultMap.put("destdatas", destdatasList);
            resultMap.put("srcdatas", srcdatasList);
        } catch (Exception e) {
            resultMap.put("status", "-1");
            resultMap.put("message", e.getMessage());
        }
        return resultMap;
    }

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

}
