package com.engine.kq.cmd.reportfielddefine;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.User;
import weaver.systeminfo.SystemEnv;

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

public class SaveFieldOrderDefineCmd extends AbstractCommonCommand<Map<String, Object>> {

    public SaveFieldOrderDefineCmd(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 {
            JSONArray destdatas = JSONArray.parseArray((String) params.get("destdatas"));

            List valueList = new ArrayList();//每一个记录使用记录SQL操作的参数集合
            List paramList = new ArrayList();//所有记录使用记录SQL操作的参数集合

            int destIndex = 0;//已选列的顺序
            int srcIndex = 0;//待选列的顺序

            String[] defaultCol = new String[]{"hrmresource.dspOrder,390525,0,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"};
            if(destdatas.size()==0){
                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(",");

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

                for (int j = 0; j < destdatas.size(); j++) {
                    JSONObject jsonObject = destdatas.getJSONObject(j);
                    String _dataIndex = jsonObject.getString("dataIndex");
                    String _ascOrDesc = jsonObject.getString("ascOrDesc");
                    if (dataIndex.equals(_dataIndex)) {
                        ascOrDesc = _ascOrDesc;
                        sort = "1";
                        destIndex = j;
                    }
                }


                valueList = new ArrayList();
                valueList.add(user.getUID());
                valueList.add(dataIndex);
                valueList.add(labelId);
                valueList.add(sort);
                valueList.add(ascOrDesc);
                valueList.add(sort.equals("1") ? destIndex : srcIndex++);
                paramList.add(valueList);
            }

            RecordSet recordSet = new RecordSet();
            String clearSql = "delete from kq_report_order where userId=?";
            boolean flag = recordSet.executeUpdate(clearSql,user.getUID());
            if (!flag) {
                throw new Exception();
            }

            String insertSql = "INSERT INTO kq_report_order(userId,dataIndex,labelId,sort,ascOrDesc,orders) values(?,?,?,?,?,?)";
            flag = recordSet.executeBatchSql(insertSql, paramList);
            if (flag) {
                resultMap.put("status", "1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(83551, user.getLanguage()));//保存成功
            } else {
                resultMap.put("status", "-1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(84544, user.getLanguage()));//保存失败
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("status", "-1");
            resultMap.put("message", " delete data of kq_report_order failed."+e.getMessage());
        }
        return resultMap;
    }

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

}
