ProductdowngeneralController.php 15 KB

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