cqp 3 miesięcy temu
rodzic
commit
5dfb38b087
3 zmienionych plików z 217 dodań i 21 usunięć
  1. 26 0
      app/Model/Employee.php
  2. 39 21
      app/Service/EmployeeService.php
  3. 152 0
      app/Service/Service.php

+ 26 - 0
app/Model/Employee.php

@@ -26,4 +26,30 @@ class Employee extends DataScopeBaseModel
     const AUTH_ONE = 1; // 我的
     const AUTH_TWO = 2; // 部门
     const AUTH_THREE = 3; // 全部
+
+    const SEX_ONE = 1;
+    const SEX_TWO = 2;
+    const SEX_TYPE = [
+        self::SEX_ONE => '男',
+        self::SEX_TWO => '女',
+    ];
+
+    const Education = [
+        1 => '高中及以下',
+        2 => '中专/技校/职高',
+        3 => '大专(高职/专科)',
+        4 => '本科(学士)',
+        5 => '硕士研究生',
+        6 => '博士研究生',
+        7 => '博士后',
+    ];
+
+    const TYPE_ONE = 1; // 在职
+    const TYPE_TWO = 2; // 离职
+    const TYPE_THREE = 3; // 休假
+    const State_Type = [
+        self::TYPE_ONE => '在职',
+        self::TYPE_TWO => '离职',
+        self::TYPE_THREE => '休假',
+    ];
 }

+ 39 - 21
app/Service/EmployeeService.php

@@ -59,9 +59,18 @@ class EmployeeService extends Service
             $model = $model->where('id',$data['id'])->first();
             $model->number = $data['number'];
             $model->title = $data['title'];
-            $model->mobile = $data['mobile'] ?? '';
+            $model->mobile = $data['mobile'];
+            $model->sex = $data['sex'];
+            $model->education = $data['education'];
+            $model->id_card = $data['id_card'];
+            $model->major = $data['major'];
+            $model->p_title = $data['p_title'];
+            $model->state = $data['state'];
             $model->is_admin = $data['is_admin'];
-            if($model->is_admin && $data['password'] !== '******') $model->password = Hash::make($data['password']);
+            if($model->is_admin && $data['password'] !== '******') {
+                $model->password = Hash::make($data['password']);
+                $model->p_version = $model->p_version + 1;
+            }
             $model->save();
 
             EmployeeDepartPermission::where('employee_id',$data['id'])->delete();
@@ -111,11 +120,17 @@ class EmployeeService extends Service
 
             $model->number = $data['number'];
             $model->title = $data['title'];
-            $model->mobile = $data['mobile'] ?? '';
+            $model->mobile = $data['mobile'];
+            $model->sex = $data['sex'];
+            $model->education = $data['education'];
+            $model->id_card = $data['id_card'];
+            $model->major = $data['major'];
+            $model->p_title = $data['p_title'];
+            $model->state = $data['state'];
             $model->crt_id = $user['id'];
             $model->is_admin = $data['is_admin'];
             $model->account = $data['account'] ?? "";
-            if($model->is_admin && $data['password']) $model->password = Hash::make($data['password']);
+            if($model->is_admin) $model->password = Hash::make($data['password']);
             $model->top_depart_id = $data['top_depart_id'];
             $model->save();
 
@@ -169,7 +184,7 @@ class EmployeeService extends Service
     public function employeeList($data,$user){
         $model = Employee::TopClear($user,$data);
         $model = $model->where('del_time',0)
-            ->select('number','mobile','title','id','is_admin', 'account', 'crt_time')
+            ->select('number','mobile','title','id','is_admin', 'account', 'crt_time', 'state')
             ->orderBy('id','desc');
 
         if(! empty($data['number'])) $model->where('number', 'LIKE', '%'.$data['number'].'%');
@@ -209,6 +224,7 @@ class EmployeeService extends Service
 
             // 业务状态字段
             $item['is_admin_title'] = Employee::IS_ADMIN_TITLE[$item['is_admin']] ?? "";
+            $item['state_title'] = Employee::State_Type[$item['state']] ?? "";
             $item['crt_time']       = !empty($item['crt_time']) ? date("Y-m-d", $item['crt_time']) : "";
         }
 
@@ -263,36 +279,38 @@ class EmployeeService extends Service
     public function employeeRule(&$data, $user,$is_add = true){
         if(empty($data['number'])) return [false,'工号不存在'];
         if(empty($data['title'])) return [false,'姓名不存在'];
+        if(! empty($data['sex']) && ! isset(Employee::SEX_TYPE[$data['sex']])) return [false, '性别不存在'];
+        if(empty($data['mobile'])) return [false,'联系电话不能为空'];
+//        if(! $this->isValidPhone($data['mobile'])) return [false, '手机号码格式错误'];
+        if(! empty($data['education']) && ! isset(Employee::Education[$data['education']])) return [false, '学历不存在'];
+        if(empty($data['id_card'])) return [false, '身份证号不能为空'];
         if(empty($data['depart'])) return [false,'部门不能为空'];
-        if(! empty($data['is_admin']) && empty($data['password'])) return [false, '密码不能为空'];
+        if(empty($data['is_use'])) return [false,'状态不能为空'];
+        if(! isset(Employee::State_Type[$data['state']])) return [false,'状态不存在'];
+        if(! empty($data['is_admin'])){
+            if(empty($data['password'])) return [false, '密码不能为空'];
+            if(mb_strlen($data['password']) < 6) return [false, '密码长度不得小于6位长度'];
+        }
         $data['top_depart_id'] = $user['top_depart_id'];
 
-        $mobile = $data['mobile'] ?? "";
-        $number = $data['number'] ?? "";
         if(! $is_add){
             if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
             $bool = Employee::where('del_time',0)
                 ->where('id','<>',$data['id'])
-                ->where(function ($query) use ($mobile, $number){
-                    $query->where('number', $number);
-                    $query->when(! empty($mobile), function ($query) use ($mobile) {
-                        return $query->orWhere('mobile', $mobile);
-                    });
-                })->exists();
+                ->where('top_depart_id', $data['top_depart_id'])
+                ->where('number', $data['number'])
+                ->exists();
         }else{
             if(! empty($data['is_admin'])){
                 $code = Depart::where('id', $user['top_depart_id'])->value('code');
                 $data['account'] = $code . "_" . $data['number'];
             }
             $bool = Employee::where('del_time',0)
-                ->where(function ($query) use ($mobile, $number){
-                    $query->where('number', $number);
-                    $query->when(! empty($mobile), function ($query) use ($mobile) {
-                        return $query->orWhere('mobile', $mobile);
-                    });
-                })->exists();
+                ->where('top_depart_id', $data['top_depart_id'])
+                ->where('number', $data['number'])
+                ->exists();
         }
-        if($bool) return [false,'工号或手机号码已存在'];
+        if($bool) return [false,'人员工号已存在'];
 
         return [true,''];
     }

+ 152 - 0
app/Service/Service.php

@@ -279,4 +279,156 @@ class Service
 
         return true;
     }
+
+    function changeDateToDate($time, $is_end = false){
+        if(empty($time)) return '';
+
+        // 创建一个 DateTime 对象并设置时区为 UTC
+        $dateTime = new \DateTime($time, new \DateTimeZone('UTC'));
+
+        // 将时区设置为 PRC
+        $dateTime->setTimezone(new \DateTimeZone('Asia/Shanghai'));
+
+        $str = "00:00:00";
+        if($is_end) $str = "23:59:59";
+
+        // 将日期时间格式化为特定格式
+        $formattedDate = $dateTime->format('Y-m-d ' . $str);
+
+        return strtotime($formattedDate);
+    }
+
+    //前端传来的时间段 转换为时间戳
+    //精确到秒
+    function changeDateToTimeStampAboutRange($time_range, $is_end = true){
+        if(empty($time_range[0]) || empty($time_range[1])) return [];
+
+        // 创建一个 DateTime 对象并设置时区为 UTC
+        $dateTime = new \DateTime($time_range[0], new \DateTimeZone('UTC'));
+        $dateTime1 = new \DateTime($time_range[1], new \DateTimeZone('UTC'));
+
+        // 将时区设置为 PRC
+        $dateTime->setTimezone(new \DateTimeZone('Asia/Shanghai'));
+        $dateTime1->setTimezone(new \DateTimeZone('Asia/Shanghai'));
+
+        // 将日期时间格式化为特定格式
+        $formattedDate = $dateTime->format('Y-m-d');
+        $formattedDate1 = $dateTime1->format('Y-m-d');
+
+        $str = " 23:59:59";
+        if(! $is_end) $str = " 00:00:00";
+        $return = [];
+        $return[] = strtotime($formattedDate . " 00:00:00");
+        $return[] = strtotime($formattedDate1 . $str);
+
+        return $return;
+    }
+
+    /**
+     * 检查数字是否合法,并可验证其正负性和小数位数
+     *
+     * @param mixed $number 要检查的值
+     * @param int $decimals 允许的最大小数位数(默认 2)
+     * @param string|null $sign 要求的符号:'positive' 大于 0 , 'negative' 小于0, 'zero' 等于0, 'non-negative' 大于或等于 0, 'non-positive' 小于或等于 0 null 表示不限制
+     * @param bool $strict 是否严格模式(不允许整数传字符串等)
+     * @return array ['valid' => bool, 'error' => string|null, 'info' => array]
+     */
+    public function checkNumber($number, $decimals = 2, $sign = null, $strict = false)
+    {
+        $result = [
+            'valid' => false,
+            'error' => null,
+            'info' => []
+        ];
+
+        // 1. 类型检查
+        if ($strict) {
+            if (!is_numeric($number) || is_bool($number)) {
+                $result['error'] = '必须是数字类型';
+                return $result;
+            }
+        } else {
+            // 宽松模式:允许字符串数字
+            if (!is_numeric($number)) {
+                $result['error'] = '不是有效数字';
+                return $result;
+            }
+        }
+
+        $num = (float)$number; // 统一转为浮点数处理
+
+        // 2. 正负号检查
+        if ($sign !== null) {
+            switch ($sign) {
+                case 'positive':
+                    if ($num <= 0) {
+                        $result['error'] = '数字必须大于 0';
+                        return $result;
+                    }
+                    break;
+                case 'negative':
+                    if ($num >= 0) {
+                        $result['error'] = '数字必须小于 0';
+                        return $result;
+                    }
+                    break;
+                case 'zero':
+                    if ($num != 0) {
+                        $result['error'] = '数字必须等于 0';
+                        return $result;
+                    }
+                    break;
+                case 'non-negative': // 大于等于 0
+                    if ($num < 0) {
+                        $result['error'] = '数字必须大于或等于 0';
+                        return $result;
+                    }
+                    break;
+                case 'non-positive': // 小于等于 0
+                    if ($num > 0) {
+                        $result['error'] = '数字必须小于或等于 0';
+                        return $result;
+                    }
+                    break;
+                default:
+                    $result['error'] = "不支持的符号类型: '{$sign}'";
+                    return $result;
+            }
+        }
+
+        // 3. 小数位数检查
+        // 将数字转为标准格式,避免科学计数法
+        $numStr = rtrim(rtrim(sprintf('%.10F', $num), '0'), '.'); // 去除末尾无意义的0
+        if ($numStr === '' || $numStr === '-') {
+            $numStr = '0';
+        }
+
+        // 检查是否有小数点
+        if (strpos($numStr, '.') === false) {
+            $decimalPlaces = 0;
+        } else {
+            $parts = explode('.', $numStr);
+            $decimalPlaces = strlen($parts[1]);
+        }
+
+        if ($decimalPlaces > $decimals) {
+            $result['error'] = "小数位数超过 {$decimals} 位(实际 {$decimalPlaces} 位)";
+            return $result;
+        }
+
+        // 4. 附加信息
+        $result['valid'] = true;
+        $result['info'] = [
+            'original' => $number,
+            'value' => $num,
+            'decimal_places' => $decimalPlaces,
+            'sign' => $num > 0 ? 'positive' : ($num < 0 ? 'negative' : 'zero'),
+        ];
+
+        return $result;
+    }
+
+    function isValidPhone($phone) {
+        return preg_match('/^1(3\d|4[5-9]|5[0-35-9]|6[5-7]|7[0-8]|8\d|9[0-35-9])\d{8}$/', $phone);
+    }
 }