params["improve_url"][$this->schoolGroupId]) ? Yii::app()->params["improve_url"][$this->schoolGroupId] : null; if (!$apiUrl) self::responseMsg(0, '接口配置信息错误'); $params = array( "schoolId" => $this->schoolId, "gpId" => $gpId, "classId" => $classId, "type" => $type, "studentIds" => array(), ); $classinfo = ClassModel::model()->find("class_id=:id", array(":id" => $classId)); $Sem = new SSemester(); $code = $Sem->conn->createQuery()->from('semester')->where("semester_id = '" . $classinfo['semester_id'] . "'") ->limit(1) ->query() ->read(); $product = SGeneralProduct::model()->find('gp_id=:gp_id and class_id=:classid', array(':gp_id' => $gpId, ':classid' => $classId)); $intarr = array( "schoolId" => $this->schoolId, "clazzId" => $classId, "semester" => $code['refer_code'], "grade" => $classinfo['grade'], "students" => array(), "examTime" => $product['create_time'] ? $product['create_time'] : time(), "classify" => $type, ); //判断学科 $intarr['subject'] = $product['subject_id']; $pdfPathColumn = 'pdf_path'; $isGenerateColumn = 'is_create_pdf'; $desc = ProductDownload::$desc; //假期作业不需要带产品类型名称 if(in_array($type,array(ProductDownload::PRODUCT_TYPE_HOLIDAY_HOMEWORK))) { $productName = $product['name']; }else{ $productName = $product['name'] . $desc[$type]; } $productDownload = new ProductDownload($type); //*****获取生成的学生***// $generateStu = $productDownload->getGenerateStu('general_product_student', 'gp_id', $pdfPathColumn, $isGenerateColumn, $gpId, $studentsIds); if(!$generateStu){ self::responseMsg(0, '无生成的pdf'); } $params['studentIds'] = $generateStu['studentIds']; $pathArr = $generateStu['pathArr']; if (Yii::app()->params["limit_open"]) { $params["studentIds"] = $productDownload->getCanDownloadStu($generateStu['studentIds'], $intarr, $this->schoolId); array_push($params['studentIds'],0); } //**************** Step-2 判断是否已经存在下载任务 ********************// if ($classDownload) { $packTask = $this->getExistTask($gpId, $classId, $type, count($params['studentIds'])); if (isset($packTask->pack_status) && $packTask->pack_status == 0) { self::responseMsg(3); } if (isset($packTask->pack_url) && $packTask->pack_url) { $this->updateDownloadStatus($params["studentIds"],$gpId); echo json_encode(array("success" => 1, "downloadPath" => $packTask->pack_url)); exit; } } //**************** Step-3 根据接口获取文件大小来判断走原有接口打包流程还是走脚本任务 ************// $sizeRs = Curl::post($apiUrl . "/rest/download_file_size/index", array('path_arr' => $pathArr)); if (!($sizeRs = json_decode($sizeRs))) { echo json_encode(array("success" => 0, "message" => '请求接口失败')); exit; } if (isset($sizeRs->size)) { //************ Step-3-1 大于限定值 发送任务到后台打包 ******// if (Yii::app()->params["pack_product_file_limit"] < $sizeRs->size) { $packProductModel = new BusinessPackProductTask(); $packProductModel->school_id = $this->schoolId; $packProductModel->auth_username = $this->authUsername; $packProductModel->product_type = $type; $packProductModel->product_name = $productName; $packProductModel->subject_id = $this->subjectId; $packProductModel->class_id = $classId; $packProductModel->class_name = $classinfo['class_name']; $packProductModel->unique_key = $gpId; $packProductModel->student_count = count($params["studentIds"]); $packProductModel->pack_json = json_encode($params); $packProductModel->pack_api_url = $apiUrl . "/rest/download_general_pdf/index"; $packProductModel->create_time = time(); $packProductModel->save(); $this->updateDownloadStatus($params["studentIds"],$gpId); echo json_encode(array("success" => 3, "message" => "")); exit; } else { //************ Step-3-2 小于限定值 原有接口流程 ******// $rs = Curl::post($apiUrl . "/rest/download_general_pdf/index", $params); if (!($rs = json_decode($rs))) { $rs = array("success" => 0, "message" => "请求接口失败",); } if ($rs->success != 0) { $rs->success = 2; } if (isset($rs->downloadPath)) { $this->updateDownloadStatus($params["studentIds"],$gpId); } if ($this->schoolGroupId && isset($rs->downloadPath)) { if (YII_ENV == 'pro' || YII_ENV == 'production') { $rs->downloadPath = preg_replace('/http\:\/\/zstatic\d{1,2}/', 'http://zstatic' . $this->schoolGroupId, $rs->downloadPath); } } echo json_encode($rs); exit; } } else { echo json_encode(array("success" => 0, "message" => $sizeRs->message)); exit; } } /** * 获取已存在的下载任务 * @param $unique_id * @param $class_id * @param $type * @param $studentCount * @return bool|mixed */ private function getExistTask($unique_id, $class_id, $type, $studentCount) { $schoolId = $this->schoolId; $subjectId = $this->subjectId; $effectTime = time() - 86400; $sql = <<2 and create_time>{$effectTime} order by create_time desc limit 1; SQL; $rs = BusinessPackProductTask::model()->findBySql($sql); return $rs; } /** * 更新下载状态 * @param $studentsIds * @param $gpId * @param $type */ private function updateDownloadStatus($studentsIds, $gpId) { $time = time(); $sql = "update general_product_student set is_download = 1,download_time={$time} where gp_id = '{$gpId}' and student_id in (" . implode(',', $studentsIds) . ")"; $this->sConn->createCommand($sql)->execute(); } //FIXME 添加下载任务 public function actionAddTask(){ $gpId = Req::get("gpId"); $classId = Req::get("classId"); $type = Req::get("type"); $studentsIds = Req::get("studentsIds"); $printType = Req::get("printType"); $absentFilter = Req::get("absent_filter"); if (!$gpId || !$classId) self::responseMsg(0, '缺少参数'); $apiUrl = isset(Yii::app()->params["improve_url"][$this->schoolGroupId]) ? Yii::app()->params["improve_url"][$this->schoolGroupId] : null; if (!$apiUrl) self::responseMsg(0, '接口配置信息错误'); $params = array( "schoolId" => $this->schoolId, "gpId" => $gpId, "classId" => $classId, "type" => $type, "studentIds" => array(), ); $classinfo = ClassModel::model()->find("class_id=:id", array(":id" => $classId)); $Sem = new SSemester(); $code = $Sem->conn->createQuery()->from('semester')->where("semester_id = '" . $classinfo['semester_id'] . "'") ->limit(1) ->query() ->read(); $product = SGeneralProduct::model()->find('gp_id=:gp_id and class_id=:classid', array(':gp_id' => $gpId, ':classid' => $classId)); $intarr = array( "schoolId" => $this->schoolId, "clazzId" => $classId, "semester" => $code['refer_code'], "grade" => $classinfo['grade'], "students" => array(), "examTime" => $product['create_time']?$product['create_time']:time(), "classify" => $type, ); //判断学科 $intarr['subject'] = $product['subject_id']; if (in_array($intarr['subject'], $this->mathSubjectId)) { $intarr['subject'] = 3; } $pdfPathColumn = 'pdf_path'; $isGenerateColumn = 'is_create_pdf'; $desc = ProductDownload::$desc; //假期作业不需要带产品类型名称 if(in_array($type,array(ProductDownload::PRODUCT_TYPE_HOLIDAY_HOMEWORK))) { $productName = $product['name']; }else { $productName = $product['name'] . $desc[$type]; } $productDoanload = new ProductDownload($type); //*****获取生成的学生***// $generateStu = $productDoanload->getGenerateStu('general_product_student', 'gp_id', $pdfPathColumn, $isGenerateColumn, $gpId, $studentsIds, $absentFilter); if(!$generateStu){ echo json_encode(array("success" => 0, "message" => "无生成的pdf"));exit(); } $params['studentIds'] = $generateStu['studentIds']; if ($printType == 'third_isp') { $params["subjectId"] = 8; $params["type"] = 3; } if (Yii::app()->params["limit_open"]) { $params["studentIds"] = $productDoanload->getCanDownloadStu($generateStu['studentIds'], $intarr, $this->schoolId); } $packProductModel = new BusinessPackProductTask(); $packProductModel->school_id = $this->schoolId; $packProductModel->auth_username = $this->authUsername; $packProductModel->product_type = $type; $packProductModel->product_name = $productName; $packProductModel->subject_id = $this->subjectId; $packProductModel->class_id = $classId; $packProductModel->class_name = $classinfo['class_name']; $packProductModel->unique_key = $gpId; $packProductModel->student_count = count($params['studentIds']); $packProductModel->pack_json = json_encode($params); $packProductModel->pack_api_url = $apiUrl . "/rest/download_general_pdf/index"; $packProductModel->create_time = time(); $packProductModel->save(); $this->updateDownloadStatus($params["studentIds"],$gpId); echo json_encode(array("success" => 3, "message" => "")); exit; } //FIXME 批量 public function actionBatch(){ $json_params = file_get_contents('php://input'); $params_arr = json_decode($json_params, true); $notFound = 0; foreach ($params_arr as $param) { $gpId = isset($param['gpId']) ? $param['gpId'] : null; $classId = isset($param['classId']) ? $param['classId'] : null; $type = isset($param['type']) ? $param['type'] : null; $absentFilter = isset($param['absent_filter']) ? $param['absent_filter'] : false; if (!$gpId || !$classId || !$type) { echo json_encode(array("success" => 0, "message" => "缺少参数"));exit(); } $apiUrl = isset(Yii::app()->params["improve_url"][$this->schoolGroupId]) ? Yii::app()->params["improve_url"][$this->schoolGroupId] : null; if (!$apiUrl) { echo json_encode(array("success" => 0, "message" => "接口配置信息错误")); exit(); } $params = array( "schoolId" => $this->schoolId, "gpId" => $gpId, "classId" => $classId, "type" => $type, "studentIds" => array(), ); $classinfo = ClassModel::model()->find("class_id=:id", array(":id" => $classId)); //判断学科 $product = SGeneralProduct::model()->find('gp_id=:gp_id and class_id=:classid', array(':gp_id' => $gpId, ':classid' => $classId)); $pdfPathColumn = 'pdf_path'; $isGenerateColumn = 'is_create_pdf'; $desc = ProductDownload::$desc; //假期作业不需要带产品类型名称 if(in_array($type,array(ProductDownload::PRODUCT_TYPE_HOLIDAY_HOMEWORK))) { $productName = $product['name']; }else { $productName = $product['name'] . $desc[$type]; } $productDownload = new ProductDownload($type); $condition = array('product_type'=>$type); //*****获取生成的学生***// $generateStu = $productDownload->getGenerateStu('general_product_student', 'gp_id', $pdfPathColumn, $isGenerateColumn, $gpId, array(), $absentFilter); if(!$generateStu){ $notFound++; continue; } $params['studentIds'] = $generateStu['studentIds']; $params["subjectId"] = $this->subjectId; $params["type"] = $type; //**************** Step-2 判断是否已经存在下载任务 ********************// $packTask = $this->getExistTask($gpId, $classId, $type, count($params['studentIds'])); if ($packTask) { continue; } //************Step-3 批量直接发送任务到后台打包 ******// $packProductModel = new BusinessPackProductTask(); $packProductModel->school_id = $this->schoolId; $packProductModel->auth_username = $this->authUsername; $packProductModel->product_type = $type; $packProductModel->product_name = $productName; $packProductModel->subject_id = $this->subjectId; $packProductModel->class_id = $classId; $packProductModel->class_name = $classinfo['class_name']; $packProductModel->unique_key = $gpId; $packProductModel->student_count = count($params["studentIds"]); $packProductModel->pack_json = json_encode($params); $packProductModel->pack_api_url = $apiUrl . "/rest/download_general_pdf/index"; $packProductModel->create_time = time(); $packProductModel->save(); $this->updateDownloadStatus($params["studentIds"],$gpId); } if($notFound && $notFound==count($params_arr)){ echo json_encode(array("success" => 0, "message" => "无生成的pdf"));exit(); } echo json_encode(array("success" => 1, "message" => "success")); } }