<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Api id="__xCWF21i6kHDZyVpAxEM">
  <name>资格证修改考生考试信息（成绩，签到状态等）</name>
  <parentId>__LgZds5MVHW3o08T7UcX</parentId>
  <applicationid>__LgZds5MVHW3o08T7UcX</applicationid>
  <status>published</status>
  <apiGroup></apiGroup>
  <encode>UTF-8</encode>
  <requestType>put</requestType>
  <requestUrl>/exam/updatePersonExamInfo/{id}</requestUrl>
  <tags></tags>
  <responseType>json</responseType>
  <responseScript><![CDATA[(function () {
  var id = getParameter("id");
  var result = new Packages.java.util.HashMap(); //创建指定类对象
  var jsonStr = getParameter("_content");
  if (isNotNull(jsonStr)) {
    var json = JSON.parse(jsonStr);
    result.put("msg", "success");
    result.put("code", "0");
    result.put("type", "update");
    var type = json.orType;//0是实操1是理论
    var signIn = json.signIn;
    var photo = json.photo;
    var idCardNo = json.idCardNo;
    var examId = json.examId;
    var persondoc = findDocument(id);
    var applyid = persondoc.findItem("applyid").getValue()
    var key = persondoc.findItem("idCardNo").getValue() + persondoc.findItem("examId").getValue();;
    result.put("key", key);
    if (isNotNull(signIn) && isNotNull(id)) {
      if ("0".equals(type)) {
        var selectsql1 = "select item_operateSignIn from tlk_system_exam_person where id = '" + id + "' ";
        var datas = queryByDSName("tms", selectsql1);
        if (datas != null) {
          var item_signin = '';
          for (var iterator = datas.iterator(); iterator.hasNext();) {
            var map1 = iterator.next();//取值
            item_signin = map1.get("item_operateSignIn");
          }
          if ("1".equals(item_signin)) {
            result.put("msg", "该人员已签到！");
            result.put("code", "-1");
            result.put("type", "select");
            return result;
          }
        }
        var operator = getWebUser().getName();
        var updatesql1 = "update tlk_system_exam_person set item_operateSignIn = '" + signIn + "',item_operateSignType='0' ,item_operate_sign_date = now(),item_operate_sign_operator = '" + operator + "' where id = '" + id + "'";
        updateByDSName("tms", updatesql1);
        var persondoc = findDocument(id);
        var applyid = persondoc.findItem("applyid").getValue()
        var applydoc = findDocument(applyid);
        var orderid = applydoc.findItem("orderid").getValue();
        var orderupdatesql = "update tlk_learn_order_person set ITEM_ORDER_NO_REFUND_STATUS = '3' where id='"+orderid+"' and ITEM_ORDER_NO_REFUND_STATUS='' and item_order_no!='' ";
        updateByDSName("tms", orderupdatesql);
        var process = getDocumentProcess();
        var formProcess = getFormProcess();
        var signInfo = formProcess.doView("__5XTVnM0mm10ikDskg9U");
        var signInfoDoc = process.doNew(signInfo, getWebUser(), createParamsTable());
        signInfoDoc.addStringItem("exam_id", examId);
        signInfoDoc.addStringItem("person_id", persondoc.findItem("securityId").getValue());
        signInfoDoc.addStringItem("person_name", persondoc.findItem("name").getValue());
        signInfoDoc.addStringItem("id_card", idCardNo);
        signInfoDoc.addStringItem("type", type);
        signInfoDoc.addStringItem("resuilt", signIn);
        process.doCreate(signInfoDoc);
        removeFromRedis(key);
        return result;
      } else if ("1".equals(type) || !isNotNull(type)) {
        var selectsql1 = "select item_signin from tlk_system_exam_person where id = '" + id + "' ";
        var datas = queryByDSName("tms", selectsql1);
        if (datas != null) {
          var item_signin = '';
          for (var iterator = datas.iterator(); iterator.hasNext();) {
            var map1 = iterator.next();//取值
            item_signin = map1.get("item_signin");
          }
          if ("1".equals(item_signin)) {
            result.put("msg", "该人员已签到！");
            result.put("code", "-1");
            result.put("type", "select");
            return result;
          }
        }
        var operator = getWebUser().getName();
        var updatesql1 = "update tlk_system_exam_person set item_signin = '" + signIn + "',item_operateSignType='0',item_sign_date = now(),item_sign_operator = '" + operator + "' where id = '" + id + "'";
        updateByDSName("tms", updatesql1);

        var persondoc = findDocument(id);
        var process = getDocumentProcess();
        var formProcess = getFormProcess();
        var signInfo = formProcess.doView("__5XTVnM0mm10ikDskg9U");
        var signInfoDoc = process.doNew(signInfo, getWebUser(), createParamsTable());
        signInfoDoc.addStringItem("exam_id", examId);
        signInfoDoc.addStringItem("person_id", persondoc.findItem("securityId").getValue());
        signInfoDoc.addStringItem("person_name", persondoc.findItem("name").getValue());
        signInfoDoc.addStringItem("id_card", idCardNo);
        signInfoDoc.addStringItem("type", type);
        signInfoDoc.addStringItem("resuilt", signIn);
        process.doCreate(signInfoDoc);
        removeFromRedis(key);
        return result;

      }

    }
    if (isNotNull(photo) && isNotNull(id)) {
      if ("1".equals(type) || !isNotNull(type)) {
        var updatesql2 = "update tlk_system_exam_person set item_photo = '" + photo + "' where id = '" + id + "'";
        updateByDSName("tms", updatesql2);
        removeFromRedis(key);
        return result;
      } else if ("0".equals(type)) {
        var updatesql2 = "update tlk_system_exam_person set item_operatePhoto = '" + photo + "' where id = '" + id + "'";
        updateByDSName("tms", updatesql2);
        removeFromRedis(key);
        return result;
      }
    }

    if ("0".equals(type)) {

      var process = getDocumentProcess();
      var operateStatus = json.scStatus;
      var operateReason = json.scRemarks;
      if (isNotNull(operateStatus)) {
        var persondoc = findDocument(id);
        persondoc.findItem("operateScore").setValue(100)
        //persondoc.findItem("sumScore").setValue(100)
        persondoc.findItem("operateStatus").setValue(operateStatus)
        persondoc.findItem("operateCTime").setValue(getToday())
        persondoc.findItem("jumpFace").setValue(0)
        if ("0".equals(operateStatus)) {
          persondoc.findItem("testState").setValue("0")
          persondoc.findItem("operateReason").setValue(operateReason)
          persondoc.findItem("operateScore").setValue(0)
          // persondoc.findItem("sumScore").setValue(0)
        }
        process.doUpdate(persondoc);
      }
      removeFromRedis(key);
    } else if ("1".equals(type) || !isNotNull(type)) {
      var process = getDocumentProcess();
      var writtenScore = json.writtenScore;
      var sumScore = json.sumScore;
      var writtenStatus = json.writtenStatus;
      var writtenRecordMore = json.writtenRecordMore;
      var writtenState = json.writtenState;
      if (isNotNull(writtenStatus)) {
        var persondoc = findDocument(id);
        var examId = persondoc.findItem("examId").getValue();//考试id
        var applyId = persondoc.findItem("applyId").getValue();//报名记录id
        var examDoc = findDocument(examId);
        var applyDoc = findDocument(applyId);
        persondoc.findItem("writtenScore").setValue(writtenScore);
        persondoc.findItem("writtenStatus").setValue(writtenStatus);
        persondoc.findItem("writtenRecordMore").setValue(writtenRecordMore);
        persondoc.findItem("apptype").setValue("0");
        persondoc.findItem("writtenState").setValue(writtenState);
        persondoc.findItem("sumScore").setValue(sumScore);
        applyDoc.findItem("apptype").setValue(0)
        applyDoc.findItem("writtenScore").setValue(writtenScore)
        applyDoc.findItem("examId").setValue(examId)
        var operateScore = persondoc.findItem("operateScore").getValue(); 
        //计算考试结果
        var isComputerExam = examDoc.findItem("isComputerExam").getValue(); //是否有实操
        var isStamina = examDoc.findItem("isStamina").getValue(); //是否有理论
        var examRules = examDoc.findItem("examRules").getValue(); //考试规则
        var writtenRatio = examDoc.findItem("writtenRatio").getValue();//理论占比
        var operateRatio= examDoc.findItem("operateRatio").getValue();//实操占比
        var examStandard= examDoc.findItem("examStandard").getValue();//考试合格分数
        var writtenPassScore = examDoc.findItem("writtenPassScore").getValue();//理论合格分数
        var operatePassScore= examDoc.findItem("operatePassScore").getValue();//实操合格分数
        if("1".equals(isComputerExam) && "0".equals(isStamina)){//只有理论考试
          if("1".equals(examRules)){
            var passScore=writtenPassScore
            }else{
              var passScore=examStandard
              writtenScore = writtenScore * (writtenRatio / 100);
            }
          if(writtenScore < passScore){//考试分数小于合格分数
            persondoc.findItem("testState").setValue("0");
          }else{
            persondoc.findItem("testState").setValue("1");
          }
        }else if("0".equals(isComputerExam) && "1".equals(isStamina) ){//只有实操考试
          if("1".equals(examRules)){
            var passScore=operatePassScore
            }else{
              var passScore=examStandard
              operateScore = operateScore * (operateRatio / 100);
            }
          if(operateScore < passScore){//考试分数小于合格分数
            persondoc.findItem("testState").setValue("0");
          }else{
            persondoc.findItem("testState").setValue("1");
          }
        }else if ("1".equals(isComputerExam) && "1".equals(isStamina) ){//两个都有
          if("0".equals(examRules)){//考试规则为需要两科累加合格
            writtenScore = writtenScore * (writtenRatio / 100);
            operateScore = operateScore * (operateRatio / 100);
            if((writtenScore + operateScore) < examStandard){//不合格
              persondoc.findItem("testState").setValue("0");
            }else{
              persondoc.findItem("testState").setValue("1");
            }
          }else{//需要两个都合格
            if(writtenScore >= writtenPassScore && operateScore >= operatePassScore){
              persondoc.findItem("testState").setValue("1");
            }else{

              persondoc.findItem("testState").setValue("0");
            }

          }

        }
        process.doUpdate(persondoc);
        process.doUpdate(applyDoc);

      }
    }
    removeFromRedis(key);
  } else {
    result.put("msg", "参数不能为空");
    result.put("code", "-1");
    result.put("type", "update");
  }
  return result;
})()
]]></responseScript>
</Api>