智慧教务系统
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.
 
 
 
 
 
 

153 lines
5.0 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\personnel\Personnel;
use app\model\sys\SysUser;
use app\service\admin\user\UserService;
use core\base\BaseAdminService;
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,delete_time';
$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);
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,delete_time';
$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']);
return $info;
}
/**
* 添加人力资源-人员
* @param array $data
* @return mixed
*/
public function add(array $data)
{
$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();
$res = $this->model->create($data);
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)
{
$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);
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;
}
}