WorkGroupMsgTaskServices.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\services\work;
  3. use app\dao\work\WorkGroupMsgTaskDao;
  4. use app\jobs\work\WorkGroupMsgJob;
  5. use app\services\BaseServices;
  6. use crmeb\services\wechat\Work;
  7. use crmeb\traits\ServicesTrait;
  8. use think\facade\Log;
  9. /**
  10. * 企业微信群发消息任务
  11. * Class WorkGroupMsgTaskServices
  12. * @package app\services\work
  13. * @mixin WorkGroupMsgTaskDao
  14. */
  15. class WorkGroupMsgTaskServices extends BaseServices
  16. {
  17. use ServicesTrait;
  18. /**
  19. * WorkGroupmsgTaskServices constructor.
  20. * @param WorkGroupMsgTaskDao $dao
  21. */
  22. public function __construct(WorkGroupMsgTaskDao $dao)
  23. {
  24. $this->dao = $dao;
  25. }
  26. /**
  27. * 获取成员列表
  28. * @param array $where
  29. * @return array
  30. */
  31. public function getTaksList(array $where)
  32. {
  33. [$page, $limit] = $this->getPageValue();
  34. $list = $this->dao->getDataList($where, ['*'], $page, $limit, 'send_time', [
  35. 'member', 'sendResult' => function ($query) use ($where) {
  36. $query->whereIn('msg_id', $where['msg_id'])->field(['userid', 'count(*) as num_count'])->group('userid');
  37. }
  38. ]);
  39. $count = $this->dao->count($where);
  40. return compact('list', 'count');
  41. }
  42. /**
  43. * 获取群发成员发送任务列表
  44. * @param int $type
  45. * @param string $msgid
  46. * @param string|null $cursor
  47. * @return bool
  48. */
  49. public function getTaks(int $type, string $msgid, string $cursor = null)
  50. {
  51. try {
  52. $response = Work::getGroupmsgTask($msgid, 500, $cursor);
  53. $taskList = $response['task_list'] ?? [];
  54. foreach ($taskList as $item) {
  55. $info = $this->dao->get(['msg_id' => $msgid, 'userid' => $item['userid']]);
  56. if (!$info) {
  57. $info = $this->dao->save([
  58. 'msg_id' => $msgid,
  59. 'userid' => $item['userid'],
  60. 'status' => $item['status'],
  61. 'create_time' => time(),
  62. 'send_time' => $item['send_time'] ?? 0
  63. ]);
  64. } else {
  65. $info->status = $item['status'];
  66. $info->send_time = $item['send_time'] ?? 0;
  67. $info->save();
  68. }
  69. WorkGroupMsgJob::dispatchDo('getSendResult', [$type, $item['userid'], $msgid, null]);
  70. }
  71. if ($response['next_cursor']) {
  72. WorkGroupMsgJob::dispatchDo('getTaks', [$type, $msgid, $cursor]);
  73. }
  74. return true;
  75. } catch (\Throwable $e) {
  76. Log::error([
  77. 'message' => '获取群发成员发送任务列表失败:' . $e->getMessage(),
  78. 'file' => $e->getFile(),
  79. 'line' => $e->getLine(),
  80. ]);
  81. return false;
  82. }
  83. }
  84. /**
  85. * 统计群发模板数据
  86. * @param array $msgIds
  87. * @return array
  88. */
  89. public function getSendMsgStatistics(array $msgIds, int $type = 0)
  90. {
  91. $data = [];
  92. $data['user_count'] = $data['unuser_count'] = $data['external_user_count'] = $data['external_unuser_count'] = 0;
  93. $data['user_count'] = $this->dao->count(['msg_id' => $msgIds, 'status' => 2]);
  94. $data['unuser_count'] = $this->dao->count(['msg_id' => $msgIds, 'status' => 0]);
  95. /** @var WorkGroupMsgSendResultServices $service */
  96. $service = app()->make(WorkGroupMsgSendResultServices::class);
  97. if ($type) {
  98. $data['external_user_count'] = $service->count(['msg_id' => $msgIds, 'notChatId' => true, 'status' => 1]);
  99. $data['external_unuser_count'] = $service->count(['msg_id' => $msgIds, 'notChatId' => true, 'status' => [0, 2, 3]]);
  100. } else {
  101. $data['external_user_count'] = $service->count(['msg_id' => $msgIds, 'status' => 1]);
  102. $data['external_unuser_count'] = $service->count(['msg_id' => $msgIds, 'status' => [0, 2, 3]]);
  103. }
  104. return $data;
  105. }
  106. }