package com.engine.kq.bean;

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

/**
 * 考勤人员编号--用于Excel导入
 */
public class KQScheduleCode extends BaseBean {
  private String keyField = "";
  private String workcode = "";
  private String lastname = "";
  private String devicecode = "";
  private String schedulecode = "";
  private int userlanguage = 7;

  /**
   * 校验数据有效性--用于Excel导入
   *
   * @return
   */
  public String valExcelData() {
    String sql = "";
    RecordSet rs = new RecordSet();
    String errorInfo = "";
    int userid = 0;
    if (this.keyField.equals("workcode")) {
      if (workcode.length() == 0) {
        errorInfo = SystemEnv.getHtmlLabelName(83770, userlanguage);
      } else {
        userid = getUserId(workcode);
        if (userid == 0) {
          errorInfo = SystemEnv.getHtmlLabelName(83772, userlanguage);
        }
      }
    } else if (this.keyField.equals("lastname")) {
      if (lastname.length() == 0) {
        errorInfo = SystemEnv.getHtmlLabelName(83774, userlanguage);
      } else {
        userid = getUserId(lastname);
        if (userid == 0) {
          errorInfo = SystemEnv.getHtmlLabelName(83777, userlanguage);
        }
      }
    }
    if (schedulecode.length() == 0) {
      errorInfo = SystemEnv.getHtmlLabelName(511425, userlanguage);
    }
    boolean isOneDevice = true;
    sql = " select count(1) from kq_schedule_device ";
    rs.executeQuery(sql);
    if(rs.next()){
      if(rs.getInt(1)>1){
        isOneDevice = false;
      }
    }
    if(!isOneDevice){
      if (devicecode.length() == 0) {
        errorInfo = SystemEnv.getHtmlLabelName(	513724, userlanguage);
      }else{
        boolean flag = false;
        sql = " select count(1) from kq_schedule_device where devicecode='"+this.devicecode+"'";
        rs.executeQuery(sql);
        if(rs.next()){
          if(rs.getInt(1)>0){
            flag = true;
          }
        }
        if(!flag) {
          errorInfo = SystemEnv.getHtmlLabelName(513724, userlanguage);
        }
      }
    }
    return errorInfo;
  }

  /**
   * 保存数据
   *
   * @return
   */
  public String save() {
    boolean flag = false;
    RecordSet rs = new RecordSet();
    RecordSetTrans rst = new RecordSetTrans();
    String sql = "";
    try {
      rst.setAutoCommit(false);
      String[] tmpScheduleCodes = Util.splitString(this.schedulecode, ",");
      int tmpUserId = 0;
      if (keyField.equals("workcode")) {
        tmpUserId = getUserId(this.workcode);
      } else if (keyField.equals("lastname")) {
        tmpUserId = getUserId(this.lastname);
      }
      if (tmpUserId > 0) {
        String tmpDeviceId = "";
        sql = "  select * from kq_schedule_device where devicecode='" + this.devicecode + "'";
        rs.executeQuery(sql);
        if (rs.next()) {
          tmpDeviceId = rs.getString("id");
        }

        if(tmpDeviceId.length()==0){
          sql = " select id from kq_schedule_device ";
          rs.executeQuery(sql);
          if(rs.getCounts()==1){
            if(rs.next()){
              if(rs.getInt(1)>1){
                tmpDeviceId = Util.null2String(rs.getString("id"));
              }
            }
          }
        }

        for (int k = 0; k < tmpScheduleCodes.length; k++) {
          //删除本次同步数据
          sql = " delete from kq_schedule_code where resourceid=? and schedulecode=? and deviceid=? ";
          rst.executeUpdate(sql, tmpUserId,tmpScheduleCodes[k],tmpDeviceId.length()==0?null:tmpDeviceId);

          sql = " insert into kq_schedule_code (resourceid ,schedulecode ,deviceid) "
                  + " values(?,?,?)";
          rst.executeUpdate(sql, tmpUserId,tmpScheduleCodes[k],tmpDeviceId.length()==0?null:tmpDeviceId);
        }
        sql = "  SELECT deviceid,schedulecode,COUNT(1) AS num from kq_schedule_code group BY deviceid,schedulecode HAVING COUNT(1)>1  ";
        rst.executeQuery(sql);
        if(rst.next()){
          flag = false;
          rst.rollback();
        }else{
          flag = true;
          rst.commit();
        }
      }
    } catch (Exception e) {
      rst.rollback();
      writeLog(e);
    }
    return String.valueOf(flag);
  }

  private int getUserId(String val) {
    int userid = 0;
    RecordSet rs = new RecordSet();
    String sql = "";
    if(Util.isEnableMultiLang()) {
      if (rs.getDBType().equalsIgnoreCase("sqlserver")) {
        sql = "select id from hrmresource where ltrim(rtrim(dbo.convToMultiLang(ltrim(rtrim(" + keyField + "))," + userlanguage + ")))='" + Util.null2String(val).trim() + "'";
      } else {
        sql = "select id from hrmresource where ltrim(rtrim(convToMultiLang(ltrim(rtrim(" + keyField + "))," + userlanguage + ")))='" + Util.null2String(val).trim() + "'";
      }
    }else{
       sql = "select id from hrmresource where ltrim(rtrim( "+ keyField +"))='" + Util.null2String(val).trim() + "'";
    }
    rs.execute(sql);
    if (rs.next()) {
      userid = rs.getInt("id");
    }
    return userid;
  }

  public String getWorkcode() {
    return this.workcode;
  }

  public void setWorkcode(String workcode) {
    this.workcode = workcode;
  }

  public String getLastname() {
    return this.lastname;
  }

  public void setLastname(String lastname) {
    this.lastname = lastname;
  }

  public void setDevicecode(String devicecode) {
    this.devicecode = devicecode;
  }

  public String getSchedulecode() {
    return this.schedulecode;
  }

  public void setSchedulecode(String schedulecode) {
    this.schedulecode = schedulecode;
  }

  public void setKeyField(String keyField) {
    this.keyField = keyField;
  }

  public void setUserLanguage(String userlanguage) {
    this.userlanguage = Util.getIntValue((String) userlanguage);
  }
}
