WorkWelcomeServices.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\WorkWelcomeDao;
  13. use app\services\BaseServices;
  14. use crmeb\traits\service\ContactWayQrCode;
  15. use crmeb\traits\ServicesTrait;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use think\exception\ValidateException;
  20. /**
  21. * 企业微信欢迎语
  22. * Class WorkWelcomeServices
  23. * @package app\services\\work
  24. * @mixin WorkWelcomeDao
  25. */
  26. class WorkWelcomeServices extends BaseServices
  27. {
  28. use ServicesTrait, ContactWayQrCode;
  29. /**
  30. * WorkWelcomeServices constructor.
  31. * @param WorkWelcomeDao $dao
  32. */
  33. public function __construct(WorkWelcomeDao $dao)
  34. {
  35. $this->dao = $dao;
  36. }
  37. /**
  38. * 获取列表
  39. * @param array $where
  40. * @return array
  41. * @throws DataNotFoundException
  42. * @throws DbException
  43. * @throws ModelNotFoundException
  44. */
  45. public function getList(array $where)
  46. {
  47. [$page, $limit] = $this->getPageValue();
  48. $list = $this->dao->getDataList($where, ['*'], $page, $limit, ['sort', 'create_time'], [
  49. 'userList' => function ($query) {
  50. $query->with([
  51. 'member' => function ($query) {
  52. $query->field(['userid', 'name']);
  53. }
  54. ]);
  55. }
  56. ]);
  57. $count = $this->dao->count($where);
  58. return compact('list', 'count');
  59. }
  60. /**
  61. * 获取欢迎语
  62. * @param int $id
  63. * @return array
  64. * @throws DataNotFoundException
  65. * @throws DbException
  66. * @throws ModelNotFoundException
  67. */
  68. public function getWelcomeInfo(int $id)
  69. {
  70. $welcomeInfo = $this->dao->get($id, ['*'], [
  71. 'userList' => function ($query) {
  72. $query->with([
  73. 'member' => function ($query) {
  74. $query->field(['userid', 'name']);
  75. }
  76. ]);
  77. }
  78. ]);
  79. if (!$welcomeInfo) {
  80. throw new ValidateException('没有查到欢迎语');
  81. }
  82. return $welcomeInfo->toArray();
  83. }
  84. /**
  85. * 保存欢迎语
  86. * @param array $data
  87. * @param int $id
  88. * @return bool
  89. */
  90. public function saveWelcome(array $data, int $id = 0)
  91. {
  92. $userids = $data['userids'];
  93. unset($data['userids']);
  94. $this->checkWelcome(['text' => ['content' => $data['content']], 'attachments' => $data['attachments']], 0);
  95. /** @var WorkWelcomeRelationServices $relationServices */
  96. $relationServices = app()->make(WorkWelcomeRelationServices::class);
  97. $this->transaction(function () use ($data, $id, $relationServices, $userids) {
  98. if ($id) {
  99. $this->dao->update($id, $data);
  100. } else {
  101. $res = $this->dao->save($data);
  102. $id = $res->id;
  103. }
  104. $userData = [];
  105. foreach ($userids as $userid) {
  106. $userData[] = [
  107. 'welcome_id' => $id,
  108. 'userid' => $userid
  109. ];
  110. }
  111. $relationServices->delete(['welcome_id' => $id]);
  112. if ($userData) {
  113. $relationServices->saveAll($userData);
  114. }
  115. });
  116. return true;
  117. }
  118. /**
  119. * 获取欢迎语
  120. * @param string $userId
  121. * @return array
  122. * @throws DataNotFoundException
  123. * @throws DbException
  124. * @throws ModelNotFoundException
  125. */
  126. public function getWorkWelcome(string $userId)
  127. {
  128. if ($userId) {
  129. /** @var WorkWelcomeRelationServices $service */
  130. $service = app()->make(WorkWelcomeRelationServices::class);
  131. $welcomeIds = $service->getColumn(['userid' => $userId], 'welcome_id');
  132. if ($welcomeIds) {
  133. $welcomeList = $this->dao->getDataList(['id' => $welcomeIds], ['*'], 1, 1, ['sort' => 'desc', 'create_time' => 'desc']);
  134. $welcomeWords = $welcomeList[0] ?? [];
  135. }
  136. }
  137. if (empty($welcomeWords)) {
  138. $welcomeList = $this->dao->getDataList(['type' => 0], ['*'], 1, 1, ['sort' => 'desc', 'create_time' => 'desc']);
  139. $welcomeWords = $welcomeList[0] ?? [];
  140. }
  141. return [
  142. 'text' => [
  143. 'content' => $welcomeWords['content'] ?? '',
  144. ],
  145. 'attachments' => $welcomeWords['attachments'] ?? [],
  146. ];
  147. }
  148. /**
  149. * 删除欢迎语
  150. * @param int $id
  151. * @return bool
  152. */
  153. public function deleteWelcome(int $id)
  154. {
  155. /** @var WorkWelcomeRelationServices $relationServices */
  156. $relationServices = app()->make(WorkWelcomeRelationServices::class);
  157. $this->transaction(function () use ($id, $relationServices) {
  158. $relationServices->delete(['welcome_id' => $id]);
  159. $this->dao->delete($id);
  160. });
  161. return true;
  162. }
  163. }