Browse Source

修改招聘信息

15313670163 1 month ago
parent
commit
3938b5b297
1 changed files with 188 additions and 99 deletions
  1. 188 99
      app/JsonRpc/NewsService.php

+ 188 - 99
app/JsonRpc/NewsService.php

@@ -29,6 +29,8 @@ use Directory;
 use Ramsey\Uuid\Uuid;
 use Hyperf\Utils\Random;
 
+use function Hyperf\Support\retry;
+
 #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class NewsService implements NewsServiceInterface
 {
@@ -1068,9 +1070,23 @@ class NewsService implements NewsServiceInterface
         }
         //   3:企业会员
         if($user['type_id'] == 3){
-            array_push($where,['user_id', $data['user_id']]);
+            array_push($where,['job_recruiting.user_id', $data['user_id']]);
+            array_push($where,['job_recruiting.website_id', $data['website_id']]);
         }
-        $result = JobRecruiting::where($where)
+        // 如果 $where 为空,则不添加 where 条件
+        if (empty($where)) {
+            $result['rows'] = JobRecruiting::leftJoin('website', 'job_recruiting.website_id', '=', 'website.id')
+                ->leftJoin('user', 'job_recruiting.user_id', '=', 'user.id')
+                ->select('job_recruiting.*', 'website.website_name as website_name', 'user.user_name as user_name')
+                ->orderBy("updated_at", "desc")
+                ->offset(($data['page'] - 1) * $data['page_size'])
+                ->limit($data['page_size'])
+                ->get()
+                ->all();
+
+            $result['count'] = JobRecruiting::count();
+        } else {
+            $result['rows'] = JobRecruiting::where($where)
                 ->leftJoin('website', 'job_recruiting.website_id', '=', 'website.id')
                 ->leftJoin('user', 'job_recruiting.user_id', '=', 'user.id')
                 ->select('job_recruiting.*', 'website.website_name as website_name', 'user.user_name as user_name')
@@ -1079,6 +1095,8 @@ class NewsService implements NewsServiceInterface
                 ->limit($data['page_size'])
                 ->get()
                 ->all();
+            $result['count'] = JobRecruiting::where($where)->count();
+        }
         if (empty($result)) {
             return Result::error("暂无招聘信息", 0);
         }
@@ -1089,23 +1107,21 @@ class NewsService implements NewsServiceInterface
     * */
     public function getJobCompany(array $data): array
     {
-        $where = [];
-        if(isset($data['job_id']) && !empty($data['job_id'])){
-            $where = [ 'job_id' => $data['job_id'] ];
-        }
-        $user = User::where('id', $data['user_id'])->first();
+        $user = User::where('user.id', $data['user_id'])
+        ->leftJoin('user_info', 'user_info.user_id', 'user.id')
+        ->select('user.user_name','user.mobile','user.email','user.type_id', 'user_info.*')
+        ->first();
+        // return Result::success($user);
         if(empty($user)){
             return Result::error("用户不存在", 0);
         }
-        if($user['type_id'] != 3){
-            $result = JobCompany::where($where)->first();
-        }else{
+        if($user['type_id'] == 3){
             $result = [
-                'id' => 0,
+                // 'id' => 0,
                 'user_id' => $data['user_id'],
                 'website_id' =>  $data['website_id'],
                 'business_name' => $user['business_name'],       // 企业名称
-                'hy_id' => $user['hy_id'],                       // 企业所属行业
+                'company_hy_id' => $user['company_hy_id'],                       // 企业所属行业
                 'company_nature' => $user['company_nature'],     // 公司性质
                 'company_size' => $user['company_size'],         // 公司规模
                 'introduction' => $user['introduction'],         // 公司简介
@@ -1116,6 +1132,41 @@ class NewsService implements NewsServiceInterface
                 'address' => $user['address'],                   // 企业地址
                 'email' => $user['email'],                       // 企业邮箱
             ];
+        }else if($user['type_id'] == 10000){
+            if(!isset($data['job_id']) || empty($data['job_id'])){
+                return Result::error("请传递职位id", 0);
+            }
+            $job = JobRecruiting::where('id',$data['job_id'])->first();
+            if(empty($job)){
+                return Result::error("招聘信息不存在", 0);   
+            }
+            // return Result::success($job);
+            if($job['user_type'] != 3){
+                $result = JobCompany::where('job_id', $job['id'])->get()->toArray();
+            }else{
+                $user = User::where('user.id', $job['user_id'])
+                ->leftJoin('user_info', 'user_info.user_id', 'user.id')
+                ->select('user.user_name','user.mobile','user.email','user.type_id', 'user_info.*')
+                ->first();
+                $result = [
+                    // 'id' => 0,
+                    'user_id' => $data['user_id'],
+                    'website_id' =>  $data['website_id'],
+                    'business_name' => $user['business_name'],       // 企业名称
+                    'company_hy_id' => $user['company_hy_id'],                       // 企业所属行业
+                    'company_nature' => $user['company_nature'],     // 公司性质
+                    'company_size' => $user['company_size'],         // 公司规模
+                    'introduction' => $user['introduction'],         // 公司简介
+                    'real_name' => $user['real_name'],               // 企业联系人
+                    'mobile' => $user['mobile'],                     // 企业联系电话
+                    'company_url' => $user['company_url'],           // 企业网址
+                    'address_arr_id' => $user['address_arr_id'],     // 企业网址
+                    'address' => $user['address'],                   // 企业地址
+                    'email' => $user['email'],                       // 企业邮箱
+                ];
+            }
+        }else{
+            return Result::error("用户类型错误", 0);
         }
         if(empty($result)){
             return Result::error("公司信息不存在", 0);
@@ -1127,34 +1178,76 @@ class NewsService implements NewsServiceInterface
     * */
     public function upJobCompany(array $data): array
     {
-        $where = [];
-        $where = [ 'user_id' => $data['user_id'] ];
-        $user = User::where('id', $data['user_id'])->first();
+        $user = User::where('user.id', $data['user_id'])
+        ->leftJoin('user_info', 'user_info.user_id', 'user.id')
+        ->select('user.user_name','user.type_id', 'user_info.*')
+        ->first();
         if(empty($user)){
             return Result::error("用户不存在", 0);
         }
-        if($user['type_id'] != 3){
-            $result = JobCompany::where($where)->first();
+        $data['address_arr_id'] = isset($data['address_arr_id'])? json_encode(array_map('intval', $data['address_arr_id'])) : '';
+        $company = [
+            'business_name' => $data['business_name'],       // 企业名称
+            'company_hy_id' => $data['company_hy_id'],                       // 企业所属行业
+            'company_nature' => $data['company_nature'],     // 公司性质
+            'company_size' => $data['company_size'],         // 公司规模
+            'introduction' => $data['introduction'],         // 公司简介
+            'real_name' => $data['real_name'],               // 企业联系人
+            'company_url' => $data['company_url'],           // 企业网址
+            'address_arr_id' => $data['address_arr_id'],     // 企业地址
+            'address' => $data['address'],                   // 企业地址
+        ];
+        // return Result::success($data);
+        // 管理员操作
+        if($user['type_id'] == 10000){
+            if(!isset($data['job_id']) || empty($data['job_id'])){
+                return Result::error("请传递职位id", 0);
+            }
+            $job = JobRecruiting::where('id',$data['job_id'])->first();
+            if(empty($job)){
+                return Result::error("招聘信息不存在", 0);   
+            }
+            // return Result::success($job);
+                // 管理员操作管理员
+            if($job['user_type']!= 3){
+                unset($data['user_id']);
+                unset($data['website_id']);
+                // return Result::success($data);
+                $result['company'] = JobCompany::where('job_id', $job['id'])->update($data);
+            }else{
+                // 管理员操作企业会员
+                // 开启事务
+                Db::beginTransaction();
+                try {
+                    $result['userinfo'] = UserInfo::where('user_id', $job['user_id'])->update($company);
+                    $result['user'] = User::where('id', $job['user_id'])->update(['mobile' => $data['mobile'], 'email' => $data['email']]);
+                    // 提交事务
+                    Db::commit();
+                } catch (\Exception $e) {
+                    // 回滚事务
+                    Db::rollBack();
+                    throw $e;
+                }
+            }
+        // 企业会员操作
+        }else if($user['type_id'] == 3){
+            // 开启事务
+            Db::beginTransaction();
+            try {
+                $result['userinfo'] = UserInfo::where('user_id', $data['user_id'])->update($company);
+                $result['user'] = User::where('id', $data['user_id'])->update(['mobile' => $data['mobile'], 'email' => $data['email']]);
+                // 提交事务
+                Db::commit();
+            } catch (\Exception $e) {
+                // 回滚事务
+                Db::rollBack();
+                throw $e;
+            }
         }else{
-            $result = [
-                'id' => 0,
-                'user_id' => $data['user_id'],
-                'website_id' =>  $data['website_id'],
-                'business_name' => $user['business_name'],       // 企业名称
-                'hy_id' => $user['hy_id'],                       // 企业所属行业
-                'company_nature' => $user['company_nature'],     // 公司性质
-                'company_size' => $user['company_size'],         // 公司规模
-                'introduction' => $user['introduction'],         // 公司简介
-                'real_name' => $user['real_name'],               // 企业联系人
-                'mobile' => $user['mobile'],                     // 企业联系电话
-                'company_url' => $user['company_url'],           // 企业网址
-                'address_arr_id' => $user['address_arr_id'],     // 企业网址
-                'address' => $user['address'],                   // 企业地址
-                'email' => $user['email'],                       // 企业邮箱
-            ];
+            return Result::error("用户类型错误", 0);
         }
         if(empty($result)){
-            return Result::error("公司信息不存在", 0);
+            return Result::error("修改失败", 0);
         }
         return Result::success($result);
     }
@@ -1167,52 +1260,31 @@ class NewsService implements NewsServiceInterface
         if(empty($job)){
             return Result::error("招聘信息不存在", 0);
         }
+        // return Result::success($job);
         $user = User::where('id', $data['user_id'])->first();
         if(empty($user)){
             return Result::error("用户不存在", 0);
         }
-        if($job['user_type'] == 3){
-            $result = JobRecruiting::where('job_recruiting.id', $data['id'])->first();
-            // 假设当前类实例为 $this
-            $jobCompanyData = $this->getJobCompany([
-                'user_id' => $data['user_id'],
-                'website_id' => $job['website_id']
-                // 假设 $data 是当前方法的参数数组
-            ]);
-            // ->leftJoin('user_info', 'job_recruiting.user_id', 'user_info.id')
-            // ->leftJoin('user', 'job_recruiting.user_id', 'user.id')
-            // ->select(
-            //     'job_recruiting.*',
-            //     'user_info.business_name',          // 企业名称
-            //     'user_info.hy_id',                  // 企业所属行业
-            //     'user_info.company_size',           // 公司规模
-            //     'user_info.company_nature',         // 公司性质
-            //     'user_info.introduction',           // 公司简介
-            //     'user_info.real_name',              // 企业联系人
-            //     'user.mobile',                      // 企业联系电话
-            //     'user_info.company_url',            // 企业网址
-            //     'user_info.address_arr_id',         // 企业地址
-            //     'user_info.address as address_info',                // 企业地址
-            //     'user.email',                       // 企业邮箱
-            // )
-            
-        }else{
-            $result = JobRecruiting::where('job_recruiting.id', $data['id'])
-            ->leftJoin('job_company', 'job_recruiting.id', 'job_company.job_id')
-            ->select(
-                'job_recruiting.*','job_company.id as company_id','job_company.website_id as webid',          // 企业名称
-                'job_company.*',                 // 企业名称
-            )
-            ->first();
+        $company_data = [
+            'user_id' => $data['user_id'],
+            'website_id' => $data['website_id'],
+        ];
+        if($user['type_id']!= 3){
+            $company_data['job_id'] = $data['id'];
         }
-        if (empty($result)) {
+        // return Result::success($company_data);
+        $jobCompanyData = $this->getJobCompany($company_data);
+        $company  = $jobCompanyData['data'] ?? [];
+        // return Result::success($jobCompanyData);
+        if(empty($company)){
+            return Result::error($jobCompanyData['message'], 0);
+        }
+        if (empty($job)) {
             return Result::error("暂无招聘信息", 0);
         }
-        if(!empty($result['address_arr_id'])){
-            // 假设此处是要将字符串解析为数组,如果是JSON格式可以使用json_decode
-            $address_arr = json_decode($result['address_arr_id'], true);
-            $address = District::whereIn('id', $address_arr)->pluck('name');
-            $result['address'] = $address->toArray();
+        $result = array_merge($job->toArray(), $company);
+        if (empty($result)) {
+            return Result::error("暂无招聘信息", 0);
         }
         return Result::success($result);
     }
@@ -1228,12 +1300,12 @@ class NewsService implements NewsServiceInterface
         $data['user_type'] = $user['type_id'];
         $data['cat_arr_id'] = array_values(array_unique($data['cat_arr_id']));
         $data['city_arr_id'] = array_values(array_unique($data['city_arr_id']));
-        $data['cat_arr_id'] = isset($data['cat_arr_id'])? json_encode($data['cat_arr_id']) : '';
-        $data['city_arr_id'] = isset($data['city_arr_id'])? json_encode($data['city_arr_id']) : '';
+        $data['cat_arr_id'] = isset($data['cat_arr_id'])? json_encode(array_map('intval', $data['cat_arr_id'])) : '';
+        $data['city_arr_id'] = isset($data['city_arr_id'])? json_encode(array_map('intval', $data['city_arr_id'])) : '';
         // 公司地址 管理员必填
-        if(isset($data['address_arr_id']) &&!empty($data['address_arr_id'])){
+        if(isset($data['address_arr_id']) && !empty($data['address_arr_id'])){
             $data['address_arr_id'] = array_values(array_unique($data['address_arr_id']));
-            $data['address_arr_id'] = isset($data['address_arr_id'])? json_encode($data['address_arr_id']) : '';
+            $data['address_arr_id'] = isset($data['address_arr_id'])? json_encode(array_map('intval', $data['address_arr_id'])) : '';
         }
         //   管理员-企业相关信息
         $company = [
@@ -1251,7 +1323,7 @@ class NewsService implements NewsServiceInterface
         ];
         //去掉相关企业信息 
         $data = array_diff_key($data, array_flip(array_keys($company)));
-        $company['user_id'] = $user['id'];
+        // return Result::success($data);
         //   3:企业会员  10000:管理员
         if($user['type_id'] == 3 || $user['type_id'] == 10000){
             Db::beginTransaction();
@@ -1269,6 +1341,7 @@ class NewsService implements NewsServiceInterface
                         throw new \Exception("网站不存在");
                     }
                     $company['job_id'] = $jobId;
+                    $company['user_id'] = $user['id'];
                     $company['website_id'] = $data['website_id'];
                     $companyInsertResult = JobCompany::insert($company);
                     if (empty($companyInsertResult)) {
@@ -1292,23 +1365,23 @@ class NewsService implements NewsServiceInterface
     public function upJobRecruiting(array $data): array
     {
         $job = JobRecruiting::where('id', $data['id'])->first();
+        // return Result::success($job);
         if(empty($job)){
             return Result::error("招聘信息不存在", 0);
         }
         $user = User::where('id', $data['user_id'])->first();
-        if(empty($user) || $user['type_id'] != $data['user_type']){
+        // return Result::success($user);
+        if(empty($user)){
             return Result::error("用户不存在", 0);
         }
-        $data['user_type'] = $user['type_id'];
+        if($user['type_id']==3 && $job['user_id']!= $user['id']){
+            return Result::error("用户暂无权限修改此招聘信息!", 0);
+        }
+        // $data['user_type'] = $user['type_id'];
         $data['cat_arr_id'] = array_values(array_unique($data['cat_arr_id']));
         $data['city_arr_id'] = array_values(array_unique($data['city_arr_id']));
-        $data['cat_arr_id'] = isset($data['cat_arr_id'])? json_encode($data['cat_arr_id']) : '';
-        $data['city_arr_id'] = isset($data['city_arr_id'])? json_encode($data['city_arr_id']) : '';
-        // 公司地址 管理员必填
-        if(isset($data['address_arr_id']) &&!empty($data['address_arr_id'])){
-            $data['address_arr_id'] = array_values(array_unique($data['address_arr_id']));
-            $data['address_arr_id'] = isset($data['address_arr_id'])? json_encode($data['address_arr_id']) : '';
-        }
+        $data['cat_arr_id'] = isset($data['cat_arr_id'])? json_encode(array_map('intval', $data['cat_arr_id'])) : '';
+        $data['city_arr_id'] = isset($data['city_arr_id'])? json_encode(array_map('intval', $data['city_arr_id'])) : '';
         //   管理员-企业相关信息
         $company = [
             'business_name' => $data['business_name']?? null,
@@ -1323,31 +1396,42 @@ class NewsService implements NewsServiceInterface
             'address' => $data['address']?? null,
             'email' => $data['email']?? null,
         ];
+        $company['user_id'] = $job['user_id'];
+        $company['job_id'] = $data['id'];
         //去掉相关企业信息 
+        unset($data['user_type']);
         $data = array_diff_key($data, array_flip(array_keys($company)));
-        $company['user_id'] = $user['id'];
+        // return Result::success($data);
         //   3:企业会员  10000:管理员
         if($user['type_id'] == 3 || $user['type_id'] == 10000){
             Db::beginTransaction();
             try {
                 $jobId = JobRecruiting::where('id',$data['id'])->update($data);
+                // $jobId = JobRecruiting::where('id',$data['id'])->first();
                 if (empty($jobId)) {
-                    throw new \Exception("添加失败");
+                    Db::rollback();
+                    return Result::error("修改职位信息失败");
                 }
+                // $company = $this->upJobCompany($company);
+                // return $company;
                 if($user['type_id'] == 10000){
-                    if($data['website_id'] == 0 || !isset($data['website_id'])){
-                        throw new \Exception("请选择所属网站");
+                    if($data['website_id'] == 0 || !isset($data['website_id']) || empty($data['website_id'])){
+                        Db::rollback();
+                        return Result::error("请选择所属网站");
                     }
                     $website = Website::where('id', $data['website_id'])->first();
                     if(empty($website)){
-                        throw new \Exception("网站不存在");
+                        Db::rollback();
+                        return Result::error("网站不存在");
                     }
                     $company['job_id'] = $jobId;
                     $company['website_id'] = $data['website_id'];
-                    $companyInsertResult = JobCompany::where('job_id',$data['id'])->update($company);
-                    if (empty($companyInsertResult)) {
-                        throw new \Exception("添加失败");   
-                    }
+                    $companyData = $this->upJobCompany($company);
+                    return $companyData;
+                    // if (empty($companyData)) {
+                    //     Db::rollback();
+                    //     return Result::error("修改失败");
+                    // }
                 }
                 $data['website_id'] = $user['website_id'];
                 Db::commit();
@@ -1373,16 +1457,21 @@ class NewsService implements NewsServiceInterface
         if(empty($job)){
             return Result::error("招聘信息不存在", 0);
         }
+        if($user['type_id']==3 && $job['user_id']!= $user['id']){
+            return Result::error("用户暂无权限修改此招聘信息!", 0);
+        }
         Db::beginTransaction();
         try {
             $result['job'] = JobRecruiting::where('id', $data['id'])->delete();
             if (empty($result['job'])) {
-                throw new \Exception("删除招聘信息失败");
+                Db::rollBack();
+                return Result::error("删除招聘信息失败");
             }
-            if ($job['type_id'] == 10000) {
+            if ($job['user_type'] == 10000) {
                 $result['company'] = JobCompany::where('job_id', $data['id'])->delete(); // 修正为JobCompany
                 if (empty($result['company'])) {
-                    throw new \Exception("删除企业相关信息失败");
+                    Db::rollBack();
+                    return Result::error("删除企业相关信息失败");
                 }
             }
             Db::commit();