Customer.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\admin\controller\qingdongams\weixin;
  3. use addons\qingdongams\model\DingCustomer;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\Exception;
  7. use addons\qingdongams\model\DingStaff;
  8. use addons\qingdongams\model\Customer as CustomerModel;
  9. /**
  10. * 企业微信客户同步
  11. */
  12. class Customer extends Backend
  13. {
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->model = new DingCustomer();
  18. }
  19. /**
  20. * 企业微信客户列表
  21. */
  22. public function index()
  23. {
  24. $this->request->filter(['strip_tags']);
  25. if ($this->request->isAjax()) {
  26. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  27. $wheres['ding_customer.type'] = 1;
  28. $list = $this->model->with(['ownerstaff'])->where($where)->where($wheres)->order($sort, $order)->paginate($limit);
  29. $row = $list->items();
  30. $result = array("total" => $list->total(), "rows" => $row);
  31. return json($result);
  32. }
  33. return $this->view->fetch();
  34. }
  35. /**
  36. * 删除企业微信客户
  37. */
  38. public function del($ids = null)
  39. {
  40. if ($this->request->isAjax()) {
  41. $map['id'] = array('in', $ids);
  42. $result = $this->model->where($map)->delete();
  43. if (!$result) {
  44. $this->error('删除失败');
  45. }
  46. $this->success('删除成功');
  47. }
  48. return $this->view->fetch();
  49. }
  50. /**
  51. * 同步到CRM
  52. */
  53. public function batch()
  54. {
  55. $info = $this->model->where(array('type' => 1, 'status' => 0))->select();
  56. if (!$info) {
  57. $this->error('无数据可同步');
  58. }
  59. $success = 0;
  60. $error_info = [];
  61. foreach ($info as $k => $v) {
  62. Db::startTrans();
  63. try {
  64. $staff = DingStaff::where(array('user_id' => $v['owner_staff_id']))->find();
  65. if (!$staff['staff_id']) {
  66. throw new Exception('请先同步所属员工');
  67. }
  68. $customerData = array(
  69. 'name' => $v['name'],
  70. 'source' => $v['source'],
  71. 'address' => $v['address'],
  72. 'address_detail' => $v['address_detail'],
  73. 'remarks' => $v['remark'],
  74. 'create_staff_id' => $staff['staff_id'],
  75. 'owner_staff_id' => $staff['staff_id'],
  76. );
  77. $customerinfo = CustomerModel::create($customerData);
  78. $idinfo = CustomerModel::getLastInsID();
  79. //更新状态
  80. $dingStatus = $this->model->where(array('id' => $v['id']))->update(array('status' => 1, 'customer_id' => $idinfo));
  81. if (!$customerinfo) {
  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. }