Record.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace app\admin\controller\qingdong\customer;
  3. use addons\qingdong\model\Field;
  4. use addons\qingdong\model\File;
  5. use addons\qingdong\model\Remind;
  6. use addons\qingdong\model\Staff;
  7. use addons\qingdong\model\Customer;
  8. use app\admin\controller\qingdong\Base;
  9. use addons\qingdong\model\Contacts;
  10. use addons\qingdong\model\Comment;
  11. use addons\qingdong\model\Record as RecordModel;
  12. use think\DB;
  13. use think\Exception;
  14. /**
  15. * 客户跟进记录
  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. $need = input('need','','trim');
  27. $customer_id = input('customer_id','','trim');
  28. $contacts_id = input('contacts_id','','trim');
  29. $leads_id = input('leads_id','','trim');
  30. $contract_id = input('contract_id','','trim');
  31. $busniess_id = input('busniess_id','','trim');
  32. //0:全部 1:我负责的 2:下属负责的 3:今日待跟进 4:今日已跟进
  33. $type = input('type',0);
  34. $this->request->filter(['strip_tags']);
  35. if ($this->request->isAjax()) {
  36. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  37. switch($type){
  38. case 1:
  39. $staff = Staff::info();
  40. $wheres['create_staff_id'] = $staff->id;
  41. break;
  42. case 2:
  43. $wheres['create_staff_id'] = array('in',Staff::getLowerStaffId());
  44. break;
  45. case 3:
  46. $start = date('Y-m-d 00:00:00');
  47. $end = date('Y-m-d 23:59:59');
  48. $record = collection(RecordModel::where(array('relation_type'=>1,'next_time'=>array(array('egt',$start),array('elt',$end))))->field("id,relation_id")->select())->toArray();
  49. $relationId = [];
  50. foreach($record as $k=>$v){
  51. $whereRe['id'] = array('gt',$v['id']);
  52. $whereRe['relation_id'] = $v['relation_id'];
  53. $recordData = RecordModel::where($whereRe)->count();
  54. if($recordData == 0){
  55. $relationId[] = $v['id'];
  56. }
  57. }
  58. $wheres['id'] = array('in',$relationId);
  59. $staff = Staff::info();
  60. $wheres['create_staff_id'] = $staff->id;
  61. break;
  62. case 4:
  63. $start = date('Y-m-d 00:00:00');
  64. $end = date('Y-m-d 23:59:59');
  65. $record = collection(RecordModel::where(array('relation_type'=>1,'next_time'=>array(array('egt',$start),array('elt',$end))))->field("id,relation_id")->select())->toArray();
  66. $relationId = [];
  67. foreach($record as $k=>$v){
  68. $whereRe['id'] = array('gt',$v['id']);
  69. $whereRe['relation_id'] = $v['relation_id'];
  70. $recordData = RecordModel::where($whereRe)->count();
  71. if($recordData >=1){
  72. $relationId[] = $v['id'];
  73. }
  74. }
  75. $wheres['id'] = array('in',$relationId);
  76. $staff = Staff::info();
  77. $wheres['create_staff_id'] = $staff->id;
  78. break;
  79. default:
  80. $wheres['create_staff_id'] = array('in',Staff::getMyStaffIds());
  81. break;
  82. }
  83. $wheres['relation_type'] = 1;
  84. if(isset($need) && $need =='customer'){
  85. $staff = Staff::info();
  86. $wheres['create_staff_id'] = $staff->id;
  87. $wheres['reminds_id'] = $staff->id;
  88. $wheres['status'] = 0;
  89. $wheres['next_time'] = array(array('egt',date('Y-m-d 00:00:00')),array('lt',date('Y-m-d 23:59:59')));
  90. $customerlist = $this->model->where($wheres)->column('relation_id');
  91. if($customerlist){
  92. $whereExit['relation_id'] = array('in',$customerlist);
  93. $whereExit['next_time'] = array('gt',date('Y-m-d 23:59:59'));
  94. $recordIds = $this->model->where($whereExit)->column('id');
  95. if($recordIds){
  96. $wheres['id'] = array('in',$recordIds);
  97. }
  98. }
  99. }
  100. if(isset($need) && $need =='contacts'){
  101. $staff = Staff::info();
  102. $wheres['create_staff_id'] = $staff->id;
  103. $wheres['status'] = 0;
  104. $wheres['relation_type'] = 2;
  105. $wheres['next_time'] = array(array('egt',date('Y-m-d 00:00:00')),array('lt',date('Y-m-d 23:59:59')));
  106. $customerlist = $this->model->where($wheres)->column('relation_id');
  107. if($customerlist){
  108. $whereExit['relation_id'] = array('in',$customerlist);
  109. $whereExit['next_time'] = array('gt',date('Y-m-d 23:59:59'));
  110. $recordIds = $this->model->where($whereExit)->column('id');
  111. if($recordIds){
  112. $wheres['id'] = array('in',$recordIds);
  113. }
  114. }
  115. }
  116. if(isset($customer_id) && $customer_id){
  117. $wheres['relation_type'] = 1;
  118. $wheres['relation_id'] = $customer_id;
  119. unset($wheres['create_staff_id']);
  120. }
  121. if(isset($contacts_id) && $contacts_id){
  122. $wheres['relation_type'] = 2;
  123. $wheres['relation_id'] = $contacts_id;
  124. unset($wheres['create_staff_id']);
  125. }
  126. if(isset($leads_id) && $leads_id){
  127. $wheres['relation_type'] = 4;
  128. $wheres['relation_id'] = $leads_id;
  129. unset($wheres['create_staff_id']);
  130. }
  131. if(isset($contract_id) && $contract_id){
  132. $wheres['relation_type'] = 3;
  133. $wheres['relation_id'] = $contract_id;
  134. unset($wheres['create_staff_id']);
  135. }
  136. if(isset($busniess_id) && $busniess_id){
  137. $wheres['relation_type'] = 5;
  138. $wheres['relation_id'] = $busniess_id;
  139. unset($wheres['create_staff_id']);
  140. }
  141. $staff = Staff::info();
  142. if($staff->group_ids != 1 && $staff->group_ids != 9){
  143. $wheres['create_staff_id'] = $staff->id;
  144. }
  145. $list = $this->model->where($where)->where($wheres)->with(['staff','customer'])->order($sort, $order)->paginate($limit);
  146. foreach($list as $k=>$v){
  147. if(!isset($v['customer']) || !$v['customer']){
  148. $v['customer'] = array('id'=>'','name'=>'');
  149. }
  150. //获取staff名称
  151. $v['staff_name'] = Staff::where(['id'=>$v['staff_id']])->value('name');
  152. }
  153. $row = $list->items();
  154. $result = array("total" => $list->total(), "rows" => $row);
  155. return json($result);
  156. }
  157. return $this->view->fetch();
  158. }
  159. /**
  160. * 添加跟进
  161. */
  162. public function add($ids = null) {
  163. $relation_type=input('relation_type',1);
  164. if ($this->request->isPost()) {
  165. $params = $this->request->post('row/a');
  166. $params['follow_type'] = 1;
  167. // 表单验证
  168. if (($result = $this->qingdongValidate($params, 'Record', 'create')) !== true) {
  169. $this->error($result);
  170. }
  171. if(!empty($params['files'])){
  172. $params['files']=File::getId($params['files']);
  173. }
  174. Db::startTrans();
  175. try {
  176. $result = $this->model::createRecord($params);
  177. Db::commit();
  178. } catch (Exception $e) {
  179. Db::rollback();
  180. $this->error($e->getMessage());
  181. }
  182. if ($result) {
  183. $this->success('创建跟进记录成功');
  184. }
  185. $this->error('创建失败');
  186. }
  187. $follow= Field::getField('客户状态');
  188. $customer=Customer::getList();
  189. $customers = [];
  190. foreach($customer as $k=>$v){
  191. $customers[$v['id']] = $v['name'];
  192. }
  193. if($relation_type == 2){
  194. $contacts=Contacts::where(['id'=>$ids])->column('id,name');
  195. $this->assign('contacts',$contacts);
  196. }
  197. $remind=Remind::where(['type'=>Remind::RECORD_TYPE])->find();
  198. $staff_ids=$remind['staff_ids']??'';
  199. $this->assign('staff_ids', $staff_ids);
  200. $this->assign('ids', $ids);
  201. $this->assign('relation_type', $relation_type);
  202. $this->assign('customer', $customers);
  203. $this->assign('follow', $follow);
  204. $this->assign('staff', Staff::where([])->column('name','id'));
  205. return $this->view->fetch();
  206. }
  207. /**
  208. * 跟进详情
  209. */
  210. public function detail($ids = null) {
  211. $row = $this->model->with(['staff','file'])->where([
  212. 'id' => $ids,
  213. ])->find();
  214. if (empty($row)) {
  215. $this->error(__('No Results were found'));
  216. }
  217. $row = $row->toArray();
  218. if($row['staff_id']){
  219. $createname = Staff::where(['id'=>$row['staff_id']])->value('name');
  220. }else{
  221. $createname = isset($row['staff']['name']) ? $row['staff']['name'] : '';
  222. }
  223. $row['createname'] = $createname;
  224. $this->assign('row', $row);
  225. $this->assignconfig("idinfo", ['id' => $ids]);
  226. return $this->view->fetch();
  227. }
  228. /**
  229. * 评论
  230. */
  231. public function comment() {
  232. $this->request->filter(['strip_tags']);
  233. if ($this->request->isAjax()) {
  234. $record_id = input('record_id','');
  235. $wheres['relation_id'] = $record_id;
  236. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  237. $list = Comment::where($where)->where($wheres)->with(['staff','record'])->order($sort, $order)->paginate($limit);
  238. $row = $list->items();
  239. $result = array("total" => $list->total(), "rows" => $row);
  240. return json($result);
  241. }
  242. return $this->view->fetch();
  243. }
  244. }