Leads.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace addons\qingdong\controller;
  3. use addons\qingdong\model\Event;
  4. use addons\qingdong\model\Form;
  5. use addons\qingdong\model\FormField;
  6. use addons\qingdong\model\Leads as LeadsModel;
  7. use addons\qingdong\model\LeadsFile;
  8. use addons\qingdong\model\LeadsOther;
  9. use addons\qingdong\model\Staff;
  10. use addons\qingdong\model\Record;
  11. use think\Db;
  12. use think\Exception;
  13. /**
  14. * 线索接口
  15. */
  16. class Leads extends StaffApi {
  17. protected $noNeedLogin = [];
  18. protected $noNeedRight = [];
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. try{
  23. \think\Db::execute("SET @@sql_mode='';");
  24. }catch (Exception $e){
  25. }
  26. }
  27. //添加线索
  28. public function addLeads() {
  29. $params = $this->request->post();
  30. if (empty($params['leads'])) {
  31. $this->error('线索信息不能为空');
  32. }
  33. // 表单验证
  34. if (($result = $this->qingdongValidate($params['leads'], get_class(), 'create')) !== true) {
  35. $this->error($result);
  36. }
  37. $result = FormField::checkFields(FormField::LEADS_TYPE, $params['leads']);
  38. if ($result !== true) {
  39. $this->error($result);
  40. }
  41. try {
  42. $params['leads'] = Form::updateFormParams(Form::LEADS_TYPE, $params['leads']);
  43. $leadsId = LeadsModel::createLeads($params['leads']);
  44. } catch (Exception $e) {
  45. $this->error($e->getMessage());
  46. }
  47. if ($result) {
  48. $this->success('添加线索成功');
  49. }
  50. }
  51. //获取线索列表
  52. public function getList() {
  53. $name = input('name', '', 'trim');
  54. $mobile = input('mobile', '', 'trim');
  55. $limit = input("limit/d", 10);
  56. $params = $this->request->post();
  57. $where= FormField::updateWhereField(FormField::LEADS_TYPE,$params);
  58. if (isset($params['createtime']) && $params['createtime']) {//跟进状态
  59. $createtime = $params['createtime'];
  60. $createtime = explode(',', $createtime);
  61. $where['createtime'] = ['between', [strtotime($createtime[0]), strtotime($createtime[1])+86400-1]];
  62. }
  63. $wheres=[];
  64. //0:全部 1:我负责的 2:下属负责的 3:今日待跟进 4:今日已跟进 5:从未跟进的
  65. $team = input('team',0);
  66. switch($team){
  67. case 1:
  68. $staff = Staff::info();
  69. $wheres['owner_staff_id'] = $staff->id;
  70. break;
  71. case 2:
  72. $wheres['owner_staff_id'] = array('in',Staff::getLowerStaffId());
  73. break;
  74. case 3:
  75. $start = date('Y-m-d 00:00:00');
  76. $end = date('Y-m-d 23:59:59');
  77. $record = collection(Record::where(array('status'=>0,'relation_type'=>4,'next_time'=>array(array('egt',$start),array('elt',$end))))->field("id,relation_id")->select())->toArray();
  78. $relationId = [];
  79. foreach($record as $k=>$v){
  80. $whereRe['id'] = array('gt',$v['id']);
  81. $whereRe['relation_id'] = $v['relation_id'];
  82. $recordData = Record::where($whereRe)->count();
  83. if($recordData == 0){
  84. $relationId[] = $v['relation_id'];
  85. }
  86. }
  87. $wheres['id'] = array('in',$relationId);
  88. $staff = Staff::info();
  89. $wheres['owner_staff_id'] = $staff->id;
  90. break;
  91. case 4:
  92. $start = date('Y-m-d 00:00:00');
  93. $end = date('Y-m-d 23:59:59');
  94. $relationId = Record::where(array('status'=>1,'relation_type'=>4,'next_time'=>array(array('egt',$start),array('elt',$end))))->field("id,relation_id")->column('relation_id');
  95. $wheres['id'] = array('in',$relationId);
  96. $staff = Staff::info();
  97. $wheres['owner_staff_id'] = $staff->id;
  98. break;
  99. case 5:
  100. $record = collection(Record::where(array('relation_type'=>4))->column('relation_id'))->toArray();
  101. $wheres['id'] = array('not in',$record);
  102. $staff = Staff::info();
  103. $wheres['owner_staff_id'] = $staff->id;
  104. break;
  105. default:
  106. $wheres['owner_staff_id'] = array('in',Staff::getMyStaffIds());
  107. break;
  108. }
  109. if (isset($params['staff_id']) && $params['staff_id']) {//下级员工筛选
  110. $wheres['owner_staff_id'] = $params['staff_id'];
  111. }
  112. if ($name) {
  113. $where['name'] = ['like', "%{$name}%"];
  114. }
  115. if ($mobile) {
  116. $where['mobile'] = ['like', "%{$mobile}%"];
  117. }
  118. $where['is_transform']=0;
  119. $records = LeadsModel::where($where)->where($wheres)->with(['ownerStaff'])->field('id,owner_staff_id,name,follow,mobile,level,next_time,source')->order('id desc')->paginate($limit);
  120. $this->success('请求成功', $records);
  121. }
  122. //获取线索详情
  123. public function getDetail() {
  124. $id = input('id', '', 'intval');
  125. $leads = LeadsModel::where(['id' => $id])->with([
  126. 'createStaff',
  127. 'ownerStaff',
  128. 'tranferStaff'
  129. ])->find();
  130. if(empty($leads)){
  131. $this->error('信息不存在');
  132. }
  133. $leads=$leads->toArray();
  134. $leads=LeadsOther::getOther($leads);
  135. $this->success('请求成功', $leads);
  136. }
  137. //获取选择列表
  138. public function getSelectList() {
  139. $name = input('name','');
  140. $where = ['owner_staff_id' => $this->auth->id,'is_transform'=>0];
  141. if ($name) {
  142. $where['name'] = ['like',"%$name%"];
  143. }
  144. $records = LeadsModel::where($where)->field('id,owner_staff_id,name,follow,mobile')->order('id desc')->select();
  145. $this->success('请求成功', $records);
  146. }
  147. //转移线索
  148. public function transfer()
  149. {
  150. $id = input('id');
  151. $staff_id = input('staff_id');
  152. if (!$staff_id || !$id) {
  153. $this->error('参数错误');
  154. }
  155. $staff = Staff::get($staff_id);
  156. if (empty($staff)) {
  157. $this->error('接收对象不存在');
  158. }
  159. $row = LeadsModel::where(['id' => $id])->find();
  160. if (empty($row)) {
  161. $this->error('线索不存在');
  162. }
  163. try {
  164. LeadsModel::transfer($id, $staff_id);
  165. } catch (Exception $e) {
  166. $this->error($e->getMessage());
  167. }
  168. $this->success('转移线索成功');
  169. }
  170. //修改线索
  171. public function editLeads() {
  172. $id = input('id');
  173. $params = $this->request->post();
  174. $row = LeadsModel::where(['id' => $id, 'owner_staff_id' => $this->auth->id])->find();
  175. if (empty($row)) {
  176. $this->error('您不是线索负责人无法修改信息');
  177. }
  178. // 表单验证
  179. if (($result = $this->qingdongValidate($params, get_class(), 'create')) !== true) {
  180. $this->error($result);
  181. }
  182. $result = FormField::checkFields(FormField::LEADS_TYPE, $params,$id);
  183. if ($result !== true) {
  184. $this->error($result);
  185. }
  186. try {
  187. $params= Form::updateFormParams(Form::LEADS_TYPE, $params);
  188. LeadsModel::updateLeads($params);
  189. } catch (Exception $e) {
  190. $this->error($e->getMessage());
  191. }
  192. if ($result) {
  193. $this->success('修改线索成功');
  194. }
  195. }
  196. //删除线索
  197. public function delLeads() {
  198. $id = input('id');
  199. $model = new LeadsModel();
  200. $row = $model->where(['owner_staff_id' => $this->auth->id, 'id' => $id])->find();
  201. if (empty($row)) {
  202. $this->error('您没有权限删除线索');
  203. }
  204. if($row->owner_staff_id != $this->auth->id){
  205. $this->error('您没有权限删除线索');
  206. }
  207. Db::startTrans();
  208. try{
  209. $model->where(['id' => $id])->delete();
  210. $enentWhere = [
  211. 'relation_id' => $id,
  212. 'type' => 2,
  213. 'relation_type' => Event::LEADS_TYPE,
  214. ];
  215. Event::where($enentWhere)->delete();
  216. Db::commit();
  217. }catch (Exception $e){
  218. Db::rollback();
  219. $this->error($e->getMessage());
  220. }
  221. $this->success('删除成功');
  222. }
  223. //获取附件列表
  224. public function getFilesList() {
  225. $id = input('leads_id');
  226. $files = LeadsFile::where(['leads_id' => $id])->field('file_id')->with(['file'])->select();
  227. $this->success('请求成功', $files);
  228. }
  229. }