package com.engine.kq.wfset.action;

import com.alibaba.fastjson.JSON;
import com.engine.kq.biz.KQAttProcSetComInfo;
import com.engine.kq.biz.KQFlowActiontBiz;
import com.engine.kq.biz.KQLeaveRulesBiz;
import com.engine.kq.enums.KqSplitFlowTypeEnum;
import com.engine.kq.log.KQLog;
import com.engine.kq.wfset.bean.SplitBean;
import com.engine.kq.wfset.util.KQFlowLeaveUtil;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.workflow.WorkflowComInfo;

/**
 * 兼容E8的请假冻结action
 */
public class KqFreezeVacationAction extends BaseBean implements Action {
  private KQLog kqLog = new KQLog();

  @Override
  public String execute(RequestInfo request) {
    //e8 的状态标识
    int DEDUCTION = 0;
    int FREEZE = 1;
    int RELEASE = 2;
    this.writeLog("KqFreezeVacationAction", "do action on request:" + request.getRequestid());
    String requestid = request.getRequestid();
    kqLog.info("do KqFreezeVacationAction on requestid:"+requestid);
    int requestidInt = Util.getIntValue(requestid, 0);

    String workflowid = request.getWorkflowid();
    String formid = new WorkflowComInfo().getFormId(workflowid);
    KQAttProcSetComInfo kqAttProcSetComInfo = new KQAttProcSetComInfo();
    int cur_kqtype = Util.getIntValue(kqAttProcSetComInfo.getkqType(workflowid),-1);

    if(cur_kqtype != KqSplitFlowTypeEnum.LEAVE.getFlowtype()){
      return Action.SUCCESS;
    }

    try {
      List<SplitBean> splitBeans = new ArrayList<SplitBean>();

      KQFlowLeaveUtil kqFlowLeaveUtil = new KQFlowLeaveUtil();
      KQFlowActiontBiz kqFlowActiontBiz = new KQFlowActiontBiz();
      ResourceComInfo rci = new ResourceComInfo();
      DateTimeFormatter datetimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
      RecordSet rs = new RecordSet();
      RecordSet rs1 = new RecordSet();
      String proc_set_sql = "select * from kq_att_proc_set where field001 = ? and field002 = ? ";
      rs.executeQuery(proc_set_sql, workflowid,formid);
      if(rs.next()){
        String proc_set_id = rs.getString("id");
        //得到这个考勤流程设置是否使用明细
        String usedetails = rs.getString("usedetail");
        int kqtype = Util.getIntValue(rs.getString("field006"));
        Map<String, String> map = new HashMap<>();
        if(requestidInt > 0){
          map.put("requestId", "and t.requestId = " + requestidInt);
        }

        Map<String, String> sqlMap = kqFlowActiontBiz.handleSql(proc_set_id, usedetails, requestidInt,kqtype,map);
        Map<String,String> result = kqFlowLeaveUtil.handleKQLeaveAction(sqlMap, splitBeans, datetimeFormatter, Util.getIntValue(workflowid), requestidInt, rci);
        if(!result.isEmpty()){
          String error = Util.null2String(result.get("message"));
          request.getRequestManager().setMessageid("666" + request.getRequestid() + "999");
          request.getRequestManager().setMessagecontent(error);
          return Action.FAILURE_AND_CONTINUE;
        }

      }
      kqLog.info("KqFreezeVacationAction splitBeans:"+ JSON.toJSONString(splitBeans));
      String batchSql = "insert into KQ_ATT_VACATION (requestId,workflowId,dataid,detailid,resourceId,fromDate,fromTime,toDate,toTime,duration,newLeaveType,durationrule,status)"+
          " values(?,?,?,?,?,?,?,?,?,?,?,?,?) ";
      List<List> params = new ArrayList<List>();

      String delSql = "delete from KQ_ATT_VACATION where requestId=?";
      List<List> delparams = new ArrayList<List>();

      for(SplitBean bean : splitBeans){
        List<Object> beanParams =  new ArrayList<Object>();
        String newLeaveType = bean.getNewLeaveType();
        //这个表里只存储含有假期余额的
        boolean balanceEnable = KQLeaveRulesBiz.getBalanceEnable(newLeaveType);
        kqLog.info("KqFreezeVacationAction newLeaveType:"+ newLeaveType+":balanceEnable:"+balanceEnable);
        if(!balanceEnable) {
          continue;
        }
        beanParams.add(bean.getRequestId());
        beanParams.add(bean.getWorkflowId());
        beanParams.add(bean.getDataId());
        beanParams.add(bean.getDetailId());
        beanParams.add(bean.getResourceId());
        beanParams.add(bean.getFromDate());
        beanParams.add(bean.getFromTime());
        beanParams.add(bean.getToDate());
        beanParams.add(bean.getToTime());
        beanParams.add(bean.getDuration());
        beanParams.add(bean.getNewLeaveType());
        beanParams.add(bean.getDurationrule());
        beanParams.add(FREEZE);
        params.add(beanParams);

        List<Object> delParam =  new ArrayList<Object>();
        delParam.add(bean.getRequestId());
        delparams.add(delParam);
      }
      if(!params.isEmpty()){
        boolean delOk = rs1.executeBatchSql(delSql, delparams);
        if(!delOk){
          request.getRequestManager().setMessageid("666" + request.getRequestid() + "999");
          request.getRequestManager().setMessagecontent(""+weaver.systeminfo.SystemEnv.getHtmlLabelName(1232,weaver.general.ThreadVarLanguage.getLang())+"action"+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005361,weaver.general.ThreadVarLanguage.getLang())+""+params);
          kqLog.info("KqFreezeVacationAction删除数据失败:"+requestid);
          return Action.FAILURE_AND_CONTINUE;
        }

        boolean isOk = rs1.executeBatchSql(batchSql, params);
        if(!isOk){
          request.getRequestManager().setMessageid("666" + request.getRequestid() + "999");
          request.getRequestManager().setMessagecontent(""+weaver.systeminfo.SystemEnv.getHtmlLabelName(1232,weaver.general.ThreadVarLanguage.getLang())+"action"+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005361,weaver.general.ThreadVarLanguage.getLang())+""+params);
          return Action.FAILURE_AND_CONTINUE;
        }
      }else{
        kqLog.info("KqFreezeVacationAction没有冻结数据:"+requestid);
      }
    } catch (Exception e) {
      kqLog.info("KqFreezeVacationAction:"+e);
      request.getRequestManager().setMessageid("11111" + request.getRequestid() + "22222");
      request.getRequestManager().setMessagecontent("请假流程【冻结action】报错，请联系管理员！");
      return Action.FAILURE_AND_CONTINUE;
    }

    return Action.SUCCESS;

  }

}
