Record.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\admin\controller\qingdong\leads;
  3. use addons\qingdong\model\Field;
  4. use addons\qingdong\model\File;
  5. use addons\qingdong\model\Leads;
  6. use addons\qingdong\model\Staff;
  7. use app\admin\controller\qingdong\Base;
  8. use addons\qingdong\model\Record as RecordModel;
  9. use think\DB;
  10. use think\Exception;
  11. /**
  12. * 跟进记录列表
  13. */
  14. class Record extends Base {
  15. public function _initialize() {
  16. parent::_initialize();
  17. $this->model = new RecordModel();
  18. }
  19. /**
  20. * 跟进记录列表
  21. */
  22. public function index() {
  23. $this->request->filter(['strip_tags']);
  24. $need = input('need','');
  25. if ($this->request->isAjax()) {
  26. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  27. //0:全部 1:我负责的 2:下属负责的 3:今日待跟进 4:今日已跟进
  28. $type = input('type',0);
  29. switch($type){
  30. case 1:
  31. $staff = Staff::info();
  32. $wheres['create_staff_id'] = $staff->id;
  33. break;
  34. case 2:
  35. $wheres['create_staff_id'] = array('in',Staff::getLowerStaffId());
  36. break;
  37. case 3:
  38. $start = date('Y-m-d 00:00:00');
  39. $end = date('Y-m-d 23:59:59');
  40. $record = collection(RecordModel::where(array('relation_type'=>4,'next_time'=>array(array('egt',$start),array('elt',$end))))->field("id,relation_id")->select())->toArray();
  41. $relationId = [];
  42. foreach($record as $k=>$v){
  43. $whereRe['id'] = array('gt',$v['id']);
  44. $whereRe['relation_id'] = $v['relation_id'];
  45. $recordData = RecordModel::where($whereRe)->count();
  46. if($recordData == 0){
  47. $relationId[] = $v['id'];
  48. }
  49. }
  50. $wheres['id'] = array('in',$relationId);
  51. $staff = Staff::info();
  52. $wheres['create_staff_id'] = $staff->id;
  53. break;
  54. case 4:
  55. $start = date('Y-m-d 00:00:00');
  56. $end = date('Y-m-d 23:59:59');
  57. $record = collection(RecordModel::where(array('relation_type'=>4,'next_time'=>array(array('egt',$start),array('elt',$end))))->field("id,relation_id")->select())->toArray();
  58. $relationId = [];
  59. foreach($record as $k=>$v){
  60. $whereRe['id'] = array('gt',$v['id']);
  61. $whereRe['relation_id'] = $v['relation_id'];
  62. $recordData = RecordModel::where($whereRe)->count();
  63. if($recordData >=1){
  64. $relationId[] = $v['id'];
  65. }
  66. }
  67. $wheres['id'] = array('in',$relationId);
  68. $staff = Staff::info();
  69. $wheres['create_staff_id'] = $staff->id;
  70. break;
  71. default:
  72. $wheres['create_staff_id'] = array('in',Staff::getMyStaffIds());
  73. break;
  74. }
  75. $wheres['relation_type'] = 4;
  76. if(isset($need) && $need =='leads'){
  77. $staff = Staff::info();
  78. $wheres['create_staff_id'] = $staff->id;
  79. $wheres['status'] = 0;
  80. $wheres['follow_type'] = ['neq', '其它'];
  81. $wheres['next_time'] = array(array('egt',date('Y-m-d 00:00:00')),array('lt',date('Y-m-d 23:59:59')));
  82. $infolist = $this->model->where($wheres)->column('relation_id');
  83. if($infolist){
  84. $whereExit['relation_id'] = array('in',$infolist);
  85. $whereExit['next_time'] = array('gt',date('Y-m-d 23:59:59'));
  86. $recordIds = $this->model->where($whereExit)->column('id');
  87. if($recordIds){
  88. $wheres['id'] = array('in',$recordIds);
  89. }
  90. }
  91. }
  92. $list = $this->model->where($where)->where($wheres)->with(['staff','leads'])->order($sort, $order)->paginate($limit);
  93. $row = $list->items();
  94. $result = array("total" => $list->total(), "rows" => $row);
  95. return json($result);
  96. }
  97. return $this->view->fetch();
  98. }
  99. /**
  100. * 添加跟进
  101. */
  102. public function add($ids = null) {
  103. $relation_type=input('relation_type',4);
  104. if ($this->request->isPost()) {
  105. $params = $this->request->post('row/a');
  106. // 表单验证
  107. if (($result = $this->qingdongValidate($params, 'Record', 'create')) !== true) {
  108. $this->error($result);
  109. }
  110. if(!empty($params['files'])){
  111. $params['files']=File::getId($params['files']);
  112. }
  113. Db::startTrans();
  114. try {
  115. $result = $this->model::createRecord($params);
  116. Db::commit();
  117. } catch (Exception $e) {
  118. Db::rollback();
  119. $this->error($e->getMessage());
  120. }
  121. if ($result) {
  122. $this->success('创建跟进记录成功');
  123. }
  124. $this->error('创建失败');
  125. }
  126. $follow= Field::getField('客户状态');
  127. $staff=Staff::where([])->column('name','id');
  128. if($ids){
  129. $leads=Leads::where(['id'=>$ids])->column('id,name');
  130. }else{
  131. $leads=Leads::where(['owner_staff_id'=>['in',Staff::getMyStaffIds()]])->column('id,name');
  132. }
  133. $this->assign('ids', $ids);
  134. $this->assign('relation_type', $relation_type);
  135. $this->assign('leads', $leads);
  136. $this->assign('staff', $staff);
  137. $this->assign('follow', $follow);
  138. return $this->view->fetch();
  139. }
  140. }