| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- namespace App\Service;
- use App\Model\CalendarDetails;
- use App\Model\Employee;
- use App\Model\Device;
- use Illuminate\Support\Facades\DB;
- class DeviceService extends Service
- {
- public function deviceEdit($data,$user){
- list($status,$msg) = $this->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'] ?? 0;
- $model->original_value = $data['original_value'] ?? 0;
- $model->initial_value = $data['initial_value'] ?? 0;
- $model->save();
- 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'] ?? 0;
- $model->original_value = $data['original_value'] ?? 0;
- $model->initial_value = $data['initial_value'] ?? 0;
- $model->top_depart_id = $data['top_depart_id'] ?? 0;
- $model->crt_id = $user['id'];
- $model->save();
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- 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]);
- 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['in_time'] = $customer['in_time'] ? date("Y-m-d",$customer['in_time']): '';
- return [true, $customer];
- }
- public function deviceCommon($data,$user, $field = []){
- if(empty($field)) $field = Device::$field;
- $model = Device::Clear($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);
- 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(! 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];
- }
- }
|