deviceRule($data, $user, false); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = Device::where('id',$data['id'])->first(); $model->code = $data['code'] ?? ''; $model->title = $data['title'] ?? ''; $model->size = $data['size'] ?? ''; $model->mark = $data['mark'] ?? ''; $model->is_use = $data['is_use'] ?? 0; $model->type = $data['type'] ?? 0; $model->in_time = $data['in_time'] ?? 0; $model->power = $data['power'] ?? ""; $model->position = $data['position'] ?? ""; $model->original_value = $data['original_value'] ?? 0; $model->initial_value = $data['initial_value'] ?? 0; $model->device_type = $data['device_type'] ?? 0; $model->save(); $time = time(); DeviceDepartDetails::where('del_time',0) ->where('main_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 deviceAdd($data,$user){ list($status,$msg) = $this->deviceRule($data, $user); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = new Device(); $model->code = $data['code'] ?? ''; $model->title = $data['title'] ?? ''; $model->size = $data['size'] ?? ''; $model->mark = $data['mark'] ?? ''; $model->is_use = $data['is_use'] ?? 0; $model->type = $data['type'] ?? 0; $model->in_time = $data['in_time'] ?? 0; $model->power = $data['power'] ?? ""; $model->position = $data['position'] ?? ""; $model->original_value = $data['original_value'] ?? 0; $model->initial_value = $data['initial_value'] ?? 0; $model->device_type = $data['device_type'] ?? 0; $model->top_depart_id = $data['top_depart_id'] ?? 0; $model->crt_id = $user['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[] = [ 'main_id' => $id, 'depart_id' => $value, 'crt_time' => $time, 'top_depart_id' => $data['top_depart_id'], ]; } if(! empty($unit)) DeviceDepartDetails::insert($unit); } } private function getDetail($id){ $data = DeviceDepartDetails::where('del_time',0) ->where('main_id', $id) ->select('depart_id', 'main_id') ->get()->toArray(); $id = array_column($data,'depart_id'); $map = Depart::whereIn('id', $id)->select('title','id','code')->get()->toArray(); $map = array_column($map,null,'id'); foreach ($data as $key => $value){ $tmp = $map[$value['main_id']] ?? []; $merge = []; $merge['depart_title'] = $tmp['title']; $merge['depart_code'] = $tmp['code']; $data[$key] = array_merge($value, $merge); } $detail = [ 'details' => $data, ]; return $detail; } public function deviceDel($data){ if($this->isEmpty($data,'id')) return [false,'请选择数据']; try { DB::beginTransaction(); $time = time(); Device::where('del_time',0) ->whereIn('id', $data['id']) ->update(['del_time' => $time]); DeviceDepartDetails::where('del_time',0) ->where('main_id', $data['id']) ->update(['del_time' => $time]); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, '']; } public function deviceDetail($data, $user){ if($this->isEmpty($data,'id')) return [false,'请选择数据']; $customer = Device::where('del_time',0) ->where('id',$data['id']) ->first(); if(empty($customer)) return [false,'设备不存在或已被删除']; $customer = $customer->toArray(); $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title'); $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): ''; $customer['use_year'] = $customer['in_time'] ? $this->getDiffYearsFromNow($customer['in_time']) : 0; $customer['in_time'] = $customer['in_time'] ? date("Y-m-d",$customer['in_time']): ''; $details = $this->getDetail($data['id']); $customer = array_merge($customer, $details); return [true, $customer]; } public function deviceCommon($data,$user, $field = []){ if(empty($field)) $field = Device::$field; $model = Device::TopClear($user,$data); $model = $model->where('del_time',0) ->select($field) ->orderby('id', 'desc'); if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%'); if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%'); if(! empty($data['id'])) $model->whereIn('id', $data['id']); if(! empty($data['is_use'])) $model->where('is_use', $data['is_use']); if(! empty($data['type'])) $model->where('type', $data['type']); if(! empty($data['crt_id'])) $model->whereIn('crt_id', $data['crt_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]); } return $model; } public function deviceList($data,$user){ $model = $this->deviceCommon($data, $user); $list = $this->limit($model,'',$data); $list = $this->fillData($list, $data, $user); return [true, $list]; } public function deviceRule(&$data, $user, $is_add = true){ if(empty($data['title'])) return [false, '设备名称不能为空']; if(empty($data['code'])) return [false, '资产编码不能为空']; if(empty($data['is_use'])) return [false, '是否启用不能为空']; if(! isset(Device::Use[$data['is_use']])) return [false, '是否启用错误']; if(empty($data['type'])) return [false, '固定资产类型不能为空']; if(! isset(Device::$type[$data['type']])) return [false, '固定资产类型错误']; if(! isset(Device::$device_type[$data['device_type']])) return [false, '设备标签错误']; if(! empty($data['in_time'])) $data['in_time'] = $this->changeDateToDate($data['in_time']); if(! empty($data['original_value'])){ $res = $this->checkNumber($data['original_value'],0,'positive'); if(! $res['valid']) return [false,'原始价值:' . $res['error']]; } $res = $this->checkNumber($data['initial_value'],0,'positive'); if(! $res['valid']) return [false,'期初原值:' . $res['error']]; $data['top_depart_id'] = $user['top_depart_id']; if($is_add){ $bool = Device::where('code',$data['code']) ->where('top_depart_id', $data['top_depart_id']) ->where('del_time',0) ->exists(); }else{ if(empty($data['id'])) return [false,'ID不能为空']; $bool = Device::where('code',$data['code']) ->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, $ergs, $user){ if(empty($data['data'])) return $data; $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id'))); $map = []; if(isset($ergs['search_for_month_work'])) { $device_ids = array_column($data['data'], 'id'); list($status, $map) = $this->getDevicesMonthStats($device_ids, $ergs['search_for_month_work'], $user); } 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]['in_time'] = $value['in_time'] ? date('Y-m-d',$value['in_time']) : ''; $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? ''; $data['data'][$key]['is_use_title'] = Device::Use[$value['is_use']] ?? ''; $data['data'][$key]['type_title'] = Device::$type[$value['type']] ?? ''; if(isset($ergs['search_for_month_work'])) $data['data'][$key]['month_dw'] = $map[$value['id']] ?? []; } return $data; } public function getDevicesMonthStats($device_ids, $month, $user) { $topDepartId = $user['top_depart_id']; if(is_numeric($month)){ $monthStart = $month; }else{ $monthStart = $this->changeDateToDate($month); $monthStr = date("Y-m", $monthStart); } $endTime = strtotime("+1 month", $monthStart) - 1; // 1. 获取当月标准工作日天数 $standardWorkDays = DB::table('calendar_details') ->where('top_depart_id', $topDepartId) ->where('del_time', 0) ->where('time', '>=', $monthStart) ->where('time', '<=', $endTime) ->where('is_work', CalendarDetails::TYPE_ONE) ->count(); if ($standardWorkDays <= 0) return [false, '工作日信息未设置']; // 2. 获取公司通用工时设置 (设备统一使用这个) $commonWorkMin = DB::table('work_range_details') ->where('top_depart_id', $topDepartId) ->where('del_time', 0) ->sum('total_work_min'); if ($commonWorkMin <= 0) return [false, '公司工作时段未设置']; // 3. 计算结果 $finalWorkMin = $standardWorkDays * $commonWorkMin; $result = []; foreach ($device_ids as $deviceId) { $result[$deviceId] = [ 'attendance_days' => (float)$standardWorkDays, // 设备没有请假加班,出勤天数即标准天数 'final_work_hour' => round($finalWorkMin / 60, 2), // 标准总工时转小时 ]; } return [true, $result]; } }