BaseExport.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 rtrim($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 rtrim($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. $realFile = $this->mkdir($path) . __DS__ . $this->getFileName();
  195. $this->completed = false;
  196. $this->getExportId();
  197. $this->getUserId();
  198. $fileNameUpdated = false;
  199. $this->_fp = fopen($realFile, 'w');
  200. @exec('chown -R www:www /'.$realFile);
  201. @exec('chmod -R 777 /'.$realFile);
  202. // 获取列表数据及表头
  203. $this->_listModel = new $this->listModelClass();
  204. $this->_listModel->isExport = true;
  205. if(method_exists($this->_listModel, 'getExportHeaders')){
  206. if(method_exists($this->_listModel, 'exportPrepare')) {//导出数据提前设置参数
  207. $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]);
  208. }
  209. $headers = $this->_listModel->getExportHeaders($this->userId);
  210. fputcsv($this->_fp, $headers);
  211. unset($headers);
  212. $this->_updateFirst($realFile, 1);
  213. } else {
  214. throw new Exception($this->listModelClass.'的getExportHeaders方法不存在');
  215. }
  216. $this->_loopWriteData($realFile, $fileNameUpdated);
  217. $this->complete();
  218. return true;
  219. }
  220. /**
  221. * 循环写入数据
  222. * @param $realFile
  223. * @param $fileNameUpdated
  224. * @param int $page
  225. * @param int $counter
  226. * @return string
  227. * @throws Exception
  228. */
  229. private function _loopWriteData($realFile, &$fileNameUpdated, $page = 0, $counter = 0){
  230. if(method_exists($this->_listModel, 'getList')){
  231. $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]);
  232. } else {
  233. throw new Exception($this->listModelClass.'的getList方法不存在');
  234. }
  235. if($page >= $list['totalPages']){
  236. return 'finish';
  237. }
  238. if(!empty($list['list'])){
  239. foreach($list['list'] as $columnData){
  240. fputcsv($this->_fp, Tool::arrTextConvert($columnData));
  241. $counter++;
  242. if ($list['totalCount'] < $counter) {
  243. // $counter = $list['totalCount'];
  244. return 'finish';
  245. }
  246. $percent = intval(($counter / $list['totalCount']) * 100);
  247. $this->_updatePercent($percent);
  248. // if (!$fileNameUpdated) {
  249. // $this->_updateFirst($realFile, $percent);
  250. // } else {
  251. // $this->_updatePercent($percent);
  252. // }
  253. // $fileNameUpdated = true;
  254. unset($percent, $columnData);
  255. }
  256. $page++;
  257. unset($list);
  258. return $this->_loopWriteData($realFile, $fileNameUpdated, $page, $counter);
  259. }
  260. unset($list);
  261. return 'finish';
  262. }
  263. /**
  264. * 完成
  265. * @return bool
  266. * @throws Exception
  267. * @throws \yii\base\InvalidConfigException
  268. * @throws \yii\httpclient\Exception
  269. */
  270. public function complete() {
  271. $this->_updatePercent(100);
  272. if ($this->completed) {
  273. return true;
  274. }
  275. if ($this->_fp) {
  276. fclose($this->_fp);
  277. }
  278. // 把导出的文件上传至静态文件服务器
  279. if ($this->isRemote) {
  280. $realFile = $this->getSaveBasePath() . __DS__ . $this->getSavePath() . __DS__ . $this->getFileName();
  281. $remoteUploadApi = RemoteUploadApi::instance();
  282. if ($uploadResult = $remoteUploadApi->upload($realFile)) {
  283. Export::updateAll(['REMOTE_URL' => $uploadResult['url'], 'IS_EXPORTING' => 0, 'EXPORT_PERCENT' => 100, 'ENDED_AT' => Date::nowTime()], 'ID=:ID', [':ID' => $this->exportId]);
  284. } else {
  285. $this->_errorHandle();
  286. throw new Exception('文件远程上传失败');
  287. }
  288. // 删除本地临时文件
  289. unlink($realFile);
  290. } else {
  291. Export::updateAll(['IS_EXPORTING' => 0, 'EXPORT_PERCENT' => 100, 'ENDED_AT' => Date::nowTime()], 'ID=:ID', [':ID' => $this->exportId]);
  292. }
  293. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin(100, ['MODEL' => 'EXPORT', 'ID' => $this->exportId, 'FIELD' => 'EXPORT_PERCENT']);
  294. CacheHelper::deleteAsyncParams($this->taskId);
  295. $this->completed = true;
  296. }
  297. /**
  298. * @param $message
  299. */
  300. private function _errorNotice($message) {
  301. Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($this->userId, $message, false);
  302. }
  303. /**
  304. * 首次更新
  305. * @param $exportId
  306. * @param $realName
  307. */
  308. private function _updateFirst($realName, $percent) {
  309. $fileName = str_replace($this->getSaveBasePath() . __DS__, '', $realName);
  310. Export::updateAll([
  311. 'FILE_NAME' => str_replace(__DS__, '/', $fileName),
  312. 'UPDATED_AT' => Date::nowTime(),
  313. 'EXPORT_PERCENT' => $percent,
  314. ], 'ID=:ID', [':ID' => $this->exportId]);
  315. }
  316. /**
  317. * 更新百分比
  318. * @param $exportId
  319. * @param $percent
  320. */
  321. private function _updatePercent($percent) {
  322. // 把数据写入数据库中
  323. Export::updateAll(['EXPORT_PERCENT' => $percent], 'ID=:ID', [':ID' => $this->exportId]);
  324. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'EXPORT', 'ID' => $this->exportId, 'FIELD' => 'EXPORT_PERCENT']);
  325. }
  326. /**
  327. * 发生错误处理
  328. * @param $exportId
  329. */
  330. private function _errorHandle() {
  331. Export::updateAll(['IS_EXPORTING' => 0], 'ID=:ID', [':ID' => $this->exportId]);
  332. }
  333. /**
  334. * 导出逻辑
  335. * @param $filter
  336. * @param $listName
  337. * @param null $consoleRouter
  338. * @throws Exception
  339. */
  340. public function exportHandle($filter, $listName, $consoleRouter = null){
  341. $params = [
  342. 'moduleId' => $this->moduleId,
  343. 'listName' => $listName,
  344. 'action' => $consoleRouter ? $consoleRouter : Yii::$app->controller->id.'/'.Yii::$app->controller->action->id, // 这里这么写,是因为调用的异步路由和同步的控制器和方法是一样的,所以,只要不传默认调和同步一样的异步方法
  345. 'userId' => Yii::$app->user->id,
  346. ];
  347. $this->webToAsync($params,$filter);
  348. }
  349. /**
  350. * 页面到异步的请求
  351. * @param $params
  352. * @param array $extend
  353. * @return bool
  354. * @throws Exception
  355. */
  356. public function webToAsync($params, $extend = []) {
  357. $default = [
  358. 'moduleId' => null,
  359. 'listName' => null,
  360. 'remark' => null,
  361. 'action' => null,
  362. 'userId' => null,
  363. ];
  364. $params = Tool::deepParse($params, $default);
  365. if (!$params['moduleId'] || !$params['listName'] || !$params['action']) {
  366. throw new Exception('请设置moduleId,listName和action');
  367. }
  368. // 把文件对应的相关资料存入数据库中
  369. $export = new Export();
  370. $export->EXPORT_NAME = $params['listName'] . '_' . date('y年m月d日H时i分s秒导出', Date::nowTime());
  371. $export->MODULE_NAME = $params['moduleId'];
  372. $export->REMARK = $params['remark'];
  373. $export->IS_EXPORTING = 1;
  374. $export->ADMIN_ID = \Yii::$app->user->id;
  375. $export->STARTED_AT = Date::nowTime();
  376. $export->CREATED_AT = Date::nowTime();
  377. $export->EXPORT_PERCENT = 0;
  378. $export->ENDED_AT = 0;
  379. $export->UPDATED_AT = 0;
  380. if (!$export->save()) {
  381. throw new Exception(Form::formatErrorsForApi($export->getErrors()));
  382. }
  383. $extend = array_merge($extend, [
  384. 'exportId' => $export->ID,
  385. 'userId' => $params['userId'],
  386. ]);
  387. // 异步处理添加任务
  388. $taskKey = \Yii::$app->swooleAsyncTimer->asyncHandle($params['action'], $extend);
  389. if($taskKey === false){
  390. // 删除刚刚添加的导出数据
  391. Export::deleteAll(['ID'=>$export->ID]);
  392. throw new Exception('导出失败,请求异步处理服务器失败');
  393. }
  394. return $taskKey;
  395. }
  396. }