Leadspool.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace addons\qingdong\controller;
  3. use addons\qingdong\model\Event;
  4. use addons\qingdong\model\Leads as LeadsModel;
  5. use addons\qingdong\model\LeadsFile;
  6. use addons\qingdong\model\LeadsOther;
  7. use addons\qingdong\model\Staff;
  8. use addons\qingdong\model\Record;
  9. use think\Db;
  10. use think\Exception;
  11. /**
  12. * 线索池接口
  13. */
  14. class Leadspool extends StaffApi {
  15. protected $noNeedLogin = [];
  16. protected $noNeedRight = [];
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. try{
  21. \think\Db::execute("SET @@sql_mode='';");
  22. }catch (Exception $e){
  23. }
  24. }
  25. //添加线索
  26. public function addLeads() {
  27. $params = $this->request->post();
  28. if (empty($params['leads'])) {
  29. $this->error('线索信息不能为空');
  30. }
  31. try {
  32. $leadsId = LeadsModel::createLeads($params['leads'],1);
  33. } catch (Exception $e) {
  34. $this->error($e->getMessage());
  35. }
  36. if ($leadsId) {
  37. $this->success('添加线索成功');
  38. }
  39. }
  40. //获取线索列表
  41. public function getList() {
  42. $name = input('name', '', 'trim');
  43. $mobile = input('mobile', '', 'trim');
  44. $limit = input("limit/d", 10);
  45. $params = $this->request->post();
  46. $where = [];
  47. if (isset($params['createtime']) && $params['createtime']) {//跟进状态
  48. $createtime = $params['createtime'];
  49. $createtime = explode(',', $createtime);
  50. $where['createtime'] = ['between', [strtotime($createtime[0]), strtotime($createtime[1])+86400-1]];
  51. }
  52. if ($name) {
  53. $where['name'] = ['like', "%{$name}%"];
  54. }
  55. if ($mobile) {
  56. $where['mobile'] = ['like', "%{$mobile}%"];
  57. }
  58. $records = LeadsModel::where($where)->where('owner_staff_id is null or owner_staff_id = 0')->with(['createStaff'])->field('id,create_staff_id,name,follow,mobile,level,next_time,source')->order('id desc')->paginate($limit);
  59. $this->success('请求成功', $records);
  60. }
  61. //获取线索详情
  62. public function getDetail() {
  63. $id = input('id', '', 'intval');
  64. $leads = LeadsModel::where(['id' => $id])->with([
  65. 'createStaff',
  66. ])->find();
  67. if(empty($leads)){
  68. $this->error('信息不存在');
  69. }
  70. $leads=$leads->toArray();
  71. $leads=LeadsOther::getOther($leads);
  72. $this->success('请求成功', $leads);
  73. }
  74. //获取选择列表
  75. public function getSelectList() {
  76. $name = input('name','');
  77. $where = ['owner_staff_id' => $this->auth->id,'is_transform'=>0];
  78. if ($name) {
  79. $where['name'] = ['like',"%$name%"];
  80. }
  81. $records = LeadsModel::where($where)->field('id,owner_staff_id,name,follow,mobile')->order('id desc')->select();
  82. $this->success('请求成功', $records);
  83. }
  84. //转移线索
  85. public function transfer()
  86. {
  87. $id = input('id');
  88. $staff_id = input('staff_id');
  89. if (!$staff_id && !$id) {
  90. $this->error('参数错误');
  91. }
  92. $row = LeadsModel::where(['id' =>['in',$id] ])->find();
  93. if (empty($row)) {
  94. $this->error('线索不存在');
  95. }
  96. $id = explode(',',$id);
  97. try {
  98. LeadsModel::transfer($id, $staff_id);
  99. } catch (Exception $e) {
  100. $this->error($e->getMessage());
  101. }
  102. $this->success('转移成功');
  103. }
  104. //修改线索
  105. public function editLeads() {
  106. $id = input('id');
  107. $params = $this->request->post();
  108. $row = LeadsModel::where(['id' => $id])->find();
  109. if (empty($row)) {
  110. $this->error('线索信息不存在');
  111. }
  112. try {
  113. $result = LeadsModel::updateLeads($params);
  114. } catch (Exception $e) {
  115. $this->error($e->getMessage());
  116. }
  117. if ($result) {
  118. $this->success('修改线索成功');
  119. }
  120. }
  121. //删除线索
  122. public function delLeads() {
  123. $id = input('id');
  124. $model = new LeadsModel();
  125. $row = $model->where(['id' => $id])->find();
  126. if (empty($row)) {
  127. $this->error('线索不存在');
  128. }
  129. Db::startTrans();
  130. try{
  131. $model->where(['id' => $id])->delete();
  132. $enentWhere = [
  133. 'relation_id' => $id,
  134. 'type' => 2,
  135. 'relation_type' => Event::LEADS_TYPE,
  136. ];
  137. Event::where($enentWhere)->delete();
  138. Db::commit();
  139. }catch (Exception $e){
  140. Db::rollback();
  141. $this->error($e->getMessage());
  142. }
  143. $this->success('删除成功');
  144. }
  145. //获取附件列表
  146. public function getFilesList() {
  147. $id = input('leads_id');
  148. $files = LeadsFile::where(['leads_id' => $id])->field('file_id')->with(['file'])->select();
  149. $this->success('请求成功', $files);
  150. }
  151. //领取线索
  152. public function achieve()
  153. {
  154. $id = input('id');
  155. if (empty($id)) {
  156. $this->error('参数错误');
  157. }
  158. $row = LeadsModel::where(['id' =>$id ])->find();
  159. if (empty($row)) {
  160. $this->error('线索不存在');
  161. }
  162. try {
  163. LeadsModel::transfer($id, $this->auth->id);
  164. } catch (Exception $e) {
  165. $this->error($e->getMessage());
  166. }
  167. $this->success('领取成功');
  168. }
  169. }