Contacts.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace app\admin\controller\qingdong\dingding;
  3. use addons\qingdong\model\DingContacts;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\Exception;
  7. use addons\qingdong\model\DingStaff;
  8. use addons\qingdong\model\DingCustomer;
  9. use addons\qingdong\model\Contacts as ContactsModel;
  10. use addons\qingdong\model\Customer;
  11. /**
  12. * 钉钉同步联系人
  13. */
  14. class Contacts extends Backend {
  15. public function _initialize() {
  16. parent::_initialize();
  17. $this->model = new DingContacts();
  18. }
  19. /**
  20. * 联系人列表
  21. * @return string|\think\response\Json
  22. */
  23. public function index() {
  24. $this->request->filter(['strip_tags']);
  25. if ($this->request->isAjax()) {
  26. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  27. $list = $this->model->with(['ownerstaff','customer'])->where(['ding_contacts.type'=>0])->where($where)->order($sort, $order)->paginate($limit);
  28. $row = $list->items();
  29. $result = array("total" => $list->total(), "rows" => $row);
  30. return json($result);
  31. }
  32. return $this->view->fetch();
  33. }
  34. /**
  35. * 删除联系人
  36. */
  37. public function del($ids = null) {
  38. if ($this->request->isAjax()) {
  39. $map['id'] = array('in', $ids);
  40. $result = $this->model->where($map)->delete();
  41. if (!$result) {
  42. $this->error('删除失败');
  43. }
  44. $this->success('删除成功');
  45. }
  46. return $this->view->fetch();
  47. }
  48. /**
  49. * 同步到CRM联系人
  50. */
  51. public function batch()
  52. {
  53. $info = $this->model->where(array('status' => 0))->select();
  54. if (!$info) {
  55. $this->error('无数据可同步');
  56. }
  57. Db::startTrans();
  58. try{
  59. foreach($info as $k=>$v){
  60. $customer = DingCustomer::where(array('id'=>$v['customer_id']))->find();
  61. if(!$customer['customer_id']){
  62. throw new Exception('请先同步客户');
  63. }
  64. $cusData = Customer::where(array('id'=>$customer['customer_id']))->find();
  65. if(!$cusData){
  66. throw new Exception('客户不存在,请重新同步客户');
  67. }
  68. $contactData = array(
  69. 'customer_id' => $customer['customer_id'],
  70. 'name' => $v['name'],
  71. 'post' => $v['post'],
  72. 'email' => $v['email'],
  73. 'mobile' => $v['mobile'],
  74. 'remarks' => $v['remarks'],
  75. 'create_staff_id' => DingStaff::where(array('user_id'=>$v['create_staff_id']))->value('staff_id'),
  76. 'owner_staff_id' => DingStaff::where(array('user_id'=>$v['owner_staff_id']))->value('staff_id'),
  77. );
  78. $contactinfo = ContactsModel::create($contactData);
  79. //更新状态
  80. $dingStatus = $this->model->where(array('id'=>$v['id']))->update(array('status'=>1));
  81. if(!$contactinfo || !$dingStatus){
  82. throw new Exception('同步失败');
  83. }
  84. }
  85. Db::commit();
  86. }catch (Exception $e){
  87. Db::rollback();
  88. $this->error($e->getMessage());
  89. }
  90. $this->success('同步成功');
  91. }
  92. }