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->type_2 = $data['type_2'] ?? 0; $model->in_time = $data['in_time'] ?? 0; $model->power = $data['power'] ?? ''; $model->number = $data['number'] ?? 0; $model->save(); $time = time(); DeviceDetails::where('del_time',0) ->where('device_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->type_2 = $data['type_2'] ?? 0; $model->in_time = $data['in_time'] ?? 0; $model->power = $data['power'] ?? ''; $model->number = $data['number'] ?? 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[] = [ 'device_id' => $id, 'time' => $value['time'], 'total_hours' => $value['total_hours'], 'total_hours_2' => $value['total_hours_2'], 'crt_time' => $time, ]; } if(! empty($unit)) DeviceDetails::insert($unit); } } private function getDetail($id){ $data = DeviceDetails::where('del_time',0) ->where('device_id', $id) ->get()->toArray(); $unit = []; foreach ($data as $value){ $unit[] = [ 'time' => date("Y-m",$value['time']), 'total_hours' => $value['total_hours'], 'total_hours_2' => $value['total_hours_2'], ]; } $detail = [ 'details' => $unit, ]; foreach ($detail as $key => $value) { if (empty($value)) { $detail[$key] = (object)[]; // 转成 stdClass 对象 } } 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]); DeviceDetails::where('del_time',0) ->where('device_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('emp_name'); $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): ''; $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::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['type_2'])) $model->where('type_2', $data['type_2']); 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); return [true, $list]; } public function deviceRule(&$data, $user, $is_add = true){ if(empty($data['code'])) return [false, '编码不能为空']; if(empty($data['title'])) 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(! empty($data['in_time'])) $data['in_time'] = $this->changeDateToDate($data['in_time']); if(empty($data['type_2'])) return [false, '类型不能为空']; if(! isset(Device::$type_2[$data['type_2']])) return [false, '类型错误']; if(! empty($data['details'])){ list($status, $msg) = $this->checkArrayRepeat($data['details'],'time','设备年月'); if(! $status) return [false, $msg]; foreach ($data['details'] as $key => $value){ if(empty($value['time'])) return [false, '设备年月不能为空']; $data['details'][$key]['time'] = $this->changeDateToMonth($value['time']); $res = $this->checkNumber($value['total_hours'],0,'positive'); if(! $res['valid']) return [false,'满勤工时:' . $res['error']]; $res = $this->checkNumber($value['total_hours_2'],0,'positive'); if(! $res['valid']) return [false,'实际工时:' . $res['error']]; } } if($is_add){ $bool = Device::where('code',$data['code']) ->where('crt_id', $user['id']) ->where('del_time',0) ->exists(); }else{ if(empty($data['id'])) return [false,'ID不能为空']; $bool = Device::where('code',$data['code']) ->where('crt_id', $user['id']) ->where('id','<>',$data['id']) ->where('del_time',0) ->exists(); } if($bool) return [false, '编码已存在']; return [true, $data]; } public function fillData($data){ 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]['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']] ?? ''; $data['data'][$key]['type_2_title'] = Device::$type_2[$value['type_2']] ?? ''; } return $data; } }