ProductdownexamController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2021/9/23 0023
  6. * Time: 14:35
  7. */
  8. class ProductdownexamController extends Controller{
  9. public function actionGetUrl()
  10. {
  11. $examId = Req::get("examId");
  12. $classId = Req::get("classId");
  13. $type = Req::get("type");
  14. $studentsIds = Req::get("studentsIds");
  15. $classDownload = $studentsIds ? false : true;
  16. if (!$examId || !$classId || !$type) {
  17. self::responseMsg(0, '缺少参数');
  18. }
  19. $apiUrl = isset(Yii::app()->params["improve_url"][$this->schoolGroupId]) ? Yii::app()->params["improve_url"][$this->schoolGroupId] : null;
  20. if (!$apiUrl) self::responseMsg(0, '接口配置信息错误');
  21. $params = array(
  22. "schoolId" => $this->schoolId,
  23. "examId" => $examId,
  24. "classId" => $classId,
  25. "type" => $type,
  26. "studentIds" => array(),
  27. );
  28. $classinfo = ClassModel::model()->find("class_id=:id", array(":id" => $classId));
  29. $Sem = new SSemester();
  30. $code = $Sem->conn->createQuery()->from('semester')->where("semester_id = '" . $classinfo['semester_id'] . "'")
  31. ->limit(1)
  32. ->query()
  33. ->read();
  34. $sql = "select p.add_time from exam e join class_exam_printer p on p.exam_id=e.exam_id
  35. join class c on e.class_id = c.class_id where e.class_id = '{$classId}' and e.exam_id = '{$examId}'";
  36. $time = $this->sConn->createCommand($sql)->queryRow();
  37. $intarr = array(
  38. "schoolId" => $this->schoolId,
  39. "clazzId" => $classId,
  40. "semester" => $code['refer_code'],
  41. "grade" => $classinfo['grade'],
  42. "students" => array(),
  43. "examTime" => isset($time['add_time'])? $time['add_time'] : time(),
  44. "classify" => $type,
  45. );
  46. //判断学科
  47. $examInfo = Exam::model()->findByPk($examId);
  48. $intarr['subject'] = $examInfo['subject_id'];
  49. if (in_array($intarr['subject'], $this->mathSubjectId) && $intarr['subject']!=41) {
  50. $intarr['subject'] = 3;
  51. }
  52. $pdfPathColumn = 'pdf_path';
  53. $isGenerateColumn = 'is_create_pdf';
  54. $desc = ProductDownload::$desc;
  55. $productName = $examInfo->name . $desc[$type];
  56. $productDownload = new ProductDownload($type);
  57. //*****获取生成的学生***//
  58. $generateStu = $productDownload->getGenerateStu('exam_product_status', 'exam_id', $pdfPathColumn, $isGenerateColumn, $examId, $studentsIds);
  59. if(!$generateStu){
  60. self::responseMsg(0, '无生成的pdf');
  61. }
  62. $params['studentIds'] = $generateStu['studentIds'];
  63. $pathArr = $generateStu['pathArr'];
  64. if (Yii::app()->params["limit_open"]) {
  65. $params["studentIds"] = $productDownload->getCanDownloadStu($generateStu['studentIds'], $intarr, $this->schoolId);
  66. }
  67. ProductProcessTime::model()->updateDownloadTime($params["studentIds"],$type,$examId);
  68. //**************** Step-2 判断是否已经存在下载任务 ********************//
  69. if ($classDownload) {
  70. $packTask = $this->getExistTask($examId, $classId, $type, count($params['studentIds']));
  71. if (isset($packTask->pack_status) && $packTask->pack_status == 0) {
  72. self::responseMsg(3);
  73. }
  74. if (isset($packTask->pack_url) && $packTask->pack_url) {
  75. if($this->updateDownloadStatus($params["studentIds"],$examId,$type) !== false){
  76. ProductProcessTime::model()->updateDownloadTime($params["studentIds"],$type,$examId);
  77. }
  78. echo json_encode(array("success" => 1, "downloadPath" => $packTask->pack_url));
  79. exit;
  80. }
  81. }
  82. //**************** Step-3 根据接口获取文件大小来判断走原有接口打包流程还是走脚本任务 ************//
  83. $sizeRs = Curl::post($apiUrl . "/rest/download_file_size/index", array('path_arr' => $pathArr));
  84. if (!($sizeRs = json_decode($sizeRs))) {
  85. echo json_encode(array("success" => 0, "message" => '请求接口失败'));
  86. exit;
  87. }
  88. if (isset($sizeRs->size)) {
  89. //************ Step-3-1 大于限定值 发送任务到后台打包 ******//
  90. if (Yii::app()->params["pack_product_file_limit"] < $sizeRs->size) {
  91. $packProductModel = new BusinessPackProductTask();
  92. $packProductModel->school_id = $this->schoolId;
  93. $packProductModel->auth_username = $this->authUsername;
  94. $packProductModel->product_type = $type;
  95. $packProductModel->product_name = $productName;
  96. $packProductModel->subject_id = $this->subjectId;
  97. $packProductModel->class_id = $classId;
  98. $packProductModel->class_name = $classinfo['class_name'];
  99. $packProductModel->unique_key = $examId;
  100. $packProductModel->student_count = count($params["studentIds"]);
  101. $packProductModel->pack_json = json_encode($params);
  102. $packProductModel->pack_api_url = $apiUrl . "/rest/download_exam_pdf/index";
  103. $packProductModel->create_time = time();
  104. $packProductModel->save();
  105. if($this->updateDownloadStatus($params["studentIds"],$examId,$type) !== false){
  106. ProductProcessTime::model()->updateDownloadTime($params["studentIds"],$type,$examId);
  107. }
  108. echo json_encode(array("success" => 3, "message" => ""));
  109. exit;
  110. } else {
  111. //************ Step-3-2 小于限定值 原有接口流程 ******//
  112. $rs = Curl::post($apiUrl . "/rest/download_exam_pdf/index", $params);
  113. if (!($rs = json_decode($rs))) {
  114. $rs = array("success" => 0, "message" => "请求接口失败",);
  115. }
  116. if ($rs->success != 0) {
  117. $rs->success = 2;
  118. }
  119. if (isset($rs->downloadPath)) {
  120. if($this->updateDownloadStatus($params["studentIds"],$examId,$type) !== false){
  121. ProductProcessTime::model()->updateDownloadTime($params["studentIds"],$type,$examId);
  122. }
  123. }
  124. if ($this->schoolGroupId && isset($rs->downloadPath)) {
  125. if (YII_ENV == 'pro' || YII_ENV == 'production') {
  126. $rs->downloadPath = preg_replace('/http\:\/\/zstatic\d{1,2}/', 'http://zstatic' . $this->schoolGroupId, $rs->downloadPath);
  127. }
  128. }
  129. echo json_encode($rs);
  130. exit;
  131. }
  132. } else {
  133. echo json_encode(array("success" => 0, "message" => $sizeRs->message));
  134. exit;
  135. }
  136. }
  137. /**
  138. * 获取已存在的下载任务
  139. * @param $unique_id
  140. * @param $class_id
  141. * @param $type
  142. * @param $studentCount
  143. * @return bool|mixed
  144. */
  145. private function getExistTask($unique_id, $class_id, $type, $studentCount)
  146. {
  147. $schoolId = $this->schoolId;
  148. $subjectId = $this->subjectId;
  149. $effectTime = time() - 86400;
  150. $sql = <<<SQL
  151. select pack_status,pack_url from pack_product_task where unique_key='{$unique_id}' and school_id={$schoolId} and subject_id={$subjectId}
  152. and product_type={$type} and class_id='{$class_id}' and student_count={$studentCount} and pack_status<>2 and create_time>{$effectTime} order by create_time desc limit 1;
  153. SQL;
  154. $rs = BusinessPackProductTask::model()->findBySql($sql);
  155. return $rs;
  156. }
  157. /**
  158. * 更新下载状态
  159. * @param $studentsIds
  160. * @param $examId
  161. * @param $type
  162. * @return mixed
  163. */
  164. private function updateDownloadStatus($studentsIds, $examId, $type)
  165. {
  166. $time = time();
  167. $sql = "update exam_product_status set is_download = 1,download_time={$time} where `product_type`={$type} and exam_id = '{$examId}' and student_id in (" . implode(',', $studentsIds) . ")";
  168. return $this->sConn->createCommand($sql)->execute();
  169. }
  170. //FIXME 添加下载任务
  171. public function actionAddTask(){
  172. $examId = Req::get("examId");
  173. $classId = Req::get("classId");
  174. $type = Req::get("type");
  175. $studentsIds = Req::get("studentsIds");
  176. $printType = Req::get("printType");
  177. $absentFilter = Req::get("absent_filter");
  178. if (!$examId || !$classId || !$type) self::responseMsg(0, '缺少参数');
  179. $apiUrl = isset(Yii::app()->params["improve_url"][$this->schoolGroupId]) ? Yii::app()->params["improve_url"][$this->schoolGroupId] : null;
  180. if (!$apiUrl) self::responseMsg(0, '接口配置信息错误');
  181. $params = array(
  182. "schoolId" => $this->schoolId,
  183. "examId" => $examId,
  184. "classId" => $classId,
  185. "type" => $type,
  186. "studentIds" => array(),
  187. );
  188. $classinfo = ClassModel::model()->find("class_id=:id", array(":id" => $classId));
  189. $Sem = new SSemester();
  190. $code = $Sem->conn->createQuery()->from('semester')->where("semester_id = '" . $classinfo['semester_id'] . "'")
  191. ->limit(1)
  192. ->query()
  193. ->read();
  194. $sql = "select p.add_time from exam e join class_exam_printer p on p.exam_id=e.exam_id
  195. join class c on e.class_id = c.class_id where e.class_id = '{$classId}' and e.exam_id = '{$examId}'";
  196. $time = $this->sConn->createCommand($sql)->queryRow();
  197. $intarr = array(
  198. "schoolId" => $this->schoolId,
  199. "clazzId" => $classId,
  200. "semester" => $code['refer_code'],
  201. "grade" => $classinfo['grade'],
  202. "students" => array(),
  203. "examTime" => isset($time['add_time'])? $time['add_time'] : time(),
  204. "classify" => $type,
  205. );
  206. //判断学科
  207. $examInfo = Exam::model()->findByPk($examId);
  208. $intarr['subject'] = $examInfo['subject_id'];
  209. if (in_array($intarr['subject'], $this->mathSubjectId) && $intarr['subject']!=41) {
  210. $intarr['subject'] = 3;
  211. }
  212. $pdfPathColumn = 'pdf_path';
  213. $isGenerateColumn = 'is_create_pdf';
  214. $desc = ProductDownload::$desc;
  215. $productName = $examInfo->name . $desc[$type];
  216. $productDoanload = new ProductDownload($type);
  217. //*****获取生成的学生***//
  218. $generateStu = $productDoanload->getGenerateStu('exam_product_status', 'exam_id', $pdfPathColumn, $isGenerateColumn, $examId, $studentsIds, $absentFilter);
  219. if(!$generateStu){
  220. echo json_encode(array("success" => 0, "message" => "无生成的pdf"));exit();
  221. }
  222. $params['studentIds'] = $generateStu['studentIds'];
  223. if ($printType == 'third_isp') {
  224. $params["subjectId"] = 8;
  225. $params["type"] = 3;
  226. }
  227. if (Yii::app()->params["limit_open"]) {
  228. $params["studentIds"] = $productDoanload->getCanDownloadStu($generateStu['studentIds'], $intarr, $this->schoolId);
  229. }
  230. $packProductModel = new BusinessPackProductTask();
  231. $packProductModel->school_id = $this->schoolId;
  232. $packProductModel->auth_username = $this->authUsername;
  233. $packProductModel->product_type = $type;
  234. $packProductModel->product_name = $productName;
  235. $packProductModel->subject_id = $this->subjectId;
  236. $packProductModel->class_id = $classId;
  237. $packProductModel->class_name = $classinfo['class_name'];
  238. $packProductModel->unique_key = $examId;
  239. $packProductModel->student_count = count($params['studentIds']);
  240. $packProductModel->pack_json = json_encode($params);
  241. $packProductModel->pack_api_url = $apiUrl . "/rest/download_exam_pdf/index";
  242. $packProductModel->create_time = time();
  243. $packProductModel->save();
  244. if($this->updateDownloadStatus($params["studentIds"],$examId,$type) !== false){
  245. ProductProcessTime::model()->updateDownloadTime($params["studentIds"],$type,$examId);
  246. }
  247. echo json_encode(array("success" => 3, "message" => ""));
  248. exit;
  249. }
  250. //FIXME 批量
  251. public function actionBatch(){
  252. $json_params = file_get_contents('php://input');
  253. $params_arr = json_decode($json_params, true);
  254. $notFound = 0;
  255. foreach ($params_arr as $param) {
  256. $examId = isset($param['examId']) ? $param['examId'] : null;
  257. $classId = isset($param['classId']) ? $param['classId'] : null;
  258. $type = isset($param['type']) ? $param['type'] : null;
  259. $absentFilter = isset($param['absent_filter']) ? $param['absent_filter'] : false;
  260. if (!$examId || !$classId || !$type) {
  261. echo json_encode(array("success" => 0, "message" => "缺少参数"));exit();
  262. }
  263. $apiUrl = isset(Yii::app()->params["improve_url"][$this->schoolGroupId]) ? Yii::app()->params["improve_url"][$this->schoolGroupId] : null;
  264. if (!$apiUrl) {
  265. echo json_encode(array("success" => 0, "message" => "接口配置信息错误"));
  266. exit();
  267. }
  268. $params = array(
  269. "schoolId" => $this->schoolId,
  270. "examId" => $examId,
  271. "classId" => $classId,
  272. "type" => $type,
  273. "studentIds" => array(),
  274. );
  275. $classinfo = ClassModel::model()->find("class_id=:id", array(":id" => $classId));
  276. //判断学科
  277. $examInfo = Exam::model()->findByPk($examId);
  278. $pdfPathColumn = 'pdf_path';
  279. $isGenerateColumn = 'is_create_pdf';
  280. $desc = ProductDownload::$desc;
  281. $productName = $examInfo->name . $desc[$type];
  282. $productDownload = new ProductDownload($type);
  283. $condition = array('product_type'=>$type);
  284. //*****获取生成的学生***//
  285. $generateStu = $productDownload->getGenerateStu('exam_product_status', 'exam_id', $pdfPathColumn, $isGenerateColumn, $examId, array(), $absentFilter, $condition);
  286. if(!$generateStu){
  287. $notFound++;
  288. continue;
  289. }
  290. $params['studentIds'] = $generateStu['studentIds'];
  291. $params["subjectId"] = $this->subjectId;
  292. $params["type"] = $type;
  293. ProductProcessTime::model()->updateDownloadTime($params["studentIds"],$type,$examId);
  294. //**************** Step-2 判断是否已经存在下载任务 ********************//
  295. $packTask = $this->getExistTask($examId, $classId, $type, count($params['studentIds']));
  296. if ($packTask) {
  297. continue;
  298. }
  299. //************Step-3 批量直接发送任务到后台打包 ******//
  300. $packProductModel = new BusinessPackProductTask();
  301. $packProductModel->school_id = $this->schoolId;
  302. $packProductModel->auth_username = $this->authUsername;
  303. $packProductModel->product_type = $type;
  304. $packProductModel->product_name = $productName;
  305. $packProductModel->subject_id = $this->subjectId;
  306. $packProductModel->class_id = $classId;
  307. $packProductModel->class_name = $classinfo['class_name'];
  308. $packProductModel->unique_key = $examId;
  309. $packProductModel->student_count = count($params["studentIds"]);
  310. $packProductModel->pack_json = json_encode($params);
  311. $packProductModel->pack_api_url = $apiUrl . "/rest/download_exam_pdf/index";
  312. $packProductModel->create_time = time();
  313. $packProductModel->save();
  314. if($this->updateDownloadStatus($params["studentIds"],$examId,$type) !== false){
  315. ProductProcessTime::model()->updateDownloadTime($params["studentIds"],$type,$examId);
  316. }
  317. }
  318. if($notFound && $notFound==count($params_arr)){
  319. echo json_encode(array("success" => 0, "message" => "无生成的pdf"));exit();
  320. }
  321. echo json_encode(array("success" => 1, "message" => "success"));
  322. }
  323. }