package com.engine.kq.cmd.group;

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 com.engine.kq.util.ExcelUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

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

  private HttpServletRequest request;
  private HttpServletResponse response;

  public ExportExcelCmd(Map<String, Object> params, HttpServletRequest request, HttpServletResponse response, User user) {
    this.user = user;
    this.params = params;
    this.request = request;
    this.response = response;
  }

  @Override
  public Map<String, Object> execute(CommandContext commandContext) {
    Map<String, Object> retmap = new HashMap<String, Object>();
    KQGroupComInfo KQGroupComInfo = new KQGroupComInfo();
    RecordSet rs = new RecordSet();
    String sql = "";
    try {
      if (!HrmUserVarify.checkUserRight("HrmKQGroup:Add", user)) {
        retmap.put("status", "-1");
        retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
        return retmap;
      }
      KQGroupComInfo kqGroupComInfo = new KQGroupComInfo();
      String groupId = Util.null2String(params.get("groupId"));

      LinkedHashMap<String, Object> workbook = new LinkedHashMap<>();
      List<Object> lsSheet = new ArrayList<>();
      Map<String, Object> sheet = null;
      List<Object> titleList = null;
      Map<String,Object> title = null;
      List<List<Object>> dataList = null;
      List<Object> data = null;

      sheet = new HashMap<>();
      sheet.put("sheetName", SystemEnv.getHtmlLabelName(20817, user.getLanguage()));
      sheet.put("sheetTitle", SystemEnv.getHtmlLabelName(389949, user.getLanguage()).replace("$groupName$",kqGroupComInfo.getGroupname(groupId)));
      titleList = new ArrayList<>();

      title = new HashMap<>();
      title.put("title",SystemEnv.getHtmlLabelName(801, user.getLanguage()));
      title.put("width",15*256);
      titleList.add(title);
      title = new HashMap<>();
      title.put("title",SystemEnv.getHtmlLabelName(802, user.getLanguage()));
      title.put("width",15*256);
      titleList.add(title);
      title = new HashMap<>();
      title.put("title",SystemEnv.getHtmlLabelName(388714, user.getLanguage())+"("+SystemEnv.getHtmlLabelName(125675, user.getLanguage())+")");
      title.put("width",15*256);
      titleList.add(title);
      title = new HashMap<>();
      title.put("title",SystemEnv.getHtmlLabelName(389220, user.getLanguage()));
      title.put("width",50*256);
      titleList.add(title);
      title = new HashMap<>();
      title.put("title",SystemEnv.getHtmlLabelName(389221, user.getLanguage()));
      title.put("width",50*256);
      titleList.add(title);

      sheet.put("titleList", titleList);

      dataList = new ArrayList<>();
       sql = " select locationname,longitude,latitude,checkscope,address from kq_location where groupid = ? order by id ";
      rs.executeQuery(sql, groupId);
      while (rs.next()) {
        data = new ArrayList<>();
        data.add(rs.getString("longitude"));
        data.add(rs.getString("latitude"));
        data.add(rs.getString("checkscope"));
        data.add(rs.getString("address"));
        data.add(rs.getString("locationname"));
        dataList.add(data);
      }
      sheet.put("dataList", dataList);
      lsSheet.add(sheet);

      sheet = new HashMap<>();
      sheet.put("sheetName", SystemEnv.getHtmlLabelName(389126, user.getLanguage()));
      sheet.put("sheetTitle", SystemEnv.getHtmlLabelName(389952, user.getLanguage()).replace("$groupName$",kqGroupComInfo.getGroupname(groupId)));
      titleList = new ArrayList<>();
      title = new HashMap<>();
      title.put("title",SystemEnv.getHtmlLabelName(195, user.getLanguage()));
      title.put("width",10*256);
      titleList.add(title);
      title = new HashMap<>();
      title.put("title",SystemEnv.getHtmlLabelName(32072, user.getLanguage()));
      title.put("width",50*256);
      titleList.add(title);
      sheet.put("titleList", titleList);
      dataList = new ArrayList<>();
      sql = " select wifiname,mac,groupid from kq_wifi where groupid = ? order by id ";
      rs.executeQuery(sql, groupId);
      while (rs.next()) {
        data = new ArrayList<>();
        data.add(rs.getString("wifiname"));
        data.add(rs.getString("mac"));
        dataList.add(data);
      }
      sheet.put("dataList", dataList);
      lsSheet.add(sheet);

      workbook.put("sheet", lsSheet);
      workbook.put("fileName", SystemEnv.getHtmlLabelName(389959, user.getLanguage()).replace("$groupName$",kqGroupComInfo.getGroupname(groupId)));
      ExcelUtil ExcelUtil = new ExcelUtil();
      ExcelUtil.export(workbook,request,response);

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

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