Record.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Comment;
  4. use addons\qingdongams\model\Contacts;
  5. use addons\qingdongams\model\Message;
  6. use addons\qingdongams\model\Record as RecordModel;
  7. use addons\qingdongams\model\RecordRead;
  8. use addons\qingdongams\model\Customer;
  9. use addons\qingdongams\model\Contract;
  10. use addons\qingdongams\model\Leads;
  11. use addons\qingdongams\model\Staff;
  12. use addons\qingdongams\model\Business;
  13. use think\Db;
  14. use think\Exception;
  15. use function EasyWeChat\Kernel\Support\get_client_ip;
  16. /**
  17. * 跟进记录(无定位)
  18. */
  19. class Record extends StaffApi {
  20. protected $noNeedLogin = [];
  21. protected $noNeedRight = [];
  22. /**
  23. * 获取跟进记录
  24. */
  25. public function getList() {
  26. $relation_type = input('relation_type', '', 'intval');// 1客户 2联系人 3合同 5商机
  27. $relation_id = input('relation_id', '', 'intval');
  28. $customer_id = input('customer_id', '', 'intval');
  29. $limit = input("limit/d", 10);
  30. $is_read = input('is_read', 0);
  31. $type = input('type', 0);// 0 全部 1 我创建 2 下属创建
  32. $follow_type = input('follow_type', '');
  33. $times = input('times','');
  34. $where = [];
  35. if ($relation_type) {
  36. $where['relation_type'] = $relation_type;
  37. if($relation_id){
  38. $where['relation_id'] = $relation_id;
  39. }
  40. }
  41. if ($type == 1) {//我的客户
  42. $where['create_staff_id'] = $this->auth->id;
  43. } elseif ($type == 2) {//下属负责的客户
  44. $where['create_staff_id'] = ['in', Staff::getLowerStaffId()];
  45. }else{
  46. $where['create_staff_id'] = ['in', Staff::getMyStaffIds()];
  47. }
  48. if($customer_id){
  49. $where['relation_type'] = 1;
  50. $where['relation_id'] = $customer_id;
  51. unset($where['create_staff_id']);
  52. }
  53. if($follow_type){
  54. $where['follow_type'] = $follow_type;
  55. }else{
  56. $where['follow_type'] = ['neq', '其它'];
  57. }
  58. if ($times) {
  59. $where['createtime'] = ['between', setTimes($times,'time')];
  60. }
  61. $staff_id = $this->auth->id;
  62. if ($is_read == 1) {//已读
  63. $ids = RecordRead::where(['staff_id' => $staff_id])->column('record_id');
  64. $where['id'] = ['in', $ids];
  65. } elseif ($is_read == 2) {//未读
  66. $ids = RecordRead::where(['staff_id' => $staff_id])->column('record_id');
  67. $where['id'] = ['not in', $ids];
  68. }
  69. $records = RecordModel::where($where)->with([
  70. 'creatstaff',
  71. 'file',
  72. 'read' => function ($query) use ($staff_id) {
  73. $query->where(['staff_id' => $staff_id]);
  74. }
  75. ])->order('id desc')->paginate($limit)->toArray();
  76. $data = $records['data'];
  77. foreach ($data as $k => $v) {
  78. $v['staff'] = $v['creatstaff'];
  79. $customerWhere['id'] = $v['relation_id']?? '';
  80. $v['comment_num'] =0;//评论数
  81. if($v['relation_type'] == RecordModel::CUSTOMER_TYPE) {
  82. $v['comment_num'] =Comment::where(array('relation_type'=>1,'relation_id'=>$v['id']))->count();
  83. $v['relation_name'] = Customer::where(['id' => $v['relation_id']])->value('name');
  84. }elseif($v['relation_type'] == RecordModel::CONTACTS_TYPE) {
  85. $v['comment_num'] =Comment::where(array('relation_type'=>2,'relation_id'=>$v['id']))->count();
  86. $v['relation_name'] = Contacts::where(['id' => $v['relation_id']])->value('name');
  87. }elseif($v['relation_type'] == RecordModel::CONTRACT_TYPE) {
  88. $v['comment_num'] =Comment::where(array('relation_type'=>3,'relation_id'=>$v['id']))->count();
  89. $v['relation_name'] = Contract::where(['id' => $v['relation_id']])->value('name');
  90. }elseif($v['relation_type'] == RecordModel::LEADS_TYPE) {
  91. $v['comment_num'] =Comment::where(array('relation_type'=>4,'relation_id'=>$v['id']))->count();
  92. $v['relation_name'] = Leads::where(['id' => $v['relation_id']])->value('name');
  93. } elseif($v['relation_type'] == RecordModel::BUSINESS_TYPE) {
  94. $v['comment_num'] =Comment::where(array('relation_type'=>5,'relation_id'=>$v['id']))->count();
  95. $v['relation_name'] = Business::where(['id' => $v['relation_id']])->value('name');
  96. }else{
  97. $v['customer'] = [];
  98. }
  99. if (!empty($v['read'])) {
  100. $v['is_read'] = 1;
  101. } else {
  102. $v['is_read'] = 0;
  103. }
  104. $data[$k] = $v;
  105. }
  106. $this->success('请求成功', [
  107. 'total' => $records['total'],
  108. 'per_page' => $records['per_page'],
  109. 'current_page' => $records['current_page'],
  110. 'last_page' => $records['last_page'],
  111. 'data' => $data
  112. ]);
  113. $this->success('请求成功', $records);
  114. }
  115. /**
  116. * 创建跟进记录
  117. */
  118. public function createRecord() {
  119. $params = $this->request->post();
  120. if(isset($params['relation_process'])){
  121. $params['follow_type'] = $params['relation_process'];
  122. unset($params['relation_process']);
  123. }
  124. // 表单验证
  125. if (($result = $this->qingdongamsValidate($params, get_class(), 'create')) !== true) {
  126. $this->error($result);
  127. }
  128. Db::startTrans();
  129. try {
  130. $result = RecordModel::createRecord($params);
  131. Db::commit();
  132. } catch (Exception $e) {
  133. Db::rollback();
  134. $this->error($e->getMessage());
  135. }
  136. if ($result) {
  137. $this->success('创建跟进记录成功');
  138. }
  139. }
  140. /**
  141. * 获取跟进记录详情
  142. */
  143. public function getRecordDetail() {
  144. $id = input('id');
  145. if (empty($id)) {
  146. $this->error('参数不能为空');
  147. }
  148. $record = RecordModel::where(['id' => $id])->with([
  149. 'staff',
  150. 'file'
  151. ])->find();
  152. if (empty($record)) {
  153. $this->error('根据记录不存在');
  154. }
  155. $record = $record->toArray();
  156. if ($record['relation_type'] == RecordModel::CUSTOMER_TYPE) {
  157. $record['relation_name'] = Customer::where(['id' => $record['relation_id']])->value('name');
  158. } elseif ($record['relation_type'] == RecordModel::CONTACTS_TYPE) {
  159. $record['relation_name'] = Contacts::where(['id' => $record['relation_id']])->value('name');
  160. } elseif ($record['relation_type'] == RecordModel::CONTRACT_TYPE) {
  161. $record['relation_name'] = Contract::where(['id' => $record['relation_id']])->value('name');
  162. } elseif($record['relation_type'] == RecordModel::LEADS_TYPE) {
  163. $record['relation_name'] = Leads::where(['id' => $record['relation_id']])->value('name');
  164. } elseif($record['relation_type'] == RecordModel::BUSINESS_TYPE) {
  165. $record['relation_name'] = Business::where(['id' => $record['relation_id']])->value('name');
  166. }else{
  167. $record['relation_name']='';
  168. }
  169. $reminds_id = $record['reminds_id'];
  170. $reminds_id = explode(',', $reminds_id);
  171. $names = Staff::where(['id' => ['in', $reminds_id],'status'=>1])->column('name');
  172. $record['staff_name'] = implode(',', $names);
  173. if($record['staff_id']){
  174. $record['staff'] = Staff::where(['id'=>$record['staff_id']])->field('id,img,name,post')->find();
  175. }
  176. //标记通知已读
  177. Message::setRead(Message::RECORD_TYPE, $id, $this->auth->id);
  178. //添加阅读记录
  179. RecordRead::addRead($id, $this->auth->id);
  180. $this->success('请求成功', $record);
  181. }
  182. /**
  183. * 添加评论
  184. */
  185. public function addComment() {
  186. $content = input('content');
  187. $record_id = input('record_id');
  188. $relation_type = input('relation_type');
  189. if (empty($content)) {
  190. $this->error('评论内容不能为空');
  191. }
  192. $data = [
  193. 'relation_type' => $relation_type,
  194. 'relation_id' => $record_id,
  195. 'staff_id' => $this->auth->id,
  196. 'content' => $content,
  197. 'status' => 1,
  198. 'ip' => get_client_ip(),
  199. ];
  200. $commentModel = new Comment();
  201. $commentModel->save($data);
  202. //修改跟进状态
  203. RecordModel::where(['id'=>$record_id])->update(['status'=>1,'updatetime'=>time()]);
  204. $record = RecordModel::get($record_id);
  205. Message::addMessage(Message::COMMENT_TYPE,$record_id,$record['create_staff_id'],$this->auth->id);
  206. $staff_ids=$commentModel->where(['relation_type'=>$relation_type,'relation_id'=>$record_id])->group('staff_id')->column('staff_id');
  207. foreach ($staff_ids as $staff_id) {
  208. //发送通知
  209. if($staff_id != $this->auth->id){
  210. Message::addMessage(Message::COMMENT_TYPE,$record_id,$staff_id,$this->auth->id);
  211. }
  212. }
  213. $this->success('评论成功');
  214. }
  215. /**
  216. * 评论列表
  217. */
  218. public function commentList() {
  219. $record_id = input('record_id');
  220. $relation_type = input('relation_type');
  221. $comments = Comment::where([
  222. 'relation_type' => $relation_type,
  223. 'relation_id' => $record_id,
  224. 'status' => 1
  225. ])->field('id,staff_id,content,createtime')->order('id desc')->with(['staff'])->select();
  226. $this->success('请求成功', $comments);
  227. }
  228. /**
  229. * 获取跟进客户
  230. */
  231. public function getcustomerList() {
  232. $limit = input("limit/d", 10);
  233. $time = input('time', 0);
  234. $type = input('type', 1);
  235. //0:全部 1:我负责的 2:下属负责的
  236. $staff_type = input('staff_type', 1);
  237. switch ($staff_type) {
  238. case 1://我负责
  239. $where['create_staff_id'] = $this->auth->id;
  240. break;
  241. case 2://下属负责
  242. $where['create_staff_id'] = array('in', Staff::getLowerStaffId());
  243. break;
  244. default:
  245. $where['create_staff_id'] = array('in', Staff::getMyStaffIds());
  246. break;
  247. }
  248. if($type == 6){//合同
  249. $where['relation_type'] = 3;
  250. }elseif($type == 7){//线索
  251. $where['relation_type'] = 4;
  252. }elseif($type == 8){//联系人
  253. $where['relation_type'] = 2;
  254. }elseif($type == 9){//商机
  255. $where['relation_type'] = 5;
  256. }else{
  257. $where['relation_type'] = 1;
  258. }
  259. $where['follow_type'] = ['neq', '其它'];
  260. $staff_id = $this->auth->id;
  261. if ($time == 1) {//7天
  262. $where['next_time'] = array(array('egt',date('Y-m-d', strtotime('-7 day'))),array('lt',date('Y-m-d', strtotime('+1 day'))));
  263. } elseif ($time == 2) {//14天
  264. $where['next_time'] = array(array('egt',date('Y-m-d', strtotime('-14 day'))),array('lt',date('Y-m-d', strtotime('+1 day'))));
  265. }elseif($time == 3){
  266. $where['next_time'] = array(array('egt',date('Y-m-d', strtotime('-30 day'))),array('lt',date('Y-m-d', strtotime('+1 day'))));
  267. }elseif($time == 4){ //今日
  268. $where['next_time'] = array(array('egt',date('Y-m-d 00:00:00')),array('elt',date('Y-m-d 23:59:59')));
  269. }
  270. $where['status'] = 0;
  271. $records = RecordModel::where($where)->with([
  272. 'staff',
  273. 'file',
  274. 'read' => function ($query) use ($staff_id) {
  275. $query->where(['staff_id' => $staff_id]);
  276. }
  277. ])->order('id desc')->paginate($limit)->toArray();
  278. $data = $records['data'];
  279. foreach ($data as $k => $v) {
  280. $customerWhere['id'] = $v['relation_id']?? '';
  281. if($type == 6){
  282. //合同
  283. $v['customer'] = Contract::where($customerWhere)->find();
  284. }elseif($type == 7){
  285. //线索
  286. $v['customer'] = Leads::where($customerWhere)->find();
  287. }elseif($type == 8){
  288. //联系人
  289. $v['customer'] = Contacts::where($customerWhere)->find();
  290. }elseif($type == 9){
  291. //商机
  292. $v['customer'] = Business::where($customerWhere)->find();
  293. }
  294. else{
  295. $v['customer'] = Customer::where($customerWhere)->find();
  296. }
  297. if (!empty($v['read'])) {
  298. $v['is_read'] = 1;
  299. } else {
  300. $v['is_read'] = 0;
  301. }
  302. $data[$k] = $v;
  303. }
  304. $this->success('请求成功', [
  305. 'total' => $records['total'],
  306. 'per_page' => $records['per_page'],
  307. 'current_page' => $records['current_page'],
  308. 'last_page' => $records['last_page'],
  309. 'data' => $data
  310. ]);
  311. $this->success('请求成功', $records);
  312. }
  313. /*
  314. *待办跟进
  315. */
  316. public function record_add(){
  317. $type = input('type',0);
  318. $content = input('remarks');
  319. $record_id = input('record_id');
  320. $relation_type = input('relation_type');
  321. if (empty($content)) {
  322. $this->error('备注不能为空');
  323. }
  324. $data = [
  325. 'relation_type' => $relation_type,
  326. 'relation_id' => $record_id,
  327. 'staff_id' => $this->auth->id,
  328. 'content' => $content,
  329. 'status' => 1,
  330. 'ip' => get_client_ip(),
  331. ];
  332. Db::startTrans();
  333. $commentModel = new Comment();
  334. $resultC = $commentModel->save($data);
  335. $recordU = RecordModel::where(array('id'=>$record_id))->update(array('status'=>1,'updatetime'=>time()));
  336. $resultR= true;
  337. if($type ==1){
  338. $params = $this->request->post();
  339. // 表单验证
  340. if (($result = $this->qingdongamsValidate($params, get_class(), 'create')) !== true) {
  341. $this->error($result);
  342. }
  343. try {
  344. unset($params['type']);
  345. unset($params['remarks']);
  346. unset($params['record_id']);
  347. $resultR = RecordModel::createRecord($params);
  348. } catch (Exception $e) {
  349. Db::rollback();
  350. $this->error($e->getMessage());
  351. }
  352. }
  353. if(!$resultC || !$resultR || !$recordU){
  354. Db::rollback();
  355. $this->error('跟进失败');
  356. }
  357. Db::commit();
  358. $this->success('跟进成功');
  359. }
  360. }