123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- // +----------------------------------------------------------------------
- // | Description: 客户模块设置
- // +----------------------------------------------------------------------
- // | Author: Michael_xu | gengxiaoxu@5kcrm.com
- // +----------------------------------------------------------------------
- namespace addons\qingdongams\controller;
- use addons\qingdongams\model\Customer;
- use addons\qingdongams\model\OperationLog;
- use addons\qingdongams\model\Staff;
- use addons\qingdongams\model\Message;
- use think\Db;
- use think\Exception;
- /**
- * 客户模块设置
- */
- class Setting extends StaffApi
- {
- /**
- * 获取团队成员
- */
- public function team()
- {
- $id = input('id');
- $customer = Customer::get($id);
- if (empty($customer)) {
- $this->error('客户不存在');
- }
- $owner_staff = Staff::where(['id' => $customer->owner_staff_id])->find();
- $rw_staffs = Staff::where(['id' => ['in', explode(',', $customer->rw_staff_id)],'status'=>1])->select();
- $ro_staffs = Staff::where(['id' => ['in', explode(',', $customer->ro_staff_id)],'status'=>1])->select();
- $staffs = [];
- if ($owner_staff) {
- $staffs[] = [
- 'id' => $owner_staff->id,
- 'name' => $owner_staff->name,
- 'img' => $owner_staff->img,
- 'post' => $owner_staff->post,
- 'mobile' => $owner_staff->mobile,
- 'roles' => 1,
- 'is_edit' => 1,
- ];
- }
- foreach ($rw_staffs as $v) {
- $staffs[] = [
- 'id' => $v->id,
- 'name' => $v->name,
- 'img' => $v->img,
- 'post' => $v->post,
- 'mobile' => $v->mobile,
- 'roles' => 2,
- 'is_edit' => 1,
- ];
- }
- foreach ($ro_staffs as $v) {
- $staffs[] = [
- 'id' => $v->id,
- 'name' => $v->name,
- 'img' => $v->img,
- 'post' => $v->post,
- 'mobile' => $v->mobile,
- 'roles' => 2,
- 'is_edit' => 0,
- ];
- }
- $this->success('请求成功', $staffs);
- }
- /**
- * 修改团队成员
- */
- public function editShowStaff()
- {
- $id = input('id');
- $staff = input('staff/a');
- $model = Customer::get($id);
- if (empty($model)) {
- $this->error('客户不存在');
- }
- $ro_staff_id = [];
- $rw_staff_id = [];
- foreach ($staff as $v) {
- if ($v['is_edit'] == 1) {
- $rw_staff_id[] = $v['id'];
- } else {
- $ro_staff_id[] = $v['id'];
- }
- }
- $ro_staff_id = array_diff($ro_staff_id, $rw_staff_id);
- $rw_staff_id=implode(',', $rw_staff_id);
- $ro_staff_id=implode(',', $ro_staff_id);
- $result = $model->save(['rw_staff_id' => ",{$rw_staff_id},",
- 'ro_staff_id' => ",{$ro_staff_id},"]);
- if ($result === false) {
- $this->error('修改失败');
- }
- $this->success('修改成功');
- }
- //批量新增联合跟进人
- public function batchAddShowStaff()
- {
- $ids = input('ids');
- $staff_id = input('staff_id');
- $is_edit = input('is_edit', 0);
- if(empty($ids)){
- $this->error('参数不能为空');
- }
- $ids=explode(',',$ids);
- $customers = Customer::where(['id'=>['in',$ids]])->select();
- if (empty($customers)) {
- $this->error('客户不存在');
- }
- $staff_id = explode(',', $staff_id);
- $sname=Staff::where(['id'=>['in',$staff_id],'status'=>1])->column('name');
- $sname=implode(',',$sname);
- Db::startTrans();
- try {
- foreach ($customers as $customer){
- foreach ($staff_id as $sid) {
- $rw_staff_id=explode(',',trim($customer['rw_staff_id'],','));
- $ro_staff_id=explode(',',trim($customer['ro_staff_id'],','));
- if ($is_edit == 1) {
- if(in_array($sid,$rw_staff_id)){
- continue;
- }
- $rw_staff_id[]=$sid;
- } else {
- if(in_array($sid,$ro_staff_id)){
- continue;
- }
- $ro_staff_id[]=$sid;
- }
- OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $customer['id'],
- '批量新增联合跟进人:'.$sname);
- Message::addMessage(Message::CUSTOMER_TYPE, $customer['id'], $sid, $this->auth->id, $this->auth->name . '邀请您联合跟进客户《' . $customer['name'] . '》');
- }
- $model=new Customer();
- $ro_staff_id = array_diff($ro_staff_id, $rw_staff_id);
- $rw_staff_id=implode(',', $rw_staff_id);
- $ro_staff_id=implode(',', $ro_staff_id);
- $model->save(['rw_staff_id' => ",{$rw_staff_id},",
- 'ro_staff_id' => ",{$ro_staff_id},"],['id'=>$customer['id']]);
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('添加成功');
- }
- //批量删除联合跟进人
- public function batchDelShowStaff()
- {
- $ids = input('ids');
- $staff_id = input('staff_id');
- $is_edit = input('is_edit', 0);
- if(empty($ids)){
- $this->error('参数不能为空');
- }
- $ids=explode(',',$ids);
- $customers = Customer::where(['id'=>['in',$ids]])->select();
- if (empty($customers)) {
- $this->error('客户不存在');
- }
- $staff_id = explode(',', $staff_id);
- $sname=Staff::where(['id'=>['in',$staff_id],'status'=>1])->column('name');
- $sname=implode(',',$sname);
- Db::startTrans();
- try {
- foreach ($customers as $customer){
- foreach ($staff_id as $sid) {
- $rw_staff_id=explode(',',trim($customer['rw_staff_id'],','));
- $ro_staff_id=explode(',',trim($customer['ro_staff_id'],','));
- if ($is_edit == 1) {
- if(in_array($sid,$rw_staff_id)){
- continue;
- }
- $rw_staff_id = array_diff($rw_staff_id, [$sid]);
- } else {
- if(!in_array($sid,$ro_staff_id)){
- continue;
- }
- $ro_staff_id = array_diff($ro_staff_id, [$sid]);
- }
- OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $customer['id'],
- '批量新增联合跟进人:'.$sname);
- }
- $model=new Customer();
- $ro_staff_id = array_diff($ro_staff_id, $rw_staff_id);
- $rw_staff_id=implode(',', $rw_staff_id);
- $ro_staff_id=implode(',', $ro_staff_id);
- $model->save(['rw_staff_id' => ",{$rw_staff_id},",
- 'ro_staff_id' => ",{$ro_staff_id},"],['id'=>$customer['id']]);
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('删除成功');
- }
- }
|