package com.engine.kq.biz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.io.Writer;
import java.util.Map;
import java.util.UUID;
import weaver.conn.ConnStatement;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.common.DbFunctionUtil;

/**
 * 考勤流程action日志记录
 * 从流程action的触发到使用到最后进入表
 */
public class KQFLowEventLogBiz {

  /**
   * 记录一下流程开始日期结束日期的
   * @param resourceid
   * @param eventLogMap
   * @param eventType
   * @return
   */
  public String logEvent(String resourceid, Map<String, Object> eventLogMap, String eventType) {
    RecordSet rs = new RecordSet();

    String event_info = "";
    if(eventLogMap != null){
      event_info = JSON.toJSONString(eventLogMap,SerializerFeature.DisableCheckSpecialChar,SerializerFeature.DisableCircularReferenceDetect);
    }
    String uuid = UUID.randomUUID().toString();
    boolean isok = false;
    if(rs.getDBType().equalsIgnoreCase("oracle")&& Util.null2String(rs.getOrgindbtype()).equals("oracle")){
      String sql = "insert into kq_flow_log(resourceid,createdatetime,event_info,uuid,flowtype) "
          + " values('"+resourceid+"',"+ DbFunctionUtil.getCurrentFullTimeFunction(rs.getDBType())+",empty_clob(),'"+uuid+"','"+eventType+"') ";
      isok = rs.executeUpdate(sql);
    }else{
      String sql = "insert into kq_flow_log(resourceid,createdatetime,event_info,uuid,flowtype) "
          + " values('"+resourceid+"',"+DbFunctionUtil.getCurrentFullTimeFunction(rs.getDBType())+",'"+event_info+"','"+uuid+"','"+eventType+"') ";
      rs.executeUpdate(sql);
    }
    if(isok && rs.getDBType().equalsIgnoreCase("oracle")&& Util.null2String(rs.getOrgindbtype()).equals("oracle")){
      ConnStatement stat=null;
      try {
        stat = new ConnStatement();
        // 需要使用for update方法来进行更新，
        // 但是，特别需要注意，如果原来CLOB字段有值，需要使用empty_clob()将其清空。
        // 如果原来是null，也不能更新，必须是empty_clob()返回的结果。
        stat.setStatementSql("select event_info from kq_flow_log where uuid=? for update", false);
        stat.setString(1,uuid);
        stat.executeQuery();
        if (stat.next()) {
          oracle.sql.CLOB clob = (oracle.sql.CLOB) stat.getClob("event_info");
          Writer outStream = clob.getCharacterOutputStream();
          char[] c = event_info.toCharArray();
          outStream.write(c, 0, c.length);
          outStream.flush();
          outStream.close();
        }

      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        if (stat != null) {
          stat.close();
        }
      }
    }
    return uuid;
  }

  /**
   * 明细表里记录一下班次和考勤组信息
   * @param resourceid
   * @param workTimeEntityLogMap
   * @param main_uuid
   * @param worktimeType
   * @return
   */
  public String logDetailWorkTimeEntity(String resourceid, Map<String, Object> workTimeEntityLogMap,
      String main_uuid,String worktimeType) {
    RecordSet rs = new RecordSet();

    String serial_info = "";
    if(workTimeEntityLogMap != null){
      serial_info = JSON.toJSONString(workTimeEntityLogMap,SerializerFeature.DisableCheckSpecialChar,SerializerFeature.DisableCircularReferenceDetect);
    }
    String uuid = UUID.randomUUID().toString();
    boolean isok = false;
    if(rs.getDBType().equalsIgnoreCase("oracle")&& Util.null2String(rs.getOrgindbtype()).equals("oracle")){
      String sql = "insert into kq_flow_log_detail(resourceid,createdatetime,serial_info,flow_info,event_info,uuid,flowtype,main_uuid) "
          + " values("+resourceid+","+ DbFunctionUtil.getCurrentFullTimeFunction(rs.getDBType())+",empty_clob(),empty_clob(),empty_clob(),'"+uuid+"','"+worktimeType+"','"+main_uuid+"') ";
      isok = rs.executeUpdate(sql);
    }else{
      String sql = "insert into kq_flow_log_detail(resourceid,createdatetime,serial_info,uuid,flowtype,main_uuid) "
          + " values("+resourceid+","+DbFunctionUtil.getCurrentFullTimeFunction(rs.getDBType())+",'"+serial_info+"','"+uuid+"','"+worktimeType+"','"+main_uuid+"') ";
      rs.executeUpdate(sql);
    }

    if(rs.getDBType().equalsIgnoreCase("oracle")&& Util.null2String(rs.getOrgindbtype()).equals("oracle")){
      ConnStatement stat=null;
      try {
        stat = new ConnStatement();
        // 需要使用for update方法来进行更新，
        // 但是，特别需要注意，如果原来CLOB字段有值，需要使用empty_clob()将其清空。
        // 如果原来是null，也不能更新，必须是empty_clob()返回的结果。
        stat.setStatementSql("select serial_info from kq_flow_log_detail where uuid=? for update", false);
        stat.setString(1,uuid);
        stat.executeQuery();
        if (stat.next()) {
          oracle.sql.CLOB clob = (oracle.sql.CLOB) stat.getClob("serial_info");
          Writer outStream = clob.getCharacterOutputStream();
          char[] c = serial_info.toCharArray();
          outStream.write(c, 0, c.length);
          outStream.flush();
          outStream.close();
        }

      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        if (stat != null) {
          stat.close();
        }
      }
    }

    return uuid;
  }

  /**
   * 记录一下加班生成明细
   * @param resourceid
   * @param overtimeLogMap
   * @param uuid
   */
  public void logDetailFlowMap(String resourceid, Map<String, Object> overtimeLogMap,String uuid) {
    RecordSet rs = new RecordSet();

    String flow_info = "";
    if(overtimeLogMap != null){
      flow_info = JSON.toJSONString(overtimeLogMap, SerializerFeature.DisableCheckSpecialChar,SerializerFeature.DisableCircularReferenceDetect);
    }
    if(rs.getDBType().equalsIgnoreCase("oracle")&&rs.getOrgindbtype().equalsIgnoreCase("oracle")){
      ConnStatement stat=null;
      try {
        stat = new ConnStatement();
        // 需要使用for update方法来进行更新，
        // 但是，特别需要注意，如果原来CLOB字段有值，需要使用empty_clob()将其清空。
        // 如果原来是null，也不能更新，必须是empty_clob()返回的结果。
        stat.setStatementSql("select flow_info from kq_overtime_log_detail where uuid=? for update", false);
        stat.setString(1,uuid);
        stat.executeQuery();
        if (stat.next()) {
          oracle.sql.CLOB clob = (oracle.sql.CLOB) stat.getClob("flow_info");
          Writer outStream = clob.getCharacterOutputStream();
          char[] c = flow_info.toCharArray();
          outStream.write(c, 0, c.length);
          outStream.flush();
          outStream.close();
        }

      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        if (stat != null) {
          stat.close();
        }
      }
    }else{
      String sql = "update kq_overtime_log_detail set flow_info='"+flow_info+"' where uuid='"+uuid+"'";
      rs.executeUpdate(sql);
    }

  }

  /**
   * 记录对应的加班生成过程中使用的表sql和表数据
   * @param resourceid
   * @param eventLogMap
   * @param uuid
   * @param overtimetype
   */
  public void logDetailEvent(String resourceid, Map<String, Object> eventLogMap, String uuid, String overtimetype) {
    RecordSet rs = new RecordSet();

    String event_info = "";
    if(eventLogMap != null){
      event_info = JSON.toJSONString(eventLogMap,SerializerFeature.DisableCheckSpecialChar,SerializerFeature.DisableCircularReferenceDetect);
    }

    if(rs.getDBType().equalsIgnoreCase("oracle")&&rs.getOrgindbtype().equalsIgnoreCase("oracle")){
      ConnStatement stat=null;
      try {
        stat = new ConnStatement();
        // 需要使用for update方法来进行更新，
        // 但是，特别需要注意，如果原来CLOB字段有值，需要使用empty_clob()将其清空。
        // 如果原来是null，也不能更新，必须是empty_clob()返回的结果。
        stat.setStatementSql("select event_info from kq_flow_log_detail where uuid=? for update", false);
        stat.setString(1,uuid);
        stat.executeQuery();
        if (stat.next()) {
          oracle.sql.CLOB clob = (oracle.sql.CLOB) stat.getClob("event_info");
          Writer outStream = clob.getCharacterOutputStream();
          char[] c = event_info.toCharArray();
          outStream.write(c, 0, c.length);
          outStream.flush();
          outStream.close();
        }

      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        if (stat != null) {
          stat.close();
        }
      }
    }else{
      String sql = "update kq_overtime_log_detail set event_info='"+event_info+"' where uuid='"+uuid+"'";
      rs.executeUpdate(sql);
    }

  }
}
