Record.php 6.1 KB

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