CustomCompany.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Facades\Servers\WechatWork\ExternalContact;
  3. use App\Facades\Servers\WechatWork\User as WorkUserServer;
  4. use App\Http\Requests\Admin\CustomCompany as Request;
  5. use App\Models\Custom;
  6. use App\Models\CustomCompany as Model;
  7. use App\Models\Work\Tag as WorkTag;
  8. use App\Models\Work\User as WorkUser;
  9. /**
  10. * 客户公司资质
  11. *
  12. * @author 刘相欣
  13. *
  14. */
  15. class CustomCompany extends Auth{
  16. protected function _initialize(){
  17. parent::_initialize();
  18. $this->assign('breadcrumb1','用户管理');
  19. $this->assign('breadcrumb2','客户管理');
  20. }
  21. /**
  22. * 列表页
  23. *
  24. * */
  25. public function index(Model $Model,Custom $Custom){
  26. // 接受参数
  27. $code = request('custom_code','');
  28. $status = request('status','0');
  29. // 编码转ID
  30. $uid = $Custom->codeToId($code);
  31. // 查询条件
  32. $map = [['status','=',$status]];
  33. // 编码ID
  34. if( $uid ) $map[] = ['custom_uid','=',$uid];
  35. // 查询数据
  36. $list = $Model->query()->where($map)->orderByDesc('insert_time')->paginate(config('page_num',10));
  37. // 循环处理数据
  38. foreach ($list as $key => $value) {
  39. // 客户编码
  40. $value['custom_code'] = $Custom->idToCode($value['custom_uid']);
  41. // 客户编码
  42. $value['custom_name'] = $Custom->getValue($value['custom_uid'],'username');
  43. // 重组
  44. $list[$key] = $value;
  45. }
  46. // 分配数据
  47. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  48. $this->assign('list',$list);
  49. // 加载模板
  50. return $this->fetch();
  51. }
  52. /**
  53. * 修改状态
  54. *
  55. * */
  56. public function set_status(Request $request,Model $Model,Custom $Custom,WorkUser $WorkUser, WorkTag $WorkTag){
  57. // 指定客服
  58. $id = request('id',0);
  59. // 查询用户
  60. $oldData = $Model->where(['id'=>$id])->first();
  61. // 查询用户的外部联系人
  62. $custom = $Custom->getOne($oldData['custom_uid']);
  63. // 返回结果
  64. if( request()->isMethod('post') ){
  65. // 验证参数
  66. $request->scene('set_status')->validate();
  67. // 接收参数
  68. $status = request('status',0);
  69. $userids = request('user',[]);
  70. // 如果用户不存在
  71. if( !$oldData ) return json_send(['code'=>'error','msg'=>'查无数据']);
  72. // 如果用户不存在
  73. if( !$custom ) return json_send(['code'=>'error','msg'=>'用户不存在']);
  74. // 通过才更新
  75. if( $status == 8 ) {
  76. // 如果存在指定客服
  77. if( $userids ) {
  78. // 查询客服信息
  79. $followUsers = $WorkUser->query()->whereIn('work_userid',$userids)->where([['external_userid','=',$custom['external_userid']]])->get(['id','work_userid','external_userid','remark','remark_company','description'])->toArray();
  80. // 如果用户不存在
  81. if( !$followUsers ) return json_send(['code'=>'error','msg'=>'没有指定的客服']);
  82. // 返回结果
  83. $area = explode('/',$oldData['license_area']);
  84. // 处理地区
  85. $province = empty($area[0]) ? '' : $area[0];
  86. $city = empty($area[1]) ? '' : $area[1];
  87. // 循环处理
  88. foreach ($followUsers as $key => $value) {
  89. // 备注修改
  90. $value['remark'] = $oldData['remark'];
  91. $value['remark_company'] = $oldData['remark_company'];
  92. // 打上城市标签
  93. $WorkTag->markCityTag($value['work_userid'],$value['external_userid'],$province,$city);
  94. // 客户备注
  95. ExternalContact::remark($value['external_userid'],$value['work_userid'],['remark'=>$oldData['remark'],'remark_company'=>$oldData['remark_company']]);
  96. // 更新客服备注
  97. $result = $WorkUser->edit($value['id'],$value);
  98. // 提示新增失败
  99. if( !$result ) return json_send(['code'=>'error','msg'=>'备注修改失败']);
  100. }
  101. }
  102. // 执行修改
  103. $result = $Custom->edit($oldData['custom_uid'],['company_id'=>$id]);
  104. // 提示修改失败
  105. if( !$result ) return json_send(['code'=>'error','msg'=>'资质设置失败']);
  106. }
  107. // 执行修改
  108. $result = $Model->edit($id,['status'=>$status,'admin_uid'=>admin('uid')]);
  109. // 提示新增失败
  110. if( !$result ) return json_send(['code'=>'error','msg'=>'审核失败']);
  111. // 记录行为
  112. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  113. // 告知结果
  114. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  115. }
  116. // 错误告知
  117. if( !$oldData ) return $this->error('查无数据');
  118. // 如果没有客户
  119. if( !$custom ) return $this->error('用户不存在');
  120. // 默认不需要客服
  121. $partUserList = [];
  122. // 如果没有的外部联系人ID话
  123. if( $custom['external_userid'] ) {
  124. // 查询客服信息
  125. $followUsers = $WorkUser->query()->where([['external_userid','=',$custom['external_userid']]])->pluck('work_userid')->toArray();
  126. // 获取部门数据
  127. $partUserList = WorkUserServer::getListDepart();
  128. // 循环部门成员
  129. foreach ($partUserList as $key=>$part) {
  130. // 用户列表
  131. foreach ($part['user_list'] as $k=>$user){
  132. // 不存在跟进列表的删除
  133. if( !in_array($user['userid'],$followUsers) ) unset($part['user_list'][$k]);
  134. }
  135. // 重组
  136. $partUserList[$key] = $part;
  137. // 删除没有跟进人员的部门
  138. if( !$part['user_list'] ) unset($partUserList[$key]);
  139. }
  140. }
  141. // 分配数据
  142. $this->assign('partUserList',$partUserList);
  143. $this->assign('oldData',$oldData);
  144. $this->assign('crumbs','新增');
  145. // 加载模板
  146. return $this->fetch();
  147. }
  148. }