SignIn.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Message;
  4. use addons\qingdongams\model\Staff;
  5. use addons\qingdongams\model\Customer;
  6. use addons\qingdongams\model\StaffSignIn;
  7. use addons\qingdongams\model\StaffSignInRead;
  8. use think\Db;
  9. use think\Exception;
  10. /**
  11. * @desc 操作文档:https://doc.fastadmin.net/qingdongams
  12. * @desc 软件介绍:https://www.fastadmin.net/store/qingdongams.html
  13. * @desc 售后微信:qingdong_crm
  14. */
  15. /**
  16. * 跟进签到
  17. */
  18. class SignIn extends StaffApi {
  19. protected $noNeedLogin = [];
  20. protected $noNeedRight = [];
  21. //获取跟进记录
  22. public function getList() {
  23. $params = $this->request->post();
  24. $relation_type = input('relation_type', '', 'intval');// customer客户 event 日程 workorder 业绩目标
  25. $relation_id = input('relation_id', '', 'intval');
  26. $limit = input("limit/d", 10);
  27. $is_read = input('is_read', 0);//0 全部 1已读 2未读
  28. $staff_id = input('staff_id', 0);
  29. $customer_id = input('customer_id', 0);
  30. $createtime=input('createtime');
  31. $where = [];
  32. if ($relation_type) {
  33. $where['relation_type'] = $relation_type;
  34. $where['relation_id'] = $relation_id;
  35. }
  36. if ($staff_id) {
  37. $where['staff_id'] = $staff_id;
  38. }
  39. if ($createtime) {
  40. $times = setTimes($createtime, 'time');
  41. $where['createtime'] = ['between', $times];
  42. }
  43. if ($customer_id) {
  44. $where['customer_id'] = $customer_id;
  45. }
  46. if(empty($customer_id) && empty($relation_id) && empty($staff_id)){
  47. $where['staff_id'] = ['in', Staff::getMyStaffIds()];
  48. if (isset($params['type']) && $params['type']) {//客户分类
  49. if ($params['type'] == 1) {//我的创建
  50. $where['owner_staff_id'] = $this->auth->id;
  51. } elseif ($params['type'] == 2) {//下属创建
  52. $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  53. }
  54. }
  55. }
  56. $staff_id = $this->auth->id;
  57. if ($is_read == 1) {//已读
  58. $ids = StaffSignInRead::where(['staff_id' => $staff_id])->column('sign_in_id');
  59. $where['id'] = ['in', $ids];
  60. } elseif ($is_read == 2) {//未读
  61. $ids = StaffSignInRead::where(['staff_id' => $staff_id])->column('sign_in_id');
  62. $where['id'] = ['not in', $ids];
  63. }
  64. if(isset($params['id_list'])){//日志 查询id列表
  65. $where=[];
  66. $where['id']=['in',explode(',',$params['id_list'])];
  67. }
  68. $records = StaffSignIn::where($where)->with([
  69. 'staff',
  70. 'customer',
  71. 'read' => function ($query) use ($staff_id) {
  72. $query->where(['staff_id' => $staff_id]);
  73. },
  74. 'comment_number',
  75. 'workorder',
  76. 'event_title',
  77. ])->order('id desc')->paginate($limit)->toArray();
  78. $data = $records['data'];
  79. foreach ($data as $k => $v) {
  80. if (!empty($v['read'])) {
  81. $v['is_read'] = 1;
  82. } else {
  83. $v['is_read'] = 0;
  84. }
  85. if($v['relation_type'] == StaffSignIn::EVENT_TYPE){//日程
  86. unset($v['workorder']);
  87. }elseif($v['relation_type'] == StaffSignIn::WORKORDER_TYPE){//工单
  88. unset($v['event_title']);
  89. }else{
  90. unset($v['event_title']);
  91. unset($v['workorder']);
  92. }
  93. $data[$k] = $v;
  94. }
  95. $this->success('请求成功', [
  96. 'total' => $records['total'],
  97. 'per_page' => $records['per_page'],
  98. 'current_page' => $records['current_page'],
  99. 'last_page' => $records['last_page'],
  100. 'data' => $data
  101. ]);
  102. $this->success('请求成功', $records);
  103. }
  104. //创建
  105. public function createRecord() {
  106. $location = input('location');
  107. $lng = input('lng');
  108. $lat = input('lat');
  109. $file_ids = input('file_ids');
  110. $relation_process = input('relation_process');//跟进
  111. $customer_id = input('customer_id');
  112. $content = input('content');
  113. $relation_type = input('relation_type', '', 'trim');// customer客户 event 日程 workorder 工单
  114. $relation_id = input('relation_id', '', 'intval');
  115. $reminds_id = input('reminds_id');
  116. $next_time = input('next_time',null);
  117. if (empty($lng) || empty($lat)) {
  118. // $this->error('地理位置不能为空');
  119. }
  120. if (empty($content)) {
  121. $this->error('签到备注不能为空');
  122. }
  123. if (empty($reminds_id)) {
  124. $this->error('提醒人不能为空');
  125. }
  126. $customer = Customer::where(['id' =>$customer_id])->find();
  127. if (empty($lng) && empty($lat)) {
  128. $distance = '';
  129. } elseif (empty($customer['lng']) && empty($customer['lat'])) {
  130. $distance = '';
  131. } else {
  132. $distance = getdistance($customer['lng'], $customer['lat'], $lng, $lat);
  133. }
  134. $data = [
  135. 'staff_id' => $this->auth->id,
  136. 'location' => $location,
  137. 'lng' => $lng,
  138. 'lat' => $lat,
  139. 'file_ids' => $file_ids,
  140. 'customer_id' => $customer_id,
  141. 'relation_type' => $relation_type,//日程类型
  142. 'relation_id' => $relation_id,//签到关联id
  143. 'relation_process' => $relation_process,
  144. 'customer_status' => $customer['contract_status'],//成交状态
  145. 'content' => $content,
  146. 'distance' => $distance,
  147. 'next_time' => $next_time,
  148. 'reminds_id' => $reminds_id,
  149. ];
  150. Db::startTrans();
  151. try {
  152. $result = StaffSignIn::createSignIn($data);
  153. Customer::update(['last_time'=>$next_time],['id'=>$customer_id]);
  154. Db::commit();
  155. } catch (Exception $e) {
  156. Db::rollback();
  157. $this->error($e->getMessage());
  158. }
  159. if ($result) {
  160. $this->success('新增跟进记录成功');
  161. }
  162. }
  163. //获取根据记录详情
  164. public function getDetail() {
  165. $id = input('id');
  166. if (empty($id)) {
  167. $this->error('参数不能为空');
  168. }
  169. $record =StaffSignIn::where(['id' => $id])->with([
  170. 'staff',
  171. 'customer'
  172. ])->find();
  173. if (empty($record)) {
  174. //标记通知已读
  175. Message::setRead(Message::RECORD_TYPE, $id, $this->auth->id);
  176. Message::setRead(Message::SIGN_TYPE, $id, $this->auth->id);
  177. $this->error('记录已删除');
  178. }
  179. $reminds_id = $record['reminds_id'];
  180. $reminds_id = explode(',', $reminds_id);
  181. $names = Staff::where(['id' => ['in', $reminds_id],'status'=>1])->column('name');
  182. $record['staff_name'] = implode(',', $names);
  183. //标记通知已读
  184. Message::setRead(Message::RECORD_TYPE, $id, $this->auth->id);
  185. Message::setRead(Message::SIGN_TYPE, $id, $this->auth->id);
  186. //添加阅读记录
  187. StaffSignInRead::addRead($id, $this->auth->id);
  188. $this->success('请求成功', $record);
  189. }
  190. }