first commit
This commit is contained in:
34
app/admin/service/AccountService.php
Normal file
34
app/admin/service/AccountService.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\service;
|
||||
|
||||
use app\common\model\User as UserModel;
|
||||
|
||||
class AccountService
|
||||
{
|
||||
public function createAccount($data)
|
||||
{
|
||||
if (empty($data['username'])) {
|
||||
$data['username'] = $data['real_name'];
|
||||
}
|
||||
|
||||
# 检查数据库中是否有相同用户名账户
|
||||
$exist = UserModel::where('username', $data['username'])->find();
|
||||
if ($exist) {
|
||||
throw new \Exception('用户名已存在');
|
||||
}
|
||||
|
||||
$insertData = [
|
||||
'username' => $data['username'] ?? $data['real_name'],
|
||||
'real_name' => $data['real_name'],
|
||||
'password' => password_hash('123456', PASSWORD_DEFAULT),
|
||||
'role' => $data['role'] ?? 'user',
|
||||
'status' => 1,
|
||||
];
|
||||
|
||||
$user = UserModel::create($insertData);
|
||||
|
||||
/** @var \think\Model $user */
|
||||
return $user->visible(['id', 'username', 'real_name', 'role', 'status'])->toArray();
|
||||
}
|
||||
}
|
||||
48
app/admin/service/CustomersService.php
Normal file
48
app/admin/service/CustomersService.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\service;
|
||||
|
||||
use app\common\model\Customers as CustomersModel;
|
||||
use app\common\model\CustomerEmployees;
|
||||
|
||||
class CustomersService
|
||||
{
|
||||
public function selectCustomersWithEmployees()
|
||||
{
|
||||
//with 预载入 ,解决N+1问题
|
||||
$list = CustomersModel::with('employees')->select();
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function addCustomer($companyName)
|
||||
{
|
||||
$id = CustomersModel::create([
|
||||
'company_name' => $companyName
|
||||
]);
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function addEmployee($empName, $Id)
|
||||
{
|
||||
$id = CustomersModel::where('id', $Id)->find()->employees()->save([
|
||||
'employee_name' => $empName
|
||||
]);
|
||||
return $id;
|
||||
}
|
||||
|
||||
|
||||
public function deleteEmployee($empId)
|
||||
{
|
||||
$result = CustomerEmployees::destroy($empId);
|
||||
return $result; // 返回受影响的行数,删除一行就1,没有删除就是0
|
||||
}
|
||||
|
||||
public function deleteCustomer($customerId)
|
||||
{
|
||||
// 删除客户单位会自动删除关联的员工(假设你在模型中设置了级联删除)
|
||||
$result = CustomersModel::destroy($customerId);
|
||||
return $result; // 返回受影响的行数,删除一行就1,没有删除就是0
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user