Staff.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\admin\controller\qingdong\dingding;
  3. use addons\qingdong\model\DingStaff;
  4. use app\common\controller\Backend;
  5. use addons\qingdong\model\StaffDepartment;
  6. use addons\qingdong\model\Staff as StaffModel;
  7. use think\Db;
  8. use think\Exception;
  9. /**
  10. * 同步员工
  11. */
  12. class Staff extends Backend {
  13. public function _initialize() {
  14. parent::_initialize();
  15. $this->model = new DingStaff();
  16. }
  17. /**
  18. * 员工列表
  19. */
  20. public function index() {
  21. $this->request->filter(['strip_tags']);
  22. if ($this->request->isAjax()) {
  23. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  24. $list = $this->model->with(['department'])->where($where)->where(['ding_staff.type'=>0])->order($sort, $order)->paginate($limit);
  25. $row = $list->items();
  26. $result = array("total" => $list->total(), "rows" => $row);
  27. return json($result);
  28. }
  29. return $this->view->fetch();
  30. }
  31. /**
  32. * 删除员工
  33. */
  34. public function del($ids = null) {
  35. if ($this->request->isAjax()) {
  36. $map['id'] = array('in', $ids);
  37. $result = DingStaff::where($map)->delete();
  38. if (!$result) {
  39. $this->error('删除失败');
  40. }
  41. $this->success('删除成功');
  42. }
  43. return $this->view->fetch();
  44. }
  45. /**
  46. * 同步到CRM
  47. */
  48. public function batch()
  49. {
  50. $info = $this->model->where(array('status' => 0))->select();
  51. if (!$info) {
  52. $this->error('无数据可同步');
  53. }
  54. $success=0;
  55. $error_info=[];
  56. foreach ($info as $k => $v) {
  57. Db::startTrans();
  58. try {
  59. if(!$v['mobile']){
  60. throw new Exception('手机号同步失败,请检查权限,重新获取数据');
  61. }
  62. $department = StaffDepartment::where(array('id'=>$v['dept_id']))->find();
  63. if(!$department['role_id']){
  64. throw new Exception('请先同步所属部门');
  65. }
  66. $staffData = array(
  67. 'mobile' => $v['mobile'],
  68. 'name' => $v['name'],
  69. 'nickname' => $v['name'],
  70. 'post' => $department['name'],
  71. 'password' => '38d7e44335dae29b37ceab504602d17b',
  72. 'salt' => 'a2d622',
  73. 'img' => '',
  74. 'email' => $v['mobile'].'@163.com',
  75. 'group_ids'=>$department['role_id']
  76. );
  77. $staffinfo = StaffModel::create($staffData);
  78. $ids = StaffModel::getLastInsID();
  79. //更新状态
  80. $dingStatus = $this->model->where(array('id'=>$v['id']))->update(array('status'=>1,'staff_id'=>$ids));
  81. if (!$staffinfo) {
  82. throw new Exception('创建员工账号失败');
  83. }
  84. if (!$dingStatus) {
  85. throw new Exception('修改表状态失败');
  86. }
  87. $success++;
  88. Db::commit();
  89. }catch (Exception $e){
  90. Db::rollback();
  91. $error_info[]='员工【'.$v['name'].'】'.$e->getMessage();
  92. }
  93. }
  94. $this->success('同步成功【'.$success.'】条,同步失败【'.count($error_info).'】条;失败原因:'.implode('<br>',$error_info));
  95. }
  96. }