Customer.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace app\admin\controller\qingdong\dingding;
  3. use addons\qingdong\model\DingCustomer;
  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\Customer as CustomerModel;
  9. /**
  10. * 钉钉同步客户
  11. */
  12. class Customer extends Backend {
  13. public function _initialize() {
  14. parent::_initialize();
  15. $this->model = new DingCustomer();
  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(['ownerstaff'])->where(['ding_customer.type'=>0])->where($where)->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 = $this->model->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('type'=>0,'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. $staff = DingStaff::where(array('user_id' => $v['owner_staff_id']))->find();
  60. if (!$staff['staff_id']) {
  61. throw new Exception('请先同步所属员工');
  62. }
  63. $customerData = array(
  64. 'name' => filter_Emoji($v['name']),
  65. 'source' => $v['source'],
  66. 'address' => $v['address'],
  67. 'address_detail' => $v['address_detail'],
  68. 'remarks' => $v['remark'],
  69. 'create_staff_id' => $staff['staff_id'],
  70. 'owner_staff_id' => $staff['staff_id'],
  71. );
  72. $customerinfo = CustomerModel::create($customerData);
  73. $idinfo = CustomerModel::getLastInsID();
  74. //更新状态
  75. $dingStatus = $this->model->where(array('id' => $v['id']))->update(array('status' => 1, 'customer_id' => $idinfo));
  76. if (!$customerinfo) {
  77. throw new Exception('创建客户失败');
  78. }
  79. if (!$dingStatus) {
  80. throw new Exception('修改表状态失败');
  81. }
  82. $success++;
  83. Db::commit();
  84. } catch (Exception $e) {
  85. Db::rollback();
  86. $error_info[] = '员工【' . $v['name'] . '】' . $e->getMessage();
  87. }
  88. }
  89. $this->success('同步成功【'.$success.'】条,同步失败【'.count($error_info).'】条;失败原因:'.implode('<br>',$error_info));
  90. }
  91. }