| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574 | <?phpnamespace App\Service;use App\Model\Construction;use App\Model\ConstructionInfo;use App\Model\Customer;use App\Model\CustomerInfo;use App\Model\Depart;use App\Model\Employee;use App\Model\EmployeeDepartPermission;use App\Model\OrderOperation;use App\Model\SalesOrder;use App\Model\SalesOrderInfo;use App\Model\ScheduleInfo;use App\Model\SeeRange;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, '必填参数不能为空!'];        try {            DB::beginTransaction();            if($data['type'] == 1){                list($status, $msg) = $this->fpSaleOrderMan($data,$user);            }elseif ($data['type'] == 2){                list($status, $msg) = $this->fpCustomerMan($data,$user);            }elseif($data['type'] == 3){                list($status, $msg) = $this->fpConstructionMan($data,$user);            }else{                return [false, '非法操作'];            }            if(! $status) {                DB::rollBack();                return [false, $msg];            }            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);        }        return [true, ''];    }    public function fpCustomerMan($data,$user){        if(! is_array($data['id'])) $data_id = [$data['id']];        else $data_id = $data['id'];        $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] ?? "";                if($emp_tmp) $emp_tmp .= "(负责人)";                foreach ($data_id as $c){                    //负责人累加                    $insert[] = [                        'customer_id' => $c,                        'data_id' => $value,                        'type' => CustomerInfo::type_two,                        'crt_time' => $time,                    ];                    if(! in_array($c, $xt)){                        //协同人累加                        $insert[] = [                            'customer_id' => $c,                            'data_id' => $user['id'],                            'type' => CustomerInfo::type_three,                            'crt_time' => $time,                        ];                    }//                    $customer_tmp = $customer_map[$c] ?? "";                    $send_data[] = [                        'employee_id' => $value,                        'type' => 2,                        'state' => 0,                        'menu_id' => 16,                        'order_number' => $c,                        'tmp_data' => [                            $c,                            "客户分配",                            '成功',                            $emp_tmp,                            date('Y-m-d H:i:s'),                        ],                    ];                }            }            CustomerInfo::insert($insert);            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]);            }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]);            }            (new OaService())->sendWxOaCheckMessage($send_data);        }        //客户被指派的门店 先清空        SeeRange::where('del_time',0)            ->whereIn('data_id', $data_id)            ->where('data_type',SeeRange::type_one)            ->where('type',SeeRange::data_three)            ->update(['del_time' => $time]);        if(! empty($data['fp_top_depart_id'])){            if(! is_array($data['fp_top_depart_id'])) $fp_top_depart_id = [$data['fp_top_depart_id']];            else $fp_top_depart_id = $data['fp_top_depart_id'];            $man = $data['man'] ?? [];            $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();            list($status,$msg) = $this->checkRule($man, $fp_top_depart_id, $list);            if(! $status) return [false, $msg];            $insert = [];            foreach ($data_id as $id){                foreach ($fp_top_depart_id as $value){                    $insert[] = [                        'data_id' => $id, //客户id                        'data_type' => SeeRange::type_one,                        'param_id' => $value, //门店id                        'type' => SeeRange::data_three,                        'crt_time' => $time,                    ];                }            }            if(! empty($insert)) SeeRange::insert($insert);        }        return [true, ''];    }    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]);        $send_data = [];        if(! empty($data['man'])){            $emp_map = Employee::whereIn('id',$data['man'])                ->pluck('emp_name','id')                ->toArray();            $insert = [];            foreach ($data['man'] as $value){                $emp_tmp = $emp_map[$value] ?? "";                if($emp_tmp) $emp_tmp .= "(施工员)";                $insert[] = [                    'construction_id' => $data['id'],                    'employee_id' => $value,                    'type' => ConstructionInfo::type_three,                    'crt_time' => $time,                    'opt_id' => $user['id'],                ];                $send_data[] = [                    'employee_id' => $value,                    'type' => 2,                    'state' => 0,                    'menu_id' => 34,                    'order_number' => $data['id'],                    'tmp_data' => [                        $data['id'],                        "施工分配",                        '成功',                        $emp_tmp,                        date('Y-m-d H:i:s'),                    ],                ];            }            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);        }        (new OaService())->sendWxOaCheckMessage($send_data);        return [true, ''];    }    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){                list($status, $msg) = $this->yjSaleOrderMan($data,$user);            }elseif ($data['type'] == 2){                list($status, $msg) = $this->yjCustomerMan($data,$user);            }else{                return [false, '非法操作'];            }            if(! $status) {                DB::rollBack();                return [false, $msg];            }            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);        }        return [true, ''];    }    public function yjCustomerMan($data,$user){        $time = time();        if(! is_array($data['id'])) $data_id = [$data['id']];        else $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){                foreach ($data_id as $c){                    $insert[] = [                        'customer_id' => $c,                        'data_id' => $value,                        'type' => CustomerInfo::type_two,                        'crt_time' => $time,                    ];                }            }            CustomerInfo::insert($insert);            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]);            }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]);            }        }        //客户被移交的门店 先清空        SeeRange::where('del_time',0)            ->whereIn('data_id', $data_id)            ->where('data_type',SeeRange::type_one)            ->where('type',SeeRange::data_three)            ->update(['del_time' => $time]);        if(! empty($data['fp_top_depart_id'])){            if(! is_array($data['fp_top_depart_id'])) $fp_top_depart_id = [$data['fp_top_depart_id']];            else $fp_top_depart_id = $data['fp_top_depart_id'];            $man = $data['man'] ?? [];            $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();            list($status,$msg) = $this->checkRule($man, $fp_top_depart_id, $list);            if(! $status) return [false, $msg];            $insert = [];            foreach ($data_id as $id){                foreach ($fp_top_depart_id as $value){                    $insert[] = [                        'data_id' => $id, //客户id                        'data_type' => SeeRange::type_one,                        'param_id' => $value, //门店id                        'type' => SeeRange::data_three,                        'crt_time' => $time,                    ];                }            }            if(! empty($insert)) SeeRange::insert($insert);        }        return [true, ''];    }    public function checkRule($man_id = [], $top_depart_id = [], $depart_list){        $depart = EmployeeDepartPermission::whereIn('employee_id', $man_id)            ->select('employee_id','depart_id')            ->get()->toArray();        $depart_has = [];        foreach ($depart as $value){            $p = $this->findTopLevelId($depart_list, $value['depart_id']);            if($p > 0) $depart_has[$value['employee_id']][] = $p;        }        if(empty($depart_has)) return [false, '无相关信息能获取到门店数据,校验失败'];        if(count($depart_has) == 1){            $intersection = array_values($depart_has);            $intersection = $intersection[0];        }else{            // 提取所有的子数组            $subArrays = array_map(function($item) {                return $item;            }, $depart_has);            // 使用 array_intersect 计算交集            $intersection = call_user_func_array('array_intersect', $subArrays);            if(empty($intersection)) return [false, '非法数据,请刷新网页重新操作'];        }        foreach ($top_depart_id as $value){            if(! in_array($value, $intersection)) return [false, '非法数据,请刷新网页重新操作'];        }        return [true, ''];    }    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];    }}
 |