package com.engine.kq.biz;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.systeminfo.SystemEnv;

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

public class KQGroupInit extends BaseBean {

  /**
   * 初始化考勤组基本信息
   * @return
   */
  public boolean initGroupBase(){
    boolean flag = false;
    String sql = "";
    RecordSet rs = new RecordSet();
    try{
      int groupId = 0;
      int serialId = 0;
      sql = " select groupid,serialid from kq_inithistory";
      rs.executeQuery(sql);
      if(rs.next()){
        groupId = rs.getInt("groupid");
        serialId = rs.getInt("serialid");
      }

      if(serialId>0){
        List<Object> lsParams = new ArrayList<>();
        boolean isdelete = new KQGroupComInfo().getIsdelete(""+groupId).equals("1");
        if(groupId<=0||isdelete){
          sql = " insert into kq_group (groupname,kqtype,signintype,locationcheckscope) values(?,?,?,?) ";
          lsParams.add(""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005305,weaver.general.ThreadVarLanguage.getLang())+"");
          lsParams.add(1);
          lsParams.add(1);
          lsParams.add(300);
          rs.executeUpdate(sql,lsParams);

          rs.executeQuery("select max(id) from kq_group") ;
          if(rs.next()){
            groupId = rs.getInt(1);
          }

          sql = "update kq_inithistory set groupid = ?";
          rs.executeUpdate(sql,groupId);
        }
        flag = true;
      }
    }catch (Exception e){
      writeLog(e);
    }
    return flag;
  }

  /**
   * 初始化考勤组班次信息
   * @return
   */
  public boolean initGroupSerial(String workDate){
    boolean flag = false;
    String sql = "";
    RecordSet rs = new RecordSet();
    try{
      int groupId = 0;
      int serialId = 0;
      sql = " select groupid,serialid from kq_inithistory";
      rs.executeQuery(sql);
      if(rs.next()){
        groupId = rs.getInt("groupid");
        serialId = rs.getInt("serialid");
      }

      if(serialId>0&&groupId>0){
        for(int i=0;i<7;i++){
          int weekday = i;//星期几
          int id = 0;
          int serialIdTemp = serialId;
          if (("," + workDate + ",").indexOf("," + i + ",") == -1) {
            serialIdTemp = -1;//周末为休息
          }
          sql = " select id from kq_fixedschedulce where weekday = ? and groupid = ? ";
          rs.executeQuery(sql, weekday, groupId);
          if (rs.next()) {
            id = rs.getInt("id");
          }

          if(id>0){
            sql = "update kq_fixedschedulce set serialid =? where id=? ";
            rs.executeUpdate(sql,serialIdTemp==0?null:serialIdTemp,id);
          }else{
            sql = "insert into kq_fixedschedulce(weekday,serialid,groupid) values(?,?,?)";
            rs.executeUpdate(sql,weekday,serialIdTemp==0?null:serialIdTemp,groupId);
          }
        }
        flag = true;
      }
    }catch (Exception e){
      writeLog(e);
    }
    return flag;
  }

  /**
   * 初始化考勤组成员信息
   * @return
   */
  public boolean initGroupMembers(){
    boolean flag = false;
    String sql = "";
    RecordSet rs = new RecordSet();
    try{
      int groupId = 0;
      int serialId = 0;
      sql = " select groupid,serialid from kq_inithistory";
      rs.executeQuery(sql);
      if(rs.next()){
        groupId = rs.getInt("groupid");
        serialId = rs.getInt("serialid");
      }

      if(groupId>0&&serialId>0) {
        //考勤组成员重复判断,单条判断下
        boolean isExists = false;
        sql = " select count(id) from kq_groupmember where type = 6 and (isdelete is null or  isdelete <> '1') and groupid= ? ";
        rs.executeQuery(sql, groupId);
        if (rs.next()) {
          if (rs.getInt(1) > 0) {
            isExists = true;
          }
        }
        if (!isExists) {//初始化所有人 安全级别255
          sql = " INSERT INTO kq_groupmember ( groupid, typevalue, type, seclevel, seclevelto) VALUES (?,?,?,?,?)";
          rs.executeUpdate(sql, groupId, 0, 6, 0, 255);
        }
        flag = true;
      }
    }catch (Exception e){
      writeLog(e);
    }
    return flag;
  }
}
