| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 | <?phpnamespace App\Service;use App\Model\BasicType;use App\Model\Contacts;use App\Model\ContactsInfo;use App\Model\Employee;use Illuminate\Support\Facades\DB;class ContactsService extends Service{    public function contactsEdit($data,$user){        list($status,$msg) = $this->contactsRule($data,false);        if(!$status) return [$status,$msg];        try {            DB::beginTransaction();            $model = Contacts::where('id',$data['id'])->first();            $model->img = $data['img'] ?? '';            $model->title = $data['title'];            $model->mailbox = $data['mailbox'] ?? '';            $model->qq = $data['qq'] ?? '';            $model->postal_code = $data['postal_code'] ?? '';            $model->sex = $data['sex'] ?? '';            $model->gregorian_bir = $data['gregorian_bir'] ?? '';            $model->lunar_bir = $data['lunar_bir'] ?? '';            $model->address1 = $data['address1'] ?? '';            $model->address2 = $data['address2'] ?? '';            $model->hobby = $data['hobby'] ?? '';            $model->mark = $data['mark'] ?? '';            $model->importance = $data['importance'] ?? '';            $model->intimacy = $data['intimacy'] ?? '';            $model->depart = $data['depart'] ?? '';            $model->depart_id = $data['depart_id'] ?? '';            $model->job = $data['job'] ?? '';            $model->decision = $data['decision'] ?? 0;            $model->grade = $data['grade'] ?? 0;            $model->save();            $time = time();            ContactsInfo::where('del_time',0)                ->where('contacts_id',$data['id'])                ->update(['del_time' => $time]);            if(! empty($data['contacts'])){                $insert = [];                foreach ($data['contacts'] as $value){                    $insert[] = [                        'contacts_id' => $model->id,                        'contact_type' => $value['id'],                        'contact_info' => $value['info'],                        'type' => ContactsInfo::type_one,                        'crt_time' => $time,                    ];                }                ContactsInfo::insert($insert);            }            if(! empty($data['customers'])){                $insert = [];                foreach ($data['customers'] as $value){                    $insert[] = [                        'contacts_id' => $model->id,                        'employee_id' => $value,                        'type' => ContactsInfo::type_two,                        'crt_time' => $time,                    ];                }                ContactsInfo::insert($insert);            }            if(! empty($data['employee_one'])){                $insert = [];                foreach ($data['employee_one'] as $value){                    $insert[] = [                        'contacts_id' => $model->id,                        'employee_id' => $value,                        'type' => ContactsInfo::type_three,                        'crt_time' => $time,                    ];                }                ContactsInfo::insert($insert);            }            if(! empty($data['employee_two'])){                $insert = [];                foreach ($data['employee_two'] as $value){                    $insert[] = [                        'contacts_id' => $model->id,                        'employee_id' => $value,                        'type' => ContactsInfo::type_four,                        'crt_time' => $time,                    ];                }                ContactsInfo::insert($insert);            }            DB::commit();        }catch (\Exception $exception){            DB::rollBack();            return [false,$exception->getMessage()];        }        return [true,''];    }    public function contactsAdd($data,$user){        list($status,$msg) = $this->contactsRule($data);        if(!$status) return [$status,$msg];        try {            DB::beginTransaction();            $model = new Contacts();            $model->img = $data['img'] ?? '';            $model->title = $data['title'];            $model->mailbox = $data['mailbox'] ?? '';            $model->qq = $data['qq'] ?? '';            $model->postal_code = $data['postal_code'] ?? '';            $model->sex = $data['sex'] ?? '';            $model->gregorian_bir = $data['gregorian_bir'] ?? '';            $model->lunar_bir = $data['lunar_bir'] ?? '';            $model->address1 = $data['address1'] ?? '';            $model->address2 = $data['address2'] ?? '';            $model->hobby = $data['hobby'] ?? '';            $model->crt_id = $user['id'];            $model->mark = $data['mark'] ?? '';            $model->importance = $data['importance'] ?? '';            $model->intimacy = $data['intimacy'] ?? '';            $model->depart = $data['depart'] ?? '';            $model->depart_id = $data['depart_id'] ?? '';            $model->job = $data['job'] ?? '';            $model->decision = $data['decision'] ?? 0;            $model->grade = $data['grade'] ?? 0;            $model->save();            $time = time();            if(! empty($data['contacts'])){                $insert = [];                foreach ($data['contacts'] as $value){                    $insert[] = [                        'contacts_id' => $model->id,                        'contact_type' => $value['id'],                        'contact_info' => $value['info'],                        'type' => ContactsInfo::type_one,                        'crt_time' => $time,                    ];                }                ContactsInfo::insert($insert);            }            if(! empty($data['customers'])){                $insert = [];                foreach ($data['customers'] as $value){                    $insert[] = [                        'contacts_id' => $model->id,                        'employee_id' => $value,                        'type' => ContactsInfo::type_two,                        'crt_time' => $time,                    ];                }                ContactsInfo::insert($insert);            }            if(! empty($data['employee_one'])){                $insert = [];                foreach ($data['employee_one'] as $value){                    $insert[] = [                        'contacts_id' => $model->id,                        'employee_id' => $value,                        'type' => ContactsInfo::type_three,                        'crt_time' => $time,                    ];                }                ContactsInfo::insert($insert);            }            if(! empty($data['employee_two'])){                $insert = [];                foreach ($data['employee_two'] as $value){                    $insert[] = [                        'contacts_id' => $model->id,                        'employee_id' => $value,                        'type' => ContactsInfo::type_four,                        'crt_time' => $time,                    ];                }                ContactsInfo::insert($insert);            }            DB::commit();        }catch (\Exception $exception){            DB::rollBack();            return [false,$exception->getMessage()];        }        return [true,''];    }    public function contactsDel($data){        if($this->isEmpty($data,'id')) return [false,'请选择数据!'];        try {            DB::beginTransaction();            Contacts::whereIn('id',$data['id'])->update([                'del_time'=> time()            ]);            ContactsInfo::where('del_time',0)                ->where('contacts_id',$data['id'])                ->update(['del_time' => time()]);            DB::commit();        }catch (\Exception $exception){            DB::rollBack();            return [false,$exception->getMessage()];        }        return [true,''];    }    public function contactsDetail($data){        if($this->isEmpty($data,'id')) return [false,'请选择数据!'];        $customer = Contacts::where('del_time',0)            ->where('id',$data['id'])            ->first();        if(empty($customer)) return [false,'联系人不存在或已被删除'];        $customer = $customer->toArray();        $customer['contacts'] = $customer['customers'] = $customer['employee_one'] = $customer['employee_two'] = [];        $array = [            $customer['decision'],            $customer['grade'],        ];        $basic_map = BasicType::whereIn('id',$array)            ->pluck('title','id')            ->toArray();        $customer = [$customer];        foreach ($customer as $key => $value){            $customer[$key]['decision_title'] = $basic_map[$value['decision']] ?? '';            $customer[$key]['grade_title'] = $basic_map[$value['grade']] ?? '';        }        $customer = $customer[0];        $customer_info = ContactsInfo::where('del_time',0)            ->where('contacts_id',$customer['id'])            ->select('id','contacts_id','contact_type','contact_info','employee_id','type')            ->get()->toArray();        foreach ($customer_info as $value){            if($value['type'] == ContactsInfo::type_one){                $customer['contacts'][] = [                    'id' => $value['contact_type'],                    'info' => $value['contact_info']                ];            }elseif ($value['type'] == ContactsInfo::type_two){                $customer['customers'][] = $value['employee_id'];            }elseif ($value['type'] == ContactsInfo::type_three){                $customer['employee_one'][] = $value['employee_id'];            }elseif ($value['type'] == ContactsInfo::type_four){                $customer['employee_two'][] = $value['employee_id'];            }        }        return [true, $customer];    }    public function contactsList($data,$user){        $model = Contacts::where('del_time',0)            ->select('title','id','img','mailbox','qq','postal_code','sex','gregorian_bir','lunar_bir','hobby','address1','address2','crt_id','crt_time','mark','importance','depart','job','decision','grade')            ->orderby('id', 'desc');        if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');        $list = $this->limit($model,'',$data);        $list = $this->fillData($list);        return [true, $list];    }    public function contactsRule($data, $is_add = true){        if(empty($data['title'])) return [false,'联系人名不能为空'];        if(empty($data['contacts'])) return [false,'联系方式不能为空'];        if(empty($data['customers'])) return [false,'关联客户不能为空'];        if(empty($data['employee_one'])) return [false,'负责人不能为空'];        if($is_add){        }else{        }        return [true, $data];    }    public function fillData($data){        if(empty($data['data'])) return $data;        $array = array_unique(array_merge_recursive(array_column($data['data'],'decision'),array_column($data['data'],'grade')));        $basic_map = BasicType::whereIn('id',$array)            ->pluck('title','id')            ->toArray();        $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))            ->pluck('emp_name','id')            ->toArray();        foreach ($data['data'] as $key => $value){            $data['data'][$key]['decision_title'] = $basic_map[$value['decision']] ?? '';            $data['data'][$key]['grade_title'] = $basic_map[$value['grade']] ?? '';            $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']] ?? '';        }        return $data;    }}
 |