HrLogic.php 9.2 KB
<?php

namespace App\Http\Logic\Aside\Manage;


use App\Http\Logic\Aside\BaseLogic;
use App\Models\Manage\BelongingGroup;
use App\Models\Manage\EntryPosition;
use App\Models\Manage\Group;
use App\Models\Manage\JobLevel;
use App\Models\Manage\Manage;
use App\Models\Manage\ManageHr;
use App\Models\Manage\Menu;
use App\Models\Project\Project;
use App\Models\User\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

/**
 * Class ManageHrLogic
 * @package App\Http\Logic\Aside\Manage
 * @author zgj
 * @date 2023/5/30
 */
class HrLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->param = $this->requestAll;
        $this->model = new ManageHr();
    }

    /**
     * @remark :保存数据
     * @name   :hrSave
     * @author :lyh
     * @method :post
     * @time   :2023/9/6 10:17
     */
    public function hrSave(){
            //处理参数
        foreach ($this->model::specieField() as $v){
            $this->param = $this->setJson($v,$this->param);
        }
        if(isset($this->param['id']) && !empty($this->param['id'])){
            $this->editHrManager();
        }else{
            $this->addHrManager();
        }
        return $this->success();
    }

    /**
     * @remark :添加人事信息时 同步添加管理员账号
     * @name   :addManager
     * @author :lyh
     * @method :post
     * @time   :2023/9/6 10:18
     */
    public function addHrManager(){
        $managerModel = new Manage();
        $info = $managerModel->read(['mobile'=>$this->param['mobile']]);
        if($info !== false){
            $this->fail('当前号码已存在');
        }
        $data = [
            'name'=>$this->param['name'],
            'mobile'=>$this->param['mobile'],
            'password'=>Hash::make('globalsov6'),
            'gid'=>4,
        ];
        DB::beginTransaction();
        try {
            $managerModel = new Manage();
            $this->param['manage_id'] = $managerModel->addReturnId($data);
            $this->model->add($this->param);

            //同步到B端演示项目
            $this->syncBProjectUser($this->param['mobile'], $this->param['mobile'], $this->param['name'], $this->param['status']);

            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('系统错误请联系管理员');
        }
        return $this->success();
    }

    /**
     * @remark :编辑人事信息
     * @name   :editHrManager
     * @author :lyh
     * @method :post
     * @time   :2023/9/20 15:06
     */
    public function editHrManager(){
        $hrInfo = $this->model->read(['id'=>$this->param['id']],['id','mobile','manage_id']);
        if($hrInfo === false){
            $this->fail('当前数据不存在或已被删除');
        }
        $managerModel = new Manage();
        if($hrInfo['mobile'] != $this->param['mobile']){
            $mobileInfo = $this->model->read(['mobile'=>$this->param['mobile']],['id']);
            if($mobileInfo !== false){
                $this->fail('当前人事信息中手机号已存在');
            }
            $managerMobileInfo = $managerModel->read(['mobile'=>$this->param['mobile']],['id']);
            if($managerMobileInfo !== false){
                $this->fail('当前管理员信息中手机号已存在');
            }
        }
        DB::beginTransaction();
        try {
            //同步更新管理员手机号码
            $managerModel->edit(['mobile'=>$this->param['mobile']],['id'=>$hrInfo['manage_id']]);
            $this->model->edit($this->param,['id'=>$this->param['id']]);
            //同步到B端演示项目
            $this->syncBProjectUser($hrInfo['mobile'], $this->param['mobile'], $this->param['name'], $this->param['status']);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            $this->fail('系统错误,请联系管理员');
        }
        return $this->success();
    }

    /**
     * 同步到B端用户
     * @author zbj
     * @date 2023/10/23
     */
    public function syncBProjectUser($old_mobile, $mobile, $name, $status){
        $user = User::where('project_id', Project::DEMO_PROJECT_ID)->where('mobile', $old_mobile)->first();
        //在职
        if($status == ManageHr::STATUS_ONE){
            if(!$user){
                $user = new User();
                $user->project_id = Project::DEMO_PROJECT_ID;
                $user->mobile = $mobile;
                $user->name = $name;
                $user->password = base64_encode(md5('v6.' . substr($mobile, -6)));
                $user->type = User::TYPE_ONE;
                $user->role_id = 38; //技术总部
                $user->save();
            }
        }else{
            //离职
            if($user){
                $user->delete();
            }
        }
    }

    /**
     * @remark :获取详情
     * @name   :getHrInfo
     * @author :lyh
     * @method :post
     * @time   :2023/7/25 9:27
     */
    public function getHrInfo(){
        $data = $this->model->read($this->param);
        //查看当前用户是否有人事权限
        if(($this->manager['gid'] != ManageHr::GID_ZERO) && isset($this->param['id'])){
            $groupModel = new Group();
            $groupInfo = $groupModel->read(['id'=>$this->manager['gid']]);
            if (!in_array(20,$groupInfo['rights']) && ($data['manage_id'] != $this->manager['id'])) {
                $this->fail('无权限查看其他用户信息');
            }
        }
        foreach ($this->model::specieField() as $v){
            $data[$v] = json_decode($data[$v],true);
        }
        return $this->success($data);
    }

    /**
     * @remark :转换
     * @name   :setJson
     * @author :lyh
     * @method :post
     * @time   :2023/9/20 15:23
     */
    //数组转json存储
    public function setJson($str,$param){
        if(isset($param[$str]) && is_array($param[$str])){
            $param[$str] = json_encode($param[$str]);
        }
        return $param;
    }

    /**
     * @remark :获取所有搜索参数
     * @name   :getParams
     * @author :lyh
     * @method :post
     * @time   :2023/7/24 11:05
     */
    public function getSearchParams(){
        $data['belong_group'] = $this->belongGroupList();
        $data['education'] = $this->educationList();
        $data['entry_position'] = $this->entryPositionList();
        $data['p_level'] = $this->pLevel();
        $data['political_outlook'] = $this->politicalOutlook();
        $data['party_branch'] = $this->IsPartyBranch();
        return $data;
    }

    /**
     * @remark :获取归属小组
     * @name   :belongGroupList
     * @author :lyh
     * @method :post
     * @time   :2023/7/22 17:35
     */
    public function belongGroupList(){
        $belongingGroupModel = new BelongingGroup();
        $lists = $belongingGroupModel->list();
        return $this->success($lists);
    }

    /**
     * @remark :获取学历
     * @name   :educationList
     * @author :lyh
     * @method :post
     * @time   :2023/7/22 17:49
     */
    public function educationList(){
        $data = [
            1 => '专科',
            2 => '本科',
            3 => '研究生及以上',
            0 => '其他',
        ];
        return $this->success($data);
    }

    /**
     * @remark :入职岗位
     * @name   :entryPositionList
     * @author :lyh
     * @method :post
     * @time   :2023/7/22 17:51
     */
    public function entryPositionList(){
        $entryPositionModel = new EntryPosition();
        $lists = $entryPositionModel->list();
        return $this->success($lists);
    }

    /**
     * @remark :职务级别
     * @name   :pLevel
     * @author :lyh
     * @method :post
     * @time   :2023/7/22 18:10
     */
    public function pLevel(){
        $jobLevelModel = new JobLevel();
        $lists = $jobLevelModel->list();
        return $this->success($lists);
    }

    /**
     * @remark :政治面貌
     * @name   :politicalOutlook
     * @author :lyh
     * @method :post
     * @time   :2023/7/24 10:57
     */
    public function politicalOutlook(){
        $data = [
            1 => '预备党员',
            2 => '正式党员',
            0 => '群众',
        ];
        return $this->success($data);
    }

    /**
     * @remark :是否有党支部
     * @name   :IsPartyBranch
     * @author :lyh
     * @method :post
     * @time   :2023/7/24 11:01
     */
    public function IsPartyBranch(){
        $data = [
            1 => '是',
            2 => '否',
            0 => '无',
        ];
        return $this->success($data);
    }


    /**
     * @param $page_size
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getHrListForV5($page_size)
    {
        $list = $this->model->with('dept')->with('position')->select(['name', 'id_card', 'mobile', 'dept_id', 'entry_position', 'status'])->orderBy('id', 'desc')->paginate($page_size);
        return $list;
    }

    /**
     * @remark :设置排序
     * @name   :setSort
     * @author :lyh
     * @method :post
     * @time   :2023/12/7 9:13
     */
    public function setSort(){
        $this->model->edit(['sort'=>$this->param['sort']],['id'=>$this->param['id']]);
        return $this->success();
    }
}