| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 | <?phpnamespace App\Service;use App\Model\Construction;use App\Model\ConstructionInfo;use App\Model\Customer;use App\Model\CustomerInfo;use App\Model\Employee;use App\Model\OrderOperation;use App\Model\SalesOrder;use App\Model\SalesOrderInfo;use App\Model\ScheduleInfo;use Illuminate\Support\Facades\DB;class DeleteService extends Service{    public function getMan($data,$user){        if(empty($data['id']) || empty($data['type']) || empty($data['man_type'])) return [false, '必填参数不能为空!'];        $return = [];        if($data['type'] == 1){            $return = $this->getSaleOrderMan($data);        }        return [true, $return];    }    public function delete($data,$user){        if(empty($data['id']) || empty($data['type']) || empty($data['man_type']) || empty($data['man'])) return [false, '必填参数不能为空!'];        try {            DB::beginTransaction();            if($data['type'] == 1){                $this->delSaleOrderMan($data,$user);            }            DB::commit();        }catch (\Exception $exception){            DB::rollBack();            return [false,$exception->getMessage()];        }        return [true,''];    }    public function getSaleOrderMan($data){        $man_id = SalesOrderInfo::where('del_time',0)            ->where('sales_order_id',$data['id'])            ->where('type',$data['man_type'])            ->get('data_id')->toArray();        $man_id = array_column($man_id,'data_id');        return Employee::whereIn('id',$man_id)->select('id', 'emp_name')->get()->toArray();    }    public function delSaleOrderMan($data,$user){         $time = time();         SalesOrderInfo::where('del_time',0)             ->where('sales_order_id',$data['id'])             ->where('type',$data['man_type'])             ->update(['del_time' => $time]);         if(! empty($data['man'])){             $insert = [];             foreach ($data['man'] as $value){                 $insert[] = [                     'sales_order_id' => $data['id'],                     'data_id' => $value,                     'type' => $data['man_type'],                     'crt_time' => $time,                 ];             }             SalesOrderInfo::insert($insert);             $type = 0;             if($data['man_type'] == SalesOrderInfo::type_two) $type = OrderOperation::sixty;             if($data['man_type'] == SalesOrderInfo::type_three) $type = OrderOperation::seventeen;             $order = SalesOrder::where('id',$data['id'])->first();             $order = $order->toArray();             if($type){                 (new OrderOperationService())->add([                     'order_number' => $order['order_number'],                     'msg' => OrderOperation::$type[$type] ?? "",                     'type' => $type                 ],$user);             }         }    }    public function fp($data,$user){        if(empty($data['id']) || empty($data['type']) || empty($data['man'])) return [false, '必填参数不能为空!'];//        $key = "fpyj".$data['id'].$data['type'];//        list($status,$msg) = $this->limitingSendRequestBackgExpire($key);//        if(! $status) return [false,$msg];        try {            DB::beginTransaction();            if($data['type'] == 1){                $this->fpSaleOrderMan($data,$user);            }elseif ($data['type'] == 2){                $this->fpCustomerMan($data,$user);            }elseif($data['type'] == 3){                $this->fpConstructionMan($data,$user);            }            DB::commit();        }catch (\Exception $exception){            DB::rollBack();            return [false,$exception->getMessage()];        }        return [true,''];    }    public function fpSaleOrderMan($data,$user){        $time = time();        if(! empty($data['man'])){            $insert = [];            foreach ($data['man'] as $value){                $insert[] = [                    'sales_order_id' => $data['id'],                    'data_id' => $value,                    'type' => SalesOrderInfo::type_two,                    'crt_time' => $time,                ];            }            SalesOrderInfo::insert($insert);            $order = SalesOrder::where('id',$data['id'])->first();            $order = $order->toArray();            (new OrderOperationService())->add([                'order_number' => $order['order_number'],                'msg' => OrderOperation::$type[OrderOperation::eighteen] ?? "",                'type' => OrderOperation::eighteen            ],$user);        }    }    public function fpCustomerMan($data,$user){        $time = time();        if(! empty($data['man'])){            $insert = [];            //协同人            $xt = CustomerInfo::where('del_time',0)                ->whereIn('customer_id',$data['id'])                ->where('type',CustomerInfo::type_three)                ->where('data_id',$user['id'])                ->select('customer_id')->get()->toArray();            $xt = array_column($xt,'customer_id');            $customer_map = Customer::whereIn('id',$data['id'])                ->pluck('title','id')                ->toArray();            $emp_map = Employee::whereIn('id',$data['man'])                ->pluck('emp_name','id')                ->toArray();            //负责人清除            CustomerInfo::where('del_time',0)                ->whereIn('customer_id',$data['id'])                ->where('type', CustomerInfo::type_two)                ->update(['del_time' => $time]);            $send_data = [];            foreach ($data['man'] as $value){                $emp_tmp = $emp_map[$value] ?? "";                foreach ($data['id'] as $data_id){                    //负责人累加                    $insert[] = [                        'customer_id' => $data_id,                        'data_id' => $value,                        'type' => CustomerInfo::type_two,                        'crt_time' => $time,                    ];                    if(! in_array($data_id, $xt)){                        //协同人累加                        $insert[] = [                            'customer_id' =>$data_id,                            'data_id' => $user['id'],                            'type' => CustomerInfo::type_three,                            'crt_time' => $time,                        ];                    }                    $customer_tmp = $customer_map[$data_id] ?? "";                    $send_data[] = [                        'employee_id' => $value,                        'type' => 2,                        'state' => 0,                        'menu_id' => 16,                        'order_number' => $customer_tmp,                        'tmp_data' => [                            $customer_tmp,                            $emp_tmp,                            date('Y-m-d H:i:s'),                        ],                    ];                }            }            CustomerInfo::insert($insert);            $fp_top_depart_id = $data['fp_top_depart_id'] ?? 0;            if(! is_array($data['id'])){                $title = Customer::where('id',$data['id'])->value('title') ?? "";                (new OrderOperationService())->add([                    'order_number' => Customer::$order_number . "|" . $data['id'] . "|" . $title,                    'msg' => OrderOperation::$type[OrderOperation::twenty_three] ?? "",                    'type' => OrderOperation::twenty_three                ],$user);                Customer::where('id',$data['id'])->update(['fp_time' => $time, 'fp_top_depart_id' => $fp_top_depart_id]);            }else{                $title = Customer::whereIn('id',$data['id'])->pluck('title','id')->toArray();                foreach($data['id'] as $value){                    $t_title = $title[$value] ?? "";                    (new OrderOperationService())->add([                        'order_number' => Customer::$order_number . "|" . $value . "|" . $t_title,                        'msg' => OrderOperation::$type[OrderOperation::twenty_three] ?? "",                        'type' => OrderOperation::twenty_three                    ],$user);                }                Customer::whereIn('id',$data['id'])->update(['fp_time' => $time,'fp_top_depart_id' => $fp_top_depart_id]);            }            (new OaService())->sendWxOaCheckMessage($send_data);        }    }    public function fpConstructionMan($data,$user){        $time = time();        //负责人清除        ConstructionInfo::where('del_time',0)            ->where('construction_id',$data['id'])            ->where('type', ConstructionInfo::type_three)            ->update(['del_time' => $time]);        if(! empty($data['man'])){            $insert = [];            foreach ($data['man'] as $value){                $insert[] = [                    'construction_id' => $data['id'],                    'employee_id' => $value,                    'type' => ConstructionInfo::type_three,                    'crt_time' => $time,                    'opt_id' => $user['id'],                ];            }            ConstructionInfo::insert($insert);            $order = Construction::where('id',$data['id'])->first();            $order = $order->toArray();            (new OrderOperationService())->add([                'order_number' => $order['order_number'],                'msg' => OrderOperation::$type[OrderOperation::twenty_six] ?? "",                'type' => OrderOperation::twenty_six            ],$user);        }    }    public function yj($data,$user){        if(empty($data['id']) || empty($data['type']) || empty($data['man'])) return [false, '必填参数不能为空!'];//        $key = "fpyj".$data['id'].$data['type'];//        list($status,$msg) = $this->limitingSendRequestBackgExpire($key);//        if(! $status) return [false,$msg];        try {            DB::beginTransaction();            if($data['type'] == 1){                $this->yjSaleOrderMan($data,$user);            }elseif ($data['type'] == 2){                $this->yjCustomerMan($data,$user);            }            DB::commit();        }catch (\Exception $exception){            DB::rollBack();            return [false,$exception->getMessage()];        }        return [true,''];    }    public function yjSaleOrderMan($data,$user){        $time = time();        SalesOrderInfo::where('del_time',0)            ->where('sales_order_id',$data['id'])            ->where('type', SalesOrderInfo::type_two)            ->where('data_id', $user['id'])            ->update(['del_time' => $time]);        if(! empty($data['man'])){            $insert = [];            foreach ($data['man'] as $value){                $insert[] = [                    'sales_order_id' => $data['id'],                    'data_id' => $value,                    'type' => SalesOrderInfo::type_two,                    'crt_time' => $time,                ];            }            SalesOrderInfo::insert($insert);            $order = SalesOrder::where('id',$data['id'])->first();            $order = $order->toArray();            (new OrderOperationService())->add([                'order_number' => $order['order_number'],                'msg' => OrderOperation::$type[OrderOperation::nineteen] ?? "",                'type' => OrderOperation::nineteen            ],$user);        }    }    public function yjCustomerMan($data,$user){        $time = time();        if(! is_array($data['id'])) $data['id'] = [$data['id']];        CustomerInfo::where('del_time',0)            ->whereIn('customer_id',$data['id'])            ->where('type', CustomerInfo::type_two)            ->update(['del_time' => $time]);        if(! empty($data['man'])){            $insert = [];            foreach ($data['man'] as $value){               if(! is_array($data['id'])){                   $insert[] = [                       'customer_id' => $data['id'],                       'data_id' => $value,                       'type' => CustomerInfo::type_two,                       'crt_time' => $time,                   ];               }else{                   foreach ($data['id'] as $data_id){                       $insert[] = [                           'customer_id' => $data_id,                           'data_id' => $value,                           'type' => CustomerInfo::type_two,                           'crt_time' => $time,                       ];                   }               }            }            CustomerInfo::insert($insert);            $fp_top_depart_id = $data['fp_top_depart_id'] ?? 0;            if(! is_array($data['id'])){                $title = Customer::where('id',$data['id'])->value('title') ?? "";                (new OrderOperationService())->add([                    'order_number' => Customer::$order_number . "|" . $data['id'] . "|" . $title,                    'msg' => OrderOperation::$type[OrderOperation::twenty_four] ?? "",                    'type' => OrderOperation::twenty_four                ],$user);                Customer::where('id',$data['id'])->update(['fp_time' => $time, 'fp_top_depart_id' => $fp_top_depart_id]);            }else{                $title = Customer::whereIn('id',$data['id'])->pluck('title','id')->toArray();                foreach($data['id'] as $value){                    $t_title = $title[$value] ?? "";                    (new OrderOperationService())->add([                        'order_number' => Customer::$order_number . "|" . $value . "|" . $t_title,                        'msg' => OrderOperation::$type[OrderOperation::twenty_four] ?? "",                        'type' => OrderOperation::twenty_four                    ],$user);                }                Customer::whereIn('id',$data['id'])->update(['fp_time' => $time,'fp_top_depart_id' => $fp_top_depart_id]);            }        }    }    public function pq($data,$user){        list($status,$msg) = $this->pqRule($data,$user);        if(! $status) return [false,$msg];        try {            DB::beginTransaction();            if(! empty($msg['schedule_id']) && ! empty($msg['day_start_stamp']) && ! empty($msg['day_end_stamp'])){                //如果已经设置过排期 将子表占用数据释放                $schedule = ScheduleInfo::where('del_time',0)                    ->where('schedule_id',$msg['schedule_id'])                    ->where('start_time',$msg['day_start_stamp'])                    ->where('end_time',$msg['day_end_stamp'])                    ->where('is_use','>', ScheduleInfo::not_use)                    ->first();                if(! empty($schedule)) ScheduleInfo::where('id',$schedule->id)->update(['is_use' => ScheduleInfo::not_use]);            }            Construction::where('id',$data['id'])->update([                'schedule_id' => $data['schedule_id'],                'day_stamp' => $data['day_stamp'],                'day_start_stamp' => $data['day_start_stamp'],                'day_end_stamp' => $data['day_end_stamp'],                'pq_state' => Construction::STATE_ONE            ]);            if(! empty($data['schedule_info_id'])) ScheduleInfo::where('id',$data['schedule_info_id'])->update(['is_use' => 1]);            DB::commit();        }catch (\Exception $exception){            $this->dellimitingSendRequestBackg(ScheduleInfo::limit_key . $data['schedule_info_id']);            DB::rollBack();            return [false,$exception->getMessage()];        }        $this->dellimitingSendRequestBackg(ScheduleInfo::limit_key . $data['schedule_info_id']);        return [true,''];    }    public function pqRule(&$data,$user){        if(empty($data['id'])) return [false, '施工单不能为空!'];        $construction = Construction::where('id',$data['id'])->where('del_time',0)->first();        if(empty($construction)) return [false,'施工单不存在或已被删除'];        $construction = $construction->toArray();        if($construction['state'] != Construction::STATE_TWO) return [false,'施工单未确认无法排期!'];        if(empty($data['schedule_id']) ||empty($data['day_stamp']) || empty($data['day_start_stamp']) || empty($data['day_end_stamp'])) return [false,'排班时间信息不能为空'];        //day_stamp 日期的时间戳(0点的) day_start_stamp 日期加上开始时分的时间戳  day_end_stamp 日期加上结束时分的时间戳        if($construction['day_stamp'] != $data['day_stamp'] || $construction['day_start_stamp'] != $data['day_start_stamp'] && $construction['day_end_stamp'] != $data['day_end_stamp']) {            $schedule_info = ScheduleInfo::where('del_time',0)                ->where('day',$data['day_stamp'])                ->where('start_time',$data['day_start_stamp'])                ->where('end_time',$data['day_end_stamp'])                ->where('is_use',ScheduleInfo::not_use)                ->first();            if(empty($schedule_info)) return [false,'该时间段排班已满或不存在!'];            $data['schedule_info_id'] = $schedule_info->id;            list($status,$msg) = $this->limitingSendRequestBackg(ScheduleInfo::limit_key . $data['schedule_info_id']);            if(! $status) return [false,'操作频繁,请稍等!'];        }else{            return [false,'排期未做更新!'];        }        return [true, $construction];    }}
 |