123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992 |
- <?php
- namespace addons\qingdongams\controller;
- use addons\qingdongams\model\AreaManagement;
- use addons\qingdongams\model\Contacts;
- use addons\qingdongams\model\Customer as CustomerModel;
- use addons\qingdongams\model\CustomerFile;
- use addons\qingdongams\model\CustomerOther;
- use addons\qingdongams\model\FormField;
- use addons\qingdongams\model\Message;
- use addons\qingdongams\model\OperationLog;
- use addons\qingdongams\model\Record;
- use addons\qingdongams\model\SeaOperation;
- use addons\qingdongams\model\Seastype;
- use addons\qingdongams\model\Staff;
- use addons\qingdongams\model\StaffCollect;
- use addons\qingdongams\model\StaffSignIn;
- use think\Db;
- use think\Exception;
- /**
- * @desc 操作文档:https://doc.fastadmin.net/qingdongams
- * @desc 软件介绍:https://www.fastadmin.net/store/qingdongams.html
- * @desc 售后微信:qingdong_crm
- */
- /**
- * 客户接口
- */
- class Customer extends StaffApi
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = [];
- protected $customerType = ['A', 'B', 'C', 'D'];
- //获取select客户列表
- public function getSelectList()
- {
- $limit = input("limit/d", 20);
- $name = input('name');
- $id = input('id','');
- $where = [];
- if ($name) {
- $where['name'] = ['like', "%{$name}%"];
- }
- if ($id !== '') {
- $where['id'] = $id;
- }
- $list = CustomerModel::where($where)->with(['ownerStaff'])->field('id,name,owner_staff_id,follow')->paginate($limit);
- $this->success('请求成功', $list);
- }
- //获取搜索员工列表
- public function getSearchStaffList()
- {
- $ids = Staff::getMyStaffIds();
- $staff = Staff::where([
- 'id' => ['in', $ids],
- 'status' => 1
- ])->field('id,name,img,post')->select();
- $this->success('请求成功', $staff);
- }
- //获取客户列表
- public function getList()
- {
- $limit = input("limit/d", 10);
- $params = $this->request->post();
- $params['type'] = isset($params['type']) ? $params['type'] : 0;
- $where= FormField::updateWhereField(FormField::CUSTOMER_TYPE,$params);
- if (isset($params['name']) && $params['name']) {//客户名称
- $where['name|subname'] = ['like', "%{$params['name']}%"];
- }
- if (isset($params['level']) && $params['level']) {//客户星级
- $where['level'] = $params['level'];
- }
- if (isset($params['source']) && $params['source']) {//客户来源
- $where['source'] = $params['source'];
- }
- if (isset($params['follow']) && $params['follow']) {//跟进状态
- $where['follow'] = $params['follow'];
- }
- if (isset($params['createtime']) && $params['createtime']) {//创建时间
- $createtime = $params['createtime'];
- $times = setTimes($createtime, 'time');
- $where['createtime'] = ['between', $times];
- }
- $order = 'id desc';
- if (isset($params['sortid']) && $params['sortid']) {
- switch ($params['sortid']) {
- case 1://名称正序
- $order = 'name asc';
- break;
- case 2://名称倒序
- $order = 'name desc';
- break;
- case 3://创建时间正序
- $order = 'createtime asc';
- break;
- case 4://创建时间倒序
- $order = 'createtime desc';
- break;
- case 5://下次跟进时间正序
- $order = 'next_time asc';
- break;
- case 6://下次跟进时间倒序
- $order = 'next_time desc';
- break;
- }
- }
- if (isset($params['next_time']) && $params['next_time']) {//下次联系时间
- $next_time = $params['next_time'];
- $next_time = explode(',', $next_time);
- $where['next_time'] = ['between', [$next_time[0], $next_time[1]]];
- }
- $whereStaff = [];
- //0全部 1 我负责 2 我参与 11 我下属负责
- if (isset($params['staff_id']) && $params['staff_id']) {//下级员工筛选
- $where['owner_staff_id'] = $params['staff_id'];
- } else {
- $staff_id = $this->auth->id;
- $type = $params['type'] ?? 0;
- if ($type == 1) {//我的客户
- $whereStaff['owner_staff_id'] = $this->auth->id;
- } elseif ($type == 11) {//下属负责的客户
- $whereStaff['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
- } elseif ($type == 2) {//我参与的客户
- $whereStaff = function ($query) use ($staff_id) {
- $query->where('ro_staff_id', 'like', "%,{$staff_id},%")
- ->whereOr('rw_staff_id', 'like', "%,{$staff_id},%");
- };
- }else{
- $whereStaff = function ($query) use ($staff_id) {
- $query->where(['ro_staff_id' => ['like', "%,{$staff_id},%"]])
- ->whereOr('rw_staff_id', 'like', "%,{$staff_id},%")
- ->whereOr(['owner_staff_id' => ['in', Staff::getMyStaffIds()]]);
- };
- }
- }
- if (isset($params['contract_status']) && $params['contract_status'] !== '') {
- $where['contract_status'] = $params['contract_status'];
- }
- if (isset($params['is_import'])) {
- if ($params['is_import'] == 1) {
- $where['lng'] = 0;
- } elseif ($params['is_import'] == 2) {
- $where['lng'] = ['neq', 0];
- }
- }
- if (isset($params['prov']) && !empty(trim($params['prov']))) {//省市区筛选
- $prov = trim($params['prov']);
- $city = trim($params['city']);
- $area = trim($params['area']);
- $cityinfo = $prov.$city.$area;
- if (empty($city)) {
- $where['address|address_detail'] = ['like', "%{$prov}%"];
- } elseif (empty($area)) {
- $where['address|address_detail'] = ['like', "%{$city}%"];
- } else {
- $where['address|address_detail'] = ['like', "%{$cityinfo}%"];
- }
- }
- // $where['is_seas'] = 0;
- if(isset($params['id_list'])){//日志 查询id列表
- $where=[];
- $where['id']=['in',explode(',',$params['id_list'])];
- }
- $list = CustomerModel::where($where)->where($whereStaff);
- $list = $list->with([
- 'ownerStaff',
- 'contractTotal',
- 'receivablesTotal',
- 'contacts'
- ])->field('id,type,name,address,last_time,owner_staff_id,rw_staff_id,level,follow,lng,lat,address,updatetime,contract_status,last_time')->order($order)->paginate($limit)->toArray();
- $data = $list['data'];
- foreach ($data as $k => $v) {
- if ($v['owner_staff_id'] == $this->auth->id ||
- in_array($v['owner_staff_id'],Staff::getLowerStaffId()) ||
- in_array($this->auth->id, explode(',', $v['rw_staff_id'])) ) {
- $v['is_operation'] = 1;
- } else {
- $v['is_operation'] = 0;
- }
- if($v['contract_status']==1){
- $v['follow'] = '已成交';
- }else{
- $v['follow'] = '未成交';
- }
- $data[$k] = $v;
- }
- $this->success('请求成功', [
- 'total' => $list['total'],
- 'per_page' => $list['per_page'],
- 'current_page' => $list['current_page'],
- 'last_page' => $list['last_page'],
- 'data' => $data
- ]);
- }
- //获取公海客户列表
- public function getSeasList()
- {
- $limit = input("limit/d", 10);
- $params = $this->request->post();
- $where= FormField::updateWhereField(FormField::CUSTOMER_TYPE,$params);
- if (isset($params['name']) && $params['name']) {//客户名称
- $where['name|subname'] = ['like', "%{$params['name']}%"];
- }
- if (isset($params['level']) && $params['level']) {//客户星级
- $where['level'] = $params['level'];
- }
- if (isset($params['source']) && $params['source']) {//客户来源
- $where['source'] = $params['source'];
- }
- if (isset($params['follow']) && $params['follow']) {//跟进状态
- $where['follow'] = $params['follow'];
- }
- if (isset($params['createtime']) && $params['createtime']) {//跟进状态
- $createtime = $params['createtime'];
- $where['createtime'] = ['between', setTimes($createtime,'time')];
- }
- $order = 'id desc';
- if (isset($params['sortid']) && $params['sortid']) {//排序
- switch ($params['sortid']) {
- case 1://名称正序
- $order = 'name asc';
- break;
- case 2://名称倒序
- $order = 'name desc';
- break;
- case 3://创建时间正序
- $order = 'createtime asc';
- break;
- case 4://创建时间倒序
- $order = 'createtime desc';
- break;
- case 5://下次跟进时间正序
- $order = 'next_time asc';
- break;
- case 6://下次跟进时间倒序
- $order = 'next_time desc';
- break;
- }
- }
- if (isset($params['next_time']) && $params['next_time']) {//下次联系时间
- $next_time = $params['next_time'];
- $next_time = explode(',', $next_time);
- $where['next_time'] = ['between', [$next_time[0], $next_time[1]]];
- }
- if (isset($params['is_chengjiao']) && $params['is_chengjiao'] == 1) {
- $where['contract_status'] = 1;
- }
- if (isset($params['prov']) && !empty(trim($params['prov']))) {//省市区筛选
- $prov = trim($params['prov']);
- $city = trim($params['city']);
- $area = trim($params['area']);
- if (empty($city)) {
- $where['address|address_detail'] = ['like', "%{$prov}%"];
- } elseif (empty($area)) {
- $where['address|address_detail'] = ['like', "%{$city}%"];
- } else {
- $where['address|address_detail'] = ['like', "%{$area}%"];
- }
- }
- //公海权限
- $rules=Staff::getStaffRule('seas');
- $whereStaff = function ($query) use ($rules) {
- foreach ($rules as $rule) {
- $query->whereOr(['seas_id' => ['like', "%,{$rule},%"]]);
- }
- };
- $where['is_seas'] = 1;
- $list = CustomerModel::where($where)->where($whereStaff)->where('owner_staff_id is null or owner_staff_id = 0');
- $list = $list->with([
- 'ownerStaff',
- 'contractTotal',
- 'receivablesTotal',
- 'contacts'
- ])->field('id,type,name,address,last_time,owner_staff_id,level,follow,lng,lat,address')->order($order)->paginate($limit)->toArray();
- $data = $list['data'];
- $this->success('请求成功', [
- 'total' => $list['total'],
- 'per_page' => $list['per_page'],
- 'current_page' => $list['current_page'],
- 'last_page' => $list['last_page'],
- 'data' => $data
- ]);
- }
- //查重客户名称
- public function selectName()
- {
- $name = input('name');
- if (CustomerModel::where(['name' => $name])->find()) {
- $this->error('客户名称已存在');
- }
- $this->success('当前客户名称可使用');
- }
- //获取区域列表
- public function getSeastypeList()
- {
- $seasIds=Staff::getStaffRule('seas');
- $seastype = Seastype::where(['id'=>['in',$seasIds]])->select();
- $this->success('请求成功', $seastype);
- }
- //新增客户
- public function addCustomer()
- {
- $params = $this->request->post();
- if (empty($params['customer'])) {
- $this->error('客户信息不能为空');
- }
- // 表单验证
- if (($result = $this->qingdongamsValidate($params['customer'], get_class(),'create')) !== true) {
- $this->error($result);
- }
- if (CustomerModel::where(['name' => $params['customer']['name']])->find()) {
- $this->error('客户名称已存在');
- }
- $result = FormField::checkFields(FormField::CUSTOMER_TYPE,$params['customer']);
- if ($result !== true) {
- $this->error($result);
- }
- Db::startTrans();
- try {
- $customerId = CustomerModel::createCustomer($params['customer']);
- if(isset($params['customer']['mobile']) && $params['customer']['mobile']){
- $retC = array(
- 'customer_id'=>$customerId,
- 'is_major'=>1,
- 'region'=>1,
- 'name'=>$params['customer']['name'],
- 'mobile'=>$params['customer']['mobile'],
- 'next_time'=>date('Y-m-d H:i:s'),
- );
- Contacts::createContacts($retC);
- }
- $content = $this->auth->name . "刚刚新建了客户《{$params['customer']['name']}》,请您审阅!";
- Message::addMessage(Message::CUSTOMER_TYPE, $customerId, $params['reminds_id'], $this->auth->id, $content);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result) {
- $this->success('新增客户成功',array('id'=>$customerId));
- }
- }
- public function getAddressProvince()
- {
- $address = input('address');
- $this->success('请求成功', AreaManagement::whereAddressProvince($address));
- }
- public function getAreaManagement()
- {
- $this->success('请求成功', AreaManagement::where([])->select());
- }
- //编辑客户
- public function editCustomer()
- {
- $id = input('id');
- $params = $this->request->post();
- $row = CustomerModel::where(['id' => $id])->find();
- if (empty($row)) {
- $this->error('客户信息不存在');
- }
- // 表单验证
- if (($result = $this->qingdongamsValidate($params, get_class(),'edit')) !== true) {
- $this->error($result);
- }
- $result = FormField::checkFields(FormField::CUSTOMER_TYPE,$params,$id);
- if ($result !== true) {
- $this->error($result);
- }
- Db::startTrans();
- try {
- $result = CustomerModel::updateCustomer($params);
- if(isset($param['mobile']) && $params['mobile']){
- Contacts::where(array('customer_id'=>$id,'is_major'=>1))->update(array('mobile'=>$params['mobile'],'updatetime'=>time()));
- }
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('修改客户成功');
- }
- //合并客户
- public function mergeCustomer()
- {
- $new_id = input('id');
- $old_id = input('old_id');
- $contacts_id = input('contacts_id');
- $params = input('data/a');
- $params['id'] = $new_id;
- CustomerModel::updateCustomer($params);
- //合并联系人
- Contacts::where(['customer_id' => $old_id])->update(['customer_id' => $new_id]);
- //合并合同
- \addons\qingdongams\model\Contract::where(['customer_id' => $old_id])->update(['customer_id' => $new_id]);
- //合并费用
- \addons\qingdongams\model\Consume::where(['customer_id' => $old_id])->update(['customer_id' => $new_id]);
- //合并回款
- \addons\qingdongams\model\Receivables::where(['customer_id' => $old_id])->update(['customer_id' => $new_id]);
- //合并任务
- \addons\qingdongams\model\Event::where(['customer_id' => $old_id])->update(['customer_id' => $new_id]);
- //合并工单
- \addons\qingdongams\model\Workorder::where(['customer_id' => $old_id])->update(['customer_id' => $new_id]);
- //删除旧用户
- CustomerModel::destroy($old_id);
- Contacts::where(['customer_id' => $new_id])->update(['is_major'=>0]);
- Contacts::where(['id' => $contacts_id])->update(['is_major'=>1]);
- Message::setRead(Message::CUSTOMER_TYPE,$old_id);
- $this->success('合并成功');
- }
- //获取合并客户详情
- public function getMergeCustomerDetail()
- {
- $ids = input('ids');
- if (empty($ids)) {
- $this->error('参数不能为空');
- }
- $ids = explode(',', $ids);
- $customers = CustomerModel::where(['id' => ['in', $ids]])->select();
- $customers=collection($customers)->toArray();
- if(empty($customers)){
- $this->error('用户不存在');
- }
- foreach ($customers as $k=>$c) {
- $customers[$k] = CustomerOther::getOther($c);
- }
- $detail = [];
- foreach ($customers as $v) {
- foreach ($v as $key => $value) {
- if($value === null){
- $detail[$key]=[];
- continue;
- }
- if($key == 'name'){
- $detail[$key][] = ['id'=>$v['id'],'name'=>$value];
- }elseif($key == 'address'){
- $detail[$key][]=['name'=>$value,'lat'=>$v['lat'],'lng'=>$v['lng'],'address_detail'=>$v['address_detail']];
- }else{
- $detail[$key][] = $value;
- }
- }
- }
- $detail['parent_id']=CustomerModel::where(['id'=>['in',$detail['parent_id']]])->field('id,name')->select();
- $contacts=Contacts::where(['customer_id'=>['in',$ids]])->field('id,name')->select();
- $detail['contacts']=$contacts;
- $this->success('请求成功', $detail);
- }
- //获取子公司
- public function getLowerCustomer()
- {
- $id = input('id');
- $customers = CustomerModel::where(['parent_id' => $id])->with([
- 'ownerStaff',
- 'contacts'
- ])->field('id,name,next_time,owner_staff_id,level,follow')->select();
- $this->success('请求成功', $customers);
- }
- // 是否可以查看客户
- public function isShowCustomer()
- {
- $id = input('id');
- $customer = CustomerModel::where(['id' => $id])->find();
- if (empty($customer)) {
- $this->error('信息不存在');
- }
- $customer = $customer->toArray();
- if ($customer['owner_staff_id'] == $this->auth->id ||
- in_array($customer['owner_staff_id'], Staff::getLowerStaffId()) ||
- in_array($this->auth->id, explode(',', $customer['rw_staff_id'])) ||
- in_array($this->auth->id, explode(',', $customer['ro_staff_id']))
- ) {
- $this->success('请求成功', ['is_show' => 1]);
- }
- $this->success('请求成功', ['is_show' => 0]);
- }
- //客户详情
- public function customerDetail()
- {
- $id = input('id');
- $customer = CustomerModel::where(['id' => $id])->with([
- 'createStaff',
- 'ownerStaff',
- 'contacts',
- 'contractTotal',
- 'receivablesTotal',
- 'consumeTotal',
- 'workorderTotal'
- ])->find();
- if (empty($customer)) {
- $this->error('信息不存在');
- }
- $customer = $customer->toArray();
- if ($customer['owner_staff_id'] == $this->auth->id ||
- in_array($customer['owner_staff_id'],Staff::getLowerStaffId()) ||
- in_array($this->auth->id, explode(',', $customer['rw_staff_id'])) ) {
- $customer['operation'] = 'update';//修改权限
- $customer['is_operation'] = 1;
- } else {
- $customer['operation'] = 'read';//只读权限
- $customer['is_operation'] = 0;
- }
- $customer['is_collect'] = StaffCollect::isCollect(StaffCollect::CUSTOMER_TYPE, $customer['id']) ? 1 : 0;
- $customer = CustomerOther::getOther($customer);
- Message::setRead(Message::CUSTOMER_TYPE, $id, $this->auth->id);
- $this->success('请求成功', $customer);
- }
- //移入公海
- public function moveSeas()
- {
- $id = input('id');
- $row = CustomerModel::where(['id' => $id])->find();
- if (empty($row)) {
- $this->error('客户不存在');
- }
- Db::startTrans();
- try {
- CustomerModel::moveSeas($id);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('放入成功');
- }
- //批量移入公海
- public function batchMoveSeas()
- {
- $ids = input('ids');
- if(empty($ids)){
- $this->error('参数为空');
- }
- $ids=explode(',',$ids);
- $row = CustomerModel::where(['id' => ['in',$ids]])->select();
- if (empty($row)) {
- $this->error('客户不存在');
- }
- Db::startTrans();
- try {
- foreach ($ids as $id){
- CustomerModel::moveSeas($id);
- }
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('放入成功');
- }
- //转移客户
- public function transfer()
- {
- $id = input('id');
- $staff_id = input('staff_id');
- if (empty($staff_id)) {
- $this->error('参数错误');
- }
- $staff = Staff::get($staff_id);
- if (empty($staff)) {
- $this->error('接收对象不存在');
- }
- //, 'owner_staff_id' => $this->auth->id
- $row = CustomerModel::where(['id' => $id])->find();
- if (empty($row)) {
- $this->error('客户不存在');
- }
- try {
- CustomerModel::transfer($id, $staff_id);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- $this->success('转移成功');
- }
- //批量转移客户
- public function batchTransfer()
- {
- $ids = input('ids');
- $staff_id = input('staff_id');
- if(empty($ids)){
- $this->error('参数为空');
- }
- $ids=explode(',',$ids);
- $row = CustomerModel::where(['id' => ['in',$ids]])->select();
- if (empty($row)) {
- $this->error('客户不存在');
- }
- Db::startTrans();
- try {
- foreach ($ids as $id){
- CustomerModel::transfer($id, $staff_id);
- }
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('放入成功');
- }
- //领取公海客户
- public function receive()
- {
- $customer_id = input('customer_id');
- $where = ['owner_staff_id' => 0];
- if ($customer_id) {
- $where['id'] = $customer_id;
- }
- $customers = CustomerModel::where($where)->count();
- if ($customers == 0) {
- $this->error('公海内暂无客户');
- }
- try {
- $id = CustomerModel::receive($customer_id);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- $this->success('领取成功', ['id' => $id]);
- }
- //重点关注客户
- public function collect()
- {
- $customer_id = input('customer_id');
- try {
- StaffCollect::addCollect(StaffCollect::CUSTOMER_TYPE, $customer_id);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- $this->success('重点关注成功');
- }
- //取消重点关注
- public function cancelCollect()
- {
- $customer_id = input('customer_id');
- try {
- StaffCollect::cancel(StaffCollect::CUSTOMER_TYPE, $customer_id);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- $this->success('取消重点关注成功');
- }
- //重点关注客户列表
- public function collectList()
- {
- $limit = input("limit/d", 10);
- $ids = StaffCollect::where(['staff_id' => $this->auth->id, 'relation_type' => 1])->column('relation_id');
- $list = CustomerModel::where(['id' => ['in', $ids]])->with([
- 'ownerStaff',
- 'contacts'
- ])->field('id,name,next_time,owner_staff_id,level,follow')->order('id desc')->paginate($limit);
- $this->success('请求成功', $list);
- }
- //获取附件列表
- public function getFilesList()
- {
- $id = input('customer_id');
- $files = CustomerFile::where(['customer_id' => $id])->field('file_id')->with(['file'])->select();
- $this->success('请求成功', $files);
- }
- //获取附件列表
- public function addFiles()
- {
- $customer_id = input('customer_id');
- $files = input('files');
- $files = CustomerFile::addFiles($files, $customer_id);
- $this->success('添加成功');
- }
- //周围客户
- public function nearby()
- {
- $lng = input('lng');
- $lat = input('lat');
- //距离
- $distance = input('distance', 5, 'intval');
- if (empty($lng) && empty($lat)) {
- $this->error('参数错误');
- }
- $range = 180 / pi() * $distance / 6372.797; //里面的 1 就代表搜索 1km 之内,单位km
- $lngR = $range / cos($lat * pi() / 180);
- $maxLat = $lat + $range; //最大纬度
- $minLat = $lat - $range; //最小纬度
- $maxLng = $lng + $lngR; //最大经度
- $minLng = $lng - $lngR; //最小经度
- $where = ['lng' => ['between', [$minLng, $maxLng]], 'lat' => ['between', [$minLat, $maxLat]]];
- $params = $this->request->post();
- if (isset($params['name']['value']) && $params['name']['value']) {//客户名称
- $where['name|subname'] = ['like', "%{$params['name']['value']}%"];
- }
- if (isset($params['level']) && $params['level']) {//客户星级
- $where['level'] = $params['level'];
- }
- if (isset($params['source']) && $params['source']) {//客户来源
- $where['source'] = $params['source'];
- }
- if (isset($params['follow']) && $params['follow']) {//跟进状态
- $where['follow'] = $params['follow'];
- }
- if (isset($params['createtime']) && $params['createtime']) {//创建时间
- $createtime = $params['createtime'];
- $times = setTimes($createtime, 'time');
- $where['createtime'] = ['between', $times];
- }
- if (isset($params['next_time']) && $params['next_time']) {//下次联系时间
- $next_time = $params['next_time'];
- $next_time = explode(',', $next_time);
- $where['next_time'] = ['between', [$next_time[0], $next_time[1]]];
- }
- $whereStaff = [];
- //0全部 1 我负责 2 我参与 11 我下属负责
- if (isset($params['staff_id']) && $params['staff_id']) {//下级员工筛选
- $where['owner_staff_id'] = $params['staff_id'];
- } else {
- $staff_id = $this->auth->id;
- $type = $params['type'] ?? 0;
- if ($type == 1) {//我的客户
- $whereStaff['owner_staff_id'] = $this->auth->id;
- } elseif ($type == 11) {//下属负责的客户
- $whereStaff['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
- } elseif ($type == 2) {//我参与的客户
- $whereStaff = function ($query) use ($staff_id) {
- $query->where('ro_staff_id', 'like', "%,{$staff_id},%")
- ->whereOr('rw_staff_id', 'like', "%,{$staff_id},%");
- };
- }else{
- $whereStaff = function ($query) use ($staff_id) {
- $query->where(['ro_staff_id' => ['like', "%,{$staff_id},%"]])
- ->whereOr('rw_staff_id', 'like', "%,{$staff_id},%")
- ->whereOr(['owner_staff_id' => ['in', Staff::getMyStaffIds()]]);
- };
- }
- }
- if (isset($params['contract_status']) && $params['contract_status'] !== '') {
- $where['contract_status'] = $params['contract_status'];
- }
- if (isset($params['is_import'])) {
- if ($params['is_import'] == 1) {
- $where['lng'] = 0;
- } elseif ($params['is_import'] == 2) {
- $where['lng'] = ['neq', 0];
- }
- }
- if (isset($params['prov']) && !empty(trim($params['prov']))) {//省市区筛选
- $prov = trim($params['prov']);
- $city = trim($params['city']);
- $area = trim($params['area']);
- $cityinfo = $prov.$city.$area;
- if (empty($city)) {
- $where['address|address_detail'] = ['like', "%{$prov}%"];
- } elseif (empty($area)) {
- $where['address|address_detail'] = ['like', "%{$city}%"];
- } else {
- $where['address|address_detail'] = ['like', "%{$cityinfo}%"];
- }
- }
- $customers = CustomerModel::where($where)->where($whereStaff)->field('id,owner_staff_id,name,location,next_time,lng,lat,address_detail')
- ->with('ownerStaff')->select();
- $data = [];
- //所属员工列表
- $myStaffIds = Staff::getMyStaffIds();
- foreach ($customers as $k => $v) {
- if ($v['owner_staff_id'] == 0) {
- $v['type'] = 0;//公海
- } elseif ($v['owner_staff_id'] == $this->auth->id) {
- $v['type'] = 1;//自己的
- } elseif (in_array($v['owner_staff_id'], $myStaffIds)) {
- $v['type'] = 2;//团队的
- } else {
- $v['type'] = 3;//其他人
- }
- $v['juli_num'] = getdistance($lng, $lat, $v['lng'], $v['lat']);
- $v['juli'] = float_number($v['juli_num']);
- $data[$v['juli_num']][] = $v;
- }
- ksort($data);
- $result = [];
- foreach ($data as $v) {
- $result = array_merge($result, array_values($v));
- }
- $this->success('请求成功', $result);
- }
- //周围客户列表
- public function nearby_customer_list()
- {
- $id = input('id');
- $page = input('page', 0);
- $limit = input('limit', 100);
- $lng = input('lng');
- $lat = input('lat');
- //我的客户
- $type = input('type');
- //距离
- $distance = input('distance', 5, 'intval');
- $name = input('name', '');
- if (empty($lng) && empty($lat)) {
- $this->error('参数错误');
- }
- $range = 180 / pi() * $distance / 6372.797; //里面的 1 就代表搜索 1km 之内,单位km
- $lngR = $range / cos($lat * pi() / 180);
- $maxLat = $lat + $range; //最大纬度
- $minLat = $lat - $range; //最小纬度
- $maxLng = $lng + $lngR; //最大经度
- $minLng = $lng - $lngR; //最小经度
- $where = ['lng' => ['between', [$minLng, $maxLng]], 'lat' => ['between', [$minLat, $maxLat]]];
- //客户分类
- if ($type == 1) {//我的客户
- $where['owner_staff_id'] = $this->auth->id;
- } elseif ($type == 2) {//下属负责的客户
- $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
- } elseif ($type == 3) {//已成交客户
- $where['contract_status'] = 1;
- }
- //单独查询用户
- if ($id) {
- $where['id'] = $id;
- }
- if ($name) {
- $where['name'] = ['like', "%{$name}%"];
- }
- $customers = CustomerModel::where($where)->where([
- 'lng' => [
- 'neq',
- 0
- ]
- ])->field('id,owner_staff_id,name,location,next_time,lng,lat,address_detail')
- ->with('ownerStaff')->select();
- $data = [];
- foreach ($customers as $k => $v) {
- $v['juli_num'] = getdistance($lng, $lat, $v['lng'], $v['lat']);
- $v['juli'] = float_number($v['juli_num']);
- $data[$v['juli']][] = $v;
- }
- ksort($data);
- $result = [];
- foreach ($data as $v) {
- $result = array_merge($result, array_values($v));
- }
- $total = count($result);
- $result = array_slice($result, $page * $limit, $limit) ?? [];
- $this->success('请求成功', ['data' => $result, 'total' => $total, 'last_page' => ceil($total / $limit)]);
- }
- // 转变 客户类型
- public function changeCustomerType()
- {
- $id = input('customer_id', '', 'intval');
- $type = input('type', '', '');
- $remark = input('remark', '');
- if (!$id) {
- $this->error('请输入客户Id!');
- }
- if (!$type || !in_array($type, $this->customerType)) {
- $this->error('请输入要转变的类型!');
- }
- if (!$remark) {
- $this->error('请输入转变原因!');
- }
- $where = [
- 'id' => $id,
- 'owner_staff_id' => ['in', Staff::getMyStaffIds()]
- ];
- $info = CustomerModel::where($where)->with('ownerStaff')->find();
- if (!$info) {
- $this->error('该客户不是您的客户,无法变更!');
- }
- if ($info['type'] == $type) {
- $this->error('客户原来正是' . $type . '级别!');
- }
- Db::startTrans();
- try {
- $remark = '[' . $this->auth->name . ']将客户' . $info['name'] . '从' . $info['type'] . '类型变更为' . $type . '类型【备注:' . $remark . '】';
- CustomerModel::changeCustomerType($id, $type, $remark);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('提交成功');
- }
- // 员工反馈给领导
- public function feedbackToLeader()
- {
- $id = input('id', '', 'intval');
- $remark = input('remark', '');
- if (!$id || !$remark) {
- $this->error('缺少参数');
- }
- $info = Message::where('id', $id)->find();
- if (!$info) {
- $this->error('找不到此条消息');
- }
- Db::startTrans();
- try {
- StaffSignIn::createSignIn([
- 'customer_id'=>$id,
- 'relation_type'=>'customer',
- 'relation_id'=>$id,
- 'content'=>$remark,
- 'staff_id'=>$this->auth->id
- ]);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('提交成功');
- }
- }
|