Staff.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\admin\controller\qingdongams\weixin;
  3. use addons\qingdongams\model\DingStaff;
  4. use app\common\controller\Backend;
  5. use addons\qingdongams\model\StaffDepartment;
  6. use addons\qingdongams\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. $wheres['ding_staff.type'] = 1;
  25. $list = $this->model->with(['department'])->where($where)->where($wheres)->order($sort, $order)->paginate($limit);
  26. $row = $list->items();
  27. $result = array("total" => $list->total(), "rows" => $row);
  28. return json($result);
  29. }
  30. return $this->view->fetch();
  31. }
  32. /**
  33. * 删除员工
  34. */
  35. public function del($ids = null) {
  36. if ($this->request->isAjax()) {
  37. $map['id'] = array('in', $ids);
  38. $result = DingStaff::where($map)->delete();
  39. if (!$result) {
  40. $this->error('删除失败');
  41. }
  42. $this->success('删除成功');
  43. }
  44. return $this->view->fetch();
  45. }
  46. /**
  47. * 同步到CRM
  48. */
  49. public function batch()
  50. {
  51. $info = $this->model->where(array('type' => 1, 'status' => 0))->select();
  52. if (!$info) {
  53. $this->error('无数据可同步');
  54. }
  55. $success=0;
  56. $error_info=[];
  57. foreach ($info as $k => $v) {
  58. Db::startTrans();
  59. try {
  60. $mobiles = $v['mobile'];
  61. if (!$mobiles || empty($mobiles)) {
  62. //手机号不存在的员工,请到员工管理中修改本企业员工手机号
  63. throw new Exception('手机号不存在无法同步员工');
  64. }
  65. $department = StaffDepartment::where(array('id' => $v['dept_id']))->find();
  66. if (!$department['role_id']) {
  67. throw new Exception('请先同步部门');
  68. }
  69. $staffData = array(
  70. 'mobile' => $mobiles,
  71. 'name' => $v['name'],
  72. 'nickname' => $v['name'],
  73. 'post' => $department['name'],
  74. 'password' => '38d7e44335dae29b37ceab504602d17b',
  75. 'salt' => 'a2d622',
  76. 'img' => '',
  77. 'email' => $mobiles . '@163.com',
  78. 'group_ids' => $department['role_id']
  79. );
  80. $staffinfo = StaffModel::create($staffData);
  81. $ids = StaffModel::getLastInsID();
  82. //更新状态
  83. $weiStatus = $this->model->where(array('id' => $v['id']))->update(array('status' => 1, 'staff_id' => $ids));
  84. if (!$staffinfo) {
  85. throw new Exception('创建员工账号失败');
  86. }
  87. if (!$weiStatus) {
  88. throw new Exception('修改表状态失败');
  89. }
  90. $success++;
  91. Db::commit();
  92. } catch (Exception $e) {
  93. Db::rollback();
  94. $error_info[]='员工【'.$v['name'].'】'.$e->getMessage();
  95. }
  96. }
  97. $this->success('同步成功【'.$success.'】条,同步失败【'.count($error_info).'】条;失败原因:'.implode('<br>',$error_info));
  98. }
  99. }