193 lines
4.9 KiB
PHP
193 lines
4.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\admin\controller\Erp;
|
|
|
|
use app\admin\BaseController;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Db;
|
|
use think\facade\Session;
|
|
use think\response\Json;
|
|
use think\db\exception\DbException;
|
|
use app\model\Erp\Organization;
|
|
use app\model\System\AdminUser;
|
|
|
|
class OrganizationController extends BaseController
|
|
{
|
|
/**
|
|
* 获取组织机构列表
|
|
*/
|
|
public function getOrganization()
|
|
{
|
|
$tid = $this->getTenantId();
|
|
if (!$tid) {
|
|
return json(['code' => 403, 'msg' => '无法获取租户信息']);
|
|
}
|
|
|
|
$list = Organization::where('delete_time', null)
|
|
->where('tid', $tid)
|
|
->select()
|
|
->toArray();
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '获取成功',
|
|
'data' => $list
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取组织机构详情
|
|
*/
|
|
public function getOrganizationDetail($id)
|
|
{
|
|
$tid = $this->getTenantId();
|
|
if (!$tid) {
|
|
return json(['code' => 403, 'msg' => '无法获取租户信息']);
|
|
}
|
|
|
|
$detail = Organization::where('id', $id)
|
|
->where('delete_time', null)
|
|
->where('tid', $tid)
|
|
->find()
|
|
->toArray();
|
|
$detail['leader_name'] = AdminUser::where('id', $detail['leader_id'])->value('name');
|
|
$detail['parent_name'] = Organization::where('id', $detail['parent_id'])->value('org_name');
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '获取成功',
|
|
'data' => $detail
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 创建组织机构
|
|
*/
|
|
public function createOrganization()
|
|
{
|
|
$tid = $this->getTenantId();
|
|
if (!$tid) {
|
|
return json(['code' => 403, 'msg' => '无法获取租户信息']);
|
|
}
|
|
|
|
$data = $this->request->post();
|
|
$data['tid'] = $tid;
|
|
|
|
$organization = Organization::create($data);
|
|
if ($organization) {
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '创建成功',
|
|
'data' => $organization
|
|
]);
|
|
} else {
|
|
return json([
|
|
'code' => 500,
|
|
'msg' => '创建失败',
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 编辑组织机构
|
|
*/
|
|
public function editOrganization($id)
|
|
{
|
|
$tid = $this->getTenantId();
|
|
if (!$tid) {
|
|
return json(['code' => 403, 'msg' => '无法获取租户信息']);
|
|
}
|
|
|
|
$data = $this->request->post();
|
|
unset($data['tid']); // 不允许修改租户ID
|
|
|
|
$organization = Organization::where('id', $id)
|
|
->where('tid', $tid)
|
|
->update($data);
|
|
if ($organization !== false) {
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '编辑成功',
|
|
'data' => $organization
|
|
]);
|
|
} else {
|
|
return json([
|
|
'code' => 500,
|
|
'msg' => '编辑失败',
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除组织机构
|
|
*/
|
|
public function deleteOrganization($id)
|
|
{
|
|
$tid = $this->getTenantId();
|
|
if (!$tid) {
|
|
return json(['code' => 403, 'msg' => '无法获取租户信息']);
|
|
}
|
|
|
|
$organization = Organization::where('id', $id)
|
|
->where('tid', $tid)
|
|
->update(['delete_time' => date('Y-m-d H:i:s')]);
|
|
if ($organization) {
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '删除成功',
|
|
]);
|
|
} else {
|
|
return json([
|
|
'code' => 500,
|
|
'msg' => '删除失败',
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取企业单位
|
|
*/
|
|
public function getCompanys()
|
|
{
|
|
$tid = $this->getTenantId();
|
|
if (!$tid) {
|
|
return json(['code' => 403, 'msg' => '无法获取租户信息']);
|
|
}
|
|
|
|
$where = [['delete_time', '=', null], ['is_company', '=', 1], ['tid', '=', $tid]];
|
|
|
|
$list = Organization::where($where)->select()->toArray();
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '获取成功',
|
|
'data' => $list
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取部门
|
|
*/
|
|
public function getDepartments()
|
|
{
|
|
$tid = $this->getTenantId();
|
|
if (!$tid) {
|
|
return json(['code' => 403, 'msg' => '无法获取租户信息']);
|
|
}
|
|
|
|
$parentId = input('parent_id/d', 0);
|
|
|
|
$where = [['delete_time', '=', null], ['is_company', '=', 0], ['tid', '=', $tid]];
|
|
|
|
if ($parentId > 0) {
|
|
$where[] = ['parent_id', '=', $parentId];
|
|
}
|
|
|
|
$list = Organization::where($where)->select()->toArray();
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '获取成功',
|
|
'data' => $list
|
|
]);
|
|
}
|
|
}
|