Record.php 13 KB

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