瀏覽代碼

小高薪

cqp 3 月之前
父節點
當前提交
75f0a93637

+ 76 - 0
app/Http/Controllers/Api/CalendarController.php

@@ -0,0 +1,76 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\Service\CalendarService;
+use Illuminate\Http\Request;
+
+class CalendarController extends BaseController
+{
+    public function calendarEdit(Request $request)
+    {
+        $service = new CalendarService();
+        $user = $request->userData;
+        list($status,$data) = $service->calendarEdit($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function calendarAdd(Request $request)
+    {
+        $service = new CalendarService();
+        $user = $request->userData;
+        list($status,$data) = $service->calendarAdd($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+
+    }
+
+    public function calendarDel(Request $request)
+    {
+        $service = new CalendarService();
+        $user = $request->userData;
+        list($status,$data) = $service->calendarDel($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+
+    }
+
+    public function calendarList(Request $request)
+    {
+        $service = new CalendarService();
+        $user = $request->userData;
+        list($status,$data) = $service->calendarList($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function calendarDetail(Request $request)
+    {
+        $service = new CalendarService();
+        $user = $request->userData;
+        list($status,$data) = $service->calendarDetail($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+}

+ 14 - 0
app/Model/Calendar.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Model;
+
+class Calendar extends DataScopeBaseModel
+{
+    protected $table = "calendar"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+    const employee_column = "crt_id";
+
+    public static $field = ['*'];
+}

+ 12 - 0
app/Model/CalendarDetails.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace App\Model;
+
+class CalendarDetails extends DataScopeBaseModel
+{
+    protected $guarded = [];
+    protected $table = "calendar_details"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+}

+ 225 - 0
app/Service/CalendarService.php

@@ -0,0 +1,225 @@
+<?php
+
+namespace App\Service;
+
+use App\Model\Calendar;
+use App\Model\CalendarDetails;
+use App\Model\Employee;
+use Illuminate\Support\Facades\DB;
+
+class CalendarService extends Service
+{
+    public function calendarEdit($data,$user){
+        list($status,$msg) = $this->calendarRule($data, $user, false);
+        if(!$status) return [$status,$msg];
+
+        try {
+            DB::beginTransaction();
+
+            $model = Calendar::where('id',$data['id'])->first();
+            $model->time = $data['time'] ?? 0;
+            $model->work_days = $data['work_days'] ?? 0;
+            $model->save();
+
+            $time = time();
+            CalendarDetails::where('del_time',0)
+                ->where('calendar_id', $model->id)
+                ->update(['del_time' => $time]);
+            $this->saveDetail($model->id, $time, $data);
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        return [true, ''];
+    }
+
+    public function calendarAdd($data,$user){
+        list($status,$msg) = $this->calendarRule($data, $user);
+        if(!$status) return [$status,$msg];
+
+        try {
+            DB::beginTransaction();
+
+            $model = new Calendar();
+            $model->time = $data['time'] ?? 0;
+            $model->work_days = $data['work_days'] ?? 0;
+            $model->crt_id = $user['id'];
+            $model->top_depart_id = $data['top_depart_id'];
+            $model->save();
+
+            $this->saveDetail($model->id, time(), $data);
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        return [true, ''];
+    }
+
+    private function saveDetail($id, $time, $data){
+        if(! empty($data['details'])){
+            $unit = [];
+            foreach ($data['details'] as $value){
+                $unit[] = [
+                    'calendar_id' => $id,
+                    'time' => $value['time'],
+                    'is_work' => $value['is_work'],
+                    'crt_time' => $time,
+                    'top_depart_id' => $value['top_depart_id'],
+                ];
+            }
+            if(! empty($unit)) CalendarDetails::insert($unit);
+        }
+    }
+
+    private function getDetail($id){
+        $data = CalendarDetails::where('del_time',0)
+            ->where('calendar_id', $id)
+            ->get()->toArray();
+
+        $unit = [];
+        foreach ($data as $value){
+            $unit[] = [
+                'time' => date("Y-m-d",$value['time']),
+                'is_work' => $value['is_work'],
+            ];
+        }
+
+        $detail = [
+            'details' => $unit,
+        ];
+
+        foreach ($detail as $key => $value) {
+            if (empty($value)) {
+                $detail[$key] = (object)[]; // 转成 stdClass 对象
+            }
+        }
+
+        return $detail;
+    }
+
+    public function calendarDel($data){
+        if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
+
+        try {
+            DB::beginTransaction();
+            $time = time();
+
+            Calendar::where('del_time',0)
+                ->whereIn('id',$data['id'])
+                ->update(['del_time' => $time]);
+
+            CalendarDetails::where('del_time',0)
+                ->whereIn('calendar_id', $data['id'])
+                ->update(['del_time' => $time]);
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        return [true, ''];
+    }
+
+    public function calendarDetail($data, $user){
+        if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
+        $customer = Calendar::where('del_time',0)
+            ->where('id',$data['id'])
+            ->first();
+        if(empty($customer)) return [false,'日历设置不存在或已被删除'];
+        $customer = $customer->toArray();
+        $customer['time']  = ! empty($customer['time']) ? date("Y-m", $customer['time']) : "";
+        $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
+        $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
+
+        $details = $this->getDetail($data['id']);
+        $customer = array_merge($customer, $details);
+
+        return [true, $customer];
+    }
+
+    public function calendarCommon($data,$user, $field = []){
+        if(empty($field)) $field = Calendar::$field;
+
+        $model = Calendar::Clear($user,$data);
+        $model = $model->where('del_time',0)
+            ->select($field)
+            ->orderby('id', 'desc');
+
+        if(! empty($data['id'])) $model->whereIn('id', $data['id']);
+        if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
+            $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
+            $model->where('crt_time','>=',$return[0]);
+            $model->where('crt_time','<=',$return[1]);
+        }
+        if(! empty($data['time'][0]) && ! empty($data['time'][1])) {
+            $return = $this->changeDateToTimeStampAboutRange($data['time']);
+            $model->where('time','>=',$return[0]);
+            $model->where('time','<=',$return[1]);
+        }
+
+        return $model;
+    }
+
+    public function calendarList($data,$user){
+        $model = $this->calendarCommon($data, $user);
+        $list = $this->limit($model,'',$data);
+        $list = $this->fillData($list,$user,$data);
+
+        return [true, $list];
+    }
+
+    public function calendarRule(&$data, $user, $is_add = true){
+        $data['top_depart_id'] = $user['top_depart_id'];
+        if(empty($data['time'])) return [false, '年月不能为空'];
+        $data['time'] = $this->changeDateToMonth($data['time']);
+
+        $res = $this->checkNumber($data['work_days'],0,'positive');
+        if(! $res['valid']) return [false,'工作日:' . $res['error']];
+
+        if(! empty($data['details'])){
+            foreach ($data['details'] as $key => $value){
+                if(empty($value['time'])) return [false, '日期不能为空'];
+                $data['details'][$key]['time'] = $this->changeDateToDate($value['time']);
+                if(! isset($value['is_work'])) return [false, '是否工作日不能为空'];
+                $data['details'][$key]['top_depart_id'] = $data['top_depart_id'];
+            }
+        }
+
+        if($is_add){
+            $bool = Calendar::where('time', $data['time'])
+                ->where('top_depart_id', $data['top_depart_id'])
+                ->where('del_time',0)
+                ->exists();
+        }else{
+            if(empty($data['id'])) return [false,'ID不能为空'];
+            $bool = Calendar::where('time',$data['time'])
+                ->where('top_depart_id', $data['top_depart_id'])
+                ->where('id','<>',$data['id'])
+                ->where('del_time',0)
+                ->exists();
+        }
+        if($bool) return [false, '该年月下的日历设置已存在'];
+
+        return [true, ''];
+    }
+
+    public function fillData($data, $user, $search){
+        if(empty($data['data'])) return $data;
+
+        $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
+        foreach ($data['data'] as $key => $value){
+            $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
+            $data['data'][$key]['time'] = $value['time'] ? date('Y-m',$value['time']) : '';
+            $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
+        }
+
+        return $data;
+    }
+}

+ 20 - 0
app/Service/Service.php

@@ -482,4 +482,24 @@ class Service
         // 结果示例:FE26030001
         // 结果示例:FE26030001
         return $prefix . $period . str_pad($sequence, $length, '0', STR_PAD_LEFT);
         return $prefix . $period . str_pad($sequence, $length, '0', STR_PAD_LEFT);
     }
     }
+
+
+    function changeDateToMonth($time)
+    {
+        if (empty($time)) {
+            return 0; // 或抛出异常,根据业务决定
+        }
+
+        // 解析输入时间,默认按 UTC 解析(兼容前端 ISO 字符串)
+        $dateTime = new \DateTime($time, new \DateTimeZone('UTC'));
+
+        // 转换为上海时区(PRC)
+        $dateTime->setTimezone(new \DateTimeZone('Asia/Shanghai'));
+
+        // 设置为当月第一天,00:00:00
+        $dateTime->modify('first day of this month');
+        $dateTime->setTime(0, 0, 0);
+
+        return $dateTime->getTimestamp(); // 推荐使用 getTimestamp() 而非 strtotime
+    }
 }
 }