| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- <?php
- namespace App\Service;
- use App\Model\Calendar;
- use App\Model\CalendarDetails;
- use App\Model\Device;
- use App\Model\DeviceDetails;
- use App\Model\Employee;
- use App\Model\EmployeeDetails;
- use App\Model\Item;
- use App\Model\ItemDetails;
- use Illuminate\Support\Facades\DB;
- class ItemService extends Service
- {
- public function itemEdit($data,$user){
- list($status,$msg) = $this->itemRule($data, $user, false);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = Item::where('id',$data['id'])->first();
- $model->code = $data['code'] ?? '';
- $model->title = $data['title'] ?? '';
- $model->mark = $data['mark'] ?? "";
- $model->start_time = $data['start_time'] ?? 0;
- $model->end_time = $data['end_time'] ?? 0;
- $model->is_use = $data['is_use'] ?? 0;
- $model->save();
- $time = time();
- ItemDetails::where('del_time',0)
- ->where('item_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 itemAdd($data,$user){
- list($status,$msg) = $this->itemRule($data, $user);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = new Item();
- $model->code = $data['code'] ?? '';
- $model->title = $data['title'] ?? '';
- $model->mark = $data['mark'] ?? "";
- $model->start_time = $data['start_time'] ?? 0;
- $model->end_time = $data['end_time'] ?? 0;
- $model->is_use = $data['is_use'] ?? 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['man_list'])){
- $unit = [];
- foreach ($data['man_list'] as $value){
- $unit[] = [
- 'item_id' => $id,
- 'type' => $value['type'],
- 'data_id' => $value['data_id'],
- 'crt_time' => $time,
- ];
- }
- if(! empty($unit)) ItemDetails::insert($unit);
- }
- if(! empty($data['device_list'])){
- $receipt = [];
- foreach ($data['device_list'] as $value){
- $receipt[] = [
- 'item_id' => $id,
- 'type' => $value['type'],
- 'data_id' => $value['data_id'],
- 'crt_time' => $time,
- ];
- }
- if(! empty($receipt)) ItemDetails::insert($receipt);
- }
- }
- private function getDetail($id){
- $data = ItemDetails::where('del_time',0)
- ->where('item_id', $id)
- ->get()->toArray();
- $id = $id2 = [];
- foreach ($data as $value){
- if($value['type'] == ItemDetails::type_one) {
- $id[] = $value['data_id'];
- }else{
- $id2[] = $value['data_id'];
- }
- }
- $map = Employee::whereIn('id', $id)->select('emp_name','id','number')->get()->toArray();
- $map = array_column($map,null,'id');
- $map2 = Device::whereIn('id', $id2)->select('code','id','title')->get()->toArray();
- $map2 = array_column($map2,null,'id');
- $unit = $receipt = [];
- foreach ($data as $value){
- if($value['type'] == ItemDetails::type_one) {
- $tmp = $map[$value['data_id']] ?? [];
- $unit[] = [
- 'type' => $value['type'],
- 'data_id' => $value['data_id'],
- 'data_title' => $tmp['emp_name'],
- 'data_code' => $tmp['number'],
- ];
- }else{
- $tmp = $map2[$value['data_id']] ?? [];
- $receipt[] = [
- 'type' => $value['type'],
- 'data_id' => $value['data_id'],
- 'data_title' => $tmp['title'] ?? "",
- 'data_code' => $tmp['code'] ?? "",
- ];
- }
- }
- $detail = [
- 'man_list' => $unit,
- 'device_list' => $receipt,
- ];
- foreach ($detail as $key => $value) {
- if (empty($value)) {
- $detail[$key] = (object)[]; // 转成 stdClass 对象
- }
- }
- return $detail;
- }
- public function getItemMap($ids){
- if(empty($ids)) return [];
- if(! is_array($ids)) $ids = [$ids];
- return Item::whereIn('id', $ids)
- ->pluck('title', 'id')
- ->toArray();
- }
- public function itemDel($data){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- try {
- DB::beginTransaction();
- $time = time();
- Item::where('del_time',0)
- ->whereIn('id',$data['id'])
- ->update(['del_time' => $time]);
- ItemDetails::where('del_time',0)
- ->where('item_id', $data['id'])
- ->update(['del_time' => $time]);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- public function itemDetail($data, $user){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- $customer = Item::where('del_time',0)
- ->where('id',$data['id'])
- ->first();
- if(empty($customer)) return [false,'项目不存在或已被删除'];
- $customer = $customer->toArray();
- $customer['start_time'] = ! empty($customer['start_time']) ? date("Y-m-d", $customer['start_time']) : "";
- $customer['end_time'] = ! empty($customer['end_time']) ? date("Y-m-d", $customer['end_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 itemCommon($data,$user, $field = []){
- if(empty($field)) $field = Item::$field;
- $model = Item::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['type'])) $model->where('type', $data['type']);
- if(! empty($data['id'])) $model->whereIn('id', $data['id']);
- if(! empty($data['is_use'])) $model->where('is_use', $data['is_use']);
- 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 itemList($data,$user){
- $model = $this->itemCommon($data, $user);
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list);
- return [true, $list];
- }
- public function itemRule(&$data, $user, $is_add = true){
- if(empty($data['code'])) return [false, '编码不能为空'];
- if(empty($data['title'])) return [false, '名称不能为空'];
- if(! empty($data['start_time'])) $data['start_time'] = $this->changeDateToDate($data['start_time']);
- if(! empty($data['end_time'])) $data['end_time'] = $this->changeDateToDate($data['end_time'],true);
- if(empty($data['is_use'])) return [false, '是否启用不能为空'];
- if(! isset(Item::Use[$data['is_use']])) return [false, '是否启用错误'];
- if(empty($data['man_list'])) return [false, '研发人员不能为空'];
- foreach ($data['man_list'] as $value){
- if(empty($value['type'])) return [false, '类型不能为空'];
- if(empty($value['data_id'])) return [false, '研发人员不能为空'];
- }
- list($status, $msg) = $this->checkArrayRepeat($data['man_list'],'data_id','研发人员');
- if(! $status) return [false, $msg];
- if(! empty($data['device_list'])){
- foreach ($data['device_list'] as $value){
- if(empty($value['type'])) return [false, '类型不能为空'];
- if(empty($value['data_id'])) return [false, '数据ID不能为空'];
- }
- }
- if($is_add){
- $bool = Item::where('code',$data['code'])
- ->where('crt_id', $user['id'])
- ->where('del_time',0)
- ->exists();
- }else{
- if(empty($data['id'])) return [false,'ID不能为空'];
- $bool = Item::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]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
- $data['data'][$key]['end_time'] = $value['end_time'] ? date('Y-m-d',$value['end_time']) : '';
- $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
- $data['data'][$key]['is_use_title'] = Item::Use[$value['is_use']] ?? "";
- }
- return $data;
- }
- //--------------------------------------------------------
- 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->each_day_hours = $data['each_day_hours'] ?? 0;
- $model->total_hours = $data['total_hours'] ?? 0;
- $model->save();
- $time = time();
- CalendarDetails::where('del_time',0)
- ->where('calendar_id', $model->id)
- ->update(['del_time' => $time]);
- $this->saveDetail1($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->each_day_hours = $data['each_day_hours'] ?? 0;
- $model->total_hours = $data['total_hours'] ?? 0;
- $model->crt_id = $user['id'];
- $model->save();
- $this->saveDetail1($model->id, time(), $data);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- private function saveDetail1($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,
- ];
- }
- if(! empty($unit)) CalendarDetails::insert($unit);
- }
- //更新人员满勤工时 设备满勤工时
- EmployeeDetails::where('del_time',0)
- ->where('time',$data['time'])
- ->update(['del_time' => $time]);
- $employee = Employee::where('del_time',0)
- ->select('id')
- ->get()->toArray();
- $employee_details = [];
- foreach ($employee as $value){
- $employee_details[] = [
- 'employee_id' => $value['id'],
- 'time' => $data['time'],
- // 'work_days' => $data['work_days'],
- // 'each_day_hours' => $data['each_day_hours'],
- 'total_hours' => $data['total_hours'],
- 'total_hours_2' => $data['total_hours'],
- 'crt_time' => $time,
- ];
- }
- if(! empty($employee_details)) EmployeeDetails::insert($employee_details);
- DeviceDetails::where('del_time',0)
- ->where('time',$data['time'])
- ->update(['del_time' => $time]);
- $device = Device::where('del_time',0)
- ->where('is_use', 1)
- ->select('id')
- ->get()->toArray();
- $device_details = [];
- foreach ($device as $value){
- $device_details[] = [
- 'device_id' => $value['id'],
- 'time' => $data['time'],
- // 'work_days' => $data['work_days'],
- // 'each_day_hours' => $data['each_day_hours'],
- 'total_hours' => $data['total_hours'],
- 'total_hours_2' => $data['total_hours'],
- 'crt_time' => $time,
- ];
- }
- if(! empty($device_details)) DeviceDetails::insert($device_details);
- }
- private function getDetail1($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)
- ->where('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->getDetail1($data['id']);
- $customer = array_merge($customer, $details);
- return [true, $customer];
- }
- public function calendarCommon($data,$user, $field = []){
- if(empty($field)) $field = Calendar::$field;
- $model = Calendar::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->fillData1($list,$user,$data);
- return [true, $list];
- }
- public function calendarRule(&$data, $user, $is_add = true){
- 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']];
- $res = $this->checkNumber($data['each_day_hours'],0,'positive');
- if(! $res['valid']) return [false,'每日工时:' . $res['error']];
- $res = $this->checkNumber($data['total_hours'],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, '是否工作日不能为空'];
- }
- }
- if($is_add){
- $bool = Calendar::where('time', $data['time'])
- ->where('del_time',0)
- ->exists();
- }else{
- if(empty($data['id'])) return [false,'ID不能为空'];
- $bool = Calendar::where('time',$data['time'])
- ->where('id','<>',$data['id'])
- ->where('del_time',0)
- ->exists();
- }
- if($bool) return [false, '该年月下的设置已存在'];
- return [true, $data];
- }
- public function fillData1($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;
- }
- }
|