Files
douba_shop/app/services/work/WorkDepartmentServices.php
Karlcarlo c9a98f342c chore: 添加静态资源文件和版本配置文件
添加大量图片、字体、音频等静态资源文件,包括用户界面图片、模板文件和安装界面素材
添加系统版本配置文件(.version)和常量定义文件(.constant)
更新供应商依赖文件,包括Symfony VarDumper组件和相关许可证文件
添加验证码配置文件(captcha.php)和.gitignore规则
2026-03-06 20:14:23 +08:00

211 lines
7.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\services\work;
use app\dao\work\WorkDepartmentDao;
use app\jobs\work\WorkMemberJob;
use app\services\BaseServices;
use crmeb\services\wechat\config\WorkConfig;
use crmeb\services\wechat\Work;
use think\annotation\Inject;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\exception\ValidateException;
/**
* 企业微信部门
* Class WorkDepartmentServices
* @package app\services\work
* @mixin WorkDepartmentDao
*/
class WorkDepartmentServices extends BaseServices
{
/**
* @var WorkDepartmentDao
*/
#[Inject]
protected WorkDepartmentDao $dao;
/**
* 获取组织架构
* @return mixed
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function getDepartmentList()
{
/** @var WorkConfig $config */
$config = app()->make(WorkConfig::class);
$data = $this->dao->getDataList(['corp_id' => $config->corpId], ['department_id', 'parentid', 'name', 'name_en']);
$departmentId = array_column($data, 'department_id');
if ($departmentId) {
/** @var WorkMemberRelationServices $memberRelationService */
$memberRelationService = app()->make(WorkMemberRelationServices::class);
$memberList = $memberRelationService->getMemberRelationList([
['department', 'in', $departmentId]
], ['member_id', 'department']);
foreach ($data as &$item) {
$memberId = [];
foreach ($memberList as $value) {
if ($value['department'] == $item['department_id']) {
$memberId[] = $value['member_id'];
}
}
$item['count'] = count($memberId);
}
}
return get_tree_children($data, 'children', 'department_id', 'parentid');
}
/**
* 同步企业微信部门和成员信息
*/
public function authDepartment()
{
$res = Work::getDepartment();
$department = $res['department'] ?? [];
$data = [];
$ids = [];
/** @var WorkConfig $config */
$config = app()->make(WorkConfig::class);
$corpId = $config->corpId;
if (!$corpId) {
throw new ValidateException('请先配置企业微信ID');
}
foreach ($department as $item) {
$item['srot'] = $item['order'] ?? '';
$item['name_en'] = $item['name_en'] ?? '';
$item['department_leader'] = json_encode($item['department_leader'] ?? []);
$item['department_id'] = $item['id'] ?? '';
unset($item['order'], $item['id']);
if ($this->dao->count(['department_id' => $item['department_id'], 'corp_id' => $corpId])) {
$this->dao->update(['department_id' => $item['department_id'], 'corp_id' => $corpId], [
'name' => $item['name'] ?? '',
'srot' => $item['srot'],
'department_leader' => $item['department_leader'],
'parentid' => $item['parentid']
]);
} else {
$item['create_time'] = time();
$item['corp_id'] = $corpId;
$data[] = $item;
}
$ids[] = $item['department_id'];
}
if ($data) {
$this->dao->saveAll($data);
}
if ($ids) {
foreach ($ids as $id) {
WorkMemberJob::dispatchDo('run', [$id]);
}
}
}
/**
* 获取部门+成员tree型数据
* @param string $corpId
* @param array $mobile
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function getMailChildren(string $corpId)
{
$list = $this->dao->getDataList(['corp_id' => $corpId], ['department_id', 'parentid', 'name', 'name_en'], 0, 0, 'srot', [
'member' => fn($query) => $query->where('corp_id', $corpId)
->field(['userid', 'id', 'mobile', 'avatar', 'thumb_avatar', 'name'])
->with('departmentRelation')
]);
$userList = [];
foreach ($list as $item) {
if ($item['member'] && is_array($item['member'])) {
$userList = array_merge($userList, $item['member']);
}
}
foreach ($list as &$item) {
$item['member'] = [];
$user = [];
foreach ($userList as $value) {
$frameIds = $value['departmentRelation'] ? array_column($value['departmentRelation'], 'department') : [];
unset($value['departmentRelation']);
if (in_array($item['department_id'], $frameIds)) {
$user[] = $value;
}
}
$item['member'] = $user;
$item['member_count'] = count($user);
}
return get_tree_children($list, 'children', 'department_id', 'parentid');
}
/**
* 创建部门
* @param array $payload
* @return \crmeb\basic\BaseModel|mixed|\think\Model
*/
public function createDepartment(array $payload)
{
$corpId = $payload['ToUserName'];
$where = ['corp_id' => $corpId, 'department_id' => $payload['Id']];
$departmentInfo = Work::getDepartmentInfo($payload['Id']);
if ($this->dao->count($where)) {
return $this->updateDepartment($corpId, (int)$payload['Id'], $departmentInfo['department']['name']);
} else {
return $this->dao->save([
'corp_id' => $corpId,
'department_id' => $payload['Id'] ?? '',
'name' => $departmentInfo['department']['name'] ?? '',
'parentid' => $departmentInfo['department']['parentid'] ?? '',
'sort' => $payload['order'] ?? '',
'create_time' => time()
]);
}
}
/**
* 更新部门
* @param string $corpId
* @param int $department_id
* @param string $name
* @return mixed
*/
public function updateDepartment(string $corpId, int $departmentId, string $name)
{
if (!$name) {
$departmentInfo = Work::getDepartmentInfo($departmentId);
$name = $departmentInfo['department']['name'] ?? '';
}
return $this->dao->update(['corp_id' => $corpId, 'department_id' => $departmentId], ['name' => $name]);
}
/**
* 删除部门
* @param string $corpId
* @param int $departmentId
* @return mixed
*/
public function deleteDepartment(string $corpId, int $departmentId)
{
return $this->dao->delete(['corp_id' => $corpId, 'department_id' => $departmentId]);
}
}