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 com.engine.kq.biz.KQReportFieldComInfo;
import weaver.conn.RecordSet;
import weaver.general.Util;
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 GetFieldDefineListCmd extends AbstractCommonCommand<Map<String, Object>> {

    public GetFieldDefineListCmd(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>();
        if (!HrmUserVarify.checkUserRight("KQReportFieldDefine:Add", user)) {
            retmap.put("status", "-1");
            retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
            return retmap;
        }

        RecordSet rs = new RecordSet();
        String sql = "";
        Map<String, Object> groupitem = null;
        List<Object> itemlist = null;
        try {
            itemlist = new ArrayList<Object>();
            groupitem = new HashMap<String, Object>();

            //生成字段的集合
            Map<String, Object> mapFields = new HashMap<String, Object>();
            mapFields.put("showTitle", false);
            mapFields.put("draggable", true);
            mapFields.put("columns", getColumns());
            mapFields.put("datas", getDatas());

            groupitem.put("title", SystemEnv.getHtmlLabelName(1361, Util.getIntValue(user.getLanguage())));
            groupitem.put("table", mapFields);
            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>> getDatas() {
        List<Map<String, Object>> datasList = new ArrayList<Map<String, Object>>();
        Map<String, Object> data = new HashMap<String, Object>();

        String groupid = Util.null2String(params.get("groupid"));
        if (groupid.equals("other")) groupid = "";
        KQReportFieldComInfo kqReportFieldComInfo = new KQReportFieldComInfo();
        while (kqReportFieldComInfo.next()) {
            if (!kqReportFieldComInfo.getIsDefinedColumn().equals("1")) continue;
            if (!Util.null2String(kqReportFieldComInfo.getGroupid()).equals(groupid)) continue;
            if (!kqReportFieldComInfo.getReportType().equals("all") && !kqReportFieldComInfo.getReportType().equals("month"))
                continue;
            if (Util.null2String(kqReportFieldComInfo.getParentid()).length() > 0) continue;
            String id = kqReportFieldComInfo.getId();
            String fieldname = kqReportFieldComInfo.getFieldname();
            String fieldlabel = kqReportFieldComInfo.getFieldlabel();
            String isenable = kqReportFieldComInfo.getIsenable();
            String defaultshow = kqReportFieldComInfo.getDefaultShow();
            // #1276494 概述：满足薪酬指标项关联考勤报表列数据需求 By T.W
//            String ifSalaryNeed = kqReportFieldComInfo.getIfSalaryNeed();
            String formula = kqReportFieldComInfo.getFormula();

            String td0 = "id";
            String td1 = "fieldname";
            String td1span = "fieldnamespan";
            String td2 = "isenable";
            String td3 = "defaultshow";
//            String td5 = "ifSalaryNeed";
            String td4 = "formulaspan";

            data = new HashMap<String, Object>();
            data.put("disabled", kqReportFieldComInfo.getIsSystem().equals("1"));
            data.put(td0, id);
            data.put(td1, fieldname);
            data.put(td1span, SystemEnv.getHtmlLabelNames(fieldlabel, user.getLanguage()));
            data.put(td2, isenable);
            data.put(td3, defaultshow);
//            data.put(td5, ifSalaryNeed);
            String formulaInfo = "";
            if (formula.length() > 0) {
                formulaInfo = SystemEnv.getHtmlLabelName(63, user.getLanguage()) + ":";
                if (formula.indexOf("beLateMins") > -1) {
                    formulaInfo += SystemEnv.getHtmlLabelName(20081, user.getLanguage()) + " " + SystemEnv.getHtmlLabelName(516156, user.getLanguage()) + ":";
                } else {
                    formulaInfo += SystemEnv.getHtmlLabelName(20082, user.getLanguage()) + " " + SystemEnv.getHtmlLabelName(516156, user.getLanguage()) + ":";
                }
                formula = formula.replace("${beLateMins}>", "").replace("${beLateMins}<=", "").replace("?1:0", "").replace(" && ", "-");
                formula = formula.replace("${leaveEarlyMins}>", "").replace("${leaveEarlyMins}<=", "").replace("?1:0", "").replace(" && ", "-");
                formulaInfo += formula;
            }
            data.put(td4, formulaInfo);
            datasList.add(data);
        }

        return datasList;
    }

    private List<Map<String, Object>> getColumns() {
        List<Map<String, Object>> columns = new ArrayList<Map<String, Object>>();
        //显示列
        String td1 = "fieldnamespan,516154,1,1";
        //启用
        String td2 = "isenable,18095,4,1";
        //默认
        String td3 = "defaultshow,149,4,1";
        //是否作为薪资项
        // #1276494 概述：满足薪酬指标项关联考勤报表列数据需求 By T.W
//        String td4 = "ifSalaryNeed,534579,4,1";
        //自定义显示列规则
        String td5 = "formulaspan,516134,1,1";

        String[] fields = new String[]{td1, td2, td3, td5};
        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 ("isenable".equals(fieldinfo[0]) || "defaultshow".equals(fieldinfo[0]) || "ifSalaryNeed".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", "20%");
                col.put("com", comLists);
            }

            if ("fieldnamespan".equals(fieldinfo[0]) || "formulaspan".equals(fieldinfo[0])) {
                comMap.put("label", "");
                comMap.put("type", "TEXT");
                comMap.put("key", tmpkey);
                comLists.add(comMap);
                col.put("width", "formulaspan".equals(fieldinfo[0]) ? "40%" : "20%");
                col.put("com", comLists);
            }
            columns.add(col);
        }
        return columns;
    }

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

}
