<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<task id="__n7eqKqHJrLUj8tQuhTc">
  <name>证书发放</name>
  <parentId>__LgZds5MVHW3o08T7UcX</parentId>
  <applicationid>__LgZds5MVHW3o08T7UcX</applicationid>
  <description></description>
  <type>1</type>
  <runningTime>2025-06-11T20:07:38.000+08:00</runningTime>
  <interval>3</interval>
  <period>34</period>
  <runtimes>0</runtimes>
  <terminateScript><![CDATA[]]></terminateScript>
  <taskScript><![CDATA[#include "echoProvince";
(function () {
  Packages.java.lang.System.err.println("================证书发放执行=================");
  var sqlO = "select id from tlk_system_exam_person WHERE ITEM_CARDSTATE='2'and LASTFLOWOPERATION is null order by item_confirmdate limit 50";
  var datasO = queryByDSName("tms", sqlO);
  var examPersonId = "";
  if (datasO.size() > 0) {
    for (var iterator = datasO.iterator(); iterator.hasNext();) {
      var map1 = iterator.next();//取值
      examPersonId += map1.get("id");
      println("examPersonId1------------"+examPersonId);
      examPersonId += ";"
      println("examPersonId2------------"+examPersonId);
    }
  }
  if (isNotNull(examPersonId)) {
    examPersonId = examPersonId.substring(0,examPersonId.length-1);
    println("examPersonId3------------"+examPersonId);
    var arrto = splitText(examPersonId, ";");
    var process = getDocumentProcess();
    for (var i = 0; i < arrto.length; i++) {
      var examPersonInfoDoc = findDocument(arrto[i]);
      var securityId = examPersonInfoDoc.findItem("securityId").getValue();
      var applyId = examPersonInfoDoc.findItem("applyId").getValue();
      var sqljudge = "select id from tlk_certification where item_personid = '"+applyId+"'";
      var datasjudge = queryByDSName("tms",sqljudge);
      if(datasjudge!="[]"){
        var sqlup = "update tlk_system_exam_person set item_cardstate = '1'  where item_applyid = '"+applyId+"' order by created desc limit 1";
        println("sqlup---------"+sqlup);
        updateByDSName("tms",sqlup); 
        continue;
      }
      //判断是否
      var confirmdate = examPersonInfoDoc.findItem("confirmdate").getValue();//成绩确认时间
      var testState = examPersonInfoDoc.findItem("testState").getValue();
      var examid = examPersonInfoDoc.findItem("examid").getValue();//考试id
      var examclass = findDocument(examid);
      var computerStartDate = examclass.findItem("computerStartDate").getValue();//考试开始时间
      if ( "1".equals(testState)) {
        var sqlR = "select * from tlk_qualify_person_apply where id='" + applyId + "' and item_applyStatus='1' ";
        var datas = queryByDSName("tms", sqlR);
        if (datas.size() > 0) {
          for (var iteratorr = datas.iterator(); iteratorr.hasNext();) {
            var map2 = iteratorr.next();//取值
            var personId = map2.get("id");
            var domainid = map2.get("domainid");
            var name = map2.get("ITEM_NAME");
            var REGISTERPOLICEADDRESS = map2.get("ITEM_REGISTERPOLICEADDRESS")
            var REGISTERPOLICEADDRESSID = map2.get("ITEM_REGISTERPOLICEADDRESSID")
            var idCardNo = map2.get("ITEM_IDCARDNO");
            var birthDay = format(map2.get("ITEM_BIRTHDAY"), "yyyy-MM-dd");
            var avatar = map2.get("ITEM_AVATAR");
            var today = format(getToday(), "yyyy-MM-dd");
            var cultivateCompany = map2.get("ITEM_examOrganization");
            var CompanyNameToGet = map2.get("ITEM_LEARNNAME");
            var cultivateCompanyCode = map2.get("ITEM_organizationId");
            var CompanyNameToGetCode = map2.get("ITEM_LEARNID");
            var areacode = map2.get("ITEM_areacode");
            var sqlq = "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+"')"
            var datasq = queryByDSName("tms", sqlq);
            if (datasq.size() > 0) {
              for (var iteratorq = datasq.iterator(); iteratorq.hasNext();) {
                var map4 = iteratorq.next();//取值
                var regionid = map4.get("ITEM_REGIONId");
              }
            }
            var sqlr="select * from baibaodunflow.vw_organization where id='"+domainid+"'";
            var datass = queryByDSName("tms", sqlr);
            if (datass.size() > 0) {
              for (var iteratorj = datass.iterator(); iteratorj.hasNext();) {
                var map3 = iteratorj.next();//取值
                var registerprovince = map3.get("place_of_register_province_name");
                var OFFICEADDRESSID = map3.get("unify_social_credit_code");
              }
            }

            //通过公司domainid获取所属监管机构hxs
            var sqlBj="select supervise_depart_name,supervise_region_code from obpm2.tenant_organizations where id='"+domainid+"'";
            var dataBj=queryByDSName("tms", sqlBj);
            if (dataBj.size() > 0) {
              for (var iteratorj = dataBj.iterator(); iteratorj.hasNext();) {
                var map8 = iteratorj.next();//取值
                var superviseDepartName = map8.get("supervise_depart_name");//获取监管机构名称
                var superviseRegionCode = map8.get("supervise_region_code");//获取监管机构地区编号
              }
            }
            if(isNotNull(superviseDepartName)){
              var length = superviseDepartName.indexOf("市", 0)
              var superviseDepartName = superviseDepartName.substring(0, length+1);
            }
            var key = superviseRegionCode.substr(0,2);
            var departName='';
            if("11".equals(key)){
              departName = "北京市";
            }
            //结束hxs

            var ResultCodeJsonObject = new Packages.cn.myapps.util.serial.number.GenerateSerialNumber();
            var numberr = "";
            var type = map2.get("ITEM_TYPE");
            if("61".equals(key)){//代表是陕西的
              var prefix="陕01"//给个默认
              var level2name = ""
              var level2code = ""
              var regindexcode="select item_name,item_code from organizationauth.tlk_departmentlevel where ITEM_LEVELS =2 and item_regionid='610000'    and  (select ITEM_indexCode from organizationauth.tlk_departmentlevel where  item_code = '"+REGISTERPOLICEADDRESSID+"') like CONCAT('%',item_code,'%') "
              var regindexcodedates=queryByDSName("tms", regindexcode);//陕西截取第二层级作为发证机关  以及  证书编码不一致
              if(regindexcodedates.size()>0){
                for (var iteratorreg = regindexcodedates.iterator(); iteratorreg.hasNext();) {
                  var mapreg = iteratorreg.next();//取值
                  level2name = mapreg.get("item_name");//获取监管机构名称
                  level2code = mapreg.get("item_code");//获取监管机构名称
                }
                if(isNotNull(level2name)){//配置不同地区的编码
                  if("西安市公安局".equals(level2name)){
                    prefix="陕01"
                  }else if("铜川市公安局".equals(level2name)){
                    prefix="陕02"
                  }else if("宝鸡市公安局".equals(level2name)){
                    prefix="陕03"
                  }else if("咸阳市公安局".equals(level2name)){
                    prefix="陕04"
                  }else if("渭南市公安局".equals(level2name)){
                    prefix="陕05"
                  }else if("延安市公安局".equals(level2name)){
                    prefix="陕06"
                  }else if("汉中市公安局".equals(level2name)){
                    prefix="陕07"
                  }else if("榆林市公安局".equals(level2name)){
                    prefix="陕08"
                  }else if("安康市公安局".equals(level2name)){
                    prefix="陕09"
                  }else if("杨凌农业高新技术产业示范区公安局".equals(level2name)){
                    prefix="陕99"
                  }else if("商洛市公安局".equals(level2name)){
                    prefix="陕10"
                  }
                }
              }
              numberr =ResultCodeJsonObject.getSerialNumberByPrefix(prefix,true,false,false,6); //自动编号的最后编号
            }else {
              if(type==1){
                numberr = ResultCodeJsonObject.getSerialNumber("bapxjyz");
                println("numberr111111111----------------------"+numberr)
                numberr = parseInt(numberr);
              }else{
                numberr = ResultCodeJsonObject.getSerialNumber("bapx");
                println("numberr111111111----------------------"+numberr)
                numberr = parseInt(numberr);
                numberr += 40000;
              }
            }
            println("numberr222222222----------------------"+numberr)
            var process = getDocumentProcess();
            var formProcess = getFormProcess();
            var subform = formProcess.doView("__JPHvq5gsbT0nuFotaqq");
            var doc = process.doNew(subform, getWebUser(), createParamsTable());
            doc.addStringItem("securityId", securityId);
            doc.addStringItem("name", name);
            doc.addStringItem("idCardType", "1");
            doc.addStringItem("idCardNo", idCardNo);
            if(isNotNull(areacode) || type==1){
              //宁夏地区
              doc.addStringItem("areacode", areacode);
              doc.addStringItem("certificateDate", today);
            }else{
              //北京地区需要将制证时间置空，签章时间会同步过来作为发证时间,
              //created 作为制证时间
              //confirmdate 成绩确认时间，其他地区作为发证时间？
              //北京地区
              //目前上线版本只有宁夏和北京所以 使用else
              doc.addStringItem("areacode", "110000");
              doc.addStringItem("certificateDate", today);//2023-07-19，发证日期改回制证日期
            }
            if(type==1){
              doc.addStringItem("certificateType", "11");
            }else{
              doc.addStringItem("certificateType", "1");
            }
            println("regionid---------------"+regionid)
            doc.addStringItem("certificateNO", (echoProvince(regionid) + numberr));
            doc.addStringItem("confirmDate", confirmdate);//成绩确认时间
            if("61".equals(key)){
              doc.addStringItem("certificateFrom",level2name);
            }else if(isNotNull(REGISTERPOLICEADDRESS)){
              var length = REGISTERPOLICEADDRESS.indexOf("市", 0)
              println("length---------------"+length);
              println("REGISTERPOLICEADDRESS11111"+REGISTERPOLICEADDRESS);
              var REGISTERPOLICEADDRESS = REGISTERPOLICEADDRESS.substring(0, length+1);
              println("REGISTERPOLICEADDRESS22222"+REGISTERPOLICEADDRESS);
              if(type==1){
                doc.addStringItem("certificateFrom", CompanyNameToGet);
              }else{
                doc.addStringItem("certificateFrom", REGISTERPOLICEADDRESS + "公安局");
              }
            }else{
              if(type==1){
                doc.addStringItem("certificateFrom", CompanyNameToGet);
              }else{
                doc.addStringItem("certificateFrom", departName + "公安局");
              }            
            }
            if("61".equals(key)){
              doc.addStringItem("certificateFromNO",level2code);
            }else{
              doc.addStringItem("certificateFromNO", OFFICEADDRESSID);
            }
            doc.addStringItem("status", 1)//datart.getItemValueAsString("ID_NUMBER"));             
            doc.addStringItem("cultivateCompany", cultivateCompany);
            doc.addStringItem("CompanyNameToGet", CompanyNameToGet);
            doc.addStringItem("cultivateCompanyCode", cultivateCompanyCode);
            doc.addStringItem("CompanyNameToGetCode", CompanyNameToGetCode);
            doc.addStringItem("oneInchPhoto", avatar);
            doc.addStringItem("birthday", birthDay);
            doc.addStringItem("personId", personId);
            doc.addDateItem("examDate", computerStartDate);
            doc.setDomainid(domainid)
            process.doCreate(doc);
          }
          examPersonInfoDoc.findItem("cardState").setValue("1");
          process.doUpdate(examPersonInfoDoc);
        }
      }
    }
  }
  Packages.java.lang.System.err.println("================结束=================");
})()]]></taskScript>
  <modifyTime>2025-06-11T20:07:38.000+08:00</modifyTime>
  <creator>admin</creator>
  <creatorid>Ai6hllhNjS2ull9TKGb</creatorid>
  <totalRuntimes>0</totalRuntimes>
  <state>0</state>
  <startupType>1</startupType>
  <dayOfMonth>1</dayOfMonth>
  <repeatTimes>0</repeatTimes>
  <frequency>0</frequency>
  <executedCount>0</executedCount>
  <rDate>2021-11-16</rDate>
  <rTime>13:23:25</rTime>
</task>