Queue.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller;
  15. use library\Controller;
  16. use library\service\ProcessService;
  17. use think\Console;
  18. use think\Db;
  19. use think\exception\HttpResponseException;
  20. use app\service\queue\ImportTable1Queue;
  21. use app\service\queue\ImportTable2Queue;
  22. /**
  23. * 上传管理
  24. * Class Queue
  25. * @package app\admin\controller
  26. */
  27. class Queue extends Controller
  28. {
  29. /**
  30. * 绑定数据表
  31. * @var string
  32. */
  33. protected $table = 'system_queue';
  34. /**
  35. * 上传记录
  36. * @auth true
  37. * @menu true
  38. * @throws \think\Exception
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. * @throws \think\exception\DbException
  42. * @throws \think\exception\PDOException
  43. */
  44. public function index()
  45. {
  46. if (session('user.username') === 'admin') try {
  47. $this->message = Console::call('xtask:state')->fetch();
  48. $this->command = ProcessService::instance()->think('xtask:start');
  49. $this->listen = preg_match('/process.*?\d+.*?running/', $this->message, $attr);
  50. // $listen = ProcessService::instance()->query('xtask:_work 71 -');
  51. // dump($listen);
  52. } catch (\Exception $exception) {
  53. $this->listen = false;
  54. $this->message = $exception->getMessage();
  55. }
  56. $this->title = '上传记录';
  57. sysoplog('上传记录', '访问上传记录页面');
  58. $this->iswin = ProcessService::instance()->iswin();
  59. $query = $this->_query($this->table)->dateBetween('create_at,start_at,end_at,date');
  60. $query->like('title,preload')->equal('status,type,is_undo')->order('id desc')->page();
  61. }
  62. /**
  63. * 数据列表处理
  64. * @auth true
  65. * @menu true
  66. * @param array $data
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. * @throws \think\exception\DbException
  70. */
  71. protected function _index_page_filter(&$data)
  72. {
  73. foreach ($data as &$v){
  74. $v['listen'] = 0;
  75. if ($v['status']==2){
  76. $listen = ProcessService::instance()->query('xtask:_work '.$v['id'].' -');
  77. $v['listen'] = count($listen);
  78. }
  79. }
  80. }
  81. /**
  82. * 导入表一数据
  83. * @auth true
  84. * @menu true
  85. * @throws \think\Exception
  86. * @throws \think\exception\PDOException
  87. */
  88. public function import_table1()
  89. {
  90. // $query = "SHOW FULL COLUMNS FROM system_courier";
  91. // $table = Db::query($query); //获取表所有字段
  92. // $field = array_column($table,'Field'); //提取表字段,放到一维数组中
  93. $username = $this->app->session->get('user')['username'];
  94. $get = $this->request->get();
  95. $import_path = $_SERVER['DOCUMENT_ROOT'].'/'.$get['path'];
  96. $url = $get['url'];
  97. $ordfilename = $get['ordfilename'];
  98. if (getConfigValue('storage_type')=='oss'){
  99. $save_dir = "./upload/excle/"; // 服务资源目录
  100. $filename = date('Ymd').time().".xlsx"; // 自定义名称
  101. $res = getFile($url,$save_dir,$filename,1);
  102. $import_path = $_SERVER['DOCUMENT_ROOT'].'/upload/excle/'.$res;
  103. }
  104. try {
  105. $result = sysqueue($ordfilename, ImportTable1Queue::URI, 0, ['type' => 1,'local_url'=>$import_path,'import_name'=>$username], 0);
  106. if ($result['status']){
  107. sysoplog('上传记录', '上传表一数据成功');
  108. $this->success($result['info']);
  109. }else{
  110. sysoplog('上传记录', '上传表一数据失败');
  111. $this->error($result['info'],[],3);
  112. }
  113. } catch (\think\exception\ValidateException $e) {
  114. $this->error($e->getMessage());
  115. }
  116. }
  117. /**
  118. * 导入表二数据
  119. * @auth true
  120. * @menu true
  121. * @throws \think\Exception
  122. * @throws \think\exception\PDOException
  123. */
  124. public function import_table2()
  125. {
  126. set_time_limit(0);
  127. $username = $this->app->session->get('user')['username'];
  128. $get = $this->request->get();
  129. $import_path = $_SERVER['DOCUMENT_ROOT'].'/'.$get['path'];
  130. $url = $get['url'];
  131. $ordfilename = $get['ordfilename'];
  132. if (getConfigValue('storage_type')=='oss'){
  133. $save_dir = "./upload/excle/"; // 服务资源目录
  134. $filename = date('Ymd').time().".xlsx"; // 自定义名称
  135. $res = getFile($url,$save_dir,$filename,1);
  136. $import_path = $_SERVER['DOCUMENT_ROOT'].'/upload/excle/'.$res;
  137. }
  138. try {
  139. $result = sysqueue($ordfilename, ImportTable2Queue::URI, 0, ['type' => 2,'local_url'=>$import_path,'import_name'=>$username], 0);
  140. if ($result['status']){
  141. sysoplog('上传记录', '上传表二数据成功');
  142. $this->success($result['info']);
  143. }else{
  144. sysoplog('上传记录', '上传表二数据失败');
  145. $this->error($result['info'],[],3);
  146. }
  147. } catch (\think\exception\ValidateException $e) {
  148. $this->error($e->getMessage());
  149. }
  150. }
  151. /**
  152. * 重置任务
  153. * @auth true
  154. * @menu true
  155. * @throws \think\Exception
  156. * @throws \think\exception\PDOException
  157. */
  158. public function redo()
  159. {
  160. $id = $this->request->get('id');
  161. Db::startTrans();
  162. try {
  163. $queue = Db::name('system_queue')->where('id',$id)->find();
  164. if ($queue['deal_count']>0){
  165. Db::name('system_queue')->where('id',$id)->update(
  166. [
  167. 'deal_count'=>0,
  168. ]
  169. );
  170. }
  171. if ($queue['type']==1){
  172. Db::name('system_values')->where('import_log_id',$id)->delete();
  173. }else{
  174. Db::name('system_table2')->where('import_log_id',$id)->delete();
  175. Db::name('system_values2')->where('import_log_id',$id)->delete();
  176. Db::name('system_values3')->where('import_log_id',$id)->delete();
  177. }
  178. Db::commit();
  179. }catch (\think\exception\ValidateException $e) {
  180. Db::rollback();
  181. sysoplog('上传记录', '重置任务ID:'.$id.'失败');
  182. $this->error('重置任务失败');
  183. }
  184. $log = ['action'=>'上传记录','content'=>'重置任务'];
  185. $this->_save($this->table,['status' => '1'],$log);
  186. }
  187. /**
  188. * WIN开始监听任务
  189. * @auth false
  190. */
  191. public function start()
  192. {
  193. try {
  194. $message = nl2br(Console::call('xtask:start')->fetch());
  195. if (preg_match('/process.*?\d+/', $message, $attr)) {
  196. $this->success('任务监听主进程启动成功!');
  197. } else {
  198. $this->error($message);
  199. }
  200. } catch (HttpResponseException $exception) {
  201. throw $exception;
  202. } catch (\Exception $e) {
  203. $this->error($e->getMessage());
  204. }
  205. }
  206. /**
  207. * WIN停止监听任务
  208. * @auth false
  209. */
  210. public function stop()
  211. {
  212. try {
  213. $message = nl2br(Console::call('xtask:stop')->fetch());
  214. if (stripos($message, 'succeeded')) {
  215. $this->success('停止任务监听主进程成功!');
  216. } elseif (stripos($message, 'finish')) {
  217. $this->success('没有找到需要停止的进程!');
  218. } else {
  219. $this->error($message);
  220. }
  221. } catch (HttpResponseException $exception) {
  222. throw $exception;
  223. } catch (\Exception $e) {
  224. $this->error($e->getMessage());
  225. }
  226. }
  227. /**
  228. * 清理3天前的记录
  229. * @auth false
  230. * @throws \think\Exception
  231. * @throws \think\exception\PDOException
  232. */
  233. public function clear()
  234. {
  235. $map = [['time', '<', strtotime('-3days')]];
  236. $result = Db::name($this->table)->where($map)->delete();
  237. if ($result !== false) {
  238. $this->success('成功清理3天前的任务记录!');
  239. } else {
  240. $this->error('清理3天前的任务记录失败!');
  241. }
  242. }
  243. /**
  244. * 删除任务
  245. * @auth true
  246. * @menu true
  247. * @throws \think\Exception
  248. * @throws \think\exception\PDOException
  249. */
  250. public function remove()
  251. {
  252. $log = ['action'=>'上传记录'];
  253. $this->_delete($this->table,$log);
  254. }
  255. /**
  256. * 回撤
  257. * @auth true
  258. * @menu true
  259. * @throws \think\Exception
  260. * @throws \think\exception\PDOException
  261. */
  262. public function undo()
  263. {
  264. $id = $this->request->get('id');
  265. Db::startTrans();
  266. try {
  267. $queue = Db::name('system_queue')->where('id',$id)->find();
  268. $queue_result = Db::name('system_queue')->where('id',$id)->update(
  269. [
  270. 'is_undo'=>2,
  271. 'undo_at'=>date('Y-m-d H:i:s'),
  272. 'undo_name'=>$this->app->session->get('user')['username']
  273. ]
  274. );
  275. if ($queue['type']==1){
  276. $values = Db::name('system_values')->where('import_log_id',$id)->delete();
  277. if ($queue_result && $values){
  278. Db::commit();
  279. sysoplog('上传记录', '回撤ID:'.$id.'成功');
  280. $this->success('回撤成功');
  281. }else{
  282. Db::rollback();
  283. sysoplog('上传记录', '回撤ID:'.$id.'失败');
  284. $this->error('撤销失败');
  285. }
  286. }else{
  287. $table2 = Db::name('system_table2')->where('import_log_id',$id)->delete();
  288. $values2 = Db::name('system_values2')->where('import_log_id',$id)->delete();
  289. $values3 = Db::name('system_values3')->where('import_log_id',$id)->delete();
  290. if ($queue_result && $table2 && $values2 && $values3){
  291. Db::commit();
  292. sysoplog('上传记录', '回撤ID:'.$id.'成功');
  293. $this->success('回撤成功');
  294. }else{
  295. Db::rollback();
  296. sysoplog('上传记录', '回撤ID:'.$id.'失败');
  297. $this->error('撤销失败');
  298. }
  299. }
  300. }catch (\think\exception\ValidateException $e) {
  301. Db::rollback();
  302. sysoplog('上传记录', '回撤ID:'.$id.'失败');
  303. $this->error('撤销失败');
  304. }
  305. }
  306. }