Leads.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Contacts;
  4. use addons\qingdongams\model\Customer;
  5. use addons\qingdongams\model\Event;
  6. use addons\qingdongams\model\Form;
  7. use addons\qingdongams\model\FormField;
  8. use addons\qingdongams\model\Leads as LeadsModel;
  9. use addons\qingdongams\model\LeadsFile;
  10. use addons\qingdongams\model\LeadsOther;
  11. use addons\qingdongams\model\Staff;
  12. use addons\qingdongams\model\Record;
  13. use think\Db;
  14. use think\Exception;
  15. /**
  16. * @desc 操作文档:https://doc.fastadmin.net/qingdongams
  17. * @desc 软件介绍:https://www.fastadmin.net/store/qingdongams.html
  18. * @desc 售后微信:qingdong_crm
  19. */
  20. /**
  21. * 线索接口
  22. */
  23. class Leads extends StaffApi {
  24. protected $noNeedLogin = [];
  25. protected $noNeedRight = [];
  26. //添加线索
  27. public function addLeads() {
  28. $params = $this->request->post();
  29. if (empty($params['leads'])) {
  30. $this->error('线索信息不能为空');
  31. }
  32. // 表单验证
  33. if (($result = $this->qingdongamsValidate($params['leads'],get_class(), 'create')) !== true) {
  34. $this->error($result);
  35. }
  36. $result = FormField::checkFields(FormField::LEADS_TYPE,$params['leads']);
  37. if ($result !== true) {
  38. $this->error($result);
  39. }
  40. try {
  41. $leadsId = LeadsModel::createLeads($params['leads']);
  42. } catch (Exception $e) {
  43. $this->error($e->getMessage());
  44. }
  45. if ($result) {
  46. $this->success('添加线索成功');
  47. }
  48. }
  49. //获取线索列表
  50. public function getList() {
  51. $name = input('name', '');
  52. $mobile = input('mobile', '');
  53. $limit = input("limit/d", 10);
  54. $params = $this->request->post();
  55. $where= FormField::updateWhereField(FormField::LEADS_TYPE,$params);
  56. if (isset($params['createtime']) && $params['createtime']) {//跟进状态
  57. $createtime = $params['createtime'];
  58. $where['createtime'] = ['between', setTimes($createtime,'time')];
  59. }
  60. //0:全部 1:我负责的 2:下属负责的 3:今日待跟进 4:今日已跟进 5:从未跟进的
  61. $type = input('type',0);
  62. switch($type){
  63. case 1:
  64. $staff = Staff::info();
  65. $where['owner_staff_id'] = $staff->id;
  66. break;
  67. case 2:
  68. $where['owner_staff_id'] = array('in',Staff::getLowerStaffId());
  69. break;
  70. case 3:
  71. $start = date('Y-m-d 00:00:00');
  72. $end = date('Y-m-d 23:59:59');
  73. $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();
  74. $relationId = [];
  75. foreach($record as $k=>$v){
  76. $relationId[] = $v['relation_id'];
  77. }
  78. $where['id'] = array('in',$relationId);
  79. $staff = Staff::info();
  80. $where['owner_staff_id'] = $staff->id;
  81. break;
  82. case 4:
  83. $start = date('Y-m-d 00:00:00');
  84. $end = date('Y-m-d 23:59:59');
  85. $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');
  86. $where['id'] = array('in',$relationId);
  87. $staff = Staff::info();
  88. $where['owner_staff_id'] = $staff->id;
  89. break;
  90. case 5:
  91. $record = collection(Record::where(array('relation_type'=>4))->column('relation_id'))->toArray();
  92. $where['id'] = array('not in',$record);
  93. $staff = Staff::info();
  94. $where['owner_staff_id'] = $staff->id;
  95. break;
  96. default:
  97. $where['owner_staff_id'] = array('in',Staff::getMyStaffIds());
  98. break;
  99. }
  100. if (isset($params['staff_id']) && $params['staff_id']) {//下级员工筛选
  101. $where['owner_staff_id'] = $params['staff_id'];
  102. }
  103. if ($name) {
  104. $where['name'] = ['like', "%{$name}%"];
  105. }
  106. if ($mobile) {
  107. $where['mobile'] = ['like', "%{$mobile}%"];
  108. }
  109. $where['is_transform'] = 0;
  110. $records = LeadsModel::where($where)->with(['ownerStaff'])->field('id,owner_staff_id,name,follow,mobile,level,next_time,source')->order('id desc')->paginate($limit);
  111. $this->success('请求成功', $records);
  112. }
  113. //获取线索详情
  114. public function getDetail()
  115. {
  116. $id = input('id', '', 'intval');
  117. $leads = LeadsModel::where(['id' => $id])->with([
  118. 'createStaff',
  119. 'ownerStaff'
  120. ])->find();
  121. if (empty($leads)) {
  122. $this->error('信息不存在');
  123. }
  124. $leads = $leads->toArray();
  125. $leads = LeadsOther::getOther($leads);
  126. $this->success('请求成功', $leads);
  127. }
  128. //获取选择列表
  129. public function getSelectList() {
  130. $where = ['owner_staff_id' => $this->auth->id];
  131. $where['is_transform'] = 0;
  132. $records = LeadsModel::where($where)->field('id,owner_staff_id,name,follow,mobile')->order('id desc')->select();
  133. $this->success('请求成功', $records);
  134. }
  135. //转移线索
  136. public function transfer()
  137. {
  138. $id = input('id');
  139. $staff_id = input('staff_id');
  140. if (!$staff_id || !$id) {
  141. $this->error('参数错误');
  142. }
  143. $staff = Staff::get($staff_id);
  144. if (empty($staff)) {
  145. $this->error('接收对象不存在');
  146. }
  147. $row = LeadsModel::where(['id' => $id])->find();
  148. if (empty($row)) {
  149. $this->error('线索不存在');
  150. }
  151. try {
  152. LeadsModel::transfer($id, $staff_id);
  153. } catch (Exception $e) {
  154. $this->error($e->getMessage());
  155. }
  156. $this->success('转移线索成功');
  157. }
  158. //修改线索
  159. public function editLeads() {
  160. $id = input('id');
  161. $params = $this->request->post();
  162. $row = LeadsModel::where(['id' => $id])->find();
  163. if (empty($row)) {
  164. $this->error('线索信息不存在');
  165. }
  166. // 表单验证
  167. if (($result = $this->qingdongamsValidate($params,get_class(), 'create')) !== true) {
  168. $this->error($result);
  169. }
  170. $result = FormField::checkFields(FormField::LEADS_TYPE,$params,$id);
  171. if ($result !== true) {
  172. $this->error($result);
  173. }
  174. try {
  175. LeadsModel::updateLeads($params);
  176. } catch (Exception $e) {
  177. $this->error($e->getMessage());
  178. }
  179. if ($result) {
  180. $this->success('修改线索成功');
  181. }
  182. }
  183. //删除线索
  184. public function delLeads() {
  185. $id = input('id');
  186. $model = new LeadsModel();
  187. $row = $model->where(['owner_staff_id' => $this->auth->id, 'id' => $id])->find();
  188. if (empty($row)) {
  189. $this->error('不是线索归属人,无法删除数据!');
  190. }
  191. if ($model->where(['id' => $id])->delete()) {
  192. $this->success('删除成功');
  193. }
  194. $this->error('删除失败');
  195. }
  196. //获取附件列表
  197. public function getFilesList() {
  198. $id = input('leads_id');
  199. $files = LeadsFile::where(['leads_id' => $id])->field('file_id')->with(['file'])->select();
  200. $this->success('请求成功', $files);
  201. }
  202. //移入线索池
  203. public function movePool()
  204. {
  205. $id = input('id');
  206. $row = LeadsModel::where(['id' => $id])->find();
  207. if (empty($row)) {
  208. $this->error('线索不存在');
  209. }
  210. Db::startTrans();
  211. try {
  212. LeadsModel::movePool($id);
  213. Db::commit();
  214. } catch (Exception $e) {
  215. Db::rollback();
  216. $this->error($e->getMessage());
  217. }
  218. $this->success('放入成功');
  219. }
  220. /**
  221. * 转为客户
  222. */
  223. public function convert_customer()
  224. {
  225. $id = input('id');
  226. $row = LeadsModel::get($id);
  227. if (empty($row)) {
  228. $this->error('线索不存在');
  229. }
  230. $form=Form::getDataValue(Form::LEADS_TYPE);
  231. $fields=[];
  232. foreach ($form as $v){
  233. if(isset($v['matching']) && $v['matching']){
  234. $fields[$v['matching']]=$v['id'];
  235. }
  236. }
  237. Db::startTrans();
  238. try {
  239. $customer=[
  240. 'name'=>$row['name'],
  241. 'mobile' => $row['mobile'],
  242. 'leads_id' => $row['id'],
  243. 'create_staff_id' => $row['owner_staff_id'],
  244. 'owner_staff_id' => $row['owner_staff_id'],
  245. ];
  246. foreach ($fields as $k => $f) {
  247. $customer[$k] = $row[$f] ?? '';
  248. }
  249. //线索转化
  250. $leads_id = '';
  251. if (isset($customer['leads_id'])) {
  252. $leads_id = $customer['leads_id'];
  253. }
  254. $customer_id = Customer::createCustomer($customer, $leads_id);
  255. $contracts = [
  256. 'customer_id' => $customer_id,
  257. 'is_major' => 1,
  258. 'name' => $row['name'],
  259. 'mobile' => $row['mobile'],
  260. 'remarks' => $row['remarks'],
  261. 'create_staff_id' => $row['owner_staff_id'],
  262. 'owner_staff_id' => $row['owner_staff_id'],
  263. ];
  264. Contacts::createContacts($contracts);
  265. Db::commit();
  266. } catch (Exception $e) {
  267. Db::rollback();
  268. $this->error($e->getMessage());
  269. }
  270. $this->success('转客成功');
  271. }
  272. }