WorkDepartmentServices.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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\WorkDepartmentDao;
  13. use app\jobs\work\WorkMemberJob;
  14. use app\services\BaseServices;
  15. use crmeb\services\wechat\config\WorkConfig;
  16. use crmeb\services\wechat\Work;
  17. use crmeb\traits\ServicesTrait;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. use think\exception\ValidateException;
  22. /**
  23. * 企业微信部门
  24. * Class WorkDepartmentServices
  25. * @package app\services\work
  26. * @mixin WorkDepartmentDao
  27. */
  28. class WorkDepartmentServices extends BaseServices
  29. {
  30. use ServicesTrait;
  31. /**
  32. * WorkDepartmentServices constructor.
  33. * @param WorkDepartmentDao $dao
  34. */
  35. public function __construct(WorkDepartmentDao $dao)
  36. {
  37. $this->dao = $dao;
  38. }
  39. /**
  40. * 获取组织架构
  41. * @return mixed
  42. * @throws DataNotFoundException
  43. * @throws DbException
  44. * @throws ModelNotFoundException
  45. */
  46. public function getDepartmentList()
  47. {
  48. /** @var WorkConfig $config */
  49. $config = app()->make(WorkConfig::class);
  50. $data = $this->dao->getDataList(['corp_id' => $config->get('corpId')], ['department_id', 'parentid', 'name', 'name_en']);
  51. $departmentId = array_column($data, 'department_id');
  52. if ($departmentId) {
  53. /** @var WorkMemberRelationServices $memberRelationService */
  54. $memberRelationService = app()->make(WorkMemberRelationServices::class);
  55. $memberList = $memberRelationService->getMemberRelationList([
  56. ['department', 'in', $departmentId]
  57. ], ['member_id', 'department']);
  58. foreach ($data as &$item) {
  59. $memberId = [];
  60. foreach ($memberList as $value) {
  61. if ($value['department'] == $item['department_id']) {
  62. $memberId[] = $value['member_id'];
  63. }
  64. }
  65. $item['count'] = count($memberId);
  66. }
  67. }
  68. return get_tree_children($data, 'children', 'department_id', 'parentid');
  69. }
  70. /**
  71. * 同步企业微信部门和成员信息
  72. */
  73. public function authDepartment()
  74. {
  75. /** @var WorkConfig $config */
  76. $config = app()->make(WorkConfig::class);
  77. $corpId = $config->get('corpId');
  78. if (!$corpId) {
  79. throw new ValidateException('请先配置企业微信ID');
  80. }
  81. $res = Work::getDepartment();
  82. if (isset($res['errcode']) && 0 !== $res['errcode']) {
  83. throw new ValidateException($res['errmsg'] ?? '请检查企业微信自建应用配置');
  84. }
  85. $department = $res['department'] ?? [];
  86. $data = [];
  87. $ids = [];
  88. foreach ($department as $item) {
  89. $item['srot'] = $item['order'] ?? '';
  90. $item['name_en'] = $item['name_en'] ?? '';
  91. $item['department_leader'] = json_encode($item['department_leader'] ?? []);
  92. $item['department_id'] = $item['id'] ?? '';
  93. unset($item['order'], $item['id']);
  94. if ($this->dao->count(['department_id' => $item['department_id'], 'corp_id' => $corpId])) {
  95. $this->dao->update($item['department_id'], [
  96. 'name' => $item['name'] ?? '',
  97. 'srot' => $item['srot'],
  98. 'department_leader' => $item['department_leader'],
  99. 'parentid' => $item['parentid']
  100. ]);
  101. } else {
  102. $item['create_time'] = time();
  103. $item['corp_id'] = $corpId;
  104. $data[] = $item;
  105. }
  106. $ids[] = $item['department_id'];
  107. }
  108. if ($data) {
  109. $this->dao->saveAll($data);
  110. }
  111. if ($ids) {
  112. foreach ($ids as $id) {
  113. WorkMemberJob::dispatchDo('run', [$id]);
  114. }
  115. }
  116. }
  117. /**
  118. * 获取部门+成员tree型数据
  119. * @param string $corpId
  120. * @param array $mobile
  121. * @return array
  122. * @throws DataNotFoundException
  123. * @throws DbException
  124. * @throws ModelNotFoundException
  125. */
  126. public function getMailChildren(string $corpId)
  127. {
  128. $list = $this->dao->getDataList(['corp_id' => $corpId], ['department_id', 'parentid', 'name', 'name_en'], 0, 0, 'srot', ['member' => function ($query) use ($corpId) {
  129. $query->where('corp_id', $corpId)
  130. ->field(['userid', 'id', 'mobile', 'avatar', 'thumb_avatar', 'name'])
  131. ->with('departmentRelation');
  132. }]);
  133. $userList = [];
  134. foreach ($list as $item) {
  135. if ($item['member'] && is_array($item['member'])) {
  136. $userList = array_merge($userList, $item['member']);
  137. }
  138. }
  139. foreach ($list as &$item) {
  140. $item['member'] = [];
  141. $user = [];
  142. foreach ($userList as $value) {
  143. $frameIds = $value['departmentRelation'] ? array_column($value['departmentRelation'], 'department') : [];
  144. unset($value['departmentRelation']);
  145. if (in_array($item['department_id'], $frameIds)) {
  146. $user[] = $value;
  147. }
  148. }
  149. $item['member'] = $user;
  150. $item['member_count'] = count($user);
  151. }
  152. return get_tree_children($list, 'children', 'department_id', 'parentid');
  153. }
  154. /**
  155. * 创建部门
  156. * @param array $payload
  157. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  158. */
  159. public function createDepartment(array $payload)
  160. {
  161. $corpId = $payload['ToUserName'];
  162. $where = ['corp_id' => $corpId, 'department_id' => $payload['Id']];
  163. $departmentInfo = Work::getDepartmentInfo($payload['Id']);
  164. if ($this->dao->count($where)) {
  165. return $this->updateDepartment($corpId, (int)$payload['Id'], $departmentInfo['department']['name']);
  166. } else {
  167. return $this->dao->save([
  168. 'corp_id' => $corpId,
  169. 'department_id' => $payload['Id'] ?? '',
  170. 'name' => $departmentInfo['department']['name'] ?? '',
  171. 'parentid' => $departmentInfo['department']['parentid'] ?? '',
  172. 'sort' => $payload['order'] ?? '',
  173. 'create_time' => time()
  174. ]);
  175. }
  176. }
  177. /**
  178. * 更新部门
  179. * @param string $corpId
  180. * @param int $department_id
  181. * @param string $name
  182. * @return mixed
  183. */
  184. public function updateDepartment(string $corpId, int $departmentId, string $name)
  185. {
  186. if (!$name) {
  187. $departmentInfo = Work::getDepartmentInfo($departmentId);
  188. $name = $departmentInfo['department']['name'] ?? '';
  189. }
  190. return $this->dao->update(['corp_id' => $corpId, 'department_id' => $departmentId], ['name' => $name]);
  191. }
  192. /**
  193. * 删除部门
  194. * @param string $corpId
  195. * @param int $departmentId
  196. * @return mixed
  197. */
  198. public function deleteDepartment(string $corpId, int $departmentId)
  199. {
  200. return $this->dao->delete(['corp_id' => $corpId, 'department_id' => $departmentId]);
  201. }
  202. }