<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<activity id="__jT71VV8CzsHL2ssDPQc">
  <name>删除</name>
  <parentId>__4OmOocgSJDtNQu7hz7F</parentId>
  <applicationid>__LgZds5MVHW3o08T7UcX</applicationid>
  <type>3</type>
  <onActionForm></onActionForm>
  <onActionView></onActionView>
  <colorType>default</colorType>
  <beforeActionScript><![CDATA[#include "sysfunction";
(function(){         
  var selectId = getParameterAsText("_selects"); 
  var process = getDocumentProcess();
  println("selectId==>" + selectId);
  if(!isNotNull(selectId)){                               
    return "请先勾选需要删除的人员";                               
  }      
  var arrto = splitText(selectId,";");  
  var arrto2 = [];
  var namearrto = [];
  var sendarrto = new Packages.java.util.ArrayList();
  var applyIds = "";//需要还原补考次数examnum和清除writtenScore的报名id
  var applyIds2 = "";

  // 获取此人员所在考试场次的结束时间
  var computerEndDateSqlStr = "SELECT sei.ITEM_COMPUTERENDDATE FROM tlk_system_exam_info sei JOIN tlk_system_exam_person sep ON sei.ID = sep.ITEM_EXAMID WHERE sep.id = " + "'" + arrto[0] + "'";
  var dateData = queryByDSName("tms",computerEndDateSqlStr);
  var computerEndDateStrFormat = "";
  // 获取当前时间 
  if(dateData.iterator().hasNext()){
    computerEndDateStrFormat = format(dateData.iterator().next().get("ITEM_COMPUTERENDDATE"),"yyyy-MM-dd HH:mm:ss");
  }
  println("computerEndDateStrFormat :" + computerEndDateStrFormat);
  // 判断当前时间是否大于所属人员考试场次安排的结束时间
  var result = computerEndDateStrFormat < format(getToday(),"yyyy-MM-dd HH:mm:ss");
  println("result :" + result);
  var insertSql="insert  into tlk_system_exam_log (FORMID,AUTHOR,item_examId,ITEM_EXAM_PERSON_ID,item_name,item_idcardNo,item_remove_time,item_signIn,item_writtenScore,item_operateScore,id) values   "
  for(var i=0;i<arrto.length;i++){   
    println("i-----"+i)
    var examPersonId =  arrto[i];//拿到考试人员id
    var sql = "select item_name,item_applyid,item_writtenScore,item_operateScore,item_operateStatus,item_teststate,item_signin,ITEM_IDCARDNO,ITEM_NAME,ITEM_EXAMID from tlk_system_exam_person where id = '"+examPersonId+"'";
    var datas = queryByDSName("tms",sql);
    if(datas!=null){
      for(var iterator = datas.iterator();iterator.hasNext();){
        // 生成19位uuid
        var examLogId = 'xxxxxxxxxxxxxxxyxxx'.replace(/[xy]/g, function(c) {
          var r = Math.random()*19|0, v = c == 'x' ? r : (r&0x3|0x8);
          return v.toString(19);
        });
        var map1 = iterator.next();//取值
        var name = map1.get("item_name");
        var applyId= map1.get("item_applyid");
        var writtenScore = map1.get("item_writtenScore");
        var operateScore= map1.get("item_operateScore");
        var testState = map1.get("item_teststate");
        var signIn= map1.get("item_signin");
        var operateStatus= map1.get("item_operateStatus");
        var idcardNo=map1.get("ITEM_IDCARDNO");
        var name=map1.get("ITEM_NAME");
        var examId = map1.get("ITEM_EXAMID");
        if(i==0){
          insertSql+="('__y5McCNE3hw4ZEujAvtR','"+getWebUser().getId()+"','"+examId+"','"+examPersonId+"','"+name+"','"+idcardNo+"','"+format(getToday(), "yyyy-MM-dd HH:mm:ss")+"','"+signIn+"','"+writtenScore+"','"+operateScore+"','"+examLogId+"')";
        }else{
          insertSql+=",('__y5McCNE3hw4ZEujAvtR','"+getWebUser().getId()+"','"+examId+"','"+examPersonId+"','"+name+"','"+idcardNo+"','"+format(getToday(), "yyyy-MM-dd HH:mm:ss")+"','"+signIn+"','"+writtenScore+"','"+operateScore+"','"+examLogId+"')";

        }
      }
    }
    if(parseDouble(writtenScore) > 0||"0".equals(operateStatus)){//|| parseDouble(operateScore) > 0  5.14版本去掉这个条件
      println("进来1")
      namearrto.push(name);
    }else if(!(parseDouble(writtenScore) == 0 && "-".equals(testState) && result) && "1".equals(signIn)){
      println("进来2")
      namearrto.push(name);
    }else{//可以删除的人员
      var boolean1 = true;
      var selectsql = "select count(1) as count from tlk_system_exam_person where item_applyid = '"+applyId+"'"
      var datass = queryByDSName("tms",selectsql);
      if(datass!=null){
        for(var iterator = datass.iterator();iterator.hasNext();){
          var map1 = iterator.next();//取值
          var count = map1.get("count");
          if(count > 1){
            boolean1 = false;
          }
        }
      }
      arrto2.push(arrto[i])
      if(!boolean1){
        applyIds += "'"
        applyIds += applyId;
        applyIds += "'"
        applyIds += ",";
      }else{
        applyIds2 += "'"
        applyIds2 += applyId;
        applyIds2 += "'"
        applyIds2 += ",";
      }
    }
  }
  if(namearrto != ""){
    var reason = "";
    for(var i=0;i<namearrto.length;i++){
      reason+=namearrto[i];
      reason+=","
    }
    reason = reason.substr(0,reason.length-1);
    return reason+"这些人员已存在成绩或已签到无法删除！"
  }else{
    try{
      insertByDSName("tms",insertSql);
    }catch(e){
      println("考试移除报错日志"+e)
    }
  }
  var userid = Packages.com.bcxin.saas.core.enums.ReceiverType.UserId;
  var Message = Packages.com.bcxin.saas.core.enums.MessageType.Message;
  var appId=getApplication();
  var domainid = getDomainid();
  var process2 = getDomainProcess();//获取企业域对象 
  var system_name = process2.doView(domainid);
  var companyname = system_name.getName();//考试机构名称
  var appName = "考试管理";
  var subject="您参加的《国家保安员证》考试已将您从考试安排中移除！";
  var senderId=getWebUser().getId()//当前用户id(作为发送者)
  for(var i=0;i<arrto2.length;i++){
    var examPersonInfo = findDocument(arrto2[i]);
    var SECURITYID = examPersonInfo.findItem("securityId").getValue();//接收者
    var name = examPersonInfo.findItem("name").getValue();
    var examId = examPersonInfo.findItem("examId").getValue();
    var examInfo = findDocument(examId);
    var examName = examInfo.findItem("examName").getValue();
    var content = "";
    var texts = "";
    texts += "【"+name+"】，您好，" ;
    texts += "["+companyname+"]已将您从《国家保安员证》考试安排中移除，考试名称："
    texts += "["+examName+"]，请等待培训学校重新安排，有任何问题可直接咨询所属保安公司或培训学校。";
    var appAvatar = "  https://bcxin-saas-prod.obs.cn-north-1.myhuaweicloud.com:443/upload%2F2021-11-01%2F1635755240828768236.png";
    var title = "";
    var titleImg = "";
    var actionName = "";
    var actionUrl = "";
    content += "appName=" + appName + "&&;" ; //应用名称
    content += "appId=" + appId + "&&;" ;  //应用id
    content += "appAvatar=" + appAvatar + "&&;" ;  //应用图标
    content += "title=" + title + "&&;" ; //标题
    content += "titleImg=" + titleImg + "&&;" ;  //标题图标
    content += "actionName=" + actionName + "&&;" ;   //操作名称
    content += "actionUrl=" + actionUrl + "&&;" ;   //操作链接
    content += "content=" + texts + "";
    var request = createDispatchMessageRequestWithSpecialUser(senderId,Message,subject,content,userid,SECURITYID)
    sendarrto.add(request);
    //清空体检时间和地址
    var applyInfoId=examPersonInfo.findItem("applyId").getValue();
    var applyInfoDoc=findDocument(applyInfoId); 
    if("0".equals(applyInfoDoc.findItem("checkup_type").getValue())&&"0".equals(examInfo.findItem("examKind").getValue())){
      println("进来体检")
      var orderId= applyInfoDoc.findItem("orderId").getValue()
      var orderDoc=findDocument(orderId);
      var checkupId= orderDoc.findItem("checkupId").getValue()
      var checkupProcess = getDocProcess('__1MmCqsHNrmMtmsitMcW');
      var checkupformService = getFormProcess();
      var checkupsubform = checkupformService.doView("__2hdjwmNJyWcPn4RoxfL");
      var taskdoc = checkupProcess.doNew(checkupsubform,getWebUser(),createParamsTable());
      taskdoc.findItem("task_type").setValue('0');
      taskdoc.findItem("is_over").setValue('0');
      taskdoc.findItem("checkup_person_id").setValue(checkupId);
      taskdoc.findItem("last_task_id").setValue(examPersonInfo.findItem("taskId").getValue());
      checkupProcess.doCreate(taskdoc); 
    }
  }
  println("sendarrto------------"+sendarrto);
  dispatchMessages(sendarrto);//批量发送站内信
  if(isNotNull(applyIds)){
    applyIds = applyIds.substr(0,applyIds.length-1);
    var sqlup = "update tlk_qualify_person_apply set ITEM_WRITTENSCORE = '0',item_examNum = '0' where id in ("+applyIds+")"
    println("sqlup-----------"+sqlup);
    updateByDSName("tms",sqlup);
    var sqlup2 = "update tlk_system_exam_person set item_isdelete = '' where item_applyid in ("+applyIds+")";
    println("sqlup2-----------"+sqlup2);
    updateByDSName("tms",sqlup2)
  }
  if(isNotNull(applyIds2)){
    applyIds2 = applyIds2.substr(0,applyIds2.length-1);
    var sqlup3 = "update tlk_qualify_person_apply set ITEM_WRITTENSCORE = '',item_examNum = '0' where id in ("+applyIds2+")"
    println("sqlup-----------"+sqlup3);
    updateByDSName("tms",sqlup3);
    var sqlup2 = "update tlk_system_exam_person set item_isdelete = '' where item_applyid in ("+applyIds2+")";
    println("sqlup2-----------"+sqlup2);
    updateByDSName("tms",sqlup2)
  }
})()]]></beforeActionScript>
  <afterActionScript><![CDATA[]]></afterActionScript>
  <hiddenScript><![CDATA[(function(){
  var domainid = getDomainid();
  var areacode = "";
  var sqlp = "SELECT tk.domainid,dl.ITEM_REGIONId FROM organizationauth.tlk_DepartmentLevel dl JOIN baibaodunflow.tlk_SetSupervise tk ON dl.id = tk.item_registerPoliceAddressID WHERE tk.domainid IN ('"+domainid+"')"
  println("sqlp---------------"+sqlp)
  var datasp = queryByDSName("tms",sqlp);
  if(datasp!=null){
    for(var iterator = datasp.iterator();iterator.hasNext();){
      var map1 = iterator.next();//取值
      areacode= map1.get("ITEM_REGIONId");
      println("areacode----------------"+areacode)
    }
  }
  if(isNotNull(areacode)){
    areacode = areacode.substr(0,2);
    println("areacode2------------------"+areacode)
  }
  if("64".equals(areacode)){
    if(!"505303218683641856--__AOkTvsah9VLicJhQ1FO".equals(domainid) && !"bdjaI4oc".equals(domainid)){
      return true
    }
  }



  return false


})()]]></hiddenScript>
  <readonlyScript><![CDATA[(function(){
  //除了北京地区的需要审核其他地区的不需要审核直接通过
  var domainid = getDomainid();
  var sql="select place_of_register_district_name,place_of_register_province_name from baibaodunflow.vw_organization where id='"+domainid+"'";
  println("sql---------------->"+sql);
  var datas = queryByDSName("tms",sql);
  if(datas!=null){//一家公司只有一条数据
    for(var iterator = datas.iterator();iterator.hasNext();){
      var map1 = iterator.next();//取值
      var place_of_register_province_name= map1.get("place_of_register_province_name");
      var place_of_register_district_name= map1.get("place_of_register_district_name");
      
      
      println("sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss"+place_of_register_province_name)
      println("sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss"+place_of_register_district_name)
      if("北京市".equals(place_of_register_province_name)){
        var examine=decodeURI(getParameter("examine"));
        var examState=decodeURI(getParameter("examState")); //examine
        println("sssssssssssssssssssssssssssssssssssssssssssssss"+examine)//考试审核状态
        /**
        待提交 0 ：可删除、添加、导出
        待审核 3：返回、导出
        审核通过 1 ：
        审核不通过 2：返回、导出
        **/
        if(("0".equals(examState)||"1".equals(examState))&&("待提交".equals(examine)||"审核通过".equals(examine))){
          println(examine+" 这个状态是可以进行删除操作的")
          return false
        }else{
          println("这个状态是不可以进行删除操作的");
          return true;
        }
      }else if("广西壮族自治区".equals(place_of_register_province_name)){
        if("南宁市".equals(place_of_register_district_name)){
          println("南宁市欢迎你")
          var examine=decodeURI(getParameter("examine"));
          var examState=decodeURI(getParameter("examState")); //examine
          println("sssssssssssssssssssssssssssssssssssssssssssssss"+examine)//考试审核状态
            /**
          待提交 0 ：可删除、添加、导出
          待审核 3：返回、导出
          审核通过 1 ：
          审核不通过 2：返回、导出
          **/
          if("0".equals(examState)&&("待提交".equals(examine)||"审核通过".equals(examine))){
            println(examine+" 这个状态是可以进行删除操作的")
            return false
          }else{
            println("这个状态是不可以进行删除操作的");
            return true;
          }
        }else{
          println("广西下的其他市")
          return true
        }
      }else{//其他省份先不管
        
      }
    }
  }
})()]]></readonlyScript>
  <editMode>0</editMode>
  <fileNameScript><![CDATA[]]></fileNameScript>
  <orderno>2</orderno>
  <parentView>__4OmOocgSJDtNQu7hz7F</parentView>
  <impmappingconfigid>__7tkXQ8UWGSqrTJkHtCB</impmappingconfigid>
  <jumpType>0</jumpType>
  <expSub>false</expSub>
  <icon></icon>
  <disableFlowNode>false</disableFlowNode>
  <changeFlowOperator>false</changeFlowOperator>
  <changeFlowCc>false</changeFlowCc>
  <multiLanguageLabel></multiLanguageLabel>
  <dispatcherMode>0</dispatcherMode>
  <dispatcherUrl><![CDATA[]]></dispatcherUrl>
  <dispatcherParams><![CDATA[[{"paramKey":"","paramValue":""}]]]></dispatcherParams>
  <jumpMode>0</jumpMode>
  <jumpActOpenType>0</jumpActOpenType>
  <withOld>false</withOld>
  <actionType>0</actionType>
  <actionSelection>0</actionSelection>
  <workFlowType>0</workFlowType>
  <contextMenu>false</contextMenu>
  <showInToolbar>true</showInToolbar>
  <label><![CDATA[]]></label>
</activity>