Record.php 3.1 KB

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