scheduleRule($data, $user, false); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = Schedule::where('id',$data['id'])->first(); $model->start_time = $data['start_time'] ?? 0; $model->end_time = $data['end_time'] ?? 0; $model->mark = $data['mark'] ?? ''; $model->save(); $time = time(); ScheduleInfo::where('schedule_id',$data['id']) ->where('del_time',0) ->update(['del_time' => $time]); if(! empty($data['day_time'])){ $insert = []; foreach ($data['day_time'] as $value){ for ($i=0;$i<$value['num'];$i++){ foreach ($data['time_range'] as $range){ $insert[] = [ 'schedule_id' => $model->id, 'crt_time' => $time, 'day' => $range ?? 0, 'start_time' => $value['start_time'] ?? "", 'end_time' => $value['end_time'] ?? "", ]; } } } ScheduleInfo::insert($insert); } DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true,'']; } public function add($data,$user){ list($status,$msg) = $this->scheduleRule($data, $user); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = new Schedule(); $model->start_time = $data['start_time'] ?? 0; $model->end_time = $data['end_time'] ?? 0; $model->mark = $data['mark'] ?? ''; $model->crt_id = $user['id']; $model->depart_id = $data['depart_id'] ?? 0; $model->top_depart_id = $data['top_depart_id'] ?? 0; $model->save(); $time = time(); if(! empty($data['day_time'])){ $insert = []; foreach ($data['day_time'] as $value){ for ($i=0;$i<$value['num'];$i++){ foreach ($data['time_range'] as $range){ $insert[] = [ 'schedule_id' => $model->id, 'crt_time' => $time, 'day' => $range ?? 0, 'start_time' => $value['start_time'] ?? "", 'end_time' => $value['end_time'] ?? "", ]; } } } ScheduleInfo::insert($insert); } DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true,'']; } public function del($data){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; try { DB::beginTransaction(); $time = time(); Schedule::where('id',$data['id'])->update(['del_time' => $time]); ScheduleInfo::where('schedule_id',$data['id']) ->where('del_time',0) ->update(['del_time' => $time]); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true,'']; } public function detail($data,$user){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; $customer = ProductActivity::where('del_time',0) ->where('id',$data['id']) ->first(); if(empty($customer)) return [false,'活动不存在或已被删除']; $customer = $customer->toArray(); $detail = ProductActivityPrice::where('del_time',0) ->where('product_activity_id',$data['id']) ->get()->toArray(); $title_map = BasicType::whereIn('id',array_column($detail,'basic_type_id')) ->pluck('title','id') ->toArray(); $customer['product'] = []; //产品字典 $map = (new ProductService())->getProductDetail(array_unique(array_column($detail,'product_id'))); foreach ($detail as $value){ $basic_tmp = [ 'basic_type_id' => $value['basic_type_id'], 'basic_type_title' => $title_map[$value['basic_type_id']] ?? '', 'price' => $value['price'], ]; if(! isset($customer['product'][$value['product_id']])){ $tmp = $map[$value['product_id']] ?? []; $tmp['activity'][] = $basic_tmp; $customer['product'][$value['product_id']] = $tmp; }else{ $customer['product'][$value['product_id']]['activity'][] = $basic_tmp; } } $customer['product'] = array_values($customer['product']); $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']): ''; return [true, $customer]; } public function getList($data,$user){ $model = ProductActivity::TopClear($user,$data); $model = $model->where('del_time',0) ->select('id','title','crt_id','mark','start_time','end_time','crt_time','type') ->orderby('id', 'desc'); if(! empty($data['type'])) $model->where('type', $data['type']); if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%'); if(! empty($data['mark'])) $model->where('mark', 'LIKE', '%'.$data['mark'].'%'); 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['activity_time'][0]) && ! empty($data['activity_time'][1])) { $start_time = $this->changeDateToDateMin($data['activity_time'][0]); $end_time = $this->changeDateToDateMin($data['activity_time'][1]); $model->where('start_time','>=',$start_time); $model->where('end_time','<=',$end_time); } $list = $this->limit($model,'',$data); $list = $this->fillData($list,$user); return [true, $list]; } public function scheduleRule(&$data, $user, $is_add = true){ if(empty($data['schedule'][0]) || empty($data['schedule'][1])) return [false,'排班日期不能为空']; $data['start_time'] = $this->changeDateToDate($data['schedule'][0]); $data['end_time'] = $this->changeDateToDate($data['schedule'][1]); $data['time_range'] = $this->getTimestampRange($data['start_time'],$data['end_time']); if(empty($data['day_time'])) return [false,'排班时间段不能为空']; foreach ($data['day_time'] as $key => $value){ if(empty($value['start_time'])) return [false,'排班时间段开始时间不能为空']; if(empty($value['end_time'])) return [false,'排班时间段结束时间不能为空']; if(empty($value['num'])) return [false,'排班车间数量不能为空']; } if($is_add){ $bool = Schedule::where('del_time',0) ->where('start_time', '<=', $data['end_time']) ->where('end_time', '>=', $data['start_time']) ->exists(); }else{ if(empty($data['id'])) return [false,'ID不能为空']; $bool = Schedule::where('del_time',0) ->where('id','<>',$data['id']) ->where('start_time', '<=', $data['end_time']) ->where('end_time', '>=', $data['start_time']) ->exists(); } if($bool) return [false,'排班日期已经设置,请不要重复设置!']; //所属部门 以及 顶级部门 if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user); $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0; return [true, $data]; } public function fillData($data, $user){ if(empty($data['data'])) return $data; $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id'))) ->pluck('emp_name','id') ->toArray(); foreach ($data['data'] as $key => $value){ $start_time = $value['start_time'] ? date("Y-m-d H:i",$value['start_time']) : ''; $end_time = $value['end_time'] ? date("Y-m-d H:i",$value['end_time']) : ''; $data['data'][$key]['activity_time'] = $start_time . '——' . $end_time; $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]['type_title'] = ProductActivity::$type_name[$value['type']] ?? ''; } return $data; } public function getTimestampRange($startTimestamp, $endTimestamp){ // 将时间戳转换为日期格式 $startDate = date('Y-m-d', $startTimestamp); $endDate = date('Y-m-d', $endTimestamp); // 创建日期范围 $dateRange = []; $currentDate = strtotime($startDate); while ($currentDate <= strtotime($endDate)) { $dateRange[] = $currentDate; $currentDate = strtotime('+1 day', $currentDate); } // 将每个日期设置为当天的 0 点 0 分 0 秒并转换为时间戳 $zeroTimestamps = []; foreach ($dateRange as $date) { $zeroTimestamps[] = strtotime(date('Y-m-d 00:00:00', $date)); } return $zeroTimestamps; } }