package com.engine.htsc.meeting.cmd;

import cn.hutool.core.date.DateUtil;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.monitor.cache.CacheFactory;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

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

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

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

  @Override
  public Map<String, Object> execute(CommandContext commandContext) {

    Map<String, Object> apidatas = new HashMap<>();


    if (null == user) {
      apidatas.put("hasRight", false);
      return apidatas;
    }
    apidatas.put("hasRight", true);

    //会议议案数据入库
    String docid = Util.null2String(params.get("fj"));
    String fjfj = Util.null2String(params.get("fjfj"));
    String sfxyddyj = Util.null2String(params.get("sfxyddyj"));
    String sfxyflyjs = Util.null2String(params.get("sfxyflyjs"));
    String sfxyjshyj = Util.null2String(params.get("sfxyjshyj"));
    String sfxytjgddh = Util.null2String(params.get("sfxytjgddh"));
    String sfxybj = Util.null2String(params.get("sfxybj")); //是否需要表决
    String xgrq = DateUtil.today();
    String sfsjhbbj = Util.null2String(params.get("sfsjhbbj"));
    String bjjg = Util.null2String(params.get("bjjg"));
    if(StringUtils.isEmpty(bjjg)) {
      bjjg = sfxybj.equals("0") ? "1" : "0"; //表决结果
    }
    String bz = Util.null2String(params.get("bz"));
    String yabq = Util.null2String(params.get("yabq"));
    String billid = Util.null2String(params.get("billid"));
    String yatjbm = Util.null2String(params.get("yatjbm"));

    RecordSet rs = new RecordSet();

    Map<String, Object> model = new HashMap<>();

    model.put("YTMC", getYtmcObj(docid));
    model.put("DDYJ", sfxyddyj);
    model.put("FLYJS", sfxyflyjs);
    model.put("JSHYJ", sfxyjshyj);
    model.put("GDDH", sfxytjgddh);
    model.put("SFXBJ", sfxybj);
    model.put("HBBJ", sfsjhbbj);
    model.put("BJJG", bjjg);
    model.put("BZ", bz);
    model.put("YABQ", getYabqObj(yabq));
    model.put("FJFJ", fjfj);
    model.put("YATJBM", getDeptObj(yatjbm));

    apidatas.put("model", model);


//    String sql = "INSERT INTO UF_HYWH_DT3 (MAINID,YTMC,SCR,DDYJ,FLYJS,JSHYJ,GDDH,SFXBJ,XGRQ,HBBJ,BJJG,BZ,YABQ) " +
//            "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";
//    rs.executeUpdate(sql,
//            billid,
//            docid,
//            user.getUID(),
//            sfxyddyj,
//            sfxyflyjs,
//            sfxyjshyj,
//            sfxytjgddh,
//            sfxybj,
//            xgrq,
//            sfsjhbbj,
//            bjjg,
//            bz,
//            yabq
//    );
//    CacheFactory.getInstance().reset();

    return apidatas;
  }

  private String getFileIdByImageFileId(String imageFileId) {
    try {
      String sql = "SELECT ID FROM DocImageFile WHERE IMAGEFILEID =?";
      RecordSet rs = new RecordSet();
      rs.executeQuery(sql, imageFileId);
      if (rs.next()) {
        return Util.null2String(rs.getString("ID"));
      }
    } catch (Exception e) {
      writeLog("getFileIdByImageFileId" + e);
    }
    return "";
  }

  private String removeSuffix(String title) {
    int i = title.lastIndexOf(".");
    try {
      return title.substring(0, i);
    } catch (Exception e) {
      return "unknow";
    }
  }

  private Map<String, Object> getYtmcObj(String docid) {
    RecordSet rs = new RecordSet();
    String sql = "SELECT IMAGEFILENAME FROM DocImageFile WHERE DOCID =?";
    rs.executeQuery(sql, docid);
    Map<String, Object> ytmc = new HashMap<>();
    if (rs.next()) {
      ytmc.put("id", docid);
      ytmc.put("name", removeSuffix(Util.null2String(rs.getString("IMAGEFILENAME"))));
    } else {
      ytmc.put("id", "");
      ytmc.put("name", "");
    }
    return ytmc;
  }

  private Map<String, Object> getYabqObj(String yabq) {
    RecordSet rs = new RecordSet();
    String yabqCondition = String.join("','", yabq.split(","));
    String sql = "SELECT wm_concat_old(BQ) as bqs FROM UF_BQ WHERE ID IN\n" +
            "('" + yabqCondition + "')";
    rs.executeQuery(sql);
    Map<String, Object> yabqObj = new HashMap<>();
    if (rs.next()) {
      yabqObj.put("id", yabq);
      yabqObj.put("name", Util.null2String(rs.getString("bqs")));
    } else {
      yabqObj.put("id", "");
      yabqObj.put("name", "");
    }
    return yabqObj;
  }

  public static Map<String, Object> getDeptObj(String yatjbm) {
    DepartmentComInfo dc = new DepartmentComInfo();
    Map<String, Object> yatjbmObj = new HashMap<>();
    String name = Util.null2String(dc.getDepartmentmark(yatjbm));
    if (!"".equalsIgnoreCase(name)) {
      yatjbmObj.put("id", yatjbm);
      yatjbmObj.put("name", name);
    } else {
      yatjbmObj.put("id", "");
      yatjbmObj.put("name", "");
    }
    return yatjbmObj;
  }

  public static void main(String[] args) {
    GCONST.setRootPath("");
    GCONST.setServerName("ecology");

    Map<String, Object> deptObj = getDeptObj("695");

    System.out.println("deptObj = " + deptObj);
  }

}
