MakeReportGo.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\common\library;
  3. use think\Env;
  4. /**
  5. * 报表生成服务类 go
  6. * Class MakeReport
  7. */
  8. class MakeReportGo
  9. {
  10. private $_redis = null;
  11. public function __construct()
  12. {
  13. if (extension_loaded('redis')) {
  14. $this->_redis = new \Redis();
  15. $this->_redis->connect(Env::get('redis.host', '127.0.0.1'), Env::get('redis.port', 6379));
  16. if (Env::get('redis.password', '')) {
  17. $this->_redis->auth(Env::get('redis.password', ''));
  18. }
  19. $this->_redis->select(Env::get('redis.seelect_go', 0));
  20. } else {
  21. exit('请开启redis扩展');
  22. }
  23. }
  24. /**
  25. * 添加报表生成任务(go)
  26. * @param $action string 和go协商好的名称用来识别哪个报表
  27. * @param $sql
  28. * @param $uid int 用户id
  29. * @param $extend array 扩展参数
  30. * @param $count int sql查询结果总条数
  31. * @param int $expire 相同任务多久内不重复生成
  32. *
  33. * @return boolean true:提交报表任务成功 ;false:提交报表失败
  34. */
  35. public function addTask($action, $sql, $uid, $extend = ['IsShow' => false], $count = 0, $expire = 3600)
  36. {
  37. $task_data = array(
  38. 'Action' => $action,
  39. 'Sql' => $sql,
  40. 'Uid' => $uid,
  41. 'Time' => request()->time(),
  42. 'Extend' => $extend,
  43. 'Count' => $count
  44. );
  45. $task_unique_id = strtolower(md5(sprintf(
  46. '%s|%s|%s|%s',
  47. $task_data['Uid'],
  48. $task_data['Action'],
  49. $task_data['Sql'],
  50. json_encode($task_data['Extend'])
  51. )));
  52. $task_unique_id = config('MAKE_REPORT_UNIQUE_KEY_PRE') . $task_unique_id;
  53. // $has_task = $this->_redis->get($task_unique_id);
  54. // 防止重复生成报表
  55. // if (empty($has_task)) {
  56. // MAKE_REPORT_KEY = ayam:task:make-report
  57. $a = $this->_redis->zAdd(config('MAKE_REPORT_KEY'), time(), json_encode($task_data));
  58. // $this->_redis->setex($task_unique_id, $expire, 1);
  59. return true;
  60. // } else {
  61. // return false;
  62. // }
  63. }
  64. // 福利
  65. public function welfareTask($action, $id, $count = 0, $expire = 3600, $bool = false)
  66. {
  67. $task_data = array(
  68. 'Action' => $action,
  69. 'Id' => $id,
  70. 'Time' => request()->time(),
  71. 'Count' => $count
  72. );
  73. $task_unique_id = strtolower(md5(sprintf(
  74. '%s|%s',
  75. $task_data['Action'],
  76. $task_data['Id']
  77. )));
  78. $task_unique_id = config('welfare_task') . $task_unique_id;
  79. $has_task = $this->_redis->get($task_unique_id);
  80. log_message('$has_task:'.$has_task.' Id:'.$task_data['Id'],'info',LOG_PATH .'welfareGrant/');
  81. // 防止重复生成
  82. if (empty($has_task)) {
  83. $tmp = $this->_redis->zAdd(config('welfare_task') . 'list', time(), json_encode($task_data));
  84. log_message(config('welfare_task') . 'list'.'__'.time().'__'.json_encode($task_data).' tmp:'.$tmp,'info',LOG_PATH .'welfareGrant/');
  85. $this->_redis->setex($task_unique_id, $expire, 1);
  86. $this->_redis->setex(config('welfare_task').':test:'.$id, $expire, 1); //调试
  87. return true;
  88. } else {
  89. if ($bool) {
  90. $this->_redis->zAdd(config('welfare_task') . 'list', time(), json_encode($task_data));
  91. }
  92. return false;
  93. }
  94. }
  95. }