WeworksingleGetGroupStatistic.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace app\command;
  3. use app\model\Company;
  4. use app\model\Employee;
  5. use app\model\WeworksingleCompanySetting;
  6. use app\model\WeworksingleExternalUserRecord;
  7. use app\model\WeworksingleUser;
  8. use think\console\Command;
  9. use think\console\Input;
  10. use think\console\Output;
  11. use weworkapi\api\apiSingle;
  12. /**
  13. * 定时拉取群聊统计数据
  14. * 拉取时间为每天晚上12点之前
  15. * Class WeworksingleGetGroupStatistic
  16. * @package app\command
  17. */
  18. class WeworksingleGetGroupStatistic extends Command
  19. {
  20. protected function configure()
  21. {
  22. $this->setName('weworksingle_get_group_statistic')
  23. ->setDescription('weworksingle get group statistic');
  24. }
  25. protected function execute(Input $input, Output $output)
  26. {
  27. $list = WeworksingleCompanySetting::where('corp_id', '<>', '')->select();
  28. foreach ($list as $k => $v) {
  29. $save_data = [];
  30. $chat_config['corp_id'] = $v['corp_id'];
  31. $chat_config['secret'] = $v['customer_secret'];
  32. $chat_config['token'] = $v['customer_token'];
  33. $chat_config['aes_key'] = $v['customer_aes_key'];
  34. $chat_config['company_id'] = $v['company_id'];
  35. if (!empty($v['customer_secret']) && !empty($v['customer_token']) && !empty($v['customer_aes_key']) && !empty($v['company_id'])) {
  36. $root_id = Company::where('id', '=', $v['company_id'])->value('root_id');
  37. $weworksingle_user = Employee::where([['root_id', '=', $root_id], ['state', '=', '在职'], ['weworksingle_uid', '<>', 0]])->column('weworksingle_uid');
  38. if (!empty($weworksingle_user)){
  39. $weworksingle_uid = WeworksingleUser::where('id', 'in', $weworksingle_user)->column('userid');
  40. foreach ($weworksingle_uid as $kk => $vv){
  41. $param = [
  42. 'day_begin_time'=> strtotime(date('Y-m-d', time())),
  43. 'day_end_time'=> strtotime(date('Y-m-d', time())),
  44. 'owner_filter'=> [
  45. 'userid_list'=> $vv
  46. ],
  47. 'offset'=> 0,
  48. 'limit'=> 1000
  49. ];
  50. $data = (new apiSingle($chat_config))->getGroupChatStatistic($param);
  51. $save_data[] = [
  52. 'owner'=> $vv,
  53. 'data'=> $data,
  54. 'root_id'=> $root_id
  55. ];
  56. }
  57. }
  58. }
  59. if (!empty($save_data)){
  60. (new WeworksingleExternalUserRecord())->saveAll($save_data);
  61. }
  62. }
  63. // 指令输出
  64. $output->writeln(date('Y-m-d H:i:s', time()) . '--weworksingle_get_group_statistic--ok');
  65. }
  66. }