WorkGroupMsgSendResultServices.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\work;
  12. use app\dao\work\WorkGroupMsgSendResultDao;
  13. use app\jobs\work\WorkGroupMsgJob;
  14. use app\services\BaseServices;
  15. use crmeb\services\wechat\Work;
  16. use crmeb\traits\ServicesTrait;
  17. use think\facade\Log;
  18. /**
  19. * 企业微信群发消息客户发送详情
  20. * Class WorkGroupMsgSendResultServices
  21. * @package app\services\work
  22. * @mixin WorkGroupMsgSendResultDao
  23. */
  24. class WorkGroupMsgSendResultServices extends BaseServices
  25. {
  26. use ServicesTrait;
  27. /**
  28. * WorkGroupMsgSendResultServices constructor.
  29. * @param WorkGroupMsgSendResultDao $dao
  30. */
  31. public function __construct(WorkGroupMsgSendResultDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * @param array $where
  37. * @return array
  38. */
  39. public function getClientList(array $where)
  40. {
  41. [$page, $limit] = $this->getPageValue();
  42. $list = $this->dao->getDataList($where, ['*'], $page, $limit, 'create_time', ['client']);
  43. $count = $this->dao->count($where);
  44. return compact('list', 'count');
  45. }
  46. /**
  47. * 获取群主发送详情
  48. * @param array $where
  49. * @return array
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function getGroupChatMsgList(array $where)
  55. {
  56. [$page, $limit] = $this->getPageValue();
  57. $list = $this->dao->getGroupChatMsg($where)->whereNotNull('chat_id')->page($page, $limit)->select()->toArray();
  58. $count = $this->dao->getGroupChatMsg($where)->whereNotNull('chat_id')->count();
  59. return compact('list', 'count');
  60. }
  61. /**
  62. * 获取企业群发成员执行结果
  63. * @param int $type
  64. * @param string $userid
  65. * @param string $msgid
  66. * @param string|null $cursor
  67. * @return bool
  68. */
  69. public function getSendResult(int $type, string $userid, string $msgid, string $cursor = null)
  70. {
  71. try {
  72. $response = Work::getGroupmsgSendResult($msgid, $userid, 500, $cursor);
  73. $sendList = $response['send_list'] ?? [];
  74. foreach ($sendList as $item) {
  75. $where = ['msg_id' => $msgid, 'userid' => $userid];
  76. if ($type) {
  77. $where['chat_id'] = $item['chat_id'];
  78. } else {
  79. $where['external_userid'] = $item['external_userid'];
  80. }
  81. $info = $this->dao->get($where);
  82. if (!$info) {
  83. $this->dao->save([
  84. 'msg_id' => $msgid,
  85. 'userid' => $item['userid'],
  86. 'chat_id' => $item['chat_id'] ?? null,
  87. 'status' => $item['status'],
  88. 'send_time' => $item['send_time'] ?? 0,
  89. 'external_userid' => $item['external_userid'] ?? null,
  90. ]);
  91. } else {
  92. $info->status = $item['status'];
  93. $info->send_time = $item['send_time'] ?? 0;
  94. $info->save();
  95. }
  96. }
  97. if ($response['next_cursor']) {
  98. WorkGroupMsgJob::dispatchDo('getSendResult', [$type, $userid, $msgid, $response['next_cursor']]);
  99. }
  100. return true;
  101. } catch (\Throwable $e) {
  102. Log::error([
  103. 'message' => '获取企业群发成员执行结果失败:' . $e->getMessage(),
  104. 'file' => $e->getFile(),
  105. 'line' => $e->getLine(),
  106. ]);
  107. return false;
  108. }
  109. }
  110. }