WeworksingleGetExternalUser.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. class WeworksingleGetExternalUser extends Command
  13. {
  14. protected function configure()
  15. {
  16. $this->setName('weworksingle_get_external_user')
  17. ->setDescription('weworksingle get external user');
  18. }
  19. protected function execute(Input $input, Output $output)
  20. {
  21. $list = WeworksingleCompanySetting::where('corp_id', '<>', '')->select();
  22. foreach ($list as $k => $v) {
  23. $chat_config['corp_id'] = $v['corp_id'];
  24. $chat_config['secret'] = $v['customer_secret'];
  25. $chat_config['token'] = $v['customer_token'];
  26. $chat_config['aes_key'] = $v['customer_aes_key'];
  27. $chat_config['company_id'] = $v['company_id'];
  28. if (!empty($v['customer_secret']) && !empty($v['customer_token']) && !empty($v['customer_aes_key']) && !empty($v['company_id'])) {
  29. $root_id = Company::where('id', '=', $v['company_id'])->value('root_id');
  30. $weworksingle_user = Employee::where([['root_id', '=', $root_id], ['state', '=', '在职'], ['weworksingle_uid', '<>', 0]])->column('weworksingle_uid');
  31. $uid = [];
  32. if (!empty($weworksingle_user)){
  33. $weworksingle_uid = WeworksingleUser::where('id', 'in', $weworksingle_user)->column('userid');
  34. foreach ($weworksingle_uid as $kk => $vv){
  35. $start_time = strtotime(date('Y-m-d',strtotime('-1 day')));
  36. $behavior = (new apiSingle($chat_config))->getUserBehaviorData([$vv], $partyid = [], $start_time, $start_time);
  37. $res = WeworksingleExternalUserRecord::where(['stat_time'=>$start_time,'userid'=>$vv,'root_id'=>$root_id])->count();
  38. if(!$res){
  39. $data = [
  40. 'userid' => $vv,
  41. 'stat_time' => $behavior['stat_time'],
  42. 'new_apply_cnt' => $behavior['new_apply_cnt'],
  43. 'new_contact_cnt' => $behavior['new_contact_cnt'],
  44. 'chat_cnt' => $behavior['chat_cnt'],
  45. 'message_cnt' => $behavior['message_cnt'],
  46. 'reply_percentage' => isset($behavior['reply_percentage']) ? $behavior['reply_percentage'] : 0,
  47. 'avg_reply_time' => isset($behavior['avg_reply_time']) ? $behavior['avg_reply_time'] : 0,
  48. 'negative_feedback_cnt' => $behavior['negative_feedback_cnt'],
  49. 'root_id' => $root_id,
  50. ];
  51. WeworksingleExternalUserRecord::create($data);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. // 指令输出
  58. $output->writeln(date('Y-m-d H:i:s', time()) . '--weworksingle_get_external_user--ok');
  59. }
  60. }