BaseExport.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. namespace common\libs\export;
  3. use common\helpers\Cache as CacheHelper;
  4. use common\helpers\Date;
  5. use common\helpers\Form;
  6. use common\helpers\http\RemoteUploadApi;
  7. use common\helpers\Tool;
  8. use common\libs\dataList\DataList;
  9. use common\models\Export;
  10. use Yii;
  11. use yii\base\Exception;
  12. use yii\base\StaticInstanceTrait;
  13. use yii\base\Component;
  14. class BaseExport extends Component {
  15. use StaticInstanceTrait;
  16. public $moduleId;
  17. /**
  18. * @var string the model class name. This property must be set.
  19. */
  20. public $modelClass;
  21. /**
  22. * @var
  23. */
  24. public $listModelClass;
  25. /**
  26. * @var int
  27. */
  28. public $pageSize = 100;
  29. /**
  30. * @var int
  31. */
  32. public $totalCount = 0;
  33. /**
  34. * @var int
  35. */
  36. public $offset = 0;
  37. /**
  38. * @var int
  39. */
  40. public $limit = 100;
  41. /**
  42. * csv文件名称
  43. * @var null
  44. */
  45. public $fileName = null;
  46. /**
  47. * 保存的基本路径
  48. * @var null
  49. */
  50. public $saveBasePath = null;
  51. /**
  52. * 保存路径
  53. * @var null
  54. */
  55. public $savePath = null;
  56. /**
  57. * 正在导出的id
  58. * 来源于exporting_file表
  59. * @var null
  60. */
  61. public $exportId = null;
  62. /**
  63. * 操作人的id
  64. * @var null
  65. */
  66. public $userId = null;
  67. /**
  68. * 任务id
  69. * @var null
  70. */
  71. public $taskId = null;
  72. /**
  73. * 是否完成
  74. * @var bool
  75. */
  76. public $completed = false;
  77. /**
  78. * @var array
  79. */
  80. public $params = [];
  81. /**
  82. * 文件句柄
  83. * @var null
  84. */
  85. private $_fp = null;
  86. /**
  87. * 是否上传远程
  88. * @var bool
  89. */
  90. public $isRemote = true;
  91. /**
  92. * @var DataList
  93. */
  94. private $_listModel;
  95. public static function factory($taskId) {
  96. return new self([
  97. 'taskId' => $taskId
  98. ]
  99. );
  100. }
  101. /**
  102. *
  103. */
  104. public function init() {
  105. parent::init();
  106. $this->isRemote = \Yii::$app->params['isRemoteUpload'];
  107. }
  108. /**
  109. * 生成复杂的路径
  110. * @return string
  111. */
  112. public function pathCreator(...$params) {
  113. $hash = md5(implode('_', $params));
  114. $first = substr($hash, 0, 3);
  115. $second = substr($hash, 3, 2);
  116. $dir = $first . __DS__ . $second . __DS__;
  117. return $dir;
  118. }
  119. /**
  120. * 获取文件名
  121. * @return string|null
  122. * @throws \Exception
  123. */
  124. public function getFileName() {
  125. $this->fileName = date('YmdHis', Date::nowTime()) . random_int(1000, 9999) . '.csv';
  126. return $this->fileName;
  127. }
  128. /**
  129. * 获取保存的路径
  130. * @return array|null
  131. */
  132. public function getSavePath() {
  133. $this->savePath = $this->pathCreator(date('Ymd', Date::nowTime()));
  134. return trim($this->savePath, __DS__);
  135. }
  136. /**
  137. * @return bool|string|null
  138. */
  139. public function getSaveBasePath() {
  140. $this->saveBasePath = Yii::getAlias('@backendApi') . __DS__ . 'web' . __DS__ . 'upload' . __DS__ . \Yii::$app->params['excelLocalDir'];
  141. return trim($this->saveBasePath, __DS__);
  142. }
  143. /**
  144. * 创建目录(递归创建)
  145. *
  146. * @param $dir
  147. * @return string|false
  148. */
  149. public function mkdir($dir) {
  150. if (!is_dir($dir)) {
  151. $this->mkdir(dirname($dir));
  152. if (!mkdir($dir, 0777))
  153. return false;
  154. @exec('chown -R www:www /'.$dir);
  155. @exec('chmod -R 777 /'.$dir);
  156. }
  157. return $dir;
  158. }
  159. /**
  160. * @return array|mixed
  161. */
  162. public function getParams() {
  163. $this->params = CacheHelper::getAsyncParams($this->taskId);
  164. return $this->params;
  165. }
  166. /**
  167. * @return mixed|null
  168. */
  169. public function getUserId() {
  170. $this->userId = isset($this->params['userId']) ? $this->params['userId'] : null;
  171. return $this->userId;
  172. }
  173. /**
  174. * @return |null
  175. */
  176. public function getExportId() {
  177. $this->exportId = isset($this->params['exportId']) ? $this->params['exportId'] : null;
  178. return $this->userId;
  179. }
  180. /**
  181. * 生成
  182. * @return bool
  183. * @throws Exception
  184. * @throws \yii\base\InvalidConfigException
  185. * @throws \yii\httpclient\Exception
  186. */
  187. public function generate() {
  188. //Logger::info(date('Y-m-d H:i:s'), 'export');
  189. $this->getParams();
  190. if (!$this->params) {
  191. throw new Exception('无法获取需要的参数');
  192. }
  193. $path = $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  194. $path = __DS__ . $path;
  195. $realFile = $this->mkdir($path) . __DS__ . $this->getFileName();
  196. $this->completed = false;
  197. $this->getExportId();
  198. $this->getUserId();
  199. $fileNameUpdated = false;
  200. $this->_fp = fopen($realFile, 'w');
  201. @exec('chown -R www:www /'.$realFile);
  202. @exec('chmod -R 777 /'.$realFile);
  203. // 获取列表数据及表头
  204. $this->_listModel = new $this->listModelClass();
  205. $this->_listModel->isExport = true;
  206. if(method_exists($this->_listModel, 'getExportHeaders')){
  207. if(method_exists($this->_listModel, 'exportPrepare')) {//导出数据提前设置参数
  208. $this->_listModel->exportPrepare(['condition' => $this->params['condition'], 'params' => $this->params['params'], 'others' => isset($this->params['others']) ? $this->params['others'] : [], 'page' => 0, 'pageSize' => $this->pageSize, 'userId' => $this->userId]);
  209. }
  210. $headers = $this->_listModel->getExportHeaders($this->userId);
  211. fputcsv($this->_fp, $headers);
  212. unset($headers);
  213. $this->_updateFirst($realFile, 1);
  214. } else {
  215. throw new Exception($this->listModelClass.'的getExportHeaders方法不存在');
  216. }
  217. $this->_loopWriteData($realFile, $fileNameUpdated);
  218. $this->complete();
  219. return true;
  220. }
  221. /**
  222. * 循环写入数据
  223. * @param $realFile
  224. * @param $fileNameUpdated
  225. * @param int $page
  226. * @param int $counter
  227. * @return string
  228. * @throws Exception
  229. */
  230. private function _loopWriteData($realFile, &$fileNameUpdated, $page = 0, $counter = 0){
  231. if(method_exists($this->_listModel, 'getList')){
  232. $list = $this->_listModel->getList(['condition'=>$this->params['condition'], 'params'=>isset($this->params['params']) ? $this->params['params'] : [], 'others'=>isset($this->params['others']) ? $this->params['others'] : [], 'page'=>$page, 'pageSize'=>$this->pageSize, 'userId'=>$this->userId]);
  233. } else {
  234. throw new Exception($this->listModelClass.'的getList方法不存在');
  235. }
  236. if($page >= $list['totalPages']){
  237. return 'finish';
  238. }
  239. if(!empty($list['list'])){
  240. foreach($list['list'] as $columnData){
  241. fputcsv($this->_fp, Tool::arrTextConvert($columnData));
  242. $counter++;
  243. if ($list['totalCount'] < $counter) {
  244. // $counter = $list['totalCount'];
  245. return 'finish';
  246. }
  247. $percent = intval(($counter / $list['totalCount']) * 100);
  248. $this->_updatePercent($percent);
  249. // if (!$fileNameUpdated) {
  250. // $this->_updateFirst($realFile, $percent);
  251. // } else {
  252. // $this->_updatePercent($percent);
  253. // }
  254. // $fileNameUpdated = true;
  255. unset($percent, $columnData);
  256. }
  257. $page++;
  258. unset($list);
  259. return $this->_loopWriteData($realFile, $fileNameUpdated, $page, $counter);
  260. }
  261. unset($list);
  262. return 'finish';
  263. }
  264. /**
  265. * 完成
  266. * @return bool
  267. * @throws Exception
  268. * @throws \yii\base\InvalidConfigException
  269. * @throws \yii\httpclient\Exception
  270. */
  271. public function complete() {
  272. $this->_updatePercent(100);
  273. if ($this->completed) {
  274. return true;
  275. }
  276. if ($this->_fp) {
  277. fclose($this->_fp);
  278. }
  279. // 把导出的文件上传至静态文件服务器
  280. if ($this->isRemote) {
  281. $realFile = $this->getSaveBasePath() . __DS__ . $this->getSavePath() . __DS__ . $this->getFileName();
  282. $remoteUploadApi = RemoteUploadApi::instance();
  283. if ($uploadResult = $remoteUploadApi->upload($realFile)) {
  284. Export::updateAll(['REMOTE_URL' => $uploadResult['url'], 'IS_EXPORTING' => 0, 'EXPORT_PERCENT' => 100, 'ENDED_AT' => Date::nowTime()], 'ID=:ID', [':ID' => $this->exportId]);
  285. } else {
  286. $this->_errorHandle();
  287. throw new Exception('文件远程上传失败');
  288. }
  289. // 删除本地临时文件
  290. unlink($realFile);
  291. } else {
  292. Export::updateAll(['IS_EXPORTING' => 0, 'EXPORT_PERCENT' => 100, 'ENDED_AT' => Date::nowTime()], 'ID=:ID', [':ID' => $this->exportId]);
  293. }
  294. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin(100, ['MODEL' => 'EXPORT', 'ID' => $this->exportId, 'FIELD' => 'EXPORT_PERCENT']);
  295. CacheHelper::deleteAsyncParams($this->taskId);
  296. $this->completed = true;
  297. }
  298. /**
  299. * @param $message
  300. */
  301. private function _errorNotice($message) {
  302. Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($this->userId, $message, false);
  303. }
  304. /**
  305. * 首次更新
  306. * @param $exportId
  307. * @param $realName
  308. */
  309. private function _updateFirst($realName, $percent) {
  310. $fileName = str_replace($this->getSaveBasePath() . __DS__, '', $realName);
  311. Export::updateAll([
  312. 'FILE_NAME' => str_replace(__DS__, '/', $fileName),
  313. 'UPDATED_AT' => Date::nowTime(),
  314. 'EXPORT_PERCENT' => $percent,
  315. ], 'ID=:ID', [':ID' => $this->exportId]);
  316. }
  317. /**
  318. * 更新百分比
  319. * @param $exportId
  320. * @param $percent
  321. */
  322. private function _updatePercent($percent) {
  323. // 把数据写入数据库中
  324. Export::updateAll(['EXPORT_PERCENT' => $percent], 'ID=:ID', [':ID' => $this->exportId]);
  325. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'EXPORT', 'ID' => $this->exportId, 'FIELD' => 'EXPORT_PERCENT']);
  326. }
  327. /**
  328. * 发生错误处理
  329. * @param $exportId
  330. */
  331. private function _errorHandle() {
  332. Export::updateAll(['IS_EXPORTING' => 0], 'ID=:ID', [':ID' => $this->exportId]);
  333. }
  334. /**
  335. * 导出逻辑
  336. * @param $filter
  337. * @param $listName
  338. * @param null $consoleRouter
  339. * @throws Exception
  340. */
  341. public function exportHandle($filter, $listName, $consoleRouter = null){
  342. $params = [
  343. 'moduleId' => $this->moduleId,
  344. 'listName' => $listName,
  345. 'action' => $consoleRouter ? $consoleRouter : Yii::$app->controller->id.'/'.Yii::$app->controller->action->id, // 这里这么写,是因为调用的异步路由和同步的控制器和方法是一样的,所以,只要不传默认调和同步一样的异步方法
  346. 'userId' => Yii::$app->user->id,
  347. ];
  348. $this->webToAsync($params,$filter);
  349. }
  350. /**
  351. * 页面到异步的请求
  352. * @param $params
  353. * @param array $extend
  354. * @return bool
  355. * @throws Exception
  356. */
  357. public function webToAsync($params, $extend = []) {
  358. $default = [
  359. 'moduleId' => null,
  360. 'listName' => null,
  361. 'remark' => null,
  362. 'action' => null,
  363. 'userId' => null,
  364. ];
  365. $params = Tool::deepParse($params, $default);
  366. if (!$params['moduleId'] || !$params['listName'] || !$params['action']) {
  367. throw new Exception('请设置moduleId,listName和action');
  368. }
  369. // 把文件对应的相关资料存入数据库中
  370. $export = new Export();
  371. $export->EXPORT_NAME = $params['listName'] . '_' . date('y年m月d日H时i分s秒导出', Date::nowTime());
  372. $export->MODULE_NAME = $params['moduleId'];
  373. $export->REMARK = $params['remark'];
  374. $export->IS_EXPORTING = 1;
  375. $export->ADMIN_ID = \Yii::$app->user->id;
  376. $export->STARTED_AT = Date::nowTime();
  377. $export->CREATED_AT = Date::nowTime();
  378. $export->EXPORT_PERCENT = 0;
  379. $export->ENDED_AT = 0;
  380. $export->UPDATED_AT = 0;
  381. if (!$export->save()) {
  382. throw new Exception(Form::formatErrorsForApi($export->getErrors()));
  383. }
  384. $extend = array_merge($extend, [
  385. 'exportId' => $export->ID,
  386. 'userId' => $params['userId'],
  387. ]);
  388. // 异步处理添加任务
  389. $taskKey = \Yii::$app->swooleAsyncTimer->asyncHandle($params['action'], $extend);
  390. if($taskKey === false){
  391. // 删除刚刚添加的导出数据
  392. Export::deleteAll(['ID'=>$export->ID]);
  393. throw new Exception('导出失败,请求异步处理服务器失败');
  394. }
  395. return $taskKey;
  396. }
  397. }