智慧教务系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

252 lines
8.4 KiB

<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的多应用管理平台
// +----------------------------------------------------------------------
// | 官方网址:https://www.niucloud.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace app\service\admin\personnel;
use app\dict\sys\UserDict;
use app\model\campus_person_role\CampusPersonRole;
use app\model\personnel\Personnel;
use app\model\personnel\PersonnelInfo;
use app\model\sys\SysUser;
use app\service\admin\user\UserService;
use core\base\BaseAdminService;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\facade\Db;
/**
* 人力资源-人员服务层
* Class PersonnelService
* @package app\service\admin\personnel
*/
class PersonnelService extends BaseAdminService
{
public function __construct()
{
parent::__construct();
$this->model = new Personnel();
}
/**
* 获取人力资源-人员列表
* @param array $where
* @return array
*/
public function getPage(array $where = [])
{
$field = 'id,name,head_img,gender,birthday,phone,address,native_place,education,profile,emergency_contact_phone,id_card_front,id_card_back,employee_number,status,is_sys_user,sys_user_id,create_time,update_time,deleted_at';
$order = 'create_time desc';
$search_model = $this->model->withSearch(["name", "gender", "phone", "address", "education", "employee_number", "status", "create_time"], $where)->field($field)->order($order);
$list = $this->pageQuery($search_model, function ($item) {
$CampusPersonRole = new CampusPersonRole();
$item['is_role'] = 1; // 始终允许角色设置
$role = $CampusPersonRole->where(['person_id' => $item['id']])->find();
if($role){
$item['role_data'] = [
'id' => $role['id'],
'campus_id' => $role['campus_id'],
'person_id' => $item['id'],
'role_id' => $role['role_id'],
'dept_id' => $role['dept_id']
];
// 移除dept_id限制,始终允许角色设置
$item['is_role'] = 1;
}else{
$item['role_data'] = [
'id' => '',
'campus_id' => '',
'person_id' => $item['id'],
'role_id' => '',
'dept_id' => ''
];
}
});
return $list;
}
/**
* 获取人力资源-人员信息
* @param int $id
* @return array
*/
public function getInfo(int $id)
{
$field = 'id,name,gender,head_img,birthday,phone,address,native_place,education,profile,emergency_contact_phone,id_card_front,id_card_back,employee_number,status,is_sys_user,sys_user_id,create_time,update_time,deleted_at';
$info = $this->model->field($field)->where([['id', "=", $id]])->findOrEmpty()->toArray();
$info['gender'] = strval($info['gender']);
$info['status'] = strval($info['status']);
$info['is_sys_user'] = strval($info['is_sys_user']);
$info['info'] = (new PersonnelInfo())->where(['person_id' => $id])->findOrEmpty()->toArray();
return $info;
}
/**
* 添加人力资源-人员
* @param array $data
* @return mixed
*/
public function add(array $data)
{
$info = $data['info'];
unset($data['info']);
$status = $this->model->where('phone', $data['phone'])->value('id');
if ($status) {
throw new \Exception('手机号已存在');
}
try {
Db::startTrans();
if ($data['is_sys_user'] === '1') {
$uid = (new UserService())->addUser([
'username' => $data['phone'],
'password' => $data['phone'],
'real_name' => $data['name'],
'head_img' => $data['head_img'],
'status' => UserDict::ON,
'role_ids' => []
]);
$data['sys_user_id'] = $uid;
}
// 员工编号
$data['employee_number'] = getEmployeeNumber();
$data['birthday'] = $info['birthday'];
$data['native_place'] = $info['native_place'];
unset($info['birthday']);
unset($info['native_place']);
$res = $this->model->create($data);
$info['person_id'] = $res->id;
(new PersonnelInfo())->insert($info);
Db::commit();
return $res->id;
} catch (\Exception $e) {
Db::rollback();
throw new \Exception($e->getMessage());
}
}
/**
* 人力资源-人员编辑
* @param int $id
* @param array $data
* @return bool
*/
public function edit(int $id, array $data)
{
$info = $data['info'];
unset($data['info']);
$status = $this->model->where('phone', $data['phone'])->value('id');
if ($status && $status != $id) {
throw new \Exception('手机号已存在');
}
try {
Db::startTrans();
if ($data['is_sys_user'] === '1') {
$uid = (new SysUser())->where(['username' => $data['phone']])->value('uid');
if (!$uid) {
$uid = (new UserService())->addUser([
'username' => $data['phone'],
'password' => $data['phone'],
'real_name' => $data['name'],
'head_img' => $data['head_img'],
'status' => UserDict::ON,
'role_ids' => []
]);
$data['sys_user_id'] = $uid;
}
}
$this->model->where([['id', '=', $id]])->update($data);
(new PersonnelInfo())->where(['person_id' => $id])->update($info);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
throw new \Exception($e->getMessage());
}
return true;
}
/**
* 删除人力资源-人员
* @param int $id
* @return bool
*/
public function del(int $id)
{
$model = $this->model->where([['id', '=', $id]])->find();
$res = $model->delete();
return $res;
}
public function to_lead_into(array $data)
{
$filePath = public_path() . $data['url'];
try {
$spreadsheet = IOFactory::load($filePath);
$worksheet = $spreadsheet->getActiveSheet();
$highestRow = $worksheet->getHighestRow();
$list = [];
// 遍历每一行
foreach ($worksheet->getRowIterator() as $rowIndex => $row) {
if ($rowIndex == 1) {
// 跳过第一行(表头)和最后一行
continue;
}
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
$data = [];
foreach ($cellIterator as $cell) {
$value = $cell->getValue(); // 获取每个单元格的值
if ($value instanceof \PhpOffice\PhpSpreadsheet\RichText\RichText) {
$value = $value->getPlainText();
}
$data[] = $value;
}
$list[] = $data;
}
$inserAll = [];
$status = ['已禁用' => 0,'待审核'=>1,'已审核' => 2];
$is_sys_user = ['是' => 1,'否'=>0];
foreach ($list as $key => $item) {
$inserAll[] = [
'name' => $item[0],
'phone' => $item[1],
'status' =>$status[$item[2]],
'is_sys_user' =>$is_sys_user[$item[3]],
'create_time' => date("Y-m-d H:i:s")
];
}
$this->model->insertAll($inserAll);
return success("导入成功");
} catch (\PhpOffice\PhpSpreadsheet\Reader\Exception $e) {
return fail($e->getMessage());
}
}
}