Leadspool.php 5.8 KB

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